remove myplc-config dependency
[sfa.git] / sfa / util / config.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import ConfigParser
5 import tempfile
6 from sfa.util.xml import XML
7
8 default_config = \
9 """
10 """
11
12 class Config:
13   
14     def __init__(self, config_file='/etc/sfa/sfa_config'):
15         self.config = ConfigParser.ConfigParser()  
16         self.filename = config_file
17         if not os.path.isfile(self.filename):
18             self.create(self.filename)
19         self.load(self.filename)
20
21     def create(self, filename):
22         if not os.path.exists(os.path.dirname(filename)):
23             os.makedirs(os.path.dirname(filename))
24         configfile = open(filename, 'w')
25         configfile.write(default_config)
26         configfile.close()
27         
28
29     def load(self, filename):
30         if filename:
31             if filename.endswith('.xml'):
32                 try:
33                     self.load_xml(filename)
34                 except:
35                     raise 
36                     self.config.read(filename)
37             else:
38                 self.config.read(filename)
39         self.set_attributes()
40                 
41     def load_xml(self, filename):
42         xml = XML(filename)
43         categories = xml.xpath('//configuration/variables/category')
44         for category in categories:
45             section_name = category.get('id')
46             if not self.config.has_section(section_name):
47                 self.config.add_section(section_name)
48             options = category.xpath('./variablelist/variable')
49             for option in options:
50                 option_name = option.get('id')
51                 value = option.xpath('./value')[0].text
52                 if not value:
53                     value = ""
54                 self.config.set(section_name, option_name, value)
55          
56
57     def locate_varname(self, varname):
58         varname = varname.lower()
59         sections = self.config.sections()
60         section_name = ""
61         var_name = ""
62         for section in sections:
63             if varname.startswith(section.lower()) and len(section) > len(section_name):
64                 section_name = section.lower()
65                 var_name = varname.replace(section_name, "")[1:]
66         if not self.config.has_option(section_name, var_name):
67             raise ConfigParser.NoOptionError(varname, section_name)
68         return (section_name, var_name)             
69
70     def set_attributes(self):
71         sections = self.config.sections()
72         for section in sections:
73             for item in self.config.items(section):
74                 name = "%s_%s" % (section, item[0])
75                 value = item[1]
76                 setattr(self, name, value)
77                 setattr(self, name.upper(), value)
78         
79
80     def verify(self, config1, config2, validate_method):
81         return True
82
83     def validate_type(self, var_type, value):
84         return True
85
86     def dump(self, sections = []):
87         if not sections:
88             sections = self.config.sections() 
89         print "" 
90         for section in sections:
91             print "[%s]" % section
92             for item in self.config.items(section):
93                 print "%s=%s" % (item[0], item[1])
94             print "" 
95         
96     def write(self, filename=None):
97         if not filename:
98             filename = self.filename
99         configfile = open(filename, 'w') 
100         self.config.write(configfile)
101     
102     def save(self, filename=None):
103         self.write(filename)
104
105     def __getattr__(self, attr):
106         return getattr(self.config, attr)
107
108 if __name__ == '__main__':
109     filename = None
110     if len(sys.argv) > 1:
111         filename = sys.argv[1]
112         config = Config(filename)
113     else:    
114         config = Config()
115     config.dump()
116