… load a convention from a local YAML file?

… load a convention from a local YAML file?#

Yes you can. To demonstrate this, let’s take a convention file, which comes with the h5RDMtoolbox

import h5rdmtoolbox as h5tbx
from h5rdmtoolbox import tutorial
yaml_filename = tutorial.get_convention_yaml_filename()

cv = h5tbx.convention.Convention.from_yaml(yaml_filename)
h5tbx.use(cv)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 3
      1 yaml_filename = tutorial.get_convention_yaml_filename()
----> 3 cv = h5tbx.convention.Convention.from_yaml(yaml_filename)
      4 h5tbx.use(cv)

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.6.0/lib/python3.10/site-packages/h5rdmtoolbox/convention/core.py:347, in Convention.from_yaml(cls, yaml_filename, overwrite)
    345 if convention_name in [d.name for d in CV_DIR.glob('*')]:
    346     if not overwrite:
--> 347         return _get_convention_from_dir(attrs['__name__'])
    348     # overwriting existing convention
    349     delete(convention_name)

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.6.0/lib/python3.10/site-packages/h5rdmtoolbox/convention/core.py:529, in _get_convention_from_dir(convention_name)
    527 sys.path.insert(0, str(_convention_py_filename.parent))
    528 # import:
--> 529 _import_convention(_convention_name)
    530 # now it is registered and can be returned:
    531 cv = get_registered_conventions()[convention_name]

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.6.0/lib/python3.10/site-packages/h5rdmtoolbox/convention/core.py:513, in _import_convention(convention_name)
    511 import importlib
    512 try:
--> 513     return importlib.import_module(f'{convention_name}')
    514 except ImportError:
    515     logger.error(f"Failed to import module {convention_name}. Most likely the created convention file is erroneous.")

File ~/.asdf/installs/python/3.10.17/lib/python3.10/importlib/__init__.py:126, in import_module(name, package)
    124             break
    125         level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1050, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1027, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:1006, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:688, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:883, in exec_module(self, module)

File <frozen importlib._bootstrap>:241, in _call_with_frames_removed(f, *args, **kwds)

File ~/.local/share/h5rdmtoolbox/2.6.0/convention/h5rdmtoolbox_tutorial_convention/h5rdmtoolbox_tutorial_convention.py:68
     64     """Type of data in file. Can be numerical, analytical or experimental."""
     65     data_type: datatypes
---> 68 class standard_name(BaseModel):
     69     """Standard name of the dataset. If not set, the long_name attribute must be given."""
     70     standard_name: standard_name

File ~/.local/share/h5rdmtoolbox/2.6.0/convention/h5rdmtoolbox_tutorial_convention/h5rdmtoolbox_tutorial_convention.py:70, in standard_name()
     68 class standard_name(BaseModel):
     69     """Standard name of the dataset. If not set, the long_name attribute must be given."""
---> 70     standard_name: standard_name

NameError: name 'standard_name' is not defined
with h5tbx.File(data_type='experimental', contact=h5tbx.__author_orcid__) as h5:
    h5.dump()