1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | read XML configuration files into a common data structure |
---|
5 | ''' |
---|
6 | |
---|
7 | ########### SVN repository information ################### |
---|
8 | # $Date: 2011-08-21 13:24:14 +0000 (Sun, 21 Aug 2011) $ |
---|
9 | # $Author: jemian $ |
---|
10 | # $Revision: 619 $ |
---|
11 | # $HeadURL$ |
---|
12 | # $Id: xmlSupport.py 619 2011-08-21 13:24:14Z jemian $ |
---|
13 | ########### SVN repository information ################### |
---|
14 | |
---|
15 | import sys |
---|
16 | try: |
---|
17 | # this is the normal Python path |
---|
18 | from xml.etree import ElementTree |
---|
19 | #from xml.etree import ElementPath |
---|
20 | except: |
---|
21 | # this works on ipnspool3.xor.aps.anl.gov |
---|
22 | from elementtree import ElementTree |
---|
23 | import os.path |
---|
24 | |
---|
25 | PVLIST_XML = 'pvlist.xml' |
---|
26 | PROPERTIES_XML = 'properties.xml' |
---|
27 | |
---|
28 | #************************************************************************** |
---|
29 | |
---|
30 | def readPropertiesXML(xmlFile): |
---|
31 | tree = ElementTree.parse(xmlFile) |
---|
32 | root = tree.getroot() |
---|
33 | props = {} |
---|
34 | |
---|
35 | #-------- simple things |
---|
36 | for key in ('title', 'www_plot_dir', 'rrd_logs_base_dir'): |
---|
37 | props[key] = root.find(key).text.strip() |
---|
38 | |
---|
39 | #-------- time-spans for the RRD file design |
---|
40 | arr = {} |
---|
41 | arr['names'] = [] |
---|
42 | spans = root.find("spans") |
---|
43 | for element in root.findall('spans//span'): |
---|
44 | dct = {} |
---|
45 | for attribute in element.keys(): |
---|
46 | dct[attribute] = element.get(attribute).strip() |
---|
47 | name = dct['name'] |
---|
48 | dct['value'] = element.text.strip() |
---|
49 | arr[name] = dct |
---|
50 | arr['names'].append(name) |
---|
51 | for attribute in spans.keys(): |
---|
52 | arr[attribute] = spans.get(attribute).strip() |
---|
53 | props['spans'] = arr |
---|
54 | |
---|
55 | #-------- various subdirectories |
---|
56 | arr = {} |
---|
57 | for element in root.findall('subdirs//subdir'): |
---|
58 | name = element.get('name').strip() |
---|
59 | arr[name] = element.text.strip() |
---|
60 | props['subdirs'] = arr |
---|
61 | |
---|
62 | #-------- definitions for constructing the RRD files |
---|
63 | arr = {} |
---|
64 | for element in root.findall('rrd/*'): |
---|
65 | arr[element.tag] = element.text.strip() |
---|
66 | props['rrd'] = arr |
---|
67 | return(props) |
---|
68 | |
---|
69 | #************************************************************************** |
---|
70 | |
---|
71 | def readPvlistXML(xmlFile): |
---|
72 | tree = ElementTree.parse(xmlFile) |
---|
73 | root = tree.getroot() |
---|
74 | db = [] |
---|
75 | for element in root.findall('EPICS_PV'): |
---|
76 | pv = element.get('pv').strip() |
---|
77 | arr = {} |
---|
78 | arr['desc'] = element.text.strip() |
---|
79 | for attribute in element.keys(): |
---|
80 | arr[attribute] = element.get(attribute).strip() |
---|
81 | # make PV names into file names, replace ":" with "-" |
---|
82 | arr['fileRoot'] = pv.replace (':', '-') |
---|
83 | db.append(arr) |
---|
84 | return(db) |
---|
85 | |
---|
86 | #************************************************************************** |
---|
87 | |
---|
88 | def readConfigurationXML(baseDir): |
---|
89 | config = {} |
---|
90 | |
---|
91 | propertiesFile = os.path.join(baseDir, PROPERTIES_XML) |
---|
92 | config['properties'] = readPropertiesXML(propertiesFile) |
---|
93 | |
---|
94 | pvListFile = os.path.join(baseDir, PVLIST_XML) |
---|
95 | config['pvList'] = readPvlistXML(pvListFile) |
---|
96 | |
---|
97 | return(config) |
---|
98 | |
---|
99 | #************************************************************************** |
---|
100 | |
---|
101 | def __someTest__(xmlFile): |
---|
102 | tree = ElementTree.parse(xmlFile) |
---|
103 | for key in tree.findall("/*"): |
---|
104 | ElementTree.dump(key) |
---|
105 | |
---|
106 | |
---|
107 | #************************************************************************** |
---|
108 | |
---|
109 | if __name__ == "__main__": |
---|
110 | if (len(sys.argv) == 2): |
---|
111 | pwd = sys.argv[1] |
---|
112 | else: |
---|
113 | pwd = '.' |
---|
114 | config = readConfigurationXML(pwd) |
---|
115 | #print(config['properties']['rrd']) |
---|
116 | print(config['properties']['spans']) |
---|
117 | #__someTest__(PROPERTIES_XML) |
---|