]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/conf.py
8bb00fa9b2f8eb53980a6d522ab8ea6f6c1986ca
[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 import osrf.net_obj
18 import osrf.ex
19 import osrf.xml_obj
20 import re
21
22 class Config(object):
23     """Loads and parses the bootstrap config file"""
24
25     config = None
26
27     def __init__(self, file, context=None):
28         self.file = file    
29         self.context = context
30         self.data = {}
31
32     #def parseConfig(self,file=None):
33     def parse_config(self):
34         self.data = osrf.xml_obj.xml_file_to_object(self.file)
35         Config.config = self
36     
37     def getValue(self, key, idx=None):
38         if self.context:
39             if re.search('/', key):
40                 key = "%s/%s" % (self.context, key)
41             else:
42                 key = "%s.%s" % (self.context, key)
43
44         val = osrf.net_obj.find_object_path(self.data, key, idx)
45         if not val:
46             raise osrf.ex.OSRFConfigException("Config value not found: " + key)
47         return val
48
49
50 def get(key, idx=None):
51     """Returns a bootstrap config value.
52
53     key -- A string representing the path to the value in the config object
54         e.g.  "domains.domain", "username"
55     idx -- Optional array index if the searched value is an array member
56     """
57     return Config.config.getValue(key, idx)
58                 
59
60 def get_no_ex(key, idx=None):
61     """ Returns a bootstrap config value without throwing an exception
62         if the item is not found. 
63
64     key -- A string representing the path to the value in the config object
65         e.g.  "domains.domain", "username"
66     idx -- Optional array index if the searched value is an array member
67     """
68     try:
69         return Config.config.getValue(key, idx)
70     except:
71         return None
72