hermes.model.context
Contents
hermes.model.context
Module Contents
Classes
The HermesContext stores the metadata for a certain project. |
|
A specialized context for use in harvest stage. |
|
The HermesContext stores the metadata for a certain project. |
Attributes
- class hermes.model.context.HermesContext(project_dir: Optional[pathlib.Path] = None)
The HermesContext stores the metadata for a certain project.
As there are different views of the metadata in the different stages, some stages use a special subclass of this context:
The harvest stages uses
HermesHarvestContext
.
- __getitem__(key: ContextPath | str) Any
Access a single entry from the context.
- Parameters
key – The path to the item that should be retrieved. Can be in dotted syntax or as a
ContextPath
instance.- Returns
The value stored under the given key.
- keys() List[hermes.model.path.ContextPath]
Get all the keys for the data stored in this context.
- init_cache(*path: str) pathlib.Path
Initialize a cache directory if not present.
- Parameters
path – The (local) path to identify the requested cache.
- Returns
The path to the requested cache file.
- get_cache(*path: str, create: bool = False) pathlib.Path
Retrieve a cache file for a given path.
This method returns an appropriate path to a file but does not make any assertions about the format, encoding, or whether the file should be exists. However, it is capable to create the enclosing directory (if you specify create = True).
- Parameters
path – The (local) path to identify the requested cache.
create – Select whether the directory should be created.
- Returns
The path to the requested cache file.
- update(_key: str, _value: Any, **kwargs: Any)
Store a new value for a given key to the context.
- Parameters
_key – The key may be a dotted name for a metadata attribute to store.
_value – The value that should be stored for the key.
kwargs – Additional information about the value. This can be used to trace back the original value. If _ep is given, it is treated as an entry point name that triggered the update.
- get_data(data: Optional[dict] = None, path: Optional[hermes.model.path.ContextPath] = None, tags: Optional[dict] = None) dict
- class hermes.model.context.HermesHarvestContext(base: HermesContext, ep: importlib.metadata.EntryPoint, config: dict = None)
Bases:
HermesContext
A specialized context for use in harvest stage.
Each harvester has its own context that is cached to
HermesContext.hermes_dir
/harvest/EP_NAME.This special context is implemented as a context manager that loads the cached data upon entering the context. When the context is left, recorded metadata is stored in a cache file possible errors are propagated to the parent context.
- update(_key: str, _value: Any, **kwargs: Any)
The updates are added to a list of values. A value is only replaced if the _key and all kwargs match.
# 'value 2' will be added (twice) ctx.update('key', 'value 1', spam='eggs') ctx.update('key', 'value 2', foo='bar') ctx.update('key', 'value 2', foo='bar', spam='eggs') # 'value 2' will replace 'value 1' ctx.update('key', 'value 1', spam='eggs') ctx.update('key', 'value 2', spam='eggs')
This way, the harvester can fully specify the source and only override values that are from the same origin (e.g., if the data changed between two runs).
See
HermesContext.update()
for more information.
- _update_key_from(_key: hermes.model.path.ContextPath, _value: Any, **kwargs)
- update_from(data: Dict[str, Any], **kwargs: Any)
Bulk-update multiple values.
If the value for a certain key is again a collection, the key will be expanded:
ctx.update_from({'arr': ['foo', 'bar'], 'author': {'name': 'Monty Python', 'email': 'eggs@spam.xxx'}})
will eventually result in the following calls:
ctx.update('arr[0]', 'foo') ctx.update('arr[1]', 'bar') ctx.update('author.name', 'Monty Python') ctx.update('author.email', 'eggs@spam.xxx')
- Parameters
data – The data that should be updated (as mapping with strings as keys).
kwargs – Additional information about the value (see
HermesContext.update()
for details).
- get_data(data: Optional[dict] = None, path: Optional[hermes.model.path.ContextPath] = None, tags: Optional[dict] = None) dict
Retrieve the data from a given path.
This method can be used to extract data and whole sub-trees from the context. If you want a complete copy of the data, you can also call this method without giving a path.
- Parameters
data – Optional a target dictionary where the data is stored. If not given, a new one is created.
path – The path to extract data from.
tags – An optional dictionary to collect the tags that belong to the extracted data. The full path will be used as key for this dictionary.
- Returns
The extracted data (i.e., the data parameter if it was given).
- class hermes.model.context.CodeMetaContext(project_dir: pathlib.Path | None = None)
Bases:
HermesContext
The HermesContext stores the metadata for a certain project.
As there are different views of the metadata in the different stages, some stages use a special subclass of this context:
The harvest stages uses
HermesHarvestContext
.
- merge_from(other: HermesHarvestContext)
- merge_contexts_from(other: HermesHarvestContext)
Merges any linked data contexts from a harvesting context into the instance’s set of contexts.
- Parameters
other – The
HermesHarvestContext
to merge the linked data contexts from
- update(_key: hermes.model.path.ContextPath, _value: Any, tags: t.Dict[str, t.Dict] | None = None)
Store a new value for a given key to the context.
- Parameters
_key – The key may be a dotted name for a metadata attribute to store.
_value – The value that should be stored for the key.
kwargs – Additional information about the value. This can be used to trace back the original value. If _ep is given, it is treated as an entry point name that triggered the update.