cape.dkit.metautils: Metadata tools for DataKit collections¶
This module provides various utilities for creating databases of
metadata. For example, the ModulePropDB class can be used to
collect properties for each module in a package.
- class cape.dkit.metautils.ModuleMetadata(mod=None, **kw)¶
Read metadata from a datakit module
- Versions:
2021-08-12
@ddalle: Version 0.1; started
- read_json(fjson)¶
Read a JSON file into the keys of a metadata object
- Call:
>>> meta.read_json(fjson) >>> meta.read_json(f)
- Inputs:
- meta:
ModuleMetadata Metadata object for one module
- fjson:
str Name of JSON file to read
- f: file
Already opened file handle
- meta:
- Versions:
2021-08-12
@ddalle: Version 1.0
- class cape.dkit.metautils.ModulePropDB(*a, **kw)¶
Module properties database
- Call:
>>> props = ModulePropDB(fname) >>> props = ModulePropDB(opts, **kw) >>> props = ModulePropDB(optlist, **kw)
- Inputs:
- Outputs:
- props:
ModulePropDB Module property database instance
- props.settings:
dict Settings popped from
".settings"key from input
- props:
- Versions:
2019-04-15
@ddalle: v1.0
- compare_module(mod, modopts)¶
Compare specified properties to those of a particular module
- Call:
>>> match = props.compare_module(mod, modopts)
- Inputs:
- props:
ModulePropDB Module property database instance
- mod:
str Name of module to test
- modopts:
dict Dictionary of properties to compare to module mod
- props:
- Outputs:
- match:
True|False Whether or not mod matches all values in modopts
- match:
- Versions:
2019-04-15
@ddalle: v1.0
- compare_module_all(mod, *a, **kw)¶
Search for specified properties in a particular module
- Call:
>>> q, keys = props.compare_module_all(mod, *a, **kw) >>> q, keys = props.compare_module_all(mod, v1, v2, ...) >>> q, keys = props.compare_module_all(mod, k1=v1, k2=v2)
- Inputs:
- props:
ModulePropDB Module property database instance
- k1:
str Name of first key to search (
_replaced with-)- k2:
str Name of second key to search
- v1:
str|any Test value 1 (for key k1 if specified, else any key)
- v2:
str|any Test value 2 (for key k2 if specified)
- kw:
dict Keyword arguments of options to match
- props:
- Outputs:
- q:
True|False Whether or not all properties were matched
- keys:
list`[:class:`str] Dictionary of modules that match, with keys that match
- q:
- Versions:
2019-04-16
@ddalle: v1.0
- classmethod from_json(fname)¶
Create metadata instance from JSON file
- Call:
>>> meta = MetaData.from_json(fname)
- Inputs:
- props:
ModulePropDB Module property database instance
- fname:
str Name of JSON file
- props:
- Versions:
2019-04-07
@ddalle: First versoin
- get_ordered_db(mod)¶
Get an
OrderedDictdatabase for one module- Call:
>>> moddb = props.get_ordered_db(mod)
- Inputs:
- props:
ModulePropDB Module property database interface instance
- mod:
str Name of module to sort
- props:
- Outputs:
- moddb:
OrderedDict Properties for module mod
- moddb:
- Versions:
2019-04-14
@ddalle: v1.0
- get_ordered_settings()¶
Get ordered version of metadata settings attribute
- Call:
>>> settings = props.get_ordered_settings()
- Inputs:
- props:
ModulePropDB Module property database instance
- props:
- Outputs:
- settings:
OrderedDict Properly ordered settings
- settings:
- Versions:
2019-09-27
@ddalle: v1.0
- get_property(mod, k, vdef=None)¶
Return a property from one module, using defaults
- Call:
>>> v = props.get_property(mod, k, vdef=None)
- Inputs:
- props:
ModulePropDB Module property database interface instance
- mod:
str Name of module to sort
- k:
str Name of property/key to return
- vdef: {
None} |str|bool Default value if not present in DB or defaults
- props:
- Outputs:
- Versions:
2019-04-17
@ddalle: v1.0
- list_modules()¶
Get a list of modules in appropriate order
- Call:
>>> mods = props.list_modules()
- Inputs:
- props:
ModulePropDB Module property database interface instance
- props:
- Outputs:
- Versions:
2019-04-14
@ddalle: v1.0
- merge(opts)¶
Merge a dictionary, where opts overrides props
- Call:
>>> props.merge(opts)
- Inputs:
- props:
ModulePropDB Module property database instance
- opts:
dict Dictionary of metadata to merge
- props:
- Versions:
2019-04-07
@ddalle: v1.0
- mergedefault(opts)¶
Merge a dictionary, where props overrides props
- Call:
>>> props.mergedefault(opts)
- Inputs:
- props:
ModulePropDB Module property database instance
- opts:
dict Dictionary of metadata to merge
- props:
- Versions:
2019-04-07
@ddalle: v1.0
- read_json(fname)¶
Read a JSON metadata file
- Call:
>>> props.read_json(fname)
- Inputs:
- props:
ModulePropDB Module property database instance
- fname:
str Name of JSON file
- props:
- Versions:
2019-04-06
@ddalle: First versoin
- search(*a, **kw)¶
Search module database for values regardless of key
Checks are made using regular expressions (in particular,
re.search()) if both the database value and the test value are strings. If no keyword is specified, the value will be searched in each key.- Call:
>>> moddb = props.search(*a, **kw) >>> moddb = props.search(v1, v2, ..., **kw) >>> moddb = props.search(k1=v1, k2=v2)
- Inputs:
- props:
ModulePropDB Module property database instance
- k1:
str Name of first key to search (
_replaced with-)- k2:
str Name of second key to search
- v1:
str|any Test value 1 (for key k1 if specified, else any key)
- v2:
str|any Test value 2 (for key k2 if specified)
- kw:
dict Keyword arguments of options to match
- props:
- Outputs:
- moddb:
dict`[:class:`list`[:class:`str]] Dictionary of modules that match, with keys that match
- moddb:
- Versions:
2019-04-16
@ddalle: v1.0
- search_db(*a, **kw)¶
Search module database for modules that match criteria
- Call:
>>> mods = props.search_db(**kw) >>> mods = props.search_db(opts, **kw)
- Inputs:
- props:
ModulePropDB Module property database instance
- opts:
dict Dictionary of options to match
- kw:
dict Keyword arguments of options to match
- props:
- Outputs:
- mods:
list`[:class:`str] List of module names
- mods:
- Versions:
2019-04-16
@ddalle: v1.0
- write_json(fname, **kw)¶
Write metadata to JSON file
- Call:
>>> props.write_json(fname)
- Inputs:
- props:
ModulePropDB Module property database instance
- fname:
str Name of JSON file
- indent: {
4} |int> 0 Spaces in indentation level
- props:
- Versions:
2019-04-06
@ddalle: First versoin
- cape.dkit.metautils.merge_dict(opts1, opts2)¶
Merge two dictionaries, using value from opts1 in conflict