borgini

Ini config for borg backup.

class borgini.Data(appdir: str, profile: str)[source]

Bases: object

Resolve the directory the configurations belong to.

Get the list of files to include and exclude. Allow the files to contain comments and avoid parsing them.

Parameters
  • appdir – Application data dirs.

  • profile – The profile that borgini is being run under.

get_exclude() Tuple[str, ...][source]

Return the values obtained from the exclude file.

Unlike the include file this doesn’t necessarily need to be populated

Every item will automatically be prefixed with --exclude for the borgbackup commandline

Returns

A lit of paths to exclude - this will override items in include

get_include() Tuple[str, ...][source]

Directories and files to include from the include file.

Returns

Tuple of paths to include in backups for that profile

get_path(key: str) str[source]

Get the path of the file by calling its key.

Parameters

key – Name of the basename file

Returns

The file’s absolute path

initialize_data_files(pathlists: Tuple[Tuple[str, ...], Tuple[str, ...], Tuple[str, ...]]) None[source]

Initialize persistent data files.

If the include or exclude files do not exist write the

starter templates to file.

Parameters

pathlists – Tuple containing two tuples of lines to write to file.

make_appdir() None[source]

Create the config directory for all the user’s settings.

class borgini.PygmentPrint(styles: str)[source]

Bases: object

Instantiate with the path to the config file for syntax styles.

Class will read the file and maintain its config throughout the process.

Parameters

styles – The path to the styles config file.

print(string: str, ini: bool = True) None[source]

Print with pygments.

Read the string entered in method. Configure syntax highlighting for either shell or ini files and processes.

Parameters
  • string – What is to be printed.

  • iniTrue for printing ini files, False for shell.

read_styles() str[source]

Read the styles file into the buffer.

Parse the configuration that is uncommented and ignore the rest.

Returns

Style config.

borgini.main() None[source]

Main function for package.

borgini.config

All things config.ini

class borgini.config.Config(raw_config: borgini.config.RawConfig)[source]

Bases: borgini.config.Proxy

Final config object suitable for running with python methods.

Subclass the config.Proxy class to inherit the translated config object from ConfigParser -> Dict.

Parameters

raw_config – The configparser.ConfigParser object.

get_key(section: str, key: str) str | None[source]

Get a key from the dictionary object in self.

If the value is not found such as the BORG_PASSPHRASE environment variable (as the string value "None" would have been omitted by config.Proxy) return None in its place to avoid an expected error and carry on omitting the key.

Parameters
  • section – The primary key and the section from configparser.ConfigParser.

  • key – The key containing the configured value.

Returns

The value of the called key.

get_keytuple(**kwargs: t.Tuple[str, ...]) tuple[str | bool | None, ...][source]

Return multiple values at once from self.dict[section].

Return as a tuple that can be unpacked by the keys passed to the method.

Parameters

kwargs – Sections to get.

Returns

A tuple of multiple any one or more values.

return_all(section: str) Tuple[str, ...][source]

Get all args belonging to a section.

The (kw)args that are boolean flags only need to exist, as their existence in the script is the switch - so these are not returned as kwargs but rather args. Treat values that aren’t boolean differently as the key and the value need to be included.

Parameters

section – The section from which the (kw)args should be retrieved.

Returns

A tuple of all switches prefixed with "--" and all kwargs.

class borgini.config.Proxy(raw_config: borgini.config.RawConfig)[source]

Bases: object

Subclass RawConfig to inherit the ConfigParser object.

Translate the string values into boolean and NoneType values or some of the tests will not work i.e. None and False will be True as the strings "None" and "False" Inherit RawConfig after the parser.Catch string has identified run-time errors.

Parameters

raw_config – Instantiated RawConfig object containing the configparser.ConfigParser object as parser.

convert_proxy() Dict[str, Any][source]

Convert ConfigParser into python friendly dictionary.

Returns

Dictionary object.

section_list() List[str][source]

Index the sections of the config.ini file.

Returns

List of config sections.

class borgini.config.RawConfig(configpath: str | os.PathLike)[source]

Bases: object

Contains the configparser.ConfigParser object.

Write to and read from the config.ini file. The boolean values get written to the file as strings, and they get read from the file into the buffer as strings too. Not all values are as they should be when read into Python so this will get subclassed into config.Proxy first.

Parameters

configpath – The path to the config.ini file - this depends on the profile used and whether this is run as "$USER" or as root.

read() None[source]

Read the config.ini file and avoid non-critical errors.

Once the config.ini is read and loaded into the buffer write it back to the file as this class will filter out non-parsable configurations back to their default. If there is a key in the config.ini file that cannot be parsed skip reading it into buffer, as it will be removed once the config is subsequently written. Any new keys and configurations that may be added in the future will also be safely added to the config.

write_new_config() None[source]

Load default values into the ConfigParser and write.

write_values() None[source]

Write values from ConfigParser to the config file.

borgini.funcs

Package entry point

Parse commandline arguments

Invoke classes, methods and functions

borgini.funcs.edit_file(editor: str, namespace: Dict[str, str], files: Dict[str, str], pygments: borgini._core.PygmentPrint, dry: bool) None[source]

Call the editor to edit a file.

If the argument passed does not correspond to an existing file print help. If an editor is not provided go on to simply print the file content.

Parameters
  • editor – The editor to edit the file with.

  • namespaceargparse.ArgumentParser’s Namespace.__dict__.

  • files – Dictionary of config file paths.

  • pygments – Instantiated print.PygmentPrint class configured with user’s style option.

  • dry – Dry mode for when we do not want to execute the code.

borgini.funcs.edit_files(edit: str, file: str, pygments: borgini._core.PygmentPrint, dry: bool) None[source]

Edit a config file with the editor of choice.

Parameters
  • edit – The editor to edit with.

  • file – The path to the file to edit.

  • pygments – Instantiated print.PygmentPrint class configured with user’s style option.

  • dry – Dry mode for when we do not want to execute the code.

borgini.funcs.get_configdir() str[source]

Get path to the config most suitable for active os and privilege.

Returns

Path to configuration dir for all profiles.

borgini.funcs.get_file_arg(namespace: t.Dict[str, str], files: t.Dict[str, str]) str | None[source]

Detect that a file has been selected to edit or view.

Parameters
  • namespace – The ArgumentParser Namespace.__dict__.

  • files – The file’s dictionary returned from data.Data.

Returns

Return an absolute path or None.

borgini.funcs.get_path(borguser: str, hostname: str, port: str, repopath: str, ssh: bool) str[source]

Get the path to the backup repository location.

If ssh is True this will return the configured ssh path on the remote system. If ssh is False the repository will need exist on the localhost.

Note: The repopath setting in the DEFAULT section will be the directory the repository is in, not the actual repository i.e. /path/to/repopath/reponame not /path/to/repopath.

Parameters
  • borguser – The remote’s user invoking borg on that machine.

  • hostname – The remote’s hostname.

  • port – The port that the remote allows ssh through.

  • repopath – The path relative to root on the remote or the localhost.

  • ssh – Boolean for whether using ssh or not.

Returns

A path configured based on the config.ini parameters.

borgini.funcs.initialize_config(configpath: str, catch: borgini.parser.Catch) borgini.config.Config[source]

If a config file does not exist create a default, else read.

Parameters
  • configpath – Path to config file.

  • catch – Instantiated Catch object.

Returns

Instantiated ConfigParser object.

borgini.funcs.initialize_datafiles(data: borgini._core.Data) None[source]

Write the default settings to the config files.

Parameters

datadata.Data.

borgini.funcs.list_profiles(show_profiles: List[str], pygments: borgini._core.PygmentPrint) None[source]

If show_profiles then display a list of profiles that exist.

Parameters
  • show_profiles – Boolean switch from the commandline.

  • pygments – Instantiated print.PygmentPrint class configured with user’s style option.

borgini.funcs.normalize_ntpath(path: str) str[source]

Format NT path to a Unix-like path.

Parameters

path – Path to format.

Returns

Returns a formatted path if running Windows otherwise the same path that came in.

borgini.funcs.read_file(filepath: str, pygments: borgini._core.PygmentPrint) None[source]

Read the file and print the output.

If reading config.ini color the text with ini-style syntax, otherwise shell-like syntax.

Parameters
  • filepath – The file to read from.

  • pygments – Instantiated print.PygmentPrint class. configured with user’s style option.

borgini.funcs.remove_profile(remove: List[str]) None[source]

Remove selected profile.

Parameters

remove – The profile entered to remove via argparse.ArgumentParser and the commandline.

borgini.funcs.set_passphrase(keyfile: str, catch: borgini.parser.Catch) None[source]

Export the BORG_PASSPHRASE env var from a keyfile.

Parameters
  • keyfile – The absolute path to the passphrase keyfile.

  • catchparser.Catch - exit if necessary and inform user why.

borgini.parser

Parse the commandline arguments and check arguments for errors and exit informatively

class borgini.parser.Catch(profile: str)[source]

Bases: object

Exit if it can be determined essential values are missing.

Exit informatively.

Parameters

profile – Profile that the process is running under so that suggestion commands can be displayed.

announce_first_run() None[source]

Announce that a new default config file has been initialized.

Not an error. Exit with zero exit status.

announce_keyfile() None[source]

Announce keyfile has been created.

If the keyfile section of the config file has been populated but the keyfile cannot be found let the user know as this would make it clearer as to why the backup might fail eventually if borgbackup prompts for a passphrase.

If no keyfile is necessary then the field should be None which this message would also notify the user about.

check_repopath_config(repopath: str) None[source]

Exit process if the path to the backup repo not provided.

Exit with a non-zero exit status.

Parameters

repopath – Path to back up repository or None.

show_command(arg: str) str[source]

Format and color the example commands provided.

Parameters

arg – The argument the command would need.

Returns

The formatted and colored example command.

class borgini.parser.RawParser[source]

Bases: argparse.ArgumentParser

Inherit ArgumentParser to be parsed from the commandline.

Format a separate Namespace object to get arguments by group.

borgini.parser.getcolor(string: str, code: int) str[source]

Return a given string in color depending on the code provided.

Parameters
  • string – String to color.

  • code – Corresponding ANSI escape code.

Returns

The colored string.