| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
xmlModule.py
Go to the documentation of this file.
1 '''
2 Created on Jul 2, 2011
3
4 @author: mplajner
5 '''
6
7 from xml.dom import minidom
8 import logging
9 from cPickle import load, dump
10 from hashlib import md5 # pylint: disable=E0611
11
12 class XMLFile(object):
13 '''Takes care of XML file operations such as reading and writing.'''
14
15 def __init__(self):
16 self.xmlResult = '<?xml version="1.0" encoding="UTF-8"?><env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">\n'
17 self.declaredVars = []
18 self.log = logging.getLogger('XMLFile')
19
20 def variable(self, path, namespace='EnvSchema', name=None):
21 '''Returns list containing name of variable, action and value.
22
23 @param path: a file name or a file-like object
24
25 If no name given, returns list of lists of all variables and locals(instead of action 'local' is filled).
26 '''
27 isFilename = type(path) is str
28 if isFilename:
29 checksum = md5()
30 checksum.update(open(path, 'rb').read())
31 checksum = checksum.digest()
32
33 cpath = path + "c" # preparsed file
34 try:
35 f = open(cpath, 'rb')
36 oldsum, data = load(f)
37 if oldsum == checksum:
38 return data
39 except IOError:
40 pass
41 except EOFError:
42 pass
43
44 caller = path
45 else:
46 caller = None
47
48 # Get file
49 doc = minidom.parse(path)
50 if namespace == '':
51 namespace = None
52
53 ELEMENT_NODE = minidom.Node.ELEMENT_NODE
54 # Get all variables
55 nodes = doc.getElementsByTagNameNS(namespace, "config")[0].childNodes
56 variables = []
57 for node in nodes:
58 # if it is an element node
59 if node.nodeType == ELEMENT_NODE:
60 action = str(node.localName)
61
62 if action == 'include':
63 if node.childNodes:
64 value = str(node.childNodes[0].data)
65 else:
66 value = ''
67 variables.append((action, (value, caller, str(node.getAttribute('hints')))))
68
69 elif action == 'search_path':
70 if node.childNodes:
71 value = str(node.childNodes[0].data)
72 else:
73 value = ''
74 variables.append((action, (value, None, None)))
75
76 else:
77 varname = str(node.getAttribute('variable'))
78 if name and varname != name:
79 continue
80
81 if action == 'declare':
82 variables.append((action, (varname, str(node.getAttribute('type')), str(node.getAttribute('local')))))
83 else:
84 if node.childNodes:
85 value = str(node.childNodes[0].data)
86 else:
87 value = ''
88 variables.append((action, (varname, value, None)))
89
90 if isFilename:
91 try:
92 f = open(cpath, 'wb')
93 dump((checksum, variables), f, protocol=2)
94 f.close()
95 except IOError:
96 pass
97 return variables
98
99
100 def resetWriter(self):
101 '''resets the buffer of writer'''
102 self.xmlResult = '<?xml version="1.0" encoding="UTF-8"?><env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">\n'
103 self.declaredVars = []
104
105 def writeToFile(self, outputFile=None):
106 '''Finishes the XML input and writes XML to file.'''
107 if outputFile is None:
108 raise IOError("No output file given")
109 self.xmlResult += '</env:config>'
110
111 doc = minidom.parseString(self.xmlResult)
112 with open(outputFile, "w") as f:
113 f.write( doc.toxml() )
114
115 return outputFile
116
117 def writeVar(self, varName, action, value, vartype='list', local=False):
118 '''Writes a action to a file. Declare undeclared elements (non-local list is default type).'''
119 if action == 'declare':
120 self.xmlResult += '<env:declare variable="'+varName+'" type="'+ vartype.lower() +'" local="'+(str(local)).lower()+'" />\n'
121 self.declaredVars.append(varName)
122 return
123
124 if varName not in self.declaredVars:
125 self.xmlResult += '<env:declare variable="'+varName+'" type="'+ vartype +'" local="'+(str(local)).lower()+'" />\n'
126 self.declaredVars.append(varName)
127 self.xmlResult += '<env:'+action+' variable="'+ varName +'">'+value+'</env:'+action+'>\n'