1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#!/usr/bin/python3
#
# This file is part of Checkbox.
#
# Copyright 2009 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
#
import re
import sys
import posixpath
from subprocess import check_output, PIPE
from checkbox.lib.conversion import string_to_type
# Command to retrieve gconf information.
COMMAND = "gconftool-2 -R / --direct --config-source xml:readwrite:$source"
# Source directory containing gconf information.
SOURCE = "~/.gconf"
def get_gconf(output):
id = None
id_pattern = re.compile(r"\s+(?P<id>\/.+):")
key_value_pattern = re.compile(r"\s+(?P<key>[\w\-]+) = (?P<value>.*)")
list_value_pattern = re.compile(r"\[(?P<list>[^\]]*)\]")
# TODO: add support for multi-line values
gconf = {}
for line in output.decode("utf-8").split("\n"):
if not line:
continue
match = id_pattern.match(line)
if match:
id = match.group("id")
continue
match = key_value_pattern.match(line)
if match:
key = match.group("key")
value = match.group("value")
if value == "(no value set)":
value = None
else:
match = list_value_pattern.match(value)
if match:
list_string = match.group("list")
if len(list_string):
value = list_string.replace(",", " ")
else:
value = ""
else:
value = string_to_type(value)
name = "%s/%s" % (id, key)
gconf[name] = value
continue
return gconf
def main():
source = posixpath.expanduser(SOURCE)
command = COMMAND.replace("$source", source)
command_output = check_output(command, stderr=PIPE, shell=True)
gconf = get_gconf(command_output)
for name, value in gconf.items():
print("name: %s" % name)
print("value: %s" % value)
# Empty line
print()
return 0
if __name__ == "__main__":
sys.exit(main())
|