python rclconfig: do not generate exception for missing file

This commit is contained in:
Jean-Francois Dockes 2013-11-12 14:40:24 +01:00
parent 134153e412
commit 3d3ee8e2e6

View File

@ -12,9 +12,14 @@ class ConfSimple:
lets you retrieve named values from the top level or a subsection""" lets you retrieve named values from the top level or a subsection"""
def __init__(self, confname, tildexp = False): def __init__(self, confname, tildexp = False):
f = open(confname, 'r')
self.dotildexpand = tildexp
self.submaps = {} self.submaps = {}
self.dotildexpand = tildexp
try:
f = open(confname, 'r')
except Exception as exc:
#print("Open Exception: %s" % exc, sys.stderr)
# File does not exist -> empty config, not an error.
return
self.parseinput(f) self.parseinput(f)
@ -37,21 +42,21 @@ class ConfSimple:
appending = True appending = True
continue continue
appending = False appending = False
#print line #print(line)
if line[0] == '[': if line[0] == '[':
line = line.strip('[]') line = line.strip('[]')
if self.dotildexpand: if self.dotildexpand:
submapkey = os.path.expanduser(line) submapkey = os.path.expanduser(line)
else: else:
submapkey = line submapkey = line
#print "Submapkey:", submapkey #print("Submapkey: [%s]" % submapkey)
continue continue
nm, sep, value = line.partition('=') nm, sep, value = line.partition('=')
if sep == '': if sep == '':
continue continue
nm = nm.strip() nm = nm.strip()
value = value.strip() value = value.strip()
#print "Name:", nm, "Value:", value #print("Name: [%s] Value: [%s]" % (nm, value))
if not submapkey in self.submaps: if not submapkey in self.submaps:
self.submaps[submapkey] = {} self.submaps[submapkey] = {}
@ -145,7 +150,7 @@ class RclConfig:
self.confdir = os.environ["RECOLL_CONFDIR"] self.confdir = os.environ["RECOLL_CONFDIR"]
else: else:
self.confdir = os.path.expanduser("~/.recoll") self.confdir = os.path.expanduser("~/.recoll")
#print "Confdir: [%s]" % self.confdir #print("Confdir: [%s]" % self.confdir)
# Also find datadir. This is trickier because this is set by # Also find datadir. This is trickier because this is set by
# "configure" in the C code. We can only do our best. Have to # "configure" in the C code. We can only do our best. Have to
# choose a preference order. Use RECOLL_DATADIR if the order is wrong # choose a preference order. Use RECOLL_DATADIR if the order is wrong
@ -160,7 +165,7 @@ class RclConfig:
self.datadir = dd self.datadir = dd
if self.datadir is None: if self.datadir is None:
self.datadir = "/usr/share/recoll" self.datadir = "/usr/share/recoll"
#print "Datadir: [%s]" % self.datadir #print("Datadir: [%s]" % self.datadir)
self.cdirs = [] self.cdirs = []
# Additional config directory, values override user ones # Additional config directory, values override user ones
@ -196,3 +201,5 @@ class RclExtraDbs:
if __name__ == '__main__': if __name__ == '__main__':
config = RclConfig() config = RclConfig()
print(config.getConfParam("topdirs")) print(config.getConfParam("topdirs"))
extradbs = RclExtraDbs(config)
print(extradbs.getActDbs())