Skip to content

Add a feature to instantiate a Resource from a YML doc #111

@davemfish

Description

@davemfish

A public function like geometamaker.load(yaml_filepath) would be useful. The key is that the caller does not know the type of resource that it's loading, so it would need to read that from the YAML before instantiating the resource. That's the difference between this new function and the class method BaseResource.load, which already exists.

We have this function implemented in natcap.invest.reports.raster_utils, but think it makes sense to have this functionality in geometamaker instead.

def geometamaker_load(filepath):
    with open(filepath, 'r', encoding='utf-8') as file:
        yaml_string = file.read()
        yaml_dict = yaml.safe_load(yaml_string)
        if not yaml_dict or ('metadata_version' not in yaml_dict
                             and 'geometamaker_version' not in yaml_dict):
            message = (f'{filepath} exists but is not compatible with '
                       f'geometamaker.')
            raise ValueError(message)

    return geometamaker.geometamaker.RESOURCE_MODELS[yaml_dict['type']](**yaml_dict)

Also see BaseResource.load for how we try to load older versions of metadata models and migrate them to the current models, if possible. And consider whether the API should include this new function and this class method, or whether they should be consolidated.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions