def toUML(self, file, output=None): ''' make an UML XMI file from a STEP.XML input ''' print(file) STEP = XML(*getContextFromFile( file, argsNS=['step="http://www.stibosystems.com/step"'])) xmi = XMI(name=os.path.basename(file)) package = xmi.makePackage('STEP', xmi.modelNS) diagram = xmi.makeClassDiagram('STEP', package) xmi.addDiagramClass(self.BaseTypes(xmi, package), diagram) xmi.addDiagramClass(self.ListOfValuesGroups(xmi, STEP, package), diagram) self.ListOfValues(xmi, STEP) xmi.addDiagramClass(self.AttributeGroups(xmi, STEP, package), diagram) self.Attributes(xmi, STEP) xmi.addDiagramClass(self.UserTypes(xmi, STEP, package), diagram) xmi.addDiagramClass(self.References(xmi, STEP, package), diagram) xmi.addDiagramClass(self.Keys(xmi, STEP, package), diagram) # export the results name = output or f'{file}.xmi' with open(name, 'w') as _output: printXML(str(xmi.doc), output=_output, colour=False)
def addMissingGroups(self, file): ''' adds missing attribute groups ''' xmlns = 'http://www.stibosystems.com/step' STEP = XML(*getContextFromFile(file, argsNS=[f'step="{xmlns}"'])) root = STEP.doc.getRootElement() attribute_groups = set() for element in getElements(STEP.ctx, '//step:AttributeGroup'): id = getAttribute(element, 'ID') attribute_groups.add(id) groups = getElement(STEP.ctx, '//step:AttributeGroupList') for element in getElements(STEP.ctx, '//step:Attribute/step:AttributeGroupLink'): id = getAttribute(element, 'AttributeGroupID') if id not in attribute_groups: print(id) element = addElement(STEP.doc, 'AttributeGroup', groups) setAttribute(element, 'ID', id) attribute_groups.add(id) addElementText(STEP.doc, 'Name', id, element) with open(file, 'w') as output: printXML(str(STEP.doc), output=output, colour=False) print(f'{file} +> {xmlns}')
def setNS(self, file): ''' used to put the step namespace into the step.xml file ''' xmlns = 'http://www.stibosystems.com/step' STEP = XML(*getContextFromFile(file)) root = STEP.doc.getRootElement() ns = root.ns() if ns: print(f'{file} => {ns}') else: setAttribute(root, 'xmlns', xmlns) with open(file, 'w') as output: printXML(str(STEP.doc), output=output, colour=False) print(f'{file} +> {xmlns}')
def addMissingNames(self, file): ''' adds missing object Names ''' xmlns = 'http://www.stibosystems.com/step' STEP = XML(*getContextFromFile(file, argsNS=[f'step="{xmlns}"'])) root = STEP.doc.getRootElement() xpath = ''' //*[ ( local-name()="UserType" or local-name()="AttributeGroup" or local-name()="Attribute" or local-name()="ListOfValuesGroup" or local-name()="ListOfValue" or local-name()="AssetCrossReferenceType" or local-name()="ProductCrossReferenceType" or local-name()="ClassificationCrossReferenceType" or local-name()="EntityCrossReferenceType" ) and @ID and not(step:Name) ] ''' for element in getElements(STEP.ctx, xpath): id = getAttribute(element, 'ID') print(id) addElementText(STEP.doc, 'Name', id, element) with open(file, 'w') as output: printXML(str(STEP.doc), output=output, colour=False) print(f'{file} +> {xmlns}')
STEP.ctx.xpathRegisterNs('step', 'http://www.stibosystems.com/step') for path, tests in keeps.items(): print(path) keepers = set() elements = getElements(STEP.ctx, '%s/%s' % (base, path)) for test in tests: for keep in getElements(STEP.ctx, '%s/%s[%s]' % (base, path, test)): keepers.add(keep) for element in elements: if element in keepers: print( '\t+%s%s%s' % (colours.Green, getAttribute(element, 'ID'), colours.Off)) else: print('\t-%s%s%s' % (colours.Red, getAttribute(element, 'ID'), colours.Off)) element.unlinkNode() element.freeNode() root = STEP.doc.getRootElement() setAttribute(root, 'ContextID', 'Australian') setAttribute(root, 'AutoApprove', 'Y') with codecs.open(file, 'w', encoding='utf8') as output: printXML(str(STEP.doc), output=output)
def save(self, file): with codecs.open(file, 'w', encoding='utf8') as output: printXML(self.doc.toxml(), output=output)
#!/usr/bin/env python3 import os, re, sys, json, xmltodict from kafka import KafkaConsumer from Perdy.pretty import prettyPrintLn from Perdy.parser import printXML consumer = KafkaConsumer(bootstrap_servers='voldemort:9092') consumer.subscribe(['quickstart-events']) for message in consumer: text = message.value.decode('utf8') try: d = json.loads(text) prettyPrintLn(d) except: try: x = xmltodict.parse(text) printXML(text, colour=True) except: print(text)