]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/conf.py
removed debug statement
[OpenSRF.git] / src / python / osrf / conf.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16
17 from osrf.utils import *
18 from osrf.ex import *
19 import re
20
21 class osrfConfig(object):
22     """Loads and parses the bootstrap config file"""
23
24     config = None
25
26     def __init__(self, file, context=None):
27         self.file = file    
28         self.context = context
29         self.data = {}
30
31     #def parseConfig(self,file=None):
32     def parseConfig(self):
33         self.data = osrfXMLFileToObject(self.file)
34         osrfConfig.config = self
35     
36     def getValue(self, key, idx=None):
37         if self.context:
38             if re.search('/', key):
39                 key = "%s/%s" % (self.context, key)
40             else:
41                 key = "%s.%s" % (self.context, key)
42
43         val = osrfObjectFindPath(self.data, key, idx)
44         if not val:
45             raise osrfConfigException("Config value not found: " + key)
46         return val
47
48
49 def osrfConfigValue(key, idx=None):
50     """Returns a bootstrap config value.
51
52     key -- A string representing the path to the value in the config object
53         e.g.  "domains.domain", "username"
54     idx -- Optional array index if the searched value is an array member
55     """
56     return osrfConfig.config.getValue(key, idx)
57                 
58
59 def osrfConfigValueNoEx(key, idx=None):
60     """ Returns a bootstrap config value without throwing an exception
61         if the item is not found. 
62
63     key -- A string representing the path to the value in the config object
64         e.g.  "domains.domain", "username"
65     idx -- Optional array index if the searched value is an array member
66     """
67     try:
68         return osrfConfig.config.getValue(key, idx)
69     except:
70         return None
71