Skip to content

Json

Control JSON module.

JsonConfig

Manage JSON configurations.

__init__(self, **kwargs) special

Initialize JSON configuration module.

Source code in compendium/filetypes/json.py
20
21
22
23
def __init__(self, **kwargs: Any) -> None:
    '''Initialize JSON configuration module.'''
    logging.info('Inializing JsonConfig')
    self.encoding = kwargs.get('encoding', 'utf-8')

dump_config(self, content, filepath)

Save settings to JSON configuration.

Source code in compendium/filetypes/json.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def dump_config(self, content: Dict[str, Any], filepath: str) -> None:
    '''Save settings to JSON configuration.'''
    try:
        with open(filepath, 'w') as f:
            json.dump(
                content, f, indent=2, sort_keys=True
                # , default=self.encoder
            )
    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 support JSON filetypes.

Source code in compendium/filetypes/json.py
26
27
28
29
@staticmethod
def filetypes() -> Tuple[str, ...]:
    '''Return support JSON filetypes.'''
    return ('json',)

load_config(self, filepath)

Load settings from JSON configuration.

Source code in compendium/filetypes/json.py
31
32
33
34
35
36
37
38
39
def load_config(self, filepath: str) -> Dict[str, Any]:
    '''Load settings from JSON configuration.'''
    logging.info('loading JSON configuration file')
    if os.path.isfile(filepath):
        with open(filepath, 'r', encoding=self.encoding) as f:
            content = json.load(f)
    else:
        content = {}
    return content