hermes.model.context

Module Contents

Classes

HermesContext

The HermesContext stores the metadata for a certain project.

HermesHarvestContext

A specialized context for use in harvest stage.

CodeMetaContext

The HermesContext stores the metadata for a certain project.

Attributes

_log

hermes.model.context._log
class hermes.model.context.HermesContext(project_dir: pathlib.Path | None = 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:

default_timestamp
hermes_name = 'hermes'
hermes_cache_name
hermes_lod_context = ()
__getitem__(key: hermes.model.path.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: dict | None = None, path: hermes.model.path.ContextPath | None = None, tags: dict | None = None) dict
error(ep: importlib.metadata.EntryPoint, error: Exception)

Add an error that occurred during processing to the error log.

Parameters:
  • ep – The entry point that produced the error.

  • error – The exception that was thrown due to the error.

purge_caches() None

Delete .hermes cache-directory if it exsis.

add_context(new_context: tuple) None

Add a new linked data context to the harvest context.

Parameters:

new_context – The new context as tuple (context name, context URI)

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.

load_cache()

Load the cached data from the HermesContext.hermes_dir.

store_cache()

Store the collected data to the HermesContext.hermes_dir.

__enter__()
__exit__(exc_type, exc_val, exc_tb)
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).

error(ep: importlib.metadata.EntryPoint, error: Exception)

See HermesContext.error()

_check_values(path, values)
get_data(data: dict | None = None, path: hermes.model.path.ContextPath | None = None, tags: dict | None = 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).

finish()

Calling this method will lead to further processors not handling the context anymore.

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:

_PRIMARY_ATTR
_CODEMETA_CONTEXT_URL = 'https://doi.org/10.5063/schema/codemeta-2.0'
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: Dict[str, 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.

find_key(item, other)
prepare_codemeta()

Updates the linked data contexts, where the CodeMeta context is the default context, and any additional contexts are named contexts. Also sets the type to ‘SoftwareSourceCode’.