7 from xml.dom
import minidom
9 from cPickle
import load, dump
10 from hashlib
import md5
13 '''Takes care of XML file operations such as reading and writing.'''
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'
18 self.
log = logging.getLogger(
'XMLFile')
20 def variable(self, path, namespace='EnvSchema', name=None):
21 '''Returns list containing name of variable, action and value.
23 @param path: a file name or a file-like object
25 If no name given, returns list of lists of all variables and locals(instead of action 'local' is filled).
27 isFilename =
type(path)
is str
30 checksum.update(open(path,
'rb').read())
31 checksum = checksum.digest()
36 oldsum, data = load(f)
37 if oldsum == checksum:
49 doc = minidom.parse(path)
53 ELEMENT_NODE = minidom.Node.ELEMENT_NODE
55 nodes = doc.getElementsByTagNameNS(namespace,
"config")[0].childNodes
59 if node.nodeType == ELEMENT_NODE:
60 action = str(node.localName)
62 if action ==
'include':
64 value = str(node.childNodes[0].data)
67 variables.append((action, (value, caller, str(node.getAttribute(
'hints')))))
69 elif action ==
'search_path':
71 value = str(node.childNodes[0].data)
74 variables.append((action, (value,
None,
None)))
77 varname = str(node.getAttribute(
'variable'))
78 if name
and varname != name:
81 if action ==
'declare':
82 variables.append((action, (varname, str(node.getAttribute(
'type')), str(node.getAttribute(
'local')))))
85 value = str(node.childNodes[0].data)
88 variables.append((action, (varname, value,
None)))
93 dump((checksum, variables), f, protocol=2)
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'
106 '''Finishes the XML input and writes XML to file.'''
107 if outputFile
is None:
108 raise IOError(
"No output file given")
111 doc = minidom.parseString(self.
xmlResult)
112 with open(outputFile,
"w")
as f:
113 f.write( doc.toxml() )
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)
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'