|
a/src/mediaserver/cdplugins/pycommon/conftree.py |
|
b/src/mediaserver/cdplugins/pycommon/conftree.py |
1 |
#!/usr/bin/env python
|
1 |
#!/usr/bin/env python
|
|
|
2 |
# Copyright (C) 2016 J.F.Dockes
|
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
|
6 |
# (at your option) any later version.
|
|
|
7 |
#
|
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
# GNU General Public License for more details.
|
|
|
12 |
#
|
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
|
14 |
# along with this program; if not, write to the
|
|
|
15 |
# Free Software Foundation, Inc.,
|
|
|
16 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
17 |
|
|
|
18 |
|
2 |
from __future__ import print_function
|
19 |
from __future__ import print_function
|
3 |
|
20 |
|
4 |
import locale
|
21 |
import locale
|
5 |
import re
|
22 |
import re
|
6 |
import os
|
23 |
import os
|
|
... |
|
... |
11 |
class ConfSimple:
|
28 |
class ConfSimple:
|
12 |
"""A ConfSimple class reads a recoll configuration file, which is a typical
|
29 |
"""A ConfSimple class reads a recoll configuration file, which is a typical
|
13 |
ini file (see the Recoll manual). It's a dictionary of dictionaries which
|
30 |
ini file (see the Recoll manual). It's a dictionary of dictionaries which
|
14 |
lets you retrieve named values from the top level or a subsection"""
|
31 |
lets you retrieve named values from the top level or a subsection"""
|
15 |
|
32 |
|
16 |
def __init__(self, confname, tildexp = False):
|
33 |
def __init__(self, confname, tildexp = False, readonly = True):
|
17 |
self.submaps = {}
|
34 |
self.submaps = {}
|
18 |
self.dotildexpand = tildexp
|
35 |
self.dotildexpand = tildexp
|
|
|
36 |
self.readonly = readonly
|
|
|
37 |
self.confname = confname
|
|
|
38 |
|
19 |
try:
|
39 |
try:
|
20 |
f = open(confname, 'r')
|
40 |
f = open(confname, 'r')
|
21 |
except Exception as exc:
|
41 |
except Exception as exc:
|
22 |
#print("Open Exception: %s" % exc, sys.stderr)
|
42 |
#print("Open Exception: %s" % exc, sys.stderr)
|
23 |
# File does not exist -> empty config, not an error.
|
43 |
# File does not exist -> empty config, not an error.
|
|
... |
|
... |
69 |
if not sk in self.submaps:
|
89 |
if not sk in self.submaps:
|
70 |
return None
|
90 |
return None
|
71 |
if not nm in self.submaps[sk]:
|
91 |
if not nm in self.submaps[sk]:
|
72 |
return None
|
92 |
return None
|
73 |
return self.submaps[sk][nm]
|
93 |
return self.submaps[sk][nm]
|
|
|
94 |
|
|
|
95 |
def _rewrite(self):
|
|
|
96 |
if self.readonly:
|
|
|
97 |
raise Exception("ConfSimple is readonly")
|
|
|
98 |
|
|
|
99 |
tname = self.confname + "-"
|
|
|
100 |
f = open(tname, 'w')
|
|
|
101 |
# First output null subkey submap
|
|
|
102 |
if '' in self.submaps:
|
|
|
103 |
for nm,value in self.submaps[''].iteritems():
|
|
|
104 |
f.write(nm + " = " + value + "\n")
|
|
|
105 |
for sk,mp in self.submaps.iteritems():
|
|
|
106 |
if sk == '':
|
|
|
107 |
continue
|
|
|
108 |
f.write("[" + sk + "]\n")
|
|
|
109 |
for nm,value in mp.iteritems():
|
|
|
110 |
f.write(nm + " = " + value + "\n")
|
|
|
111 |
f.close()
|
|
|
112 |
os.rename(tname, self.confname)
|
|
|
113 |
|
|
|
114 |
def set(self, nm, value, sk = ''):
|
|
|
115 |
if self.readonly:
|
|
|
116 |
raise Exception("ConfSimple is readonly")
|
|
|
117 |
self.submaps[sk][nm] = value
|
|
|
118 |
self._rewrite()
|
|
|
119 |
return True
|
74 |
|
120 |
|
75 |
def getNames(self, sk = ''):
|
121 |
def getNames(self, sk = ''):
|
76 |
if not sk in self.submaps:
|
122 |
if not sk in self.submaps:
|
77 |
return None
|
123 |
return None
|
78 |
return list(self.submaps[sk].keys())
|
124 |
return list(self.submaps[sk].keys())
|