2 # -*- coding: utf-8 -*-
7 General library of internal objects and utilities for Hooke.
9 Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy).
10 With algorithms contributed by Francesco Musiani (University of Bologna, Italy)
12 This program is released under the GNU General Public License version 2.
17 import xml.dom.minidom
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)
30 class HookeConfig(object):
32 Handling of Hooke configuration file
34 Mostly based on the simple-yet-useful examples of the Python Library Reference
37 FIXME: starting to look a mess, should require refactoring
40 def __init__(self, config_dir=None):
42 self.config['install']={}
43 self.config['plugins']=[]
44 self.config['drivers']=[]
45 self.config['plotmanips']=[]
46 self.config_dir = config_dir
48 def load_config(self, filename):
49 myconfig=file(config_file_path(filename, config_dir=self.config_dir))
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
55 the_file=myconfig.read()
56 the_file_lines=the_file.split('\n')
57 the_file=''.join(the_file_lines)
59 self.config_tree=xml.dom.minidom.parseString(the_file)
61 def getText(nodelist):
62 #take the text from a nodelist
63 #from Python Library Reference 13.7.2
66 if node.nodeType == node.TEXT_NODE:
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)
84 def handleInstall(install_elements):
85 for install in install_elements:
86 for node in install.childNodes:
87 if node.nodeType == node.TEXT_NODE:
89 path = os.path.abspath(getText(node.childNodes).strip())
90 self.config['install'][str(node.tagName)] = path
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)
97 def handlePlugins(plugins):
98 for plugin in plugins[0].childNodes:
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...
103 #FIXME: code duplication
104 def handleDrivers(drivers):
105 for driver in drivers[0].childNodes:
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...
111 def handlePlotmanip(plotmanips):
112 for plotmanip in plotmanips[0].childNodes:
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...
118 def handleDefaultlist(defaultlist):
122 dflist=getText(defaultlist[0].childNodes)
123 self.config['defaultlist']=dflist.strip()
125 handleConfig(self.config_tree)
126 #making items in the dictionary more machine-readable
127 for item in self.config.keys():
129 self.config[item]=float(self.config[item])
130 except TypeError: #we are dealing with a list, probably. keep it this way.
132 self.config[item]=eval(self.config[item])
133 except: #not a list, not a tuple, probably a string?
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
144 def save_config(self, config_filename):
145 print 'Not Implemented.'
151 debug stuff from latest rewrite of hooke_playlist.py
152 should be removed sooner or later (or substituted with new debug code!)
155 print confo.load_config('hooke.conf')