def testAllChirps(self): self.setup() self.loadXml() t = mx.DateTime.now() pulses = self.sim.make(self.chirps,t)[0] t += mx.DateTime.DateTimeDeltaFrom(100000000000) pulses.append(Pulse.Pulse(timeStamp=t)) decodedChirps = [] trash = [] f = file('testPulses.xml','w') from Ft.Xml import MarkupWriter writer = MarkupWriter(f) writer.startDocument() writer.startElement(u'Pulses') for p in pulses: p.writeToXml(writer) writer.endElement(u'Pulses') writer.endDocument() f.close() while(1): try: [dC,pulses,trash] = self.cd.decode(pulses,trash,100,debug=False) if dC!=None: decodedChirps.append(dC) else: break except ValueError, e: print "!", e break
class ParseTracer(object): def __init__(self): pass def start_action(self, instring, loc, expr): self.writer.startElement(u'attempt', attributes={ u'class': unicode(expr.__class__.__name__), u'loc': unicode(repr(loc)), u'expr': unicode(repr(expr)), u'lineno': unicode(lineno(loc, instring)), u'col': unicode(col(loc, instring)), }) def success_action(self, instring, tokensStart, loc, expr, tokens): self.writer.simpleElement(u'success') self.writer.endElement(u'attempt') def exception_action(self, instring, tokensStart, expr, err): self.writer.simpleElement(u'fail', attributes={u'err': unicode(repr(err))}) self.writer.endElement(u'attempt') def set_debug_actions(self, parser): parser.setDebugActions(self.start_action, self.success_action, self.exception_action) def parse(self, parser, input, stream): self.parser = parser apply_to_pyparser_tree(self.parser, self.set_debug_actions) self.writer = MarkupWriter(indent='yes', stream=stream) self.writer.startDocument() self.writer.startElement(u'trace') try: result = self.parser.parseString(input)[0] except ParseException, e: self.writer.simpleElement( u'fail', attributes={u'err': unicode(repr(e))}) #self.writer.endElement(u'attempt') self.writer.endElement(u'trace') self.writer.endDocument() raise self.writer.endElement(u'trace') self.writer.endDocument() return result
def parse(self, parser, input, stream): self.parser = parser apply_to_pyparser_tree(self.parser, self.set_debug_actions) self.writer = MarkupWriter(indent='yes', stream=stream) self.writer.startDocument() self.writer.startElement(u'trace') try: result = self.parser.parseString(input)[0] except ParseException, e: self.writer.simpleElement( u'fail', attributes={u'err': unicode(repr(e))}) #self.writer.endElement(u'attempt') raise
class ParseTracer(object): def __init__(self): pass def start_action(self, instring, loc, expr): self.writer.startElement(u'attempt', attributes={ u'class': unicode(expr.__class__.__name__), u'loc': unicode(repr(loc)), u'expr': unicode(repr(expr)), u'lineno': unicode(lineno(loc, instring)), u'col': unicode(col(loc, instring)),}) def success_action(self, instring, tokensStart, loc, expr, tokens): self.writer.simpleElement(u'success') self.writer.endElement(u'attempt') def exception_action(self, instring, tokensStart, expr, err): self.writer.simpleElement(u'fail', attributes={ u'err': unicode(repr(err))}) self.writer.endElement(u'attempt') def set_debug_actions(self, parser): parser.setDebugActions( self.start_action, self.success_action, self.exception_action) def parse(self, parser, input, stream): self.parser = parser apply_to_pyparser_tree(self.parser, self.set_debug_actions) self.writer = MarkupWriter(indent='yes', stream=stream) self.writer.startDocument() self.writer.startElement(u'trace') try: result = self.parser.parseString(input)[0] except ParseException, e: self.writer.simpleElement(u'fail', attributes={ u'err': unicode(repr(e))}) #self.writer.endElement(u'attempt') self.writer.endElement(u'trace') self.writer.endDocument() raise self.writer.endElement(u'trace') self.writer.endDocument() return result
def parse(self, parser, input, stream): self.parser = parser apply_to_pyparser_tree(self.parser, self.set_debug_actions) self.writer = MarkupWriter(indent="yes", stream=stream) self.writer.startDocument() self.writer.startElement(u"trace") try: result = self.parser.parseString(input)[0] except ParseException, e: self.writer.simpleElement(u"fail", attributes={u"err": unicode(repr(e))}) # self.writer.endElement(u'attempt') raise
class ParseTracer(object): def __init__(self): pass def start_action(self, instring, loc, expr): self.writer.startElement( u"attempt", attributes={ u"class": unicode(expr.__class__.__name__), u"loc": unicode(repr(loc)), u"expr": unicode(repr(expr)), u"lineno": unicode(lineno(loc, instring)), u"col": unicode(col(loc, instring)), }, ) def success_action(self, instring, tokensStart, loc, expr, tokens): self.writer.simpleElement(u"success") self.writer.endElement(u"attempt") def exception_action(self, instring, tokensStart, expr, err): self.writer.simpleElement(u"fail", attributes={u"err": unicode(repr(err))}) self.writer.endElement(u"attempt") def set_debug_actions(self, parser): parser.setDebugActions(self.start_action, self.success_action, self.exception_action) def parse(self, parser, input, stream): self.parser = parser apply_to_pyparser_tree(self.parser, self.set_debug_actions) self.writer = MarkupWriter(indent="yes", stream=stream) self.writer.startDocument() self.writer.startElement(u"trace") try: result = self.parser.parseString(input)[0] except ParseException, e: self.writer.simpleElement(u"fail", attributes={u"err": unicode(repr(e))}) # self.writer.endElement(u'attempt') raise finally:
def __init__(self,hfile): sio = StringIO() writer = MarkupWriter(sio,indent=u'yes',omitXmlDeclaration=u'yes') writer.startDocument() writer.startElement(u'hitsfile') writer.simpleElement(u'filename',content=u(hfile.filename)) writer.startElement(u'hits') for hit in hfile.hits: writer.startElement(u'hit') writer.simpleElement(u'score',content=u(hit.score)) writer.startElement(u'coord') writer.simpleElement(u'x',content=u(hit.x)) writer.simpleElement(u'y',content=u(hit.y)) writer.simpleElement(u'z',content=u(hit.z)) writer.endElement(u'coord') writer.endElement(u'hit') writer.endElement(u'hits') writer.endElement(u'hitsfile') writer.endDocument() self.data = sio.getvalue()
def outputRecordedData(self,stateEngine): output = self.myState outputFile = file('output.xml',"w") writer = MarkupWriter(outputFile,indent=u"yes") writer.startDocument() writer.startElement(u"Peach") dataModelIter = 0 for action in output: if action.type == 'input' or action.type == 'output': writer.startElement(u"DataModel", attributes={u"name": unicode("HotFuzzDataModel_"+str(dataModelIter))}) self.outputDataModelBlock(writer,action.template) writer.endElement(u"DataModel") dataModelIter = dataModelIter + 1 dataModelIter = 0 for action in output: writer.startElement(u"Action", attributes={u"name": unicode(action.name), u"type": unicode(action.type)}) if action.type == 'input' or action.type == 'output': writer.simpleElement(u"DataModel", attributes={u"ref": unicode("HotFuzzDataModel_"+str(dataModelIter))}) dataModelIter = dataModelIter + 1 writer.endElement(u"Action") writer.endElement(u"Peach") writer.endDocument()
def genXML(directory, out, addDate,addSize, md5op,recurs): writer = MarkupWriter(out, indent=u"yes") writer.startDocument() recurse_dir(directory, writer, addDate, addSize, md5op,recurs)
def __init__(self, output): self.writer = MarkupWriter(output, indent=u"yes") self.writer.startDocument() self.writer.startElement(u'sparql', namespace=SPARQL_XML_NAMESPACE) self.askResult = False
def write(frames,path='./',fileName='frames.xml'): import os path = os.path.join(path,fileName) if not os.path.isdir(os.path.dirname(path)): raise IOError('Path is not valid') f = file(path,'w') from Ft.Xml import MarkupWriter writer = MarkupWriter(f,indent=u'yes') writer.startDocument() writer.xmlFragment('<?xml-stylesheet type="text/xsl" href="teledrill.xsl"?>\n') writer.startElement(u'Frames') for fO in frameObjects: fO.writeToXml(writer) writer.endElement(u'Frames') writer.endDocument() f.close() return path
def bb_enable_iface_with_config(devcf): """ Configuration of eth interfaces via system-tools-backends. * Example of static interface configuration via system-tools-backends Function call: (sysret, xmlcfg, xmlcfgout) = bb_enable_iface_with_config('eth1', 'none', '10.0.0.6', '10.255.255.255', '10.0.0.1', '255.255.255.0', '10.0.0.0') xml passed to system-tools-backends: <?xml version='1.0' encoding='UTF-8' standalone='yes'?> <interface type='ethernet'> <configuration> <address>10.0.0.8</address> <auto>1</auto> <bootproto>none</bootproto> <broadcast>10.0.0.255</broadcast> <file>eth1</file> <gateway>10.0.0.1</gateway> <netmask>255.255.255.0</netmask> <network>10.0.0.0</network> </configuration> <dev>eth1</dev> <enabled>1</enabled> </interface> <!-- GST: end of request --> * Example of dhcp interface configuration via system-tools-backend Function call: (sysret, xmlcfg, xmlcfgout) = bb_enable_iface_with_config('eth1', 'dhcp') xml passed to system-tools-backends: <interface type='ethernet'> <configuration> <auto>1</auto> <bootproto>dhcp</bootproto> <file>eth0</file> </configuration> <dev>eth0</dev> <enabled>1</enabled> </interface> <!-- GST: end of request --> """ netcfg_xmlfile = tempfile.NamedTemporaryFile() netcfgout_xmlfile = tempfile.NamedTemporaryFile() writer = MarkupWriter(netcfg_xmlfile, encoding='UTF-8', indent=u"yes", standalone="yes") writer.startDocument() writer.startElement(u'interface') writer.attribute(u'type', u'ethernet') writer.startElement(u'configuration') if devcf.param['dhcp'] == 'True': conf = { 'auto' : '1', \ 'bootproto' : 'dhcp', \ 'file': devcf.param['eth_to_conf'] } else: broadcast = ipBroadcast(devcf.param['ip_computer'], devcf.param['mask_computer']) conf = { 'address' : devcf.param['ip_computer'], \ 'auto' : '1', \ 'bootproto' :'none', \ 'broadcast' : broadcast, \ 'file': devcf.param['eth_to_conf'], \ 'gateway' : devcf.param['gw_computer'], \ 'netmask' : devcf.param['mask_computer'], \ 'network' : devcf.param['net_computer']} for confparam in conf.keys(): writer.startElement(unicode(confparam)) writer.text(unicode(conf[confparam])) writer.endElement(unicode(confparam)) writer.endElement(u'configuration') writer.startElement(u'dev') writer.text(unicode(devcf.param['eth_to_conf'])) writer.endElement(u'dev') writer.startElement(u'enabled') writer.text(u'1') writer.endElement(u'enable') writer.endElement(u'interface') writer.text(u'\n') writer.comment(u' GST: end of request ') writer.endDocument() netcfg_xmlfile.flush() xmlcfg = open(netcfg_xmlfile.name).read( ) net_cfg_cmd = "cat " + netcfg_xmlfile.name + " | /usr/share/setup-tool-backends/scripts/network-conf -d enable_iface_with_config | grep -vE \"^$\" > " + netcfgout_xmlfile.name os.system(net_cfg_cmd) xmlcfgout = open(netcfgout_xmlfile.name).read( ) reader = PyExpat.Reader( ) dom = reader.fromStream(open(netcfgout_xmlfile.name, "r")) successCfg = Evaluate("success/text( )", dom.documentElement)[0].nodeValue if successCfg == '0': sysret = BBERRCFGDEV # Error configuration dev else: sysret = BBNOERR # Ok return (sysret, xmlcfg, xmlcfgout)
def write(chirps,path='./',fileName='symbols.xml'): import os path = os.path.join(path,fileName) if not os.path.isdir(os.path.dirname(path)): raise IOError('Path is not valid') myFile = file(path,'w') writer = MarkupWriter(myFile,indent=u'yes') writer.startDocument() writer.xmlFragment('<?xml-stylesheet type="text/xsl" href="teledrill.xsl"?>\n') writer.startElement(u'Symbols') for c in chirps: c.writeToXml(writer) writer.endElement(u'Symbols') writer.endDocument() myFile.close() return path
def Test(tester): tester.startGroup('MarkupWriter') tester.startTest('Basic MarkupWriter test') s = cStringIO.StringIO() writer = MarkupWriter(stream=s, indent=u"yes") writer.startDocument() writer.startElement(u'xsa') writer.startElement(u'vendor') #Element with simple text (#PCDATA) content writer.simpleElement(u'name', content=u'Centigrade systems') #Note writer.text(content) still works writer.simpleElement(u'email', content=u"*****@*****.**") writer.endElement(u'vendor') #Element with an attribute writer.startElement(u'product', attributes={u'id': u"100\u00B0"}) #Note writer.attribute(name, value, namespace=None) still works writer.simpleElement(u'name', content=u"100\u00B0 Server") #XML fragment writer.xmlFragment('<version>1.0</version><last-release>20030401</last-release>') #Empty element writer.simpleElement(u'changes') writer.endElement(u'product') writer.endElement(u'xsa') writer.endDocument() tester.compare(EXPECTED_1, s.getvalue()) tester.testDone() tester.groupDone() return
def __init__(self,stfile): sio = StringIO() writer = MarkupWriter(sio,indent=u'yes',omitXmlDeclaration=u'yes') writer.startDocument() writer.startElement(u'statfile') writer.simpleElement(u'filename',content=u(stfile.filename)) writer.startElement(u'properties') for propertyIndex,record in enumerate(stfile.records): writer.startElement(u'property') writer.attribute(u'index',u(propertyIndex)) writer.simpleElement(u'name',content=u(record.property)) writer.simpleElement(u'volume',content=u(record.volume)) writer.simpleElement(u'pvalue',content=u(record.pvalue)) writer.simpleElement(u'tvalue',content=u(record.tvalue)) writer.simpleElement(u'dof',content=u(record.dof)) writer.simpleElement(u'mean',content=u(record.mean)) writer.simpleElement(u'stdev',content=u(record.stdev)) writer.simpleElement(u'sumOfRanks',content=u(record.sumOfRanks)) writer.simpleElement(u'numSmall',content=u(record.numSmall)) writer.simpleElement(u'numBig',content=u(record.numBig)) writer.endElement(u'property') writer.endElement(u'properties') writer.startElement(u'sites') for siteIndex in range(stfile.numSites): writer.startElement(u'site') writer.attribute(u'index',u(siteIndex)) writer.attribute(u'label',u'site') for propertyIndex,record in enumerate(stfile.records): writer.startElement(u'value') writer.attribute(u'index',u(propertyIndex)) writer.text(u(record.sites[siteIndex])) writer.endElement(u'value') writer.endElement(u'site') for nonSiteIndex in range(stfile.numNonSites): writer.startElement(u'site') writer.attribute(u'index',u(nonSiteIndex)) writer.attribute(u'label',u'nonsite') for propertyIndex,record in enumerate(stfile.records): writer.startElement(u'value') writer.attribute(u'index',u(propertyIndex)) writer.text(u(record.nonsites[nonSiteIndex])) writer.endElement(u'value') writer.endElement(u'site') writer.endElement(u'sites') writer.endElement(u'statfile') writer.endDocument() self.data = sio.getvalue()
def __init__(self, sfile): sio = StringIO() writer = MarkupWriter(sio, indent=u'yes', omitXmlDeclaration=u'yes') writer.startDocument() writer.startElement(u'sitefile') writer.simpleElement(u'filename', content=u(sfile.filename)) writer.simpleElement(u'name', content=u(sfile.name)) writer.simpleElement(u'radius', content=u(sfile.radius)) writer.startElement(u'sites') for site in sfile.sites: writer.startElement(u'site') writer.simpleElement(u'pdbid', content=u(site.pdbid)) writer.startElement(u'coord') writer.simpleElement(u'x', content=u(site.x)) writer.simpleElement(u'y', content=u(site.y)) writer.simpleElement(u'z', content=u(site.z)) writer.endElement(u'coord') writer.simpleElement(u'label', content=u(site.label)) writer.simpleElement(u'originalLabel', content=u(site.originalLabel)) writer.simpleElement(u'comment', content=u(site.comment)) writer.endElement(u'site') writer.endElement(u'sites') writer.endElement(u'sitefile') writer.endDocument() self.data = sio.getvalue()
class SPARQLXMLWriter: """ 4Suite-based SPARQL XML Writer """ def __init__(self,output): self.writer = MarkupWriter(output, indent=u"yes") self.writer.startDocument() self.writer.startElement(u'sparql',namespace=SPARQL_XML_NAMESPACE) def write_header(self,allvarsL): self.writer.startElement(u'head', namespace=SPARQL_XML_NAMESPACE) for i in xrange(0,len(allvarsL)) : self.writer.startElement(u'variable',namespace=SPARQL_XML_NAMESPACE,attributes={u'name':unicode(allvarsL[i][1:])}) self.writer.endElement(u'variable') self.writer.endElement( u'head') def write_results_header(self,orderBy,distinct): self.writer.startElement(u'results',namespace=SPARQL_XML_NAMESPACE,attributes={u'ordered' : unicode(orderBy and 'true' or 'false'), u'distinct': unicode(distinct and 'true' or 'false')}) def write_start_result(self): self.writer.startElement(u'result',namespace=SPARQL_XML_NAMESPACE) self._resultStarted = True def write_end_result(self): assert self._resultStarted self.writer.endElement(u'result',namespace=SPARQL_XML_NAMESPACE) self._resultStarted = False def write_binding(self,name,val): assert self._resultStarted if val: attrs = {u'name':unicode(name)} self.writer.startElement(u'binding', namespace=SPARQL_XML_NAMESPACE, attributes=attrs) if isinstance(val,URIRef) : self.writer.startElement(u'uri', namespace=SPARQL_XML_NAMESPACE) self.writer.text(val) self.writer.endElement(u'uri') elif isinstance(val,BNode) : self.writer.startElement(u'bnode', namespace=SPARQL_XML_NAMESPACE) self.writer.text(val) self.writer.endElement(u'bnode') elif isinstance(val,Literal) : attrs = {} if val.language : attrs[(u'lang', XML_NAMESPACE)] = unicode(val.language) elif val.datatype: attrs[u'datatype'] = unicode(val.datatype) self.writer.startElement(u'literal', namespace=SPARQL_XML_NAMESPACE, attributes=attrs) self.writer.text(val) self.writer.endElement(u'literal') else: raise Exception("Unsupported RDF term: %s"%val) self.writer.endElement(u'binding') def close(self): self.writer.endElement(u'results') self.writer.endElement(u'sparql')
class SPARQLXMLWriter: """ 4Suite-based SPARQL XML Writer """ def __init__(self, output): self.writer = MarkupWriter(output, indent=u"yes") self.writer.startDocument() self.writer.startElement(u'sparql', namespace=SPARQL_XML_NAMESPACE) self.askResult = False def write_header(self, allvarsL): self.writer.startElement(u'head', namespace=SPARQL_XML_NAMESPACE) if allvarsL: for i in xrange(0, len(allvarsL)): self.writer.startElement( u'variable', namespace=SPARQL_XML_NAMESPACE, attributes={u'name': unicode(allvarsL[i])}) self.writer.endElement(u'variable') self.writer.endElement(u'head') def write_results_header(self, orderBy, distinct): self.writer.startElement(u'results', namespace=SPARQL_XML_NAMESPACE, attributes={ u'ordered': unicode(orderBy and 'true' or 'false'), u'distinct': unicode(distinct and 'true' or 'false') }) def write_start_result(self): self.writer.startElement(u'result', namespace=SPARQL_XML_NAMESPACE) self._resultStarted = True def write_end_result(self): assert self._resultStarted self.writer.endElement(u'result', namespace=SPARQL_XML_NAMESPACE) self._resultStarted = False def write_ask(self, val): self.writer.startElement(u'boolean', namespace=SPARQL_XML_NAMESPACE) self.writer.text((val and u't' or u'f') + unicode(val)[1:]) self.writer.endElement(u'boolean') self.askResult = True def write_binding(self, name, val): assert self._resultStarted if val: attrs = {u'name': unicode(name)} self.writer.startElement(u'binding', namespace=SPARQL_XML_NAMESPACE, attributes=attrs) if isinstance(val, URIRef): self.writer.startElement(u'uri', namespace=SPARQL_XML_NAMESPACE) self.writer.text(val) self.writer.endElement(u'uri') elif isinstance(val, BNode): self.writer.startElement(u'bnode', namespace=SPARQL_XML_NAMESPACE) self.writer.text(val) self.writer.endElement(u'bnode') elif isinstance(val, Literal): attrs = {} if val.language: attrs[(u'lang', XML_NAMESPACE)] = unicode(val.language) elif val.datatype: attrs[u'datatype'] = unicode(val.datatype) self.writer.startElement(u'literal', namespace=SPARQL_XML_NAMESPACE, attributes=attrs) self.writer.text(val) self.writer.endElement(u'literal') else: raise Exception("Unsupported RDF term: %s" % val) self.writer.endElement(u'binding') def close(self): if not self.askResult: self.writer.endElement(u'results') self.writer.endElement(u'sparql')
def __init__(self,output): self.writer = MarkupWriter(output, indent=u"yes") self.writer.startDocument() self.writer.startElement(u'sparql',namespace=SPARQL_XML_NAMESPACE)
def genXML(directory, out, addDate, addSize, md5op, recurs): writer = MarkupWriter(out, indent=u"yes") writer.startDocument() recurse_dir(directory, writer, addDate, addSize, md5op, recurs)
def Test(tester): tester.startGroup('MarkupWriter') tester.startTest('Basic MarkupWriter test') s = cStringIO.StringIO() writer = MarkupWriter(stream=s, indent=u"yes") writer.startDocument() writer.startElement(u'xsa') writer.startElement(u'vendor') #Element with simple text (#PCDATA) content writer.simpleElement(u'name', content=u'Centigrade systems') #Note writer.text(content) still works writer.simpleElement(u'email', content=u"*****@*****.**") writer.endElement(u'vendor') #Element with an attribute writer.startElement(u'product', attributes={u'id': u"100\u00B0"}) #Note writer.attribute(name, value, namespace=None) still works writer.simpleElement(u'name', content=u"100\u00B0 Server") #XML fragment writer.xmlFragment( '<version>1.0</version><last-release>20030401</last-release>') #Empty element writer.simpleElement(u'changes') writer.endElement(u'product') writer.endElement(u'xsa') writer.endDocument() tester.compare(EXPECTED_1, s.getvalue()) tester.testDone() tester.groupDone() return