borgini¶
Ini config for borg backup.
- class borgini.Data(appdir: str, profile: str)[source]¶
Bases:
objectResolve 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
borginiis being run under.
- get_exclude() Tuple[str, ...][source]¶
Return the values obtained from the
excludefile.Unlike the
includefile this doesn’t necessarily need to be populatedEvery item will automatically be prefixed with
--excludefor theborgbackupcommandline- 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
includefile.- 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
- class borgini.PygmentPrint(styles: str)[source]¶
Bases:
objectInstantiate 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
stylesconfig file.
borgini.config¶
All things config.ini
- class borgini.config.Config(raw_config: borgini.config.RawConfig)[source]¶
Bases:
borgini.config.ProxyFinal config object suitable for running with python methods.
Subclass the
config.Proxyclass to inherit the translated config object fromConfigParser->Dict.- Parameters
raw_config – The
configparser.ConfigParserobject.
- 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_PASSPHRASEenvironment variable (as the string value"None"would have been omitted byconfig.Proxy) returnNonein 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:
objectSubclass
RawConfigto inherit theConfigParserobject.Translate the string values into boolean and
NoneTypevalues or some of the tests will not work i.e.NoneandFalsewill beTrueas the strings"None"and"False"InheritRawConfigafter theparser.Catchstring has identified run-time errors.- Parameters
raw_config – Instantiated
RawConfigobject containing theconfigparser.ConfigParserobject asparser.
- class borgini.config.RawConfig(configpath: str | os.PathLike)[source]¶
Bases:
objectContains the
configparser.ConfigParserobject.Write to and read from the
config.inifile. 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 intoconfig.Proxyfirst.- 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.inifile and avoid non-critical errors.Once the
config.iniis 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.
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.
namespace –
argparse.ArgumentParser’sNamespace.__dict__.files – Dictionary of config file paths.
pygments – Instantiated
print.PygmentPrintclass 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.PygmentPrintclass 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
ArgumentParserNamespace.__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
sshisTruethis will return the configured ssh path on the remote system. IfsshisFalsethe repository will need exist on the localhost.Note: The
repopathsetting in theDEFAULTsection will be the directory the repository is in, not the actual repository i.e./path/to/repopath/reponamenot/path/to/repopath.- Parameters
borguser – The remote’s user invoking
borgon 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.iniparameters.
- 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
Catchobject.
- Returns
Instantiated
ConfigParserobject.
- borgini.funcs.initialize_datafiles(data: borgini._core.Data) None[source]¶
Write the default settings to the config files.
- Parameters
data –
data.Data.
- borgini.funcs.list_profiles(show_profiles: List[str], pygments: borgini._core.PygmentPrint) None[source]¶
If
show_profilesthen display a list of profiles that exist.- Parameters
show_profiles – Boolean switch from the commandline.
pygments – Instantiated
print.PygmentPrintclass 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.inicolor the text with ini-style syntax, otherwise shell-like syntax.- Parameters
filepath – The file to read from.
pygments – Instantiated
print.PygmentPrintclass. 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.ArgumentParserand the commandline.
- borgini.funcs.set_passphrase(keyfile: str, catch: borgini.parser.Catch) None[source]¶
Export the
BORG_PASSPHRASEenv var from a keyfile.- Parameters
keyfile – The absolute path to the passphrase keyfile.
catch –
parser.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:
objectExit 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
borgbackupprompts for a passphrase.If no keyfile is necessary then the field should be
Nonewhich this message would also notify the user about.