Using -key= form to reset key value
Andrew P.
9 years ago
19 | 19 | from collections import OrderedDict |
20 | 20 | from glob import iglob |
21 | 21 | |
22 | from gi.repository import GLib | |
23 | from lightdm_gtk_greeter_settings import helpers | |
24 | ||
22 | 25 | |
23 | 26 | class Config: |
24 | 27 | |
45 | 48 | default = None |
46 | 49 | |
47 | 50 | values = self._items.get(item) |
51 | ||
52 | if values and values[-1][1] == value: | |
53 | return | |
54 | ||
48 | 55 | if values and values[-1][0] == self._config._output_path: |
49 | 56 | if default is not None and value == default and len(values) == 1: |
50 | 57 | values.clear() |
69 | 76 | values = self._items.get(key) |
70 | 77 | return values[-1][0] if values else None |
71 | 78 | |
72 | def __init__(self, input_pathes, output_path): | |
73 | self._input_pathes = tuple(input_pathes) | |
74 | self._output_path = output_path | |
79 | def __init__(self): | |
80 | self._output_path = helpers.get_config_path() | |
75 | 81 | self._groups = OrderedDict() |
76 | 82 | |
77 | 83 | def read(self): |
84 | self._groups.clear() | |
85 | ||
86 | pathes = [] | |
87 | pathes += GLib.get_system_data_dirs() | |
88 | pathes += GLib.get_system_config_dirs() | |
89 | pathes.append(os.path.dirname(os.path.dirname(self._output_path))) | |
90 | ||
78 | 91 | files = [] |
79 | for path in self._input_pathes: | |
80 | if os.path.isdir(path): | |
81 | files.extend(sorted(iglob(os.path.join(path, '*.conf')))) | |
82 | elif os.path.exists(path): | |
83 | files.append(path) | |
84 | if self._output_path not in files: | |
85 | files.append(self._output_path) | |
86 | ||
87 | self._groups.clear() | |
88 | for path in files: | |
89 | config_file = configparser.RawConfigParser(strict=False, allow_no_value=True) | |
90 | config_file.read(path) | |
92 | for path in pathes: | |
93 | files += sorted(iglob(os.path.join(path, 'lightdm', | |
94 | 'lightdm-gtk-greeter.conf.d', '*.conf'))) | |
95 | files.append(os.path.join(path, 'lightdm', 'lightdm-gtk-greeter.conf')) | |
96 | ||
97 | for path in filter(os.path.isfile, files): | |
98 | config_file = configparser.RawConfigParser(strict=False) | |
99 | if not config_file.read(path): | |
100 | continue | |
91 | 101 | |
92 | 102 | for groupname, values in config_file.items(): |
93 | 103 | if groupname == 'DEFAULT': |
98 | 108 | group = self._groups[groupname] |
99 | 109 | |
100 | 110 | for key, value in values.items(): |
111 | if key.startswith('-'): | |
112 | key = key[1:] | |
113 | value = None | |
114 | ||
101 | 115 | if key in group._items: |
102 | 116 | values = group._items[key] |
103 | 117 | if value is not None or values: |
106 | 120 | group._items[key] = [(path, value)] |
107 | 121 | |
108 | 122 | def write(self): |
109 | config_file = configparser.RawConfigParser(strict=False, allow_no_value=True) | |
123 | config_file = configparser.RawConfigParser(strict=False) | |
110 | 124 | |
111 | 125 | for groupname, group in self._groups.items(): |
112 | config_file.add_section(groupname) | |
113 | config_section = config_file[groupname] | |
114 | ||
126 | config_section = None | |
115 | 127 | for key, values in group._items.items(): |
116 | 128 | if not values or values[-1][0] != self._output_path: |
117 | 129 | continue |
130 | ||
118 | 131 | if values[-1][1] is not None or len(values) > 1: |
119 | config_section[key] = values[-1][1] | |
132 | if not config_section: | |
133 | config_file.add_section(groupname) | |
134 | config_section = config_file[groupname] | |
135 | if values[-1][1] is None: | |
136 | config_section['-' + key] = '' | |
137 | else: | |
138 | config_section[key] = values[-1][1] | |
120 | 139 | |
121 | 140 | with open(self._output_path, 'w') as file: |
122 | 141 | config_file.write(file) |
142 | ||
143 | def is_writable(self): | |
144 | if os.path.exists(self._output_path) and os.access(self._output_path, os.W_OK): | |
145 | return True | |
146 | return os.access(os.path.dirname(self._output_path), os.W_OK | os.X_OK) | |
123 | 147 | |
124 | 148 | def items(self): |
125 | 149 | return self._groups.items() |
26 | 26 | |
27 | 27 | from gi.repository import ( |
28 | 28 | Gdk, |
29 | GLib, | |
30 | 29 | Gtk) |
31 | 30 | from gi.repository import Pango |
32 | 31 | from gi.repository.GObject import markup_escape_text as escape_markup |
138 | 137 | group.entry_added.connect(self.on_entry_added) |
139 | 138 | group.entry_removed.connect(self.on_entry_removed) |
140 | 139 | |
141 | config_pathes = [] | |
142 | config_pathes.extend(os.path.join(p, 'lightdm-gtk-greeter.conf.d') | |
143 | for p in GLib.get_system_data_dirs()) | |
144 | config_pathes.extend(os.path.join(p, 'lightdm-gtk-greeter.conf.d') | |
145 | for p in GLib.get_system_config_dirs()) | |
146 | config_pathes.append(os.path.join(os.path.dirname(helpers.get_config_path()), | |
147 | 'lightdm-gtk-greeter.conf.d')) | |
148 | ||
149 | self._config = Config.Config(config_pathes, helpers.get_config_path()) | |
150 | ||
151 | self._allow_edit = self._has_access_to_write(helpers.get_config_path()) | |
140 | self._config = Config.Config() | |
141 | ||
142 | self._allow_edit = self._config.is_writable() | |
152 | 143 | self._widgets.apply.props.visible = self._allow_edit |
153 | 144 | |
154 | 145 | if not self._allow_edit: |
187 | 178 | |
188 | 179 | self._read() |
189 | 180 | |
190 | def _has_access_to_write(self, path): | |
191 | if os.path.exists(path) and os.access(helpers.get_config_path(), os.W_OK): | |
192 | return True | |
193 | return os.access(os.path.dirname(helpers.get_config_path()), os.W_OK | os.X_OK) | |
194 | ||
195 | 181 | def _set_message(self, message, type_=Gtk.MessageType.INFO): |
196 | 182 | if not message: |
197 | 183 | self._widgets.infobar.hide() |
214 | 200 | |
215 | 201 | def _write(self): |
216 | 202 | for group in self._groups: |
217 | group.write(self._config) | |
203 | group.write(self._config, self._changed_entries.__contains__) | |
218 | 204 | |
219 | 205 | if self.mode != WindowMode.Embedded: |
220 | 206 | for entry in self._changed_entries: |
54 | 54 | self._entries[name] = entry |
55 | 55 | self.entry_added.emit(entry, name) |
56 | 56 | |
57 | def write(self, config): | |
57 | def write(self, config, changed=None): | |
58 | 58 | groups = set(name for name, __ in self._entries.items()) |
59 | 59 | groups_to_remove = tuple(name for name in config |
60 | 60 | if (name.startswith(self.GROUP_PREFIX) and |
61 | 61 | name[len(self.GROUP_PREFIX):].strip() not in groups)) |
62 | 62 | |
63 | 63 | for name, entry in self._entries.items(): |
64 | if changed and not changed(entry): | |
65 | continue | |
64 | 66 | groupname = '{prefix} {name}'.format(prefix=self.GROUP_PREFIX, name=name.strip()) |
65 | 67 | group = config.add_group(groupname) |
66 | 68 | for key, value in entry: |
46 | 46 | '''Read group content from specified GreeterConfig object''' |
47 | 47 | raise NotImplementedError(self.__class__) |
48 | 48 | |
49 | def write(self, config): | |
49 | def write(self, config, changed=None): | |
50 | 50 | '''Writes content of this group to specified GreeterConfig object''' |
51 | 51 | raise NotImplementedError(self.__class__) |
52 | 52 | |
108 | 108 | entry.value = value if value is not None else self._defaults[key] |
109 | 109 | entry.enabled = value is not None |
110 | 110 | |
111 | def write(self, config): | |
111 | def write(self, config, changed=None): | |
112 | 112 | for key, entry in self._entries.items(): |
113 | if changed and not changed(entry): | |
114 | continue | |
113 | 115 | del config[self._name, key] |
114 | 116 | if entry.enabled: |
115 | 117 | config[self._name, key] = entry.value, self._get_default(key) |