|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- |
| 3 | +# LightDM GTK Greeter Settings |
| 4 | +# Copyright (C) 2015 Andrew P. <[email protected]> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify it |
| 7 | +# under the terms of the GNU General Public License version 3, as published |
| 8 | +# by the Free Software Foundation. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, but |
| 11 | +# WITHOUT ANY WARRANTY; without even the implied warranties of |
| 12 | +# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 13 | +# PURPOSE. See the GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License along |
| 16 | +# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +import configparser |
| 19 | +import os |
| 20 | +from collections import OrderedDict |
| 21 | +from glob import iglob |
| 22 | + |
| 23 | + |
| 24 | +class Config: |
| 25 | + |
| 26 | + class ConfigGroup: |
| 27 | + |
| 28 | + def __init__(self, config): |
| 29 | + self._config = config |
| 30 | + self._items = OrderedDict() |
| 31 | + |
| 32 | + def __iter__(self): |
| 33 | + return iter(self._items) |
| 34 | + |
| 35 | + def __contains__(self, item): |
| 36 | + return item in self._items |
| 37 | + |
| 38 | + def __getitem__(self, item): |
| 39 | + values = self._items.get(item) |
| 40 | + return values[-1][1] if values else None |
| 41 | + |
| 42 | + def __setitem__(self, item, value): |
| 43 | + if isinstance(value, tuple): |
| 44 | + value, default = value |
| 45 | + else: |
| 46 | + default = None |
| 47 | + |
| 48 | + values = self._items.get(item) |
| 49 | + if values and values[-1][0] == self._config._output_path: |
| 50 | + if default is not None and value == default and len(values) == 1: |
| 51 | + values.clear() |
| 52 | + else: |
| 53 | + values[-1] = (self._config._output_path, value) |
| 54 | + elif values is not None: |
| 55 | + if default is None or value != default or (values and values[-1][1] != default): |
| 56 | + values.append((self._config._output_path, value)) |
| 57 | + else: |
| 58 | + if default is None or value != default: |
| 59 | + self._items[item] = [(self._config._output_path, value)] |
| 60 | + |
| 61 | + def __delitem__(self, item): |
| 62 | + values = self._items.get(item) |
| 63 | + if values is not None: |
| 64 | + if values and values[-1][0] == self._config._output_path: |
| 65 | + del values[-1] |
| 66 | + if not values: |
| 67 | + del self._items[item] |
| 68 | + |
| 69 | + def get_key_file(self, key): |
| 70 | + values = self._items.get(key) |
| 71 | + return values[-1][0] if values else None |
| 72 | + |
| 73 | + def __init__(self, input_pathes, output_path): |
| 74 | + self._input_pathes = tuple(input_pathes) |
| 75 | + self._output_path = output_path |
| 76 | + self._groups = OrderedDict() |
| 77 | + |
| 78 | + def read(self): |
| 79 | + 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) |
| 87 | + |
| 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) |
| 92 | + |
| 93 | + for groupname, values in config_file.items(): |
| 94 | + if groupname == 'DEFAULT': |
| 95 | + continue |
| 96 | + |
| 97 | + if groupname not in self._groups: |
| 98 | + self._groups[groupname] = Config.ConfigGroup(self) |
| 99 | + group = self._groups[groupname] |
| 100 | + |
| 101 | + for key, value in values.items(): |
| 102 | + if key in group._items: |
| 103 | + values = group._items[key] |
| 104 | + if value is not None or values: |
| 105 | + values.append((path, value)) |
| 106 | + elif value is not None: |
| 107 | + group._items[key] = [(path, value)] |
| 108 | + |
| 109 | + def write(self): |
| 110 | + config_file = configparser.RawConfigParser(strict=False, allow_no_value=True) |
| 111 | + |
| 112 | + for groupname, group in self._groups.items(): |
| 113 | + config_file.add_section(groupname) |
| 114 | + config_section = config_file[groupname] |
| 115 | + |
| 116 | + for key, values in group._items.items(): |
| 117 | + if not values or values[-1][0] != self._output_path: |
| 118 | + continue |
| 119 | + if values[-1][1] is not None or len(values) > 1: |
| 120 | + config_section[key] = values[-1][1] |
| 121 | + |
| 122 | + with open(self._output_path, 'w') as file: |
| 123 | + config_file.write(file) |
| 124 | + |
| 125 | + def items(self): |
| 126 | + return self._groups.items() |
| 127 | + |
| 128 | + def allitems(self): |
| 129 | + return ((g, k, items[k]) for (g, items) in self._groups.items() for k in items._items) |
| 130 | + |
| 131 | + def add_group(self, name): |
| 132 | + if name in self._groups: |
| 133 | + return self._groups[name] |
| 134 | + else: |
| 135 | + return self._groups.setdefault(name, Config.ConfigGroup(self)) |
| 136 | + |
| 137 | + def get_key_file(self, groupname, key): |
| 138 | + group = self._groups.get(groupname) |
| 139 | + return group.get_key_file(key) if group is not None else None |
| 140 | + |
| 141 | + def __iter__(self): |
| 142 | + return iter(self._groups) |
| 143 | + |
| 144 | + def __getitem__(self, item): |
| 145 | + if isinstance(item, tuple): |
| 146 | + group = self._groups.get(item[0]) |
| 147 | + return group[item[1]] if group else None |
| 148 | + return self._groups.get(item) |
| 149 | + |
| 150 | + def __setitem__(self, item, value): |
| 151 | + if isinstance(item, tuple): |
| 152 | + if not item[0] in self._groups: |
| 153 | + self._groups = Config.ConfigGroup(self) |
| 154 | + self._groups[item[0]][item[1]] = value |
| 155 | + |
| 156 | + def __delitem__(self, item): |
| 157 | + if isinstance(item, tuple): |
| 158 | + group = self._groups.get(item[0]) |
| 159 | + if group is not None: |
| 160 | + del group[item[1]] |
| 161 | + return |
| 162 | + |
| 163 | + group = self._groups.get(item) |
| 164 | + if group is not None: |
| 165 | + if not group: |
| 166 | + del self._groups[item] |
| 167 | + return |
| 168 | + |
| 169 | + keys_to_remove = [] |
| 170 | + for key, values in group._items.items(): |
| 171 | + if values[-1][0] == self._output_path: |
| 172 | + if len(values) == 1: |
| 173 | + keys_to_remove.append(key) |
| 174 | + else: |
| 175 | + values[-1] = (self._output_path, None) |
| 176 | + elif values: |
| 177 | + values.append((self._output_path, None)) |
| 178 | + |
| 179 | + if len(keys_to_remove) < len(group._items): |
| 180 | + for key in keys_to_remove: |
| 181 | + del group._items[key] |
| 182 | + else: |
| 183 | + del self._groups[item] |
0 commit comments