Really hideous merge of Rolf Schmidt's code.
[hooke.git] / hooke / config.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 '''
5 libhooke.py
6
7 General library of internal objects and utilities for Hooke.
8
9 Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy).
10 With algorithms contributed by Francesco Musiani (University of Bologna, Italy)
11
12 This program is released under the GNU General Public License version 2.
13 '''
14
15 import scipy
16 import numpy
17 import xml.dom.minidom
18 import os
19 import os.path
20 import string
21
22 #import ConfigParser
23
24 def config_file_path(filename, config_dir=None):
25     if config_dir == None:
26         config_dir = os.path.abspath(
27             os.path.join(os.path.dirname(os.path.dirname(__file__)), 'conf'))
28     return os.path.join(config_dir, filename)
29
30 class HookeConfig(object):
31     '''
32     Handling of Hooke configuration file
33
34     Mostly based on the simple-yet-useful examples of the Python Library Reference
35     about xml.dom.minidom
36
37     FIXME: starting to look a mess, should require refactoring
38     '''
39
40     def __init__(self, config_dir=None):
41         self.config={}
42         self.config['install']={}
43         self.config['plugins']=[]
44         self.config['drivers']=[]
45         self.config['plotmanips']=[]
46         self.config_dir = config_dir
47
48     def load_config(self, filename):
49         myconfig=file(config_file_path(filename, config_dir=self.config_dir))
50
51         #the following 3 lines are needed to strip newlines. otherwise, since newlines
52         #are XML elements too, the parser would read them (and re-save them, multiplying
53         #newlines...)
54         #yes, I'm an XML n00b
55         the_file=myconfig.read()
56         the_file_lines=the_file.split('\n')
57         the_file=''.join(the_file_lines)
58
59         self.config_tree=xml.dom.minidom.parseString(the_file)
60
61         def getText(nodelist):
62             #take the text from a nodelist
63             #from Python Library Reference 13.7.2
64             rc = ''
65             for node in nodelist:
66                 if node.nodeType == node.TEXT_NODE:
67                     rc += node.data
68             return rc
69
70         def handleConfig(config):
71             install_elements=config.getElementsByTagName("install")
72             display_elements=config.getElementsByTagName("display")
73             plugins_elements=config.getElementsByTagName("plugins")
74             drivers_elements=config.getElementsByTagName("drivers")
75             defaultlist_elements=config.getElementsByTagName("defaultlist")
76             plotmanip_elements=config.getElementsByTagName("plotmanips")
77             handleInstall(install_elements)
78             handleDisplay(display_elements)
79             handlePlugins(plugins_elements)
80             handleDrivers(drivers_elements)
81             handleDefaultlist(defaultlist_elements)
82             handlePlotmanip(plotmanip_elements)
83
84         def handleInstall(install_elements):
85             for install in install_elements:
86                 for node in install.childNodes:
87                     if node.nodeType == node.TEXT_NODE:
88                         continue
89                     path = os.path.abspath(getText(node.childNodes).strip())
90                     self.config['install'][str(node.tagName)] = path
91
92         def handleDisplay(display_elements):
93             for element in display_elements:
94                 for attribute in element.attributes.keys():
95                     self.config[attribute]=element.getAttribute(attribute)
96
97         def handlePlugins(plugins):
98             for plugin in plugins[0].childNodes:
99                 try:
100                     self.config['plugins'].append(str(plugin.tagName))
101                 except: #if we allow fancy formatting of xml, there is a text node, so tagName fails for it...
102                     pass
103         #FIXME: code duplication
104         def handleDrivers(drivers):
105             for driver in drivers[0].childNodes:
106                 try:
107                     self.config['drivers'].append(str(driver.tagName))
108                 except: #if we allow fancy formatting of xml, there is a text node, so tagName fails for it...
109                     pass
110
111         def handlePlotmanip(plotmanips):
112             for plotmanip in plotmanips[0].childNodes:
113                 try:
114                     self.config['plotmanips'].append(str(plotmanip.tagName))
115                 except: #if we allow fancy formatting of xml, there is a text node, so tagName fails for it...
116                     pass
117
118         def handleDefaultlist(defaultlist):
119             '''
120             default playlist
121             '''
122             dflist=getText(defaultlist[0].childNodes)
123             self.config['defaultlist']=dflist.strip()
124
125         handleConfig(self.config_tree)
126         #making items in the dictionary more machine-readable
127         for item in self.config.keys():
128             try:
129                 self.config[item]=float(self.config[item])
130             except TypeError: #we are dealing with a list, probably. keep it this way.
131                 try:
132                     self.config[item]=eval(self.config[item])
133                 except: #not a list, not a tuple, probably a string?
134                     pass
135             except ValueError: #if we can't get it to a number, it must be None or a string
136                 if string.lower(self.config[item])=='none':
137                     self.config[item]=None
138                 else:
139                     pass
140
141         return self.config
142
143
144     def save_config(self, config_filename):
145         print 'Not Implemented.'
146         pass
147
148
149 def debug():
150     '''
151     debug stuff from latest rewrite of hooke_playlist.py
152     should be removed sooner or later (or substituted with new debug code!)
153     '''
154     confo=HookeConfig()
155     print confo.load_config('hooke.conf')