onyo.lib.onyo module
- class onyo.lib.onyo.OnyoRepo(path, init=False, find_root=False)[source]
Bases:
objectRepresentation of an Onyo repository.
Identify and work with asset paths and directories. Get and set onyo config information.
- git
Reference to the
onyo.lib.git.GitRepoof this Onyo repository.
- dot_onyo
The Path of the
.onyo/subdirectory (Onyo_DIR) that contains templates, the onyo-config file, and other onyo-relevant files.
- __init__(path, init=False, find_root=False)[source]
Instantiate an
OnyoRepoobject withpathas the root directory.- Parameters:
path (
Path) – An absolute path to the root of the Onyo Repository.init (
bool(default:False)) – Initializepathas a git repo and create/populate the subdir.onyo/. Cannot be used withfind_root=True.find_root (
bool(default:False)) – Search for the root of the repository beginning atpath, and then up through parents. Cannot be used withinit==True.
- Raises:
ValueError –
find_root=Trueandinit==Trueboth specified.OnyoInvalidRepoError –
pathis not a valid path to an Onyo repository.
- property asset_paths: list[Path]
Get the absolute
Paths of all assets in this repository.This property is cached and is reset automatically on
commit().If changes are made by other means, use
clear_cache()to reset the cache.
- clear_cache()[source]
Clear the cache of this instance of OnyoRepo (and the sub-
onyo.lib.git.GitRepo).When the repository is modified using only the public API functions, the cache is consistent. This method is only necessary if the repository is modified otherwise.
- Return type:
- commit(paths, message)[source]
Commit changes to the repository.
This is resets the cache and is otherwise just a proxy for
onyo.lib.git.GitRepo.commit().
- static generate_commit_subject(format_string, max_length=80, **kwargs)[source]
Generate a commit message subject.
Path names are shortened on a best effort basis to reduce the subject length to
max_length.- Parameters:
- Return type:
- get_asset_content(path)[source]
Get a dictionary representing
path’s content.The content also includes the asset’s pseudo-keys.
- Parameters:
path (
Path) – Path of asset to load. This may be either a YAML file or an Asset Directory (onyo.lib.consts.ASSET_DIR_FILE_NAMEis automatically appended).- Return type:
- get_asset_name_keys()[source]
Get a list of keys used to generate asset names.
Key names are extracted from the format string specified in the config
onyo.assets.name-format.
- get_config(key)[source]
Get the effective value of a configuration key.
This first checks git’s normal git-config locations and then
onyo.lib.consts.ONYO_CONFIGas a fallback.The results are cached, which are cleared automatically by
commit()andset_config().If changes are made by other means, use
clear_cache()to reset the cache.
- get_editor()[source]
Return the editor to use.
This progresses through:
ONYO_CORE_EDITORenvironment variableonyo.core.editorgit’s
core.editorEDITORenvironment variablenano(fallback).
- Return type:
- get_item_paths(include=None, exclude=None, depth=0, types=None, intermediates=True)[source]
Get the Paths of all items matching paths and filters.
- Parameters:
include (
Optional[Iterable[Path]] (default:None)) – Paths under which to look for items. Default is to inventory root.exclude (
Union[Iterable[Path],Path,None] (default:None)) – Paths to exclude (i.e. items underneath will not be returned).depth (
int(default:0)) – Number of levels to descend into the directories specified byinclude. A depth of0descends recursively without limit.types (
Optional[List[Literal['assets','directories']]] (default:None)) – List of types of inventory items to consider. Equivalent toonyo.is.asset=Trueandonyo.is.directory=True. Default is['assets'].intermediates (
bool(default:True)) – Return intermediate directory items. IfFalse, the only directories explicitly contained in the returned list are leaves.
- Return type:
List[Path]
- get_templates(path=None, recursive=False)[source]
Yield ItemSpec(s) (recursively) from a path.
- Parameters:
path (
Path|None(default:None)) – Path to a Template. If relative, then it is considered relative to the template directory (onyo.lib.consts.TEMPLATE_DIR). If no path is given, the template defined in the configonyo.new.templateis used.recursive (
bool(default:False)) – Recurse into directory templatesnot (If path is not specified and the config onyo.new.template is)
set
empty. (the dictionary will be)
- Raises:
ValueError – If the requested template can’t be found.
- Return type:
- is_asset_dir(path)[source]
Whether
pathis an asset directory.An asset directory is both an asset and an inventory directory.
- Parameters:
path (
Path) – Path to check.- Return type:
- is_asset_file(path)[source]
Whether
pathis an asset file.- Parameters:
path (
Path) – Path to check.- Return type:
- is_asset_path(path)[source]
Whether
pathis an asset in the repository.- Parameters:
path (
Path) – Path to check.- Return type:
- is_inventory_dir(path)[source]
Whether
pathis an inventory directory.This only considers directories with a committed anchor file.
- Parameters:
path (
Path) – Path to check.- Return type:
- is_inventory_path(path)[source]
Whether
patha valid potential name for an asset or an inventory directory.This only checks whether
pathis suitable in principle. It does not check whether that path already exists or if it would be valid and available as an asset name.- Parameters:
path (
Path) – Path to check.- Return type:
- is_item_path(path)[source]
Whether
pathis a valid path for an item.This checks whether
pathis valid for reading an item from or creating an item at in principle. It’s not checking whetherpathactually exists.- Return type:
- is_onyo_ignored(path)[source]
Whether
pathis matched by a pattern in.onyoignore.Such a path would not considered to be an inventory item by Onyo, but could still be tracked in git.
.onyoignorefiles apply to the subtree they are placed into.- Parameters:
path (
Path) – Path to check for matching an exclude pattern in an ignore file (onyo.lib.consts.IGNORE_FILE_NAME).- Return type:
- is_onyo_path(path)[source]
Determine whether an absolute path is used by onyo internally.
Currently anything underneath .onyo/, anything named .onyo*, and an anchor files in an inventory directory is considered an onyo path.
- Parameters:
path (
Path) – The path to check.- Return type:
- mk_inventory_dirs(dirs)[source]
Create inventory directories.
Also creates an
.anchorfile for each new directory.A list of the newly-created anchor files is returned.
- Raises:
OnyoProtectedPathError –
dirscontains an invalid path.FileExistsError –
dirscontains a path pointing to an existing file
- Return type:
list[Path]
- set_config(key, value, location='onyo')[source]
Set the value of a configuration key.
- Parameters:
key (
str) – The name of the configuration key to set.value (
str) – The value to set the configuration key to.location (
Literal['system','global','local','worktree','onyo'] (default:'onyo')) – The location to set the key/value in. Valid locations are standard git-config locations ('system','global','local', and'worktree') and'onyo'(onyo.lib.consts.ONYO_CONFIG).
- Raises:
ValueError –
locationis invalid.- Return type:
- validate_anchors()[source]
Check if all inventory directories contain an
.anchorfile.- Returns:
True if all directories contain an
.anchorfile, otherwise False.- Return type:
- validate_onyo_repo()[source]
Assert whether this a full init-ed onyo repository.
- Raises:
OnyoInvalidRepoError – Validation failed
- Return type:
- write_asset(asset)[source]
Write an asset’s contents to disk.
Pseudokeys are not included in the written YAML.
- Parameters:
asset (
Item) – The asset Item to write.path – The Path to write content to. Default is the asset’s
'onyo.path.file'pseudokey.
- Raises:
ValueError – The pseudokey
'onyo.path.file'is not a valid inventory path.- Return type: