SCAutolib.models.file

This module contains classes that represent configuration files. Each class contains information and methods to manipulate specific config file except the parent (File) class that is supposed to operate on general config file.

Basic operations on config files defined in this module:
  • create

    creates content of config file usually based on template. Note, that some child classes may also update content of config file if it already existed and backup original file.

  • set

    modify values of config files, add keys or sections if necessary

  • save

    save modified content to config file

  • clean

    remove config file; note that some child classes may also restore original config file if backup exists.

Classes

class File(filepath, template=None)[source]

This class defines an interface for generic operations on config files

  • create: create content of config file based on template file

  • set: modify content of config files, add keys or sections if necessary

  • save: save config file

  • clean: remove config file

Note

Set method operates only on:

  • files compatible with ConfigParser (i.e. files containing sections)

  • simple config files without sections.

Other formats of config files are not supported.

Init of File

Parameters:
  • filepath (str or pathlib.Path) – Path of config file

  • template (str) – Path of template file

backup(name=None)[source]

Save original file to the backup directory with given name. If name is None, default name is <filename>.<extension>.backup

Parameters:

name (str) – custom file name to be set for the file

Returns:

path where the file is stored

clean()[source]

Removes config file

create()[source]

Populate internal parser object with content based on template.

exists()[source]

Checks if a file exists. Returns boolean.

get(key, section=None, separator='=')[source]

Method processes and returns the value of the key in section. If the section is not provided (section=None), then file would be parsed line by line splitting the line on separator. First match wins and is returned.

If section is provided and the file can be parsed by the ConfigParser, then this object would be used to look for the key.

Parameters:
  • key – required key

  • section (Optional[str]) – section where the key should be found

  • separator (str) – applicable only for non-configparser file. Separator that would be used to so split a line from the file. By default separator is ‘=’

Raises:
  • SCAutolib.SCAutolibException – if the key is not found the non-ConfigParser file

  • configparser.NoSectionError – if the section is not found in ConfigParser-supported file

  • KeyError – if the key is not present in ConfigParser-supported file

Returns:

value of the key in section (if set)

remove()[source]

Removes the file if it exists.

restore(name=None)[source]

Copies backup file to original file location.

save()[source]

Save content of config file stored in parser object to config file.

set(key, value, section=None, separator='=')[source]

Modify value in config file. Modification is made through the ConfigParser object if it is defined. If not, then key value pair would be written to the file through normal write() method with composed string in the following form <key><separator><value>

Note

spaces around key has to be specified as a part of the separator parameter.

Parameters:
  • key (str) – value for this key will be updated

  • value (int or str or bool) – new value to be stored in [section][key] of config file

  • section (str) – section of config file that will be modified

  • separator (str) – Character to be used as a separator between key and value in files that are not supported by ConfigParser object.

class OpensslCnf(filepath, conf_type, replace)[source]

This class provides information and methods to create and modify openssl cnf files.

Init of opensslCNF

Parameters:
  • filepath (str or pathlib.Path) – Path of config file

  • conf_type (basestring) – Identifier of cnf file

  • replace (list) – list of strings that will replace specific strings from template

create()[source]

Populate internal file object with content based on template and update specific strings

save()[source]

Save content stored in internal file object to config file.

class SSSDConf[source]

This class contains information and methods to create and modify /etc/sssd/sssd.conf file.

It is implemented as singleton, which allows to use class object _default_parser as representation of content of sssd.conf file during runtime.

Intended use is to create/update and save config file in first runtime and load content of config file to internal parser object in following runtimes.

Init of File

Parameters:
  • filepath (str or pathlib.Path) – Path of config file

  • template (str) – Path of template file

check_backups()[source]

Raises an exception if internal backup files already exists

create()[source]

Populate internal parser object with content from existing config file and update it with values from config template. Back up original files.

restore()[source]

Removes sssd.conf file in case it was created by this package or restore original sssd.conf in case the file was modified.

save()[source]

Save content of config file stored in parser object to config file.

set(key, value, section=None)[source]

Modify or add content of config file represented by ConfigParser object

If a value is set outside of a context manager, it is the user’s responsibility to revert it.

Parameters:
  • key (str) – key from section of config file to be updated

  • value (int or bool or str) – new value to be stored in [section][key] of config file

  • section (str) – section of config file to be created/updated

update_default_content()[source]

Populate internal parser object with content from current config file.

class SoftHSM2Conf(filepath, card_dir)[source]

This class provide information and methods to create and modify softhsm2.conf file.

Init of SoftHSM2Conf

Parameters:
  • filepath (str) – path where config file should be saved

  • card_dir (str) – parameter to be updated in config file

create()[source]

Populate internal file object with content based on template.

save()[source]

Save content stored in internal file object to config file.

set(*args)[source]
Raises:

NotImplementedError – if this method is called on SoftHSM2Conf.