9 from cPickle
import load, dump
10 from hashlib
import md5
12 from xml.dom
import minidom
13 from xml.parsers.expat
import ExpatError
16 '''Takes care of XML file operations such as reading and writing.'''
19 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'
21 self.
log = logging.getLogger(
'XMLFile')
23 def variable(self, path, namespace='EnvSchema', name=None):
24 '''Returns list containing name of variable, action and value.
26 @param path: a file name or a file-like object
28 If no name given, returns list of lists of all variables and locals(instead of action 'local' is filled).
30 isFilename =
type(path)
is str
33 checksum.update(open(path,
'rb').read())
34 checksum = checksum.digest()
39 oldsum, data = load(f)
40 if oldsum == checksum:
53 doc = minidom.parse(path)
54 except ExpatError, exc:
55 self.log.fatal(
'Failed to parse %s: %s', path, exc)
56 self.log.fatal(list(open(path))[exc.lineno-1].rstrip())
57 self.log.fatal(
' ' * exc.offset +
'^')
63 ELEMENT_NODE = minidom.Node.ELEMENT_NODE
65 nodes = doc.getElementsByTagNameNS(namespace,
"config")[0].childNodes
69 if node.nodeType == ELEMENT_NODE:
70 action = str(node.localName)
72 if action ==
'include':
74 value = str(node.childNodes[0].data)
77 variables.append((action, (value, caller, str(node.getAttribute(
'hints')))))
79 elif action ==
'search_path':
81 value = str(node.childNodes[0].data)
84 variables.append((action, (value,
None,
None)))
87 varname = str(node.getAttribute(
'variable'))
88 if name
and varname != name:
91 if action ==
'declare':
92 variables.append((action, (varname, str(node.getAttribute(
'type')), str(node.getAttribute(
'local')))))
95 value = str(node.childNodes[0].data)
98 variables.append((action, (varname, value,
None)))
102 f = open(cpath,
'wb')
103 dump((checksum, variables), f, protocol=2)
111 '''resets the buffer of writer'''
112 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'
116 '''Finishes the XML input and writes XML to file.'''
117 if outputFile
is None:
118 raise IOError(
"No output file given")
121 doc = minidom.parseString(self.
xmlResult)
122 with open(outputFile,
"w")
as f:
123 f.write( doc.toxml() )
127 def writeVar(self, varName, action, value, vartype='list', local=False):
128 '''Writes a action to a file. Declare undeclared elements (non-local list is default type).'''
129 if action ==
'declare':
130 self.
xmlResult +=
'<env:declare variable="'+varName+
'" type="'+ vartype.lower() +
'" local="'+(str(local)).lower()+
'" />\n'
131 self.declaredVars.append(varName)
135 self.
xmlResult +=
'<env:declare variable="'+varName+
'" type="'+ vartype +
'" local="'+(str(local)).lower()+
'" />\n'
136 self.declaredVars.append(varName)
137 self.
xmlResult +=
'<env:'+action+
' variable="'+ varName +
'">'+value+
'</env:'+action+
'>\n'