Skip to content

Xml

Control XML module.

XmlConfig

Manage XML configurations.

__init__(self, **kwargs) special

Initialize XML configuration module.

Source code in compendium/filetypes/xml.py
19
20
21
22
23
24
25
def __init__(self, **kwargs: Any) -> None:
    '''Initialize XML configuration module.'''
    logging.info('Inializing XmlConfig')
    self.encoding = kwargs.get('encoding', 'utf-8')
    self.encoder = kwargs.get('encoder', str)
    self.process_namespaces = kwargs.get('process_namespaces', False)
    self.namespaces = kwargs.get('namespaces', None)

dump_config(self, content, filepath)

Save settings to XML configuration.

Source code in compendium/filetypes/xml.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def dump_config(self, content: Dict[str, Any], filepath: str) -> None:
    '''Save settings to XML configuration.'''
    try:
        with open(filepath, 'w') as f:
            f.write(
                xmltodict.unparse(
                    content,
                    encoding=self.encoding,
                    pretty=True
                )
            )
    except IOError as err:
        if err.errno == errno.EACCES:
            logging.error(
                'You do not have permission to write to this file'
            )
            raise

filetypes() staticmethod

Return supported XML configuration filetypes.

Source code in compendium/filetypes/xml.py
27
28
29
30
@staticmethod
def filetypes() -> Tuple[str, ...]:
    '''Return supported XML configuration filetypes.'''
    return ('xml',)

load_config(self, filepath)

Load settings from XML configuration.

Source code in compendium/filetypes/xml.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def load_config(self, filepath: str) -> Dict[str, Any]:
    '''Load settings from XML configuration.'''
    logging.info('loading XML configuration file')
    if os.path.isfile(filepath):
        with open(filepath, 'r') as f:
            content = xmltodict.parse(
                f.read(),
                encoding=self.encoding,
                process_namespaces=self.process_namespaces,
                namespaces=self.namespaces
            )
    else:
        content = {}
    return content