Utils

class htv.utils.FsTools[source]

Bases: object

Static class to interact with files

static dump_file(path, content=None, exists_ok=False, **kwargs)[source]

Dump content into file

Dump the provided content into a file. If it does not exist, or any of the intermediate directories, they will be created To render a template set ‘content’ to ‘t:<template_name>’. Instead of dumping content as plain text, it will dump the rendered template.

Parameters:
  • path – Path of the output file.

  • content (str) – Content to be dumped into the file. Use ‘t:<template_name>’ to use a template.

  • exists_ok (bool) – If False and file already exists, raise FileExistError

  • kwargs – Additional arguments required to the render the template

Raises:

FileExistsError – If file already exists and param exists_ok is False

Return type:

None

Returns:

None

static dump_files(files, root_dir=None, exists_ok=False)[source]

Dump a list of files.

See also FsTools.dump_file()

Parameters:
  • files (Iterable) – list of tuples (path:str, content:str, kwargs:dict)

  • root_dir (str | Path) – Root dir for the files. If None, files’ path will be equal to the name provided

  • exists_ok (bool) – If False and file already exists, raise FileExistError

>>> _files = [
>>>    'any/other/dir',
>>>    ('.file1', 'lorem ipsum'),
>>>    ('file2.md', 't:readme.md'),
>>>    ('file3.md', 't:readme.md', dict(param1="value1", param2="value2"))
>>> ]
static set_clipboard(value)[source]
Parameters:

value (str | Path) – Text or path to file to be copied in the clipboard

static render_template(template, out=None, **kwargs)[source]

Render a template

Renders a template from the template directory (CONF[‘_JINJA_ENV’]) If out is provided, saves the rendered template into that file

Parameters:
  • template (str) – template to be used from /templates

  • out (str | Path) – output file to write the template. If None, just returns the rendered template

  • kwargs – Additional arguments for the render

Return type:

str

Returns:

The rendered template

static secure_filename(name)[source]

Returns a secure filename:

Returns a secure filename: lowercased, not containing special characters and with all spaces replaced by underscores (_)

Return type:

str

Returns:

Secured filename

static secure_dirname(name)[source]

Returns a secure dir name:

Returns a secure dir name: lowercased, not containing special characters and with all spaces replaced by dashes (-)

Return type:

str

Returns:

Secured dir name

static search_res_by_name_id(selector)[source]

Search resources by name or index

Search among cached entries and/or the entire vault for a resource. If found, its absolute path is returned. Index is between [1, N], where N is the number of cached resources

Parameters:

selector (int | str) – Resource name or cache index

Return type:

Path | None

Returns:

Path to the resource if found, None otherwise

class htv.utils.Conf(runtime, default)[source]

Bases: dict

Configuration class. Allows to have a callable runtime instance that read/write the changes to a file.

load(**kwargs)[source]

Load configuration

(Re)load configuration from local file. Then the provided kwargs are added. If local file does not exist it is created. The configured runtime parameters are added. Additionally, if any default parameter is missing it is added.

Parameters:

kwargs – parameters to be added to the configuration

Return type:

None

Returns:

None

update_values(**kwargs)[source]

Update configuration parameters

If a parameter contains environment variables, they are expanded again

Parameters:

kwargs – Values to be updated in the configuration

Return type:

None

Returns:

None

remove_values(*args)[source]

Remove configuration values

Remove configuration values then save changes to disk

Parameters:

args – Name(s) of the params to be removed from configuration

Return type:

None

Returns:

None

reset()[source]

Resets the configuration to defaults parameters

Return type:

None

Returns:

None