Package list lightdm-gtk-greeter-settings / 5f8f297
Using -key= form to reset key value Andrew P. 7 years ago
5 changed file(s) with 56 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
1919 from collections import OrderedDict
2020 from glob import iglob
2121
22 from gi.repository import GLib
23 from lightdm_gtk_greeter_settings import helpers
24
2225
2326 class Config:
2427
4548 default = None
4649
4750 values = self._items.get(item)
51
52 if values and values[-1][1] == value:
53 return
54
4855 if values and values[-1][0] == self._config._output_path:
4956 if default is not None and value == default and len(values) == 1:
5057 values.clear()
6976 values = self._items.get(key)
7077 return values[-1][0] if values else None
7178
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()
7581 self._groups = OrderedDict()
7682
7783 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
7891 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
91101
92102 for groupname, values in config_file.items():
93103 if groupname == 'DEFAULT':
98108 group = self._groups[groupname]
99109
100110 for key, value in values.items():
111 if key.startswith('-'):
112 key = key[1:]
113 value = None
114
101115 if key in group._items:
102116 values = group._items[key]
103117 if value is not None or values:
106120 group._items[key] = [(path, value)]
107121
108122 def write(self):
109 config_file = configparser.RawConfigParser(strict=False, allow_no_value=True)
123 config_file = configparser.RawConfigParser(strict=False)
110124
111125 for groupname, group in self._groups.items():
112 config_file.add_section(groupname)
113 config_section = config_file[groupname]
114
126 config_section = None
115127 for key, values in group._items.items():
116128 if not values or values[-1][0] != self._output_path:
117129 continue
130
118131 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]
120139
121140 with open(self._output_path, 'w') as file:
122141 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)
123147
124148 def items(self):
125149 return self._groups.items()
2626
2727 from gi.repository import (
2828 Gdk,
29 GLib,
3029 Gtk)
3130 from gi.repository import Pango
3231 from gi.repository.GObject import markup_escape_text as escape_markup
138137 group.entry_added.connect(self.on_entry_added)
139138 group.entry_removed.connect(self.on_entry_removed)
140139
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()
152143 self._widgets.apply.props.visible = self._allow_edit
153144
154145 if not self._allow_edit:
187178
188179 self._read()
189180
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
195181 def _set_message(self, message, type_=Gtk.MessageType.INFO):
196182 if not message:
197183 self._widgets.infobar.hide()
214200
215201 def _write(self):
216202 for group in self._groups:
217 group.write(self._config)
203 group.write(self._config, self._changed_entries.__contains__)
218204
219205 if self.mode != WindowMode.Embedded:
220206 for entry in self._changed_entries:
5454 self._entries[name] = entry
5555 self.entry_added.emit(entry, name)
5656
57 def write(self, config):
57 def write(self, config, changed=None):
5858 groups = set(name for name, __ in self._entries.items())
5959 groups_to_remove = tuple(name for name in config
6060 if (name.startswith(self.GROUP_PREFIX) and
6161 name[len(self.GROUP_PREFIX):].strip() not in groups))
6262
6363 for name, entry in self._entries.items():
64 if changed and not changed(entry):
65 continue
6466 groupname = '{prefix} {name}'.format(prefix=self.GROUP_PREFIX, name=name.strip())
6567 group = config.add_group(groupname)
6668 for key, value in entry:
4646 '''Read group content from specified GreeterConfig object'''
4747 raise NotImplementedError(self.__class__)
4848
49 def write(self, config):
49 def write(self, config, changed=None):
5050 '''Writes content of this group to specified GreeterConfig object'''
5151 raise NotImplementedError(self.__class__)
5252
108108 entry.value = value if value is not None else self._defaults[key]
109109 entry.enabled = value is not None
110110
111 def write(self, config):
111 def write(self, config, changed=None):
112112 for key, entry in self._entries.items():
113 if changed and not changed(entry):
114 continue
113115 del config[self._name, key]
114116 if entry.enabled:
115117 config[self._name, key] = entry.value, self._get_default(key)
4242 __license__ = 'GPL-3'
4343 __version__ = 'dev'
4444 __data_directory__ = '../data/'
45 __config_path__ = 'lightdm-gtk-greeter.conf'
45 __config_path__ = 'lightdm/lightdm-gtk-greeter.conf'
4646
4747
4848 try: