Package: petriflowLibrary
Petriflow Library - A tool for parsing Petriflow files in PythonPetriflowLibrary is a library in the Python language for handling objectsin the Petriflow language. The main objective of the library is to importand export Petriflow files to work more closely with these objects.Who is it for?PetriflowLibrary is very easy to use. If you are a studentor just need to get the contents of an XML document in Python to work withdata as objects, the library provides you convenient and flexible tools forprocessing them. It also provides the option to import the file from the pathon the device or as a URL where the XML document is located. During this process,the document is also validated, and if it is invalid, the library throws an error.What can it do?import XML documents based onXSDmodify XML documents (set, get, add, insert, replace)export modified XML documents using "pretty_print", which guarantees a tree-structured output documentcompatible with the latest version of Python 3fully tested using unittestsMost importantly, PetriflowLibrary will save you time and prevent you from getting parsing headaches.Install PetriflowLibrarypip install petriflowLibraryGet startedThe following is an illustration of how to use the library:Original XML file:<datatype="text"><id>text_0</id><title>Meno</title></data>Using Python to modify an XML file:frompetriflowimport*file_object=open("../resources/hypouver.xml",encoding="utf-8")doc=import_xml(file_object)file_object.close()doc.data[0].set_encryption(encryption())doc.data[0].encryption.set_valueOf_(True)export_xml(doc,"C:/Users/peterkox/Downloads/output.xml")Output XML file:<datatype="text"><id>text_0</id><title>Meno</title><encryption>true</encryption></data>Note that we opened the file with theopen()method and set the encoding to utf-8.This method has the default encoding set to None, so it would encode unicodecharacters incorrectly. Also note that we use dot notation to access individualobjects. Be careful with arrays, because autocomplete does not work with them,so you need to know exactly what you want to type or access.You can find it here:Main implementationTests- automated tests usingunittestMethod code explanationimport_xmlThe first step is parsing itself. The method takes an argument representingthe path to the file and additionally has one optional argument representingthe type of the parser itself. If this argument is not specified, the methodautomatically sets the standard parser, which is the so-called ETCompatXMLParser().It is flexible and moreover compatible for ElementTree from lxml and also takescare of what would be done by the parser provided by thexml.etree.ElementTree module in alternative case. After parsing, the ElementTreeobject is stored in thedocvariable. The second step is validation using a methodcreated by us, which ensures from the beginning that no further work is done withthe faulty document. Before saving the namespace from the XML document, we getthe root element using thedoc.getroot()method and the root tag using a separateget_root_tag()method. If the root element class is not found, it will beset to "document" as the default top layer of the document. Thefactory()methodfinally creates and returns an object instance of the given root element, whichwe will then directly work with when modifying the document as a whole. Finally,with thebuild()method, we fill this object with XML document data. Method returns"document" object.export_xmlBased on the root element object, which is the first argument of the method,we will create an ElementTree, which we will then store in the locationspecified in advance by the second argument of the method.validate_docThe method first gets the absolute path to the library and then uses it tocreate a relative path to the XSD available in the library's resources folder.By subsequently using a relative path, the method can load the XSD file.By default, we insert the loaded schema into the ElementTree using theXMLSchema()method, which returns an object on which we can now call thevalidate()method.If it is not a valid XML document, the condition catches the case, prints an errorand terminates the program.set_namespace_defsThe logic builds on iteration through the elements and searches for the rootelement based on obtaining the root when parsing the XML document. We then gothrough each key:value and insert them in a specific format. The keys are writtenin the form{standard-namespace-prefix}noNamespaceSchemaLocation, so we needto extract the attribute name from it, then we can easily get the value fromthe key. Finally, we create a dictionary where the key is the root tag and thevalue is our entire namespace, we clean the contents of the variableGenerateDSNamespaceDefs_and fill it with a new dictionary.Contact the authorQuestions about code are best asked ondiscordor in the issues.--Peter