~lightdm-gtk-greeter-team/lightdm-gtk-greeter-settings/trunk

« back to all changes in this revision

Viewing changes to lightdm_gtk_greeter_settings/Config.py

  • Committer: Andrew P.
  • Date: 2015-03-25 10:57:49 UTC
  • Revision ID: pan.pav.7c5@gmail.com-20150325105749-noqv9ntej87t1mlv
Using -key= form to reset key value

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
from collections import OrderedDict
21
21
from glob import iglob
22
22
 
 
23
from gi.repository import GLib
 
24
from lightdm_gtk_greeter_settings import helpers
 
25
 
23
26
 
24
27
class Config:
25
28
 
46
49
                default = None
47
50
 
48
51
            values = self._items.get(item)
 
52
 
 
53
            if values and values[-1][1] == value:
 
54
                return
 
55
 
49
56
            if values and values[-1][0] == self._config._output_path:
50
57
                if default is not None and value == default and len(values) == 1:
51
58
                    values.clear()
70
77
            values = self._items.get(key)
71
78
            return values[-1][0] if values else None
72
79
 
73
 
    def __init__(self, input_pathes, output_path):
74
 
        self._input_pathes = tuple(input_pathes)
75
 
        self._output_path = output_path
 
80
    def __init__(self):
 
81
        self._output_path = helpers.get_config_path()
76
82
        self._groups = OrderedDict()
77
83
 
78
84
    def read(self):
 
85
        self._groups.clear()
 
86
 
 
87
        pathes = []
 
88
        pathes += GLib.get_system_data_dirs()
 
89
        pathes += GLib.get_system_config_dirs()
 
90
        pathes.append(os.path.dirname(os.path.dirname(self._output_path)))
 
91
 
79
92
        files = []
80
 
        for path in self._input_pathes:
81
 
            if os.path.isdir(path):
82
 
                files.extend(sorted(iglob(os.path.join(path, '*.conf'))))
83
 
            elif os.path.exists(path):
84
 
                files.append(path)
85
 
        if self._output_path not in files:
86
 
            files.append(self._output_path)
 
93
        for path in pathes:
 
94
            files += sorted(iglob(os.path.join(path, 'lightdm',
 
95
                                               'lightdm-gtk-greeter.conf.d', '*.conf')))
 
96
            files.append(os.path.join(path, 'lightdm', 'lightdm-gtk-greeter.conf'))
87
97
 
88
 
        self._groups.clear()
89
 
        for path in files:
90
 
            config_file = configparser.RawConfigParser(strict=False, allow_no_value=True)
91
 
            config_file.read(path)
 
98
        for path in filter(os.path.isfile, files):
 
99
            config_file = configparser.RawConfigParser(strict=False)
 
100
            if not config_file.read(path):
 
101
                continue
92
102
 
93
103
            for groupname, values in config_file.items():
94
104
                if groupname == 'DEFAULT':
99
109
                group = self._groups[groupname]
100
110
 
101
111
                for key, value in values.items():
 
112
                    if key.startswith('-'):
 
113
                        key = key[1:]
 
114
                        value = None
 
115
 
102
116
                    if key in group._items:
103
117
                        values = group._items[key]
104
118
                        if value is not None or values:
107
121
                        group._items[key] = [(path, value)]
108
122
 
109
123
    def write(self):
110
 
        config_file = configparser.RawConfigParser(strict=False, allow_no_value=True)
 
124
        config_file = configparser.RawConfigParser(strict=False)
111
125
 
112
126
        for groupname, group in self._groups.items():
113
 
            config_file.add_section(groupname)
114
 
            config_section = config_file[groupname]
115
 
 
 
127
            config_section = None
116
128
            for key, values in group._items.items():
117
129
                if not values or values[-1][0] != self._output_path:
118
130
                    continue
 
131
 
119
132
                if values[-1][1] is not None or len(values) > 1:
120
 
                    config_section[key] = values[-1][1]
 
133
                    if not config_section:
 
134
                        config_file.add_section(groupname)
 
135
                        config_section = config_file[groupname]
 
136
                    if values[-1][1] is None:
 
137
                        config_section['-' + key] = ''
 
138
                    else:
 
139
                        config_section[key] = values[-1][1]
121
140
 
122
141
        with open(self._output_path, 'w') as file:
123
142
            config_file.write(file)
124
143
 
 
144
    def is_writable(self):
 
145
        if os.path.exists(self._output_path) and os.access(self._output_path, os.W_OK):
 
146
            return True
 
147
        return os.access(os.path.dirname(self._output_path), os.W_OK | os.X_OK)
 
148
 
125
149
    def items(self):
126
150
        return self._groups.items()
127
151