cihai / cihai
1
# -*- coding: utf8 - *-
2 2
from __future__ import absolute_import, print_function, unicode_literals
3

4 2
import os
5

6 2
from appdirs import AppDirs
7

8 2
from ._compat import string_types
9

10

11 2
def expand_config(d, dirs):
12
    """
13
    Expand configuration XDG variables, environmental variables, and tildes.
14

15
    Parameters
16
    ----------
17
    d : dict
18
        config information
19
    dirs : appdirs.AppDirs
20
        XDG application mapping
21

22
    Notes
23
    -----
24
    *Environmentable variables* are expanded via :py:func:`os.path.expandvars`.
25
    So ``${PWD}`` would be replaced by the current PWD in the shell,
26
    ``${USER}`` would be the user running the app.
27

28
    *XDG variables* are expanded via :py:meth:`str.format`. These do not have a
29
    dollar sign. They are:
30

31
    - ``{user_cache_dir}``
32
    - ``{user_config_dir}``
33
    - ``{user_data_dir}``
34
    - ``{user_log_dir}``
35
    - ``{site_config_dir}``
36
    - ``{site_data_dir}``
37

38
    See Also
39
    --------
40
    os.path.expanduser, os.path.expandvars :
41
        Standard library functions for expanding variables. Same concept, used inside.
42
    """
43 2
    context = {
44
        'user_cache_dir': dirs.user_cache_dir,
45
        'user_config_dir': dirs.user_config_dir,
46
        'user_data_dir': dirs.user_data_dir,
47
        'user_log_dir': dirs.user_log_dir,
48
        'site_config_dir': dirs.site_config_dir,
49
        'site_data_dir': dirs.site_data_dir,
50
    }
51

52 2
    for k, v in d.items():
53 2
        if isinstance(v, dict):
54 2
            expand_config(v, dirs)
55 2
        if isinstance(v, string_types):
56 2
            d[k] = os.path.expanduser(os.path.expandvars(d[k]))
57 2
            d[k] = d[k].format(**context)
58

59

60 2
class Configurator(object):
61 2
    def __init__(self, namespace=''):
62
        """
63
        Manage config. Provides facilities for loading / writing configs.
64

65
        Used on Cihai and available for its extensions.
66

67
        Parameters
68
        ----------
69
        namespace : str, optional
70
            Creates a configuration object that reads/writes on a namespace.
71

72
            Leaving this empty / reads and writes to the root level cihai config.
73

74
            Namespace is designed for plugins to manage settings.
75

76
        Attributes
77
        ----------
78
        dirs : appdirs.AppDirs
79
            XDG App directory locations for cihai
80

81
        Class Attributes
82
        ----------------
83
        data : dict
84
            property / getter for options
85
        _data : dict
86
            where the raw dictionary resides
87
        """
88 2
        self.dirs = AppDirs("cihai", "cihai team")  # appname  # app author
89

90 2
        self.namespace = namespace
91

92 2
    def get_names(self):
93
        """
94
        Return a list of possible places config can reside, and order of search.
95

96
        This is based on XDG. So it will look for
97
        """
98 0
        pass
99

100 2
    @property
101 1
    def file(self):
102
        """Find a config file where it exists, as the first place."""
103 0
        return
104

105 2
    def read(self):
106
        """Read to dictionary."""
107 0
        return
108

109 2
    def get_delta(self, **updates):
110
        """Returns the difference of whatever user customizations differ from
111
        cihai.constants.DEFAULT_CONFIG.
112
        """
113 0
        pass
114

115 2
    def write(self, **updates):
116
        """If no delta is created from DEFAULT, it not write.
117

118
        If file doesn't exist, it will create.
119
        """
120 0
        if updates:
121 0
            self._data.update(**updates)
122
        # save file
123 0
        pass

Read our documentation on viewing source code .

Loading