generateds-discuss Mailing List for generateds
Brought to you by:
dkuhlman
You can subscribe to this list here.
2020 |
Jan
|
Feb
(20) |
Mar
(7) |
Apr
(25) |
May
(7) |
Jun
|
Jul
|
Aug
(16) |
Sep
(2) |
Oct
(5) |
Nov
(15) |
Dec
(10) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2021 |
Jan
(12) |
Feb
(7) |
Mar
(16) |
Apr
|
May
|
Jun
|
Jul
|
Aug
(3) |
Sep
(3) |
Oct
(1) |
Nov
|
Dec
|
2022 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(4) |
Jun
(9) |
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2023 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(4) |
Jun
(3) |
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Dave K. <dku...@da...> - 2023-06-19 22:16:44
|
A little more follow-up -- You can run those attached files with the following: $ generateDS.py -f -o tmp01.py test02.xsd $ python tmp01.py test02-01.xml Dave K. Quoting Dave Kuhlman <dku...@da...>: > David, > > Good to hear from you. > Sorry I'm a bit slow responding. > > Here are a few words of explanation, or maybe excuse making, > depending on your point of view: > > One, `generateds` is designed around the idea that we want to > generate Python classes that help us process the elements in a XML > instance document. It generates each of those Python classes from > an `xs:complexType` definition in the XML schema. But, your schema > does not have any complex type definitions. And, therefore, we do > not get code that is of much use. > > Two, the XML instance document that you included does not validate > against your schema. You can use `xmllint` to check this. Or, > if you do not have `xmllint`, you can use `validate.py`, which is > in `generateds/utils/validate.py` in the generateds source repo: > `https://sourceforge.net/projects/generateds/` and > `https://sourceforge.net/p/generateds/code/ci/default/tree/`. > If you have Mercurial (Hg), you can download it with this: > `hg clone http://hg.code.sf.net/p/generateds/code generateds-code`. > > `test01.xsd` and `test01-01.xml` (attached) contain your schema and instance > doc (ightly patched). > > When I run `xmllint`, I see the following: > > $ xmllint --noout --schema test01.xsd test01-01.xml > test01-01.xml:1: element MyTestXML: Schemas validity error : > Element 'MyTestXML': No matching global declaration available for > the validation root. > test01-01.xml fails to validate > > `validate.py` produces a similar message because it is built on top > of `lxml` which is built on top of `libxml2` (and `libxml2-utils`, > which provides `xmllint`). > > So, my suggestion is to add a complex type. > > `test02.xsd` and `test02-01.xml` (attached) contain a schema and > instance doc with a `complexType` that uses your `simpleType`. > > The files `test02.xsd` and `test02-01.xml` (attached) provide an XML > schema and an XML instance doc that does this. Maybe it will give > you a hint about how to address your needs. > > Hope this helps. If you have more questions, please let me know. > > Dave > > > Quoting David Ferguson <fer...@gm...>: > >> Hi, >> >> I'm trying to use generateDS to parse and generate XML documents, and >> am very impressed so far - it is handling some complex XSDs very well. >> >> However I seem to have run into an issue for a simple XSD, where only >> a single simpleType enum is defined. My XDS is as follows: >> >> <?xml version="1.0" encoding="UTF-8" standalone="no"?> >> <xsi:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema" >> xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="4.1.0"> >> <xsi:simpleType name="MyTestXML"> >> <xsi:restriction base="xsd:string"> >> <xsi:enumeration value="ValueA"/> >> <xsi:enumeration value="ValueB"/> >> </xsi:restriction> >> </xsi:simpleType> >> </xsi:schema> >> >> And the XML documents are very simple, for example >> <MyTestXML>ValueA</MyTestXML>. >> >> When I run generateDS with `generateDS -o test.py test.xsd `, the >> output is just an enum, eg: >> >> class MyTestXML(str, Enum): >> VALUE_A='ValueA' >> VALUE_B='ValueB' >> >> And therefore I cannot parse, or export XML documents. Is this >> something I'm doing wrong, or a limitation in the program? >> >> Many thanks, >> David >> >> >> _______________________________________________ >> Generateds-discuss mailing list >> Gen...@li... >> https://lists.sourceforge.net/lists/listinfo/generateds-discuss > > > > -- > Dave Kuhlman > http://www.davekuhlman.org -- Dave Kuhlman http://www.davekuhlman.org |
From: Dave K. <dku...@da...> - 2023-06-19 22:05:29
|
David, Good to hear from you. Sorry I'm a bit slow responding. Here are a few words of explanation, or maybe excuse making, depending on your point of view: One, `generateds` is designed around the idea that we want to generate Python classes that help us process the elements in a XML instance document. It generates each of those Python classes from an `xs:complexType` definition in the XML schema. But, your schema does not have any complex type definitions. And, therefore, we do not get code that is of much use. Two, the XML instance document that you included does not validate against your schema. You can use `xmllint` to check this. Or, if you do not have `xmllint`, you can use `validate.py`, which is in `generateds/utils/validate.py` in the generateds source repo: `https://sourceforge.net/projects/generateds/` and `https://sourceforge.net/p/generateds/code/ci/default/tree/`. If you have Mercurial (Hg), you can download it with this: `hg clone http://hg.code.sf.net/p/generateds/code generateds-code`. `test01.xsd` and `test01-01.xml` (attached) contain your schema and instance doc (ightly patched). When I run `xmllint`, I see the following: $ xmllint --noout --schema test01.xsd test01-01.xml test01-01.xml:1: element MyTestXML: Schemas validity error : Element 'MyTestXML': No matching global declaration available for the validation root. test01-01.xml fails to validate `validate.py` produces a similar message because it is built on top of `lxml` which is built on top of `libxml2` (and `libxml2-utils`, which provides `xmllint`). So, my suggestion is to add a complex type. `test02.xsd` and `test02-01.xml` (attached) contain a schema and instance doc with a `complexType` that uses your `simpleType`. The files `test02.xsd` and `test02-01.xml` (attached) provide an XML schema and an XML instance doc that does this. Maybe it will give you a hint about how to address your needs. Hope this helps. If you have more questions, please let me know. Dave Quoting David Ferguson <fer...@gm...>: > Hi, > > I'm trying to use generateDS to parse and generate XML documents, and > am very impressed so far - it is handling some complex XSDs very well. > > However I seem to have run into an issue for a simple XSD, where only > a single simpleType enum is defined. My XDS is as follows: > > <?xml version="1.0" encoding="UTF-8" standalone="no"?> > <xsi:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema" > xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="4.1.0"> > <xsi:simpleType name="MyTestXML"> > <xsi:restriction base="xsd:string"> > <xsi:enumeration value="ValueA"/> > <xsi:enumeration value="ValueB"/> > </xsi:restriction> > </xsi:simpleType> > </xsi:schema> > > And the XML documents are very simple, for example > <MyTestXML>ValueA</MyTestXML>. > > When I run generateDS with `generateDS -o test.py test.xsd `, the > output is just an enum, eg: > > class MyTestXML(str, Enum): > VALUE_A='ValueA' > VALUE_B='ValueB' > > And therefore I cannot parse, or export XML documents. Is this > something I'm doing wrong, or a limitation in the program? > > Many thanks, > David > > > _______________________________________________ > Generateds-discuss mailing list > Gen...@li... > https://lists.sourceforge.net/lists/listinfo/generateds-discuss -- Dave Kuhlman http://www.davekuhlman.org |
From: David F. <fer...@gm...> - 2023-06-14 13:19:30
|
Hi, I'm trying to use generateDS to parse and generate XML documents, and am very impressed so far - it is handling some complex XSDs very well. However I seem to have run into an issue for a simple XSD, where only a single simpleType enum is defined. My XDS is as follows: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <xsi:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="4.1.0"> <xsi:simpleType name="MyTestXML"> <xsi:restriction base="xsd:string"> <xsi:enumeration value="ValueA"/> <xsi:enumeration value="ValueB"/> </xsi:restriction> </xsi:simpleType> </xsi:schema> And the XML documents are very simple, for example <MyTestXML>ValueA</MyTestXML>. When I run generateDS with `generateDS -o test.py test.xsd `, the output is just an enum, eg: class MyTestXML(str, Enum): VALUE_A='ValueA' VALUE_B='ValueB' And therefore I cannot parse, or export XML documents. Is this something I'm doing wrong, or a limitation in the program? Many thanks, David |
From: Andrii I. <an...@eb...> - 2023-05-18 08:50:41
|
Hi Dave, Thank you for the fast response and the suggestions! I like the flexibility of use="required", but I get this warning: [Xerces] src-attribute.2: : The property 'default' is present in attribute 'units', so the value of 'use' must be 'optional'. Here https://www.w3.org/TR/xmlschema-1#d0e2403 (3.2.3) it says "If default and use are both present, use must have the ·actual value· optional." Perhaps, for the same of compliance we will stick with the always-export-default flag. Thank you again for the help! Best regards, Andrii On 17/05/2023 23:48, Dave Kuhlman wrote: > Andrii, > > I took a little time for me to find those solutions: (1) the > "--always-export-defaults" command line option and (2) > `use="required"` in the XML schema. > > And, now I've had time to test them. They both seem to work, > depending on what you want them to do. > > 1. The "--always-export-defaults" command line option affects all > attributes (and perhaps some other things, too. > > 2. The `use="required"` in the XML schema can be applied more > selectively to specific attributes declarations. > > I've attached files that you can use to run a test. You might have > to edit `test.sh` a bit. > > Here is the results of running a test: > > $ ./test.sh > + python generateDS.py -f -o tmp01sup.py test01.xsd > + python generateDS.py -f -o tmp02sup.py --always-export-default > test01.xsd > + python generateDS.py -f -o tmp03sup.py test02.xsd > + python tmp01sup.py test01.xml > <?xml version="1.0" ?> > <collection> > <item> > <ident>item01</ident> > </item> > <item units="TB"> > <ident>item02</ident> > </item> > </collection> > + python tmp02sup.py test01.xml > <?xml version="1.0" ?> > <collection> > <item units="GB"> > <ident>item01</ident> > </item> > <item units="TB"> > <ident>item02</ident> > </item> > </collection> > + python tmp03sup.py test01.xml > <?xml version="1.0" ?> > <collection> > <item units="GB"> > <ident>item01</ident> > </item> > <item units="TB"> > <ident>item02</ident> > </item> > </collection> > > Hope this helps. Let me know when you have more questions. > > Dave > Quoting Andrii Iudin <an...@eb...>: > >> Dear Dave, >> >> Please could you help with writing out to XML an enumerated string >> restriction attribute with a default value? It looks like this: >> <xs:attribute default="GB" name="units"> >> <xs:simpleType> >> <xs:restriction base="xs:string"> >> <xs:enumeration value="TB"/> >> <xs:enumeration value="GB"/> >> <xs:enumeration value="MB"/> >> </xs:restriction> >> </xs:simpleType> >> </xs:attribute> >> >> When writing out and the value is set to "GB", no units are output >> into the XML. >> >> It appears that the skip is happening in the following generated logic: >> def _exportAttributes(self, outfile, level, already_processed, >> namespaceprefix_='', name_='datasetSizeType'): >> if self.units != "GB" and 'units' not in already_processed: >> already_processed.add('units') >> outfile.write(' units=%s' % >> (self.gds_encode(self.gds_format_string(quote_attrib(self.units), >> input_name='units')), )) >> >> At this point in code already_processed is just an empty set(). >> >> Is there any switch or change in schema that we might do to ensure >> "GB" units are added even if the value is the same as the default one? >> >> Many thanks and best regards, >> Andrii >> >> >> _______________________________________________ >> Generateds-discuss mailing list >> Gen...@li... >> https://lists.sourceforge.net/lists/listinfo/generateds-discuss > > > |
From: Dave K. <dku...@da...> - 2023-05-17 22:48:55
|
Andrii, I took a little time for me to find those solutions: (1) the "--always-export-defaults" command line option and (2) `use="required"` in the XML schema. And, now I've had time to test them. They both seem to work, depending on what you want them to do. 1. The "--always-export-defaults" command line option affects all attributes (and perhaps some other things, too. 2. The `use="required"` in the XML schema can be applied more selectively to specific attributes declarations. I've attached files that you can use to run a test. You might have to edit `test.sh` a bit. Here is the results of running a test: $ ./test.sh + python generateDS.py -f -o tmp01sup.py test01.xsd + python generateDS.py -f -o tmp02sup.py --always-export-default test01.xsd + python generateDS.py -f -o tmp03sup.py test02.xsd + python tmp01sup.py test01.xml <?xml version="1.0" ?> <collection> <item> <ident>item01</ident> </item> <item units="TB"> <ident>item02</ident> </item> </collection> + python tmp02sup.py test01.xml <?xml version="1.0" ?> <collection> <item units="GB"> <ident>item01</ident> </item> <item units="TB"> <ident>item02</ident> </item> </collection> + python tmp03sup.py test01.xml <?xml version="1.0" ?> <collection> <item units="GB"> <ident>item01</ident> </item> <item units="TB"> <ident>item02</ident> </item> </collection> Hope this helps. Let me know when you have more questions. Dave Quoting Andrii Iudin <an...@eb...>: > Dear Dave, > > Please could you help with writing out to XML an enumerated string > restriction attribute with a default value? It looks like this: > <xs:attribute default="GB" name="units"> > <xs:simpleType> > <xs:restriction base="xs:string"> > <xs:enumeration value="TB"/> > <xs:enumeration value="GB"/> > <xs:enumeration value="MB"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > > When writing out and the value is set to "GB", no units are output > into the XML. > > It appears that the skip is happening in the following generated logic: > def _exportAttributes(self, outfile, level, already_processed, > namespaceprefix_='', name_='datasetSizeType'): > if self.units != "GB" and 'units' not in already_processed: > already_processed.add('units') > outfile.write(' units=%s' % > (self.gds_encode(self.gds_format_string(quote_attrib(self.units), > input_name='units')), )) > > At this point in code already_processed is just an empty set(). > > Is there any switch or change in schema that we might do to ensure > "GB" units are added even if the value is the same as the default one? > > Many thanks and best regards, > Andrii > > > _______________________________________________ > Generateds-discuss mailing list > Gen...@li... > https://lists.sourceforge.net/lists/listinfo/generateds-discuss -- Dave Kuhlman http://www.davekuhlman.org |
From: Dave K. <dku...@da...> - 2023-05-17 18:46:28
|
Andrii, I believe there are two possible solutions to this: 1. Add `use="required"` to the attribute declaration in the XML schema. For example: <xs:attribute default="GB" use="required" name="units"> 2. Or, use the command line option "--always-export-default" when you run `generateDS.py`. You will have to figure out which of the two above fits your use case. I have to leave now for a lunch and a walk with a friend, but I'll try to do a little more checking on this later. Hope this helps. Dave Quoting Andrii Iudin <an...@eb...>: > Dear Dave, > > Please could you help with writing out to XML an enumerated string > restriction attribute with a default value? It looks like this: > <xs:attribute default="GB" name="units"> > <xs:simpleType> > <xs:restriction base="xs:string"> > <xs:enumeration value="TB"/> > <xs:enumeration value="GB"/> > <xs:enumeration value="MB"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > > When writing out and the value is set to "GB", no units are output > into the XML. > > It appears that the skip is happening in the following generated logic: > def _exportAttributes(self, outfile, level, already_processed, > namespaceprefix_='', name_='datasetSizeType'): > if self.units != "GB" and 'units' not in already_processed: > already_processed.add('units') > outfile.write(' units=%s' % > (self.gds_encode(self.gds_format_string(quote_attrib(self.units), > input_name='units')), )) > > At this point in code already_processed is just an empty set(). > > Is there any switch or change in schema that we might do to ensure > "GB" units are added even if the value is the same as the default one? > > Many thanks and best regards, > Andrii > > > _______________________________________________ > Generateds-discuss mailing list > Gen...@li... > https://lists.sourceforge.net/lists/listinfo/generateds-discuss -- Dave Kuhlman http://www.davekuhlman.org |
From: Andrii I. <an...@eb...> - 2023-05-17 10:27:18
|
Dear Dave, Please could you help with writing out to XML an enumerated string restriction attribute with a default value? It looks like this: <xs:attribute default="GB" name="units"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="TB"/> <xs:enumeration value="GB"/> <xs:enumeration value="MB"/> </xs:restriction> </xs:simpleType> </xs:attribute> When writing out and the value is set to "GB", no units are output into the XML. It appears that the skip is happening in the following generated logic: def _exportAttributes(self, outfile, level, already_processed, namespaceprefix_='', name_='datasetSizeType'): if self.units != "GB" and 'units' not in already_processed: already_processed.add('units') outfile.write(' units=%s' % (self.gds_encode(self.gds_format_string(quote_attrib(self.units), input_name='units')), )) At this point in code already_processed is just an empty set(). Is there any switch or change in schema that we might do to ensure "GB" units are added even if the value is the same as the default one? Many thanks and best regards, Andrii |
From: Dave K. <dku...@da...> - 2022-06-08 22:29:59
|
Daniel, Thanks for that offer of help with a tutorial. There already is a tutorial, *but* it needs lots of work. I have not looked at it for several years, at least. The tutorial is hidden in the tutorial subdirectory, although you will need to download the source distribution or clone the repository from SourceForge: https://sourceforge.net/projects/generateds/ Some needed changes that jump out at me: - The section on the "The graphical front-end" should be deleted completely. The graphical front end no longer can be run, now that there have been changes to the underlying graphics library (`gtk` I believe) and I was not willing to do all the work to make the necessary modifications. - The section titled "Adding application-specific behavior", tells the user to generate a subclass module by using the "-s" command line option. While that approach may be useful for some special purposes, I now feel that it's more straight-forward and more generally useful to generate a library module (with the "-o" command line option, then write user code in a separate module that imports the library module, and possibly copies and edits one of the "parse*" functions. So, maybe we need a new section on how to do that. If you have ideas and suggestions, or if you want to work on this, I'd be happy to discuss it. And, I hope you are finding `generateDS` interesting, usable, and useful. Dave Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > Dave, > > Thank you for the help, truthfully I have done very basic things > with Python up till about Feb. Since then I have taught myself the > MVC programming model with PyQt5 packages. As well as the package > installers and making the overall software OS agnostic. I have 15+ > years in LabView programming and a lot can be adopted but I haven’t > dealt with text based programming languages since highschool in > early 2000’s. > > Thank you for the compliments. Maybe through all of this I can > assist you in writing an in-depth tutorial of writing an instance to > help others out. > > Sent with BlackBerry Work > (www.blackberry.com) > > > From: Dave Kuhlman > <dku...@da...<mailto:dku...@da...>> > Date: Monday, Jun 06, 2022, 3:23 PM > To: Dewitte, Daniel W (US) > <dan...@ba...<mailto:dan...@ba...>> > Cc: Gen...@li... > <Gen...@li...<mailto:Gen...@li...>> > Subject: Re: [Generateds-discuss] How to create/ generate new xml > from generated API > > External Email Alert > > This email has been sent from an account outside of the BAE Systems network. > > Please treat the email with caution, especially if you are requested > to click on a link, decrypt/open an attachment, or enable macros. > For further information on how to spot phishing, access > “Cybersecurity OneSpace Page” and report phishing by clicking the > button “Report Phishing” on the Outlook toolbar. > > > Daniel, > > What? You don't seem like a Python newbie to me. > > About the error -- I believe that exception message is telling us > that it wants a different type for the value of > `testResults01.Personnel.set_SystemOperator`. > > Short version -- see attached file `test01.py`. > > Long attempted explanation -- see the following: > > In the XML schema (`TestResults.xsd`), search for "SystemOperator". > You will find the following: > > <xs:element name="SystemOperator" type="Person"> > <xs:annotation> > <xs:documentation>Identification information for the > operator of the ATE.</xs:documentation> > </xs:annotation> > </xs:element> > > The `type="Person"` tells us it want an instance of class `Person`. > So, look at the definition of class `Person` in the module that you > generated with `generateDS.py` (`atdsApi.py`, I believe). > > We have to construct a instance of class Person. And, since class > `Person` contains an `Address`, we can construct an instance of > class `Address`. > > I've attached a file (`test01.py`) with a modified version of your > code. And, > here is a snippet > > mailingaddress = lib.MailingAddress( > Address1="123 Pretty Lane", > City="Bigcity", > State="Arizona", > Country="USA", > PostalCode="98765", > ) > sysOp = lib.Person( > name="Dan", > Address=mailingaddress, > email="da...@so...", > phone="123.456.7890", > # etc > ) > testResults01.Personnel.set_SystemOperator(sysOp) > print (testResults01.Personnel.get_SystemOperator()) > testResults01.export(sys.stdout, 0) > > You can get clues about this if you look in the `build`, > `_buildAttributes`, and `_buildChildren` methods in classes > `PersonnelType`, `Person`, `Operator`, etc. Notice that, for the > `SystemOperator` member, it creates and instance of class `Person`. > And, since class `Person` is a subclass of class `Operator`, you may > want to look at the implementation of class `Operator` in > `atdsApi.py`. > > It's a bit confusing. It is to me at least. But, then, I suppose > XML and XML schema is supposed to be. Sigh. > > Hope this helps. > > Dave > > > > Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > >> Hi Dave, >> >> Thank you for the pointers, yes this worked great for me. I still >> get a few errors when creating the API but I will worry about those >> when I get to the external validator I have to use to ensure that >> everything is working. >> >> I also wanted to thank you, I am new to Python with this being my >> first major project. I have played around with an RPi but nothing >> major like up to this point. So some of the syntax and ways that >> Python uses is new to me and I am trying to learn. >> >> I do have some more questions at the moment and then I think I will >> be more then off and running on this next phase of this project. >> 1) You have been able to show me on how to create the initial >> instance of the Root Element, is that correct? As long as the line >> "testResults01 = lib.TestResults()" calls the root element class. >> Is that correct? >> >> 2) In the def __init__ of that element, I see that the input >> arguments are the attributes and child elements of that root >> element. See attached image. >> The first two in the init at the elements attributes, the rest are >> child elements. I can set the attributes by just replacing "None" >> with their values (hardcoded or generated). How to I generate the >> child elements (i.e. "Personnel") so that I can start setting their >> attributes and parameters? >> >> I have tried the following code: >> import sys >> import atdsApi as lib >> >> testResults01 = lib.TestResults(uuid = "1", securityClassification = >> "Unclassified", >> Personnel = lib.PersonnelType()) >> print (testResults01.get_uuid()) >> print (testResults01.get_securityClassification()) >> testResults01.export(sys.stdout, 0) >> >> I get this result: >> 1 >> Unclassified >> <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" >> xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" >> securityClassification="Unclassified"> >> <c:Personnel/> >> </c:TestResults> >> >> When I try to then go and set the attributes of Personnel which is >> controlled by PersonnelType() I get an error. >> Code: >> import sys >> import atdsApi as lib >> >> testResults01 = lib.TestResults(uuid = "1", securityClassification = >> "Unclassified", >> Personnel = lib.PersonnelType()) >> print (testResults01.get_uuid()) >> print (testResults01.get_securityClassification()) >> testResults01.Personnel.set_SystemOperator("Dan") >> print (testResults01.Personnel.get_SystemOperator()) >> testResults01.export(sys.stdout, 0) >> >> Error: >> 1 >> Unclassified >> Dan >> <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" >> xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" >> securityClassification="Unclassified"> >> <c:Personnel> >> Traceback (most recent call last): >> >> File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR >> Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line >> 16, in <module> >> testResults01.export(sys.stdout, 0) >> >> File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator\atdsApi.py", line 1314, in export >> self._exportChildren(outfile, level + 1, namespaceprefix_, >> namespacedef_, name_='TestResults', pretty_print=pretty_print) >> >> File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator\atdsApi.py", line 1342, in >> _exportChildren >> self.Personnel.export(outfile, level, namespaceprefix_, >> namespacedef_='', name_='Personnel', pretty_print=pretty_print) >> >> File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator\atdsApi.py", line 10284, in export >> self._exportChildren(outfile, level + 1, namespaceprefix_, >> namespacedef_, name_='PersonnelType', pretty_print=pretty_print) >> >> File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator\atdsApi.py", line 10304, in >> _exportChildren >> self.SystemOperator.export(outfile, level, namespaceprefix_, >> namespacedef_='', name_='SystemOperator', pretty_print=pretty_print) >> >> AttributeError: 'str' object has no attribute 'export' >> >> By reading the error it looks like it is actually on the export >> function and not the set function as it is setting the System >> Operator. >> Any ideas? >> >> I am sure to have more questions soon. >> Thank you again, >> >> Daniel DeWitte >> >> T: +1-603-885-2602 >> M: +1-603-759-4476 >> E: dan...@ba... >> >> -----Original Message----- >> From: Dave Kuhlman <dku...@da...> >> Sent: Thursday, June 2, 2022 3:56 PM >> To: Dewitte, Daniel W (US) <dan...@ba...> >> Cc: Gen...@li... >> Subject: Re: [Generateds-discuss] How to create/ generate new xml >> from generated API >> >> External Email Alert >> >> This email has been sent from an account outside of the BAE Systems network. >> >> Please treat the email with caution, especially if you are requested >> to click on a link, decrypt/open an attachment, or enable macros. >> For further information on how to spot phishing, access >> “Cybersecurity OneSpace Page” and report phishing by clicking the >> button “Report Phishing” on the Outlook toolbar. >> >> >> Daniel, >> >> Thanks for providing that info, command line, generated module, etc. >> >> When you run `generateDS.py`, the "-o" option means generate a main >> module, and the "-s" option says generate a subclass module. The >> "--super=xxxx" tells `generateDS.py` to generate an `import` >> statement in the subclass module that imports module `atdsApi.py`. >> Since you have given the same name for "-o" and "-s" that module >> imports itself. And, maybe, the main module is over-written by the >> subclass module. >> >> I don't know what your needs are, but it's likely that you do not >> want to use either "-s" or "--super" command line options. If you >> do not need the subclass module, do not generate it. >> >> My example command line does not make this very clear because, >> although it does use different names for "-o" and "-s", there is >> only one letter difference. Sorry for the confusion. >> >> Try running the following instead: >> >> $ python generateDS.py -f -o atdsApi.py --member-specs=dict >> --export="write etree validate" TestResults.xsd >> >> Then the following Python code should work: >> >> import sys >> import atdsApi as lib >> testResults01 = lib.TestResult() >> testResults01.export(sys.stdout, 0) >> >> Notice that I added "sys." in front of "stdout". >> >> Hope this helps. Let me know if/when you have more questions. >> >> Dave >> >> >> Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: >> >>> Hi Dave, >>> >>> Thank you for your quick response, so far the first part of what you >>> showed me is working, I do get some warnings though. It looks like >>> because the TestResults.xsd names multiple namespace prefix's it is >>> not carrying the "c:" prefix. >>> >>> 1) H:\Python Training\GenerateDS_Docs>python generateDS.py -f -o >>> atdsApi.py -s atdsApi.py --super=atdsApi --member-specs=dict >>> --export="write etree validate" TestResults.xsd >>> *** No suitable ns prefix for element "<XschemaElement name: >>> "Outcome" type: "Outcome">". >>> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >>> - Unknown namespace prefix "c" used. >>> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >>> - Unknown namespace prefix "c" used. >>> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >>> - Unknown namespace prefix "c" used. >>> >>> 2) I created a new .py file and have the following: >>> # -*- coding: utf-8 -*- >>> """ >>> Created on Wed May 25 11:02:45 2022 >>> >>> @author: daniel.dewitte >>> """ >>> import sys >>> import atdsApi as lib >>> >>> testResults01 = lib.TestResult() >>> testResults01.export(stdout, 0) >>> >>> When I try to run this .py I get the following errors: >>> runfile('H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >>> Importer - Python/ATDS File Creator/generateDS_xmlCreate.py', >>> wdir='H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer >>> - Python/ATDS File Creator') Traceback (most recent call last): >>> >>> File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR >>> Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line 8, >>> in <module> >>> import atdsApi as lib >>> >>> File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >>> Importer - Python/ATDS File Creator\atdsApi.py", line 68, in <module> >>> class TestResultsSub(supermod.TestResults): >>> >>> AttributeError: partially initialized module 'atdsApi' has no >>> attribute 'TestResults' (most likely due to a circular import) >>> >>> Attached the api.py for reference. >>> >>> Thanks, >>> Daniel DeWitte >>> >>> -----Original Message----- >>> From: Dave Kuhlman <dku...@da...> >>> Sent: Wednesday, June 1, 2022 7:08 PM >>> To: Dewitte, Daniel W (US) <dan...@ba...> >>> Cc: Gen...@li... >>> Subject: Re: [Generateds-discuss] How to create/ generate new xml from >>> generated API >>> >>> External Email Alert >>> >>> This email has been sent from an account outside of the BAE >>> Systems network. >>> >>> Please treat the email with caution, especially if you are requested >>> to click on a link, decrypt/open an attachment, or enable macros. >>> For further information on how to spot phishing, access “Cybersecurity >>> OneSpace Page” and report phishing by clicking the button “Report >>> Phishing” on the Outlook toolbar. >>> >>> >>> Daniel, >>> >>> Thanks for taking the time to report this. >>> >>> Here is what I've tried so far: >>> >>> 1. I copied TestResults.xml and Common.xsd to a directory. >>> >>> 2. I ran the following command: >>> >>> ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super >>> tmp01sup --member-specs=dict '--export=write etree validate' >>> TestResults.xsd >>> >>> That generated a module `tmp01sup.py`. >>> >>> 3. So, I started IPython (the plain Python interactive prompt would >>> also work) and did this: >>> >>> [ins] In [1]: import tmp01sup as lib >>> >>> Which also worked. >>> >>> 4. Then I created an instance of a class in the generated module: >>> >>> [ins] In [5]: testresults01 = lib.TestResult() >>> [ins] In [6]: testresults01.export(sys.stdout, 0) >>> <c:TestResult >>> xmlns:None="http://www.ieee.org/ATML/2006/Common" >>> xmlns:c="http://www.ieee.org/ATML/2006/Common" /> >>> >>> Which also seemed to work. >>> >>> So, now I've got to read your message a bit more carefully so that I >>> can figure out what is wrong for your case and how to fix it. >>> >>> I do not have the file `collect_schema_locations.py`, which you show >>> in your message as generating an exception. So, I can't test that. >>> Did you generate that file with `generateDS.py`? >>> >>> However the exception "TypeError: empty namespace prefix is not >>> supported in XPath" seems to occur when you have a key in the nsmap >>> that is `None`. Try removing it. Then do the `xpath` search: >>> >>> [ins] In [23]: nsmap = root.nsmap >>> [ins] In [24]: nsmap >>> Out[24]: >>> {'xs': 'http://www.w3.org/2001/XMLSchema', >>> 'c': 'http://www.ieee.org/ATML/2006/Common', >>> None: 'http://www.ieee.org/ATML/2007/TestResults'} >>> [ins] In [25]: nsmap.pop(None) >>> Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' >>> [ins] In [26]: nsmap >>> Out[26]: >>> {'xs': 'http://www.w3.org/2001/XMLSchema', >>> 'c': 'http://www.ieee.org/ATML/2006/Common'} >>> [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) >>> Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at >>> 0x7faf573acac0>] >>> >>> Does that help with your work? >>> >>> By the way, the "--one-file-per-xsd" capability is something that I >>> do not understand, myself. That code was contributed by someone else. >>> >>> Dave >>> >>> >>> Quoting "Dewitte, Daniel W (US) via Generateds-discuss" >>> <gen...@li...>: >>> >>>> Here are the actual files this time :) >>>> >>>> Daniel DeWitte >>>> >>>> From: Dewitte, Daniel W (US) >>>> Sent: Wednesday, June 1, 2022 1:06 PM >>>> To: 'Gen...@li...' >>>> <Gen...@li...> >>>> Subject: How to create/ generate new xml from generated API >>>> >>>> Hi Dave, >>>> >>>> This is one of the better tools that I have found and I think the only >>>> one that can work the way I need it too, I just need some help with >>>> getting to the finish line. >>>> >>>> My goal is generate a new .xml using a known schema .xsd with Python >>>> as the base language (required). I have 13 different reports that all >>>> have a different format that are generated on a constant basis that I >>>> need to import into a known .xsd format for upload to a database. >>>> >>>> I believe I either want to use the build methods, or the "one-per" >>>> options, but I can't get either of them to work. I asked about the >>>> build method problem on stackover flow here: Stackoverflow - Creating >>>> a new XML from an XSD using GenerateDS and >>>> Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml >>>> -from-an-xsd-using-generateds-and-python> >>>> >>>> But I am not sure that the build method is what I want to use. >>>> >>>> When I try the "one-per" I get the following errors: >>>> Command line method, I believe this error is due to that the .xsd is >>>> in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >>>>> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >>>>> --one-file-per-xsd --output-directory="OnePer" >>>>> --module-suffix="One" TestResults.xsd Traceback (most recent call >>>>> last): >>>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >>>>> in <module> >>>> main() >>>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, >>>>> in main >>>> parseAndGenerate( >>>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >>>>> in parseAndGenerate >>>> parser.parse(rootFile) >>>>> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in >>>>> parse >>>> xmlreader.IncrementalParser.parse(self, source) >>>>> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in >>>>> parse >>>> buffer = file.read(self._bufsize) >>>>> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in >>>>> decode >>>> return codecs.charmap_decode(input,self.errors,decoding_table)[0] >>>>> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >>>>> position 4013: character maps to <undefined> >>>> >>>> Approach 2 - Extraction and Generation Utilities: >>>>> (base) h:\Python Training\GenerateDS_Docs\utils>python >>>>> collect_schema_locations.py --force --verbose TestResults.xsd >>>>> testJson.txt Traceback (most recent call last): >>>>> File "h:\Python >>>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>>>> 157, in <module> >>>> main() >>>>> File "h:\Python >>>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>>>> 148, in main >>>> extract_and_generate(infile, outfile, extract_locations_fn, >>>> options) >>>>> File "h:\Python >>>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, >>>>> in extract_and_generate >>>> locations = extract_locations(infile, options) >>>>> File "h:\Python >>>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, >>>>> in extract_locations >>>> elements = root.xpath('xs:import', namespaces=root.nsmap) >>>>> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >>>>> File "src/lxml/xpath.pxi", line 259, in >>>>> lxml.etree.XPathElementEvaluator.__init__ >>>>> File "src/lxml/xpath.pxi", line 131, in >>>>> lxml.etree._XPathEvaluatorBase.__init__ >>>>> File "src/lxml/xpath.pxi", line 55, in >>>>> lxml.etree._XPathContext.__init__ File "src/lxml/extensions.pxi", >>>>> line 81, in lxml.etree._BaseContext.__init__ >>>>> TypeError: empty namespace prefix is not supported in XPath >>>> >>>> Attached are the .xsd that I am using. TestResults.xsd references >>>> Common.xsd. >>>> >>>> Any help on getting started with my overall goal will be much >>>> appreciated. I am looking to create a new XML from a known .xsd and >>>> add in the elements and child elements and attributes as needed. >>>> >>>> Thank you for the help, >>>> Daniel DeWitte >>> >>> >>> >>> -- >>> Dave Kuhlman >>> http://www.davekuhlman.org >> >> >> >> -- >> Dave Kuhlman >> http://www.davekuhlman.org > > > > -- > Dave Kuhlman > http://www.davekuhlman.org -- Dave Kuhlman http://www.davekuhlman.org |
From: Dewitte, D. W (US) <dan...@ba...> - 2022-06-06 20:06:54
|
Dave, Thank you for the help, truthfully I have done very basic things with Python up till about Feb. Since then I have taught myself the MVC programming model with PyQt5 packages. As well as the package installers and making the overall software OS agnostic. I have 15+ years in LabView programming and a lot can be adopted but I haven’t dealt with text based programming languages since highschool in early 2000’s. Thank you for the compliments. Maybe through all of this I can assist you in writing an in-depth tutorial of writing an instance to help others out. Sent with BlackBerry Work (www.blackberry.com) From: Dave Kuhlman <dku...@da...<mailto:dku...@da...>> Date: Monday, Jun 06, 2022, 3:23 PM To: Dewitte, Daniel W (US) <dan...@ba...<mailto:dan...@ba...>> Cc: Gen...@li... <Gen...@li...<mailto:Gen...@li...>> Subject: Re: [Generateds-discuss] How to create/ generate new xml from generated API External Email Alert This email has been sent from an account outside of the BAE Systems network. Please treat the email with caution, especially if you are requested to click on a link, decrypt/open an attachment, or enable macros. For further information on how to spot phishing, access “Cybersecurity OneSpace Page” and report phishing by clicking the button “Report Phishing” on the Outlook toolbar. Daniel, What? You don't seem like a Python newbie to me. About the error -- I believe that exception message is telling us that it wants a different type for the value of `testResults01.Personnel.set_SystemOperator`. Short version -- see attached file `test01.py`. Long attempted explanation -- see the following: In the XML schema (`TestResults.xsd`), search for "SystemOperator". You will find the following: <xs:element name="SystemOperator" type="Person"> <xs:annotation> <xs:documentation>Identification information for the operator of the ATE.</xs:documentation> </xs:annotation> </xs:element> The `type="Person"` tells us it want an instance of class `Person`. So, look at the definition of class `Person` in the module that you generated with `generateDS.py` (`atdsApi.py`, I believe). We have to construct a instance of class Person. And, since class `Person` contains an `Address`, we can construct an instance of class `Address`. I've attached a file (`test01.py`) with a modified version of your code. And, here is a snippet mailingaddress = lib.MailingAddress( Address1="123 Pretty Lane", City="Bigcity", State="Arizona", Country="USA", PostalCode="98765", ) sysOp = lib.Person( name="Dan", Address=mailingaddress, email="da...@so...", phone="123.456.7890", # etc ) testResults01.Personnel.set_SystemOperator(sysOp) print (testResults01.Personnel.get_SystemOperator()) testResults01.export(sys.stdout, 0) You can get clues about this if you look in the `build`, `_buildAttributes`, and `_buildChildren` methods in classes `PersonnelType`, `Person`, `Operator`, etc. Notice that, for the `SystemOperator` member, it creates and instance of class `Person`. And, since class `Person` is a subclass of class `Operator`, you may want to look at the implementation of class `Operator` in `atdsApi.py`. It's a bit confusing. It is to me at least. But, then, I suppose XML and XML schema is supposed to be. Sigh. Hope this helps. Dave Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > Hi Dave, > > Thank you for the pointers, yes this worked great for me. I still > get a few errors when creating the API but I will worry about those > when I get to the external validator I have to use to ensure that > everything is working. > > I also wanted to thank you, I am new to Python with this being my > first major project. I have played around with an RPi but nothing > major like up to this point. So some of the syntax and ways that > Python uses is new to me and I am trying to learn. > > I do have some more questions at the moment and then I think I will > be more then off and running on this next phase of this project. > 1) You have been able to show me on how to create the initial > instance of the Root Element, is that correct? As long as the line > "testResults01 = lib.TestResults()" calls the root element class. > Is that correct? > > 2) In the def __init__ of that element, I see that the input > arguments are the attributes and child elements of that root > element. See attached image. > The first two in the init at the elements attributes, the rest are > child elements. I can set the attributes by just replacing "None" > with their values (hardcoded or generated). How to I generate the > child elements (i.e. "Personnel") so that I can start setting their > attributes and parameters? > > I have tried the following code: > import sys > import atdsApi as lib > > testResults01 = lib.TestResults(uuid = "1", securityClassification = > "Unclassified", > Personnel = lib.PersonnelType()) > print (testResults01.get_uuid()) > print (testResults01.get_securityClassification()) > testResults01.export(sys.stdout, 0) > > I get this result: > 1 > Unclassified > <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" > xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" > securityClassification="Unclassified"> > <c:Personnel/> > </c:TestResults> > > When I try to then go and set the attributes of Personnel which is > controlled by PersonnelType() I get an error. > Code: > import sys > import atdsApi as lib > > testResults01 = lib.TestResults(uuid = "1", securityClassification = > "Unclassified", > Personnel = lib.PersonnelType()) > print (testResults01.get_uuid()) > print (testResults01.get_securityClassification()) > testResults01.Personnel.set_SystemOperator("Dan") > print (testResults01.Personnel.get_SystemOperator()) > testResults01.export(sys.stdout, 0) > > Error: > 1 > Unclassified > Dan > <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" > xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" > securityClassification="Unclassified"> > <c:Personnel> > Traceback (most recent call last): > > File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR > Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line > 16, in <module> > testResults01.export(sys.stdout, 0) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 1314, in export > self._exportChildren(outfile, level + 1, namespaceprefix_, > namespacedef_, name_='TestResults', pretty_print=pretty_print) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 1342, in > _exportChildren > self.Personnel.export(outfile, level, namespaceprefix_, > namespacedef_='', name_='Personnel', pretty_print=pretty_print) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 10284, in export > self._exportChildren(outfile, level + 1, namespaceprefix_, > namespacedef_, name_='PersonnelType', pretty_print=pretty_print) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 10304, in > _exportChildren > self.SystemOperator.export(outfile, level, namespaceprefix_, > namespacedef_='', name_='SystemOperator', pretty_print=pretty_print) > > AttributeError: 'str' object has no attribute 'export' > > By reading the error it looks like it is actually on the export > function and not the set function as it is setting the System > Operator. > Any ideas? > > I am sure to have more questions soon. > Thank you again, > > Daniel DeWitte > > T: +1-603-885-2602 > M: +1-603-759-4476 > E: dan...@ba... > > -----Original Message----- > From: Dave Kuhlman <dku...@da...> > Sent: Thursday, June 2, 2022 3:56 PM > To: Dewitte, Daniel W (US) <dan...@ba...> > Cc: Gen...@li... > Subject: Re: [Generateds-discuss] How to create/ generate new xml > from generated API > > External Email Alert > > This email has been sent from an account outside of the BAE Systems network. > > Please treat the email with caution, especially if you are requested > to click on a link, decrypt/open an attachment, or enable macros. > For further information on how to spot phishing, access > “Cybersecurity OneSpace Page” and report phishing by clicking the > button “Report Phishing” on the Outlook toolbar. > > > Daniel, > > Thanks for providing that info, command line, generated module, etc. > > When you run `generateDS.py`, the "-o" option means generate a main > module, and the "-s" option says generate a subclass module. The > "--super=xxxx" tells `generateDS.py` to generate an `import` > statement in the subclass module that imports module `atdsApi.py`. > Since you have given the same name for "-o" and "-s" that module > imports itself. And, maybe, the main module is over-written by the > subclass module. > > I don't know what your needs are, but it's likely that you do not > want to use either "-s" or "--super" command line options. If you > do not need the subclass module, do not generate it. > > My example command line does not make this very clear because, > although it does use different names for "-o" and "-s", there is > only one letter difference. Sorry for the confusion. > > Try running the following instead: > > $ python generateDS.py -f -o atdsApi.py --member-specs=dict > --export="write etree validate" TestResults.xsd > > Then the following Python code should work: > > import sys > import atdsApi as lib > testResults01 = lib.TestResult() > testResults01.export(sys.stdout, 0) > > Notice that I added "sys." in front of "stdout". > > Hope this helps. Let me know if/when you have more questions. > > Dave > > > Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > >> Hi Dave, >> >> Thank you for your quick response, so far the first part of what you >> showed me is working, I do get some warnings though. It looks like >> because the TestResults.xsd names multiple namespace prefix's it is >> not carrying the "c:" prefix. >> >> 1) H:\Python Training\GenerateDS_Docs>python generateDS.py -f -o >> atdsApi.py -s atdsApi.py --super=atdsApi --member-specs=dict >> --export="write etree validate" TestResults.xsd >> *** No suitable ns prefix for element "<XschemaElement name: >> "Outcome" type: "Outcome">". >> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >> - Unknown namespace prefix "c" used. >> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >> - Unknown namespace prefix "c" used. >> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >> - Unknown namespace prefix "c" used. >> >> 2) I created a new .py file and have the following: >> # -*- coding: utf-8 -*- >> """ >> Created on Wed May 25 11:02:45 2022 >> >> @author: daniel.dewitte >> """ >> import sys >> import atdsApi as lib >> >> testResults01 = lib.TestResult() >> testResults01.export(stdout, 0) >> >> When I try to run this .py I get the following errors: >> runfile('H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator/generateDS_xmlCreate.py', >> wdir='H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer >> - Python/ATDS File Creator') Traceback (most recent call last): >> >> File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR >> Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line 8, >> in <module> >> import atdsApi as lib >> >> File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator\atdsApi.py", line 68, in <module> >> class TestResultsSub(supermod.TestResults): >> >> AttributeError: partially initialized module 'atdsApi' has no >> attribute 'TestResults' (most likely due to a circular import) >> >> Attached the api.py for reference. >> >> Thanks, >> Daniel DeWitte >> >> -----Original Message----- >> From: Dave Kuhlman <dku...@da...> >> Sent: Wednesday, June 1, 2022 7:08 PM >> To: Dewitte, Daniel W (US) <dan...@ba...> >> Cc: Gen...@li... >> Subject: Re: [Generateds-discuss] How to create/ generate new xml from >> generated API >> >> External Email Alert >> >> This email has been sent from an account outside of the BAE Systems network. >> >> Please treat the email with caution, especially if you are requested >> to click on a link, decrypt/open an attachment, or enable macros. >> For further information on how to spot phishing, access “Cybersecurity >> OneSpace Page” and report phishing by clicking the button “Report >> Phishing” on the Outlook toolbar. >> >> >> Daniel, >> >> Thanks for taking the time to report this. >> >> Here is what I've tried so far: >> >> 1. I copied TestResults.xml and Common.xsd to a directory. >> >> 2. I ran the following command: >> >> ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super >> tmp01sup --member-specs=dict '--export=write etree validate' >> TestResults.xsd >> >> That generated a module `tmp01sup.py`. >> >> 3. So, I started IPython (the plain Python interactive prompt would >> also work) and did this: >> >> [ins] In [1]: import tmp01sup as lib >> >> Which also worked. >> >> 4. Then I created an instance of a class in the generated module: >> >> [ins] In [5]: testresults01 = lib.TestResult() >> [ins] In [6]: testresults01.export(sys.stdout, 0) >> <c:TestResult >> xmlns:None="http://www.ieee.org/ATML/2006/Common" >> xmlns:c="http://www.ieee.org/ATML/2006/Common" /> >> >> Which also seemed to work. >> >> So, now I've got to read your message a bit more carefully so that I >> can figure out what is wrong for your case and how to fix it. >> >> I do not have the file `collect_schema_locations.py`, which you show >> in your message as generating an exception. So, I can't test that. >> Did you generate that file with `generateDS.py`? >> >> However the exception "TypeError: empty namespace prefix is not >> supported in XPath" seems to occur when you have a key in the nsmap >> that is `None`. Try removing it. Then do the `xpath` search: >> >> [ins] In [23]: nsmap = root.nsmap >> [ins] In [24]: nsmap >> Out[24]: >> {'xs': 'http://www.w3.org/2001/XMLSchema', >> 'c': 'http://www.ieee.org/ATML/2006/Common', >> None: 'http://www.ieee.org/ATML/2007/TestResults'} >> [ins] In [25]: nsmap.pop(None) >> Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' >> [ins] In [26]: nsmap >> Out[26]: >> {'xs': 'http://www.w3.org/2001/XMLSchema', >> 'c': 'http://www.ieee.org/ATML/2006/Common'} >> [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) >> Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at >> 0x7faf573acac0>] >> >> Does that help with your work? >> >> By the way, the "--one-file-per-xsd" capability is something that I >> do not understand, myself. That code was contributed by someone else. >> >> Dave >> >> >> Quoting "Dewitte, Daniel W (US) via Generateds-discuss" >> <gen...@li...>: >> >>> Here are the actual files this time :) >>> >>> Daniel DeWitte >>> >>> From: Dewitte, Daniel W (US) >>> Sent: Wednesday, June 1, 2022 1:06 PM >>> To: 'Gen...@li...' >>> <Gen...@li...> >>> Subject: How to create/ generate new xml from generated API >>> >>> Hi Dave, >>> >>> This is one of the better tools that I have found and I think the only >>> one that can work the way I need it too, I just need some help with >>> getting to the finish line. >>> >>> My goal is generate a new .xml using a known schema .xsd with Python >>> as the base language (required). I have 13 different reports that all >>> have a different format that are generated on a constant basis that I >>> need to import into a known .xsd format for upload to a database. >>> >>> I believe I either want to use the build methods, or the "one-per" >>> options, but I can't get either of them to work. I asked about the >>> build method problem on stackover flow here: Stackoverflow - Creating >>> a new XML from an XSD using GenerateDS and >>> Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml >>> -from-an-xsd-using-generateds-and-python> >>> >>> But I am not sure that the build method is what I want to use. >>> >>> When I try the "one-per" I get the following errors: >>> Command line method, I believe this error is due to that the .xsd is >>> in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >>>> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >>>> --one-file-per-xsd --output-directory="OnePer" >>>> --module-suffix="One" TestResults.xsd Traceback (most recent call >>>> last): >>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >>>> in <module> >>> main() >>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, >>>> in main >>> parseAndGenerate( >>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >>>> in parseAndGenerate >>> parser.parse(rootFile) >>>> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in >>>> parse >>> xmlreader.IncrementalParser.parse(self, source) >>>> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in >>>> parse >>> buffer = file.read(self._bufsize) >>>> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in >>>> decode >>> return codecs.charmap_decode(input,self.errors,decoding_table)[0] >>>> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >>>> position 4013: character maps to <undefined> >>> >>> Approach 2 - Extraction and Generation Utilities: >>>> (base) h:\Python Training\GenerateDS_Docs\utils>python >>>> collect_schema_locations.py --force --verbose TestResults.xsd >>>> testJson.txt Traceback (most recent call last): >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>>> 157, in <module> >>> main() >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>>> 148, in main >>> extract_and_generate(infile, outfile, extract_locations_fn, >>> options) >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, >>>> in extract_and_generate >>> locations = extract_locations(infile, options) >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, >>>> in extract_locations >>> elements = root.xpath('xs:import', namespaces=root.nsmap) >>>> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >>>> File "src/lxml/xpath.pxi", line 259, in >>>> lxml.etree.XPathElementEvaluator.__init__ >>>> File "src/lxml/xpath.pxi", line 131, in >>>> lxml.etree._XPathEvaluatorBase.__init__ >>>> File "src/lxml/xpath.pxi", line 55, in >>>> lxml.etree._XPathContext.__init__ File "src/lxml/extensions.pxi", >>>> line 81, in lxml.etree._BaseContext.__init__ >>>> TypeError: empty namespace prefix is not supported in XPath >>> >>> Attached are the .xsd that I am using. TestResults.xsd references >>> Common.xsd. >>> >>> Any help on getting started with my overall goal will be much >>> appreciated. I am looking to create a new XML from a known .xsd and >>> add in the elements and child elements and attributes as needed. >>> >>> Thank you for the help, >>> Daniel DeWitte >> >> >> >> -- >> Dave Kuhlman >> http://www.davekuhlman.org > > > > -- > Dave Kuhlman > http://www.davekuhlman.org -- Dave Kuhlman http://www.davekuhlman.org |
From: Dave K. <dku...@da...> - 2022-06-06 19:23:24
|
Daniel, What? You don't seem like a Python newbie to me. About the error -- I believe that exception message is telling us that it wants a different type for the value of `testResults01.Personnel.set_SystemOperator`. Short version -- see attached file `test01.py`. Long attempted explanation -- see the following: In the XML schema (`TestResults.xsd`), search for "SystemOperator". You will find the following: <xs:element name="SystemOperator" type="Person"> <xs:annotation> <xs:documentation>Identification information for the operator of the ATE.</xs:documentation> </xs:annotation> </xs:element> The `type="Person"` tells us it want an instance of class `Person`. So, look at the definition of class `Person` in the module that you generated with `generateDS.py` (`atdsApi.py`, I believe). We have to construct a instance of class Person. And, since class `Person` contains an `Address`, we can construct an instance of class `Address`. I've attached a file (`test01.py`) with a modified version of your code. And, here is a snippet mailingaddress = lib.MailingAddress( Address1="123 Pretty Lane", City="Bigcity", State="Arizona", Country="USA", PostalCode="98765", ) sysOp = lib.Person( name="Dan", Address=mailingaddress, email="da...@so...", phone="123.456.7890", # etc ) testResults01.Personnel.set_SystemOperator(sysOp) print (testResults01.Personnel.get_SystemOperator()) testResults01.export(sys.stdout, 0) You can get clues about this if you look in the `build`, `_buildAttributes`, and `_buildChildren` methods in classes `PersonnelType`, `Person`, `Operator`, etc. Notice that, for the `SystemOperator` member, it creates and instance of class `Person`. And, since class `Person` is a subclass of class `Operator`, you may want to look at the implementation of class `Operator` in `atdsApi.py`. It's a bit confusing. It is to me at least. But, then, I suppose XML and XML schema is supposed to be. Sigh. Hope this helps. Dave Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > Hi Dave, > > Thank you for the pointers, yes this worked great for me. I still > get a few errors when creating the API but I will worry about those > when I get to the external validator I have to use to ensure that > everything is working. > > I also wanted to thank you, I am new to Python with this being my > first major project. I have played around with an RPi but nothing > major like up to this point. So some of the syntax and ways that > Python uses is new to me and I am trying to learn. > > I do have some more questions at the moment and then I think I will > be more then off and running on this next phase of this project. > 1) You have been able to show me on how to create the initial > instance of the Root Element, is that correct? As long as the line > "testResults01 = lib.TestResults()" calls the root element class. > Is that correct? > > 2) In the def __init__ of that element, I see that the input > arguments are the attributes and child elements of that root > element. See attached image. > The first two in the init at the elements attributes, the rest are > child elements. I can set the attributes by just replacing "None" > with their values (hardcoded or generated). How to I generate the > child elements (i.e. "Personnel") so that I can start setting their > attributes and parameters? > > I have tried the following code: > import sys > import atdsApi as lib > > testResults01 = lib.TestResults(uuid = "1", securityClassification = > "Unclassified", > Personnel = lib.PersonnelType()) > print (testResults01.get_uuid()) > print (testResults01.get_securityClassification()) > testResults01.export(sys.stdout, 0) > > I get this result: > 1 > Unclassified > <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" > xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" > securityClassification="Unclassified"> > <c:Personnel/> > </c:TestResults> > > When I try to then go and set the attributes of Personnel which is > controlled by PersonnelType() I get an error. > Code: > import sys > import atdsApi as lib > > testResults01 = lib.TestResults(uuid = "1", securityClassification = > "Unclassified", > Personnel = lib.PersonnelType()) > print (testResults01.get_uuid()) > print (testResults01.get_securityClassification()) > testResults01.Personnel.set_SystemOperator("Dan") > print (testResults01.Personnel.get_SystemOperator()) > testResults01.export(sys.stdout, 0) > > Error: > 1 > Unclassified > Dan > <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" > xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" > securityClassification="Unclassified"> > <c:Personnel> > Traceback (most recent call last): > > File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR > Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line > 16, in <module> > testResults01.export(sys.stdout, 0) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 1314, in export > self._exportChildren(outfile, level + 1, namespaceprefix_, > namespacedef_, name_='TestResults', pretty_print=pretty_print) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 1342, in > _exportChildren > self.Personnel.export(outfile, level, namespaceprefix_, > namespacedef_='', name_='Personnel', pretty_print=pretty_print) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 10284, in export > self._exportChildren(outfile, level + 1, namespaceprefix_, > namespacedef_, name_='PersonnelType', pretty_print=pretty_print) > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 10304, in > _exportChildren > self.SystemOperator.export(outfile, level, namespaceprefix_, > namespacedef_='', name_='SystemOperator', pretty_print=pretty_print) > > AttributeError: 'str' object has no attribute 'export' > > By reading the error it looks like it is actually on the export > function and not the set function as it is setting the System > Operator. > Any ideas? > > I am sure to have more questions soon. > Thank you again, > > Daniel DeWitte > > T: +1-603-885-2602 > M: +1-603-759-4476 > E: dan...@ba... > > -----Original Message----- > From: Dave Kuhlman <dku...@da...> > Sent: Thursday, June 2, 2022 3:56 PM > To: Dewitte, Daniel W (US) <dan...@ba...> > Cc: Gen...@li... > Subject: Re: [Generateds-discuss] How to create/ generate new xml > from generated API > > External Email Alert > > This email has been sent from an account outside of the BAE Systems network. > > Please treat the email with caution, especially if you are requested > to click on a link, decrypt/open an attachment, or enable macros. > For further information on how to spot phishing, access > “Cybersecurity OneSpace Page” and report phishing by clicking the > button “Report Phishing” on the Outlook toolbar. > > > Daniel, > > Thanks for providing that info, command line, generated module, etc. > > When you run `generateDS.py`, the "-o" option means generate a main > module, and the "-s" option says generate a subclass module. The > "--super=xxxx" tells `generateDS.py` to generate an `import` > statement in the subclass module that imports module `atdsApi.py`. > Since you have given the same name for "-o" and "-s" that module > imports itself. And, maybe, the main module is over-written by the > subclass module. > > I don't know what your needs are, but it's likely that you do not > want to use either "-s" or "--super" command line options. If you > do not need the subclass module, do not generate it. > > My example command line does not make this very clear because, > although it does use different names for "-o" and "-s", there is > only one letter difference. Sorry for the confusion. > > Try running the following instead: > > $ python generateDS.py -f -o atdsApi.py --member-specs=dict > --export="write etree validate" TestResults.xsd > > Then the following Python code should work: > > import sys > import atdsApi as lib > testResults01 = lib.TestResult() > testResults01.export(sys.stdout, 0) > > Notice that I added "sys." in front of "stdout". > > Hope this helps. Let me know if/when you have more questions. > > Dave > > > Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > >> Hi Dave, >> >> Thank you for your quick response, so far the first part of what you >> showed me is working, I do get some warnings though. It looks like >> because the TestResults.xsd names multiple namespace prefix's it is >> not carrying the "c:" prefix. >> >> 1) H:\Python Training\GenerateDS_Docs>python generateDS.py -f -o >> atdsApi.py -s atdsApi.py --super=atdsApi --member-specs=dict >> --export="write etree validate" TestResults.xsd >> *** No suitable ns prefix for element "<XschemaElement name: >> "Outcome" type: "Outcome">". >> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >> - Unknown namespace prefix "c" used. >> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >> - Unknown namespace prefix "c" used. >> WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS >> - Unknown namespace prefix "c" used. >> >> 2) I created a new .py file and have the following: >> # -*- coding: utf-8 -*- >> """ >> Created on Wed May 25 11:02:45 2022 >> >> @author: daniel.dewitte >> """ >> import sys >> import atdsApi as lib >> >> testResults01 = lib.TestResult() >> testResults01.export(stdout, 0) >> >> When I try to run this .py I get the following errors: >> runfile('H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator/generateDS_xmlCreate.py', >> wdir='H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer >> - Python/ATDS File Creator') Traceback (most recent call last): >> >> File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR >> Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line 8, >> in <module> >> import atdsApi as lib >> >> File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR >> Importer - Python/ATDS File Creator\atdsApi.py", line 68, in <module> >> class TestResultsSub(supermod.TestResults): >> >> AttributeError: partially initialized module 'atdsApi' has no >> attribute 'TestResults' (most likely due to a circular import) >> >> Attached the api.py for reference. >> >> Thanks, >> Daniel DeWitte >> >> -----Original Message----- >> From: Dave Kuhlman <dku...@da...> >> Sent: Wednesday, June 1, 2022 7:08 PM >> To: Dewitte, Daniel W (US) <dan...@ba...> >> Cc: Gen...@li... >> Subject: Re: [Generateds-discuss] How to create/ generate new xml from >> generated API >> >> External Email Alert >> >> This email has been sent from an account outside of the BAE Systems network. >> >> Please treat the email with caution, especially if you are requested >> to click on a link, decrypt/open an attachment, or enable macros. >> For further information on how to spot phishing, access “Cybersecurity >> OneSpace Page” and report phishing by clicking the button “Report >> Phishing” on the Outlook toolbar. >> >> >> Daniel, >> >> Thanks for taking the time to report this. >> >> Here is what I've tried so far: >> >> 1. I copied TestResults.xml and Common.xsd to a directory. >> >> 2. I ran the following command: >> >> ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super >> tmp01sup --member-specs=dict '--export=write etree validate' >> TestResults.xsd >> >> That generated a module `tmp01sup.py`. >> >> 3. So, I started IPython (the plain Python interactive prompt would >> also work) and did this: >> >> [ins] In [1]: import tmp01sup as lib >> >> Which also worked. >> >> 4. Then I created an instance of a class in the generated module: >> >> [ins] In [5]: testresults01 = lib.TestResult() >> [ins] In [6]: testresults01.export(sys.stdout, 0) >> <c:TestResult >> xmlns:None="http://www.ieee.org/ATML/2006/Common" >> xmlns:c="http://www.ieee.org/ATML/2006/Common" /> >> >> Which also seemed to work. >> >> So, now I've got to read your message a bit more carefully so that I >> can figure out what is wrong for your case and how to fix it. >> >> I do not have the file `collect_schema_locations.py`, which you show >> in your message as generating an exception. So, I can't test that. >> Did you generate that file with `generateDS.py`? >> >> However the exception "TypeError: empty namespace prefix is not >> supported in XPath" seems to occur when you have a key in the nsmap >> that is `None`. Try removing it. Then do the `xpath` search: >> >> [ins] In [23]: nsmap = root.nsmap >> [ins] In [24]: nsmap >> Out[24]: >> {'xs': 'http://www.w3.org/2001/XMLSchema', >> 'c': 'http://www.ieee.org/ATML/2006/Common', >> None: 'http://www.ieee.org/ATML/2007/TestResults'} >> [ins] In [25]: nsmap.pop(None) >> Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' >> [ins] In [26]: nsmap >> Out[26]: >> {'xs': 'http://www.w3.org/2001/XMLSchema', >> 'c': 'http://www.ieee.org/ATML/2006/Common'} >> [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) >> Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at >> 0x7faf573acac0>] >> >> Does that help with your work? >> >> By the way, the "--one-file-per-xsd" capability is something that I >> do not understand, myself. That code was contributed by someone else. >> >> Dave >> >> >> Quoting "Dewitte, Daniel W (US) via Generateds-discuss" >> <gen...@li...>: >> >>> Here are the actual files this time :) >>> >>> Daniel DeWitte >>> >>> From: Dewitte, Daniel W (US) >>> Sent: Wednesday, June 1, 2022 1:06 PM >>> To: 'Gen...@li...' >>> <Gen...@li...> >>> Subject: How to create/ generate new xml from generated API >>> >>> Hi Dave, >>> >>> This is one of the better tools that I have found and I think the only >>> one that can work the way I need it too, I just need some help with >>> getting to the finish line. >>> >>> My goal is generate a new .xml using a known schema .xsd with Python >>> as the base language (required). I have 13 different reports that all >>> have a different format that are generated on a constant basis that I >>> need to import into a known .xsd format for upload to a database. >>> >>> I believe I either want to use the build methods, or the "one-per" >>> options, but I can't get either of them to work. I asked about the >>> build method problem on stackover flow here: Stackoverflow - Creating >>> a new XML from an XSD using GenerateDS and >>> Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml >>> -from-an-xsd-using-generateds-and-python> >>> >>> But I am not sure that the build method is what I want to use. >>> >>> When I try the "one-per" I get the following errors: >>> Command line method, I believe this error is due to that the .xsd is >>> in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >>>> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >>>> --one-file-per-xsd --output-directory="OnePer" >>>> --module-suffix="One" TestResults.xsd Traceback (most recent call >>>> last): >>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >>>> in <module> >>> main() >>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, >>>> in main >>> parseAndGenerate( >>>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >>>> in parseAndGenerate >>> parser.parse(rootFile) >>>> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in >>>> parse >>> xmlreader.IncrementalParser.parse(self, source) >>>> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in >>>> parse >>> buffer = file.read(self._bufsize) >>>> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in >>>> decode >>> return codecs.charmap_decode(input,self.errors,decoding_table)[0] >>>> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >>>> position 4013: character maps to <undefined> >>> >>> Approach 2 - Extraction and Generation Utilities: >>>> (base) h:\Python Training\GenerateDS_Docs\utils>python >>>> collect_schema_locations.py --force --verbose TestResults.xsd >>>> testJson.txt Traceback (most recent call last): >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>>> 157, in <module> >>> main() >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>>> 148, in main >>> extract_and_generate(infile, outfile, extract_locations_fn, >>> options) >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, >>>> in extract_and_generate >>> locations = extract_locations(infile, options) >>>> File "h:\Python >>>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, >>>> in extract_locations >>> elements = root.xpath('xs:import', namespaces=root.nsmap) >>>> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >>>> File "src/lxml/xpath.pxi", line 259, in >>>> lxml.etree.XPathElementEvaluator.__init__ >>>> File "src/lxml/xpath.pxi", line 131, in >>>> lxml.etree._XPathEvaluatorBase.__init__ >>>> File "src/lxml/xpath.pxi", line 55, in >>>> lxml.etree._XPathContext.__init__ File "src/lxml/extensions.pxi", >>>> line 81, in lxml.etree._BaseContext.__init__ >>>> TypeError: empty namespace prefix is not supported in XPath >>> >>> Attached are the .xsd that I am using. TestResults.xsd references >>> Common.xsd. >>> >>> Any help on getting started with my overall goal will be much >>> appreciated. I am looking to create a new XML from a known .xsd and >>> add in the elements and child elements and attributes as needed. >>> >>> Thank you for the help, >>> Daniel DeWitte >> >> >> >> -- >> Dave Kuhlman >> http://www.davekuhlman.org > > > > -- > Dave Kuhlman > http://www.davekuhlman.org -- Dave Kuhlman http://www.davekuhlman.org |
From: Dewitte, D. W (US) <dan...@ba...> - 2022-06-03 20:39:25
|
Hi Dave, Thank you for the pointers, yes this worked great for me. I still get a few errors when creating the API but I will worry about those when I get to the external validator I have to use to ensure that everything is working. I also wanted to thank you, I am new to Python with this being my first major project. I have played around with an RPi but nothing major like up to this point. So some of the syntax and ways that Python uses is new to me and I am trying to learn. I do have some more questions at the moment and then I think I will be more then off and running on this next phase of this project. 1) You have been able to show me on how to create the initial instance of the Root Element, is that correct? As long as the line "testResults01 = lib.TestResults()" calls the root element class. Is that correct? 2) In the def __init__ of that element, I see that the input arguments are the attributes and child elements of that root element. See attached image. The first two in the init at the elements attributes, the rest are child elements. I can set the attributes by just replacing "None" with their values (hardcoded or generated). How to I generate the child elements (i.e. "Personnel") so that I can start setting their attributes and parameters? I have tried the following code: import sys import atdsApi as lib testResults01 = lib.TestResults(uuid = "1", securityClassification = "Unclassified", Personnel = lib.PersonnelType()) print (testResults01.get_uuid()) print (testResults01.get_securityClassification()) testResults01.export(sys.stdout, 0) I get this result: 1 Unclassified <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" securityClassification="Unclassified"> <c:Personnel/> </c:TestResults> When I try to then go and set the attributes of Personnel which is controlled by PersonnelType() I get an error. Code: import sys import atdsApi as lib testResults01 = lib.TestResults(uuid = "1", securityClassification = "Unclassified", Personnel = lib.PersonnelType()) print (testResults01.get_uuid()) print (testResults01.get_securityClassification()) testResults01.Personnel.set_SystemOperator("Dan") print (testResults01.Personnel.get_SystemOperator()) testResults01.export(sys.stdout, 0) Error: 1 Unclassified Dan <c:TestResults xmlns:None="http://www.ieee.org/ATML/2006/Common" xmlns:c="http://www.ieee.org/ATML/2006/Common" uuid="1" securityClassification="Unclassified"> <c:Personnel> Traceback (most recent call last): File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line 16, in <module> testResults01.export(sys.stdout, 0) File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer - Python/ATDS File Creator\atdsApi.py", line 1314, in export self._exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='TestResults', pretty_print=pretty_print) File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer - Python/ATDS File Creator\atdsApi.py", line 1342, in _exportChildren self.Personnel.export(outfile, level, namespaceprefix_, namespacedef_='', name_='Personnel', pretty_print=pretty_print) File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer - Python/ATDS File Creator\atdsApi.py", line 10284, in export self._exportChildren(outfile, level + 1, namespaceprefix_, namespacedef_, name_='PersonnelType', pretty_print=pretty_print) File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer - Python/ATDS File Creator\atdsApi.py", line 10304, in _exportChildren self.SystemOperator.export(outfile, level, namespaceprefix_, namespacedef_='', name_='SystemOperator', pretty_print=pretty_print) AttributeError: 'str' object has no attribute 'export' By reading the error it looks like it is actually on the export function and not the set function as it is setting the System Operator. Any ideas? I am sure to have more questions soon. Thank you again, Daniel DeWitte T: +1-603-885-2602 M: +1-603-759-4476 E: dan...@ba... -----Original Message----- From: Dave Kuhlman <dku...@da...> Sent: Thursday, June 2, 2022 3:56 PM To: Dewitte, Daniel W (US) <dan...@ba...> Cc: Gen...@li... Subject: Re: [Generateds-discuss] How to create/ generate new xml from generated API External Email Alert This email has been sent from an account outside of the BAE Systems network. Please treat the email with caution, especially if you are requested to click on a link, decrypt/open an attachment, or enable macros. For further information on how to spot phishing, access “Cybersecurity OneSpace Page” and report phishing by clicking the button “Report Phishing” on the Outlook toolbar. Daniel, Thanks for providing that info, command line, generated module, etc. When you run `generateDS.py`, the "-o" option means generate a main module, and the "-s" option says generate a subclass module. The "--super=xxxx" tells `generateDS.py` to generate an `import` statement in the subclass module that imports module `atdsApi.py`. Since you have given the same name for "-o" and "-s" that module imports itself. And, maybe, the main module is over-written by the subclass module. I don't know what your needs are, but it's likely that you do not want to use either "-s" or "--super" command line options. If you do not need the subclass module, do not generate it. My example command line does not make this very clear because, although it does use different names for "-o" and "-s", there is only one letter difference. Sorry for the confusion. Try running the following instead: $ python generateDS.py -f -o atdsApi.py --member-specs=dict --export="write etree validate" TestResults.xsd Then the following Python code should work: import sys import atdsApi as lib testResults01 = lib.TestResult() testResults01.export(sys.stdout, 0) Notice that I added "sys." in front of "stdout". Hope this helps. Let me know if/when you have more questions. Dave Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > Hi Dave, > > Thank you for your quick response, so far the first part of what you > showed me is working, I do get some warnings though. It looks like > because the TestResults.xsd names multiple namespace prefix's it is > not carrying the "c:" prefix. > > 1) H:\Python Training\GenerateDS_Docs>python generateDS.py -f -o > atdsApi.py -s atdsApi.py --super=atdsApi --member-specs=dict > --export="write etree validate" TestResults.xsd > *** No suitable ns prefix for element "<XschemaElement name: > "Outcome" type: "Outcome">". > WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS > - Unknown namespace prefix "c" used. > WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS > - Unknown namespace prefix "c" used. > WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS > - Unknown namespace prefix "c" used. > > 2) I created a new .py file and have the following: > # -*- coding: utf-8 -*- > """ > Created on Wed May 25 11:02:45 2022 > > @author: daniel.dewitte > """ > import sys > import atdsApi as lib > > testResults01 = lib.TestResult() > testResults01.export(stdout, 0) > > When I try to run this .py I get the following errors: > runfile('H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator/generateDS_xmlCreate.py', > wdir='H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer > - Python/ATDS File Creator') Traceback (most recent call last): > > File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR > Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line 8, > in <module> > import atdsApi as lib > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 68, in <module> > class TestResultsSub(supermod.TestResults): > > AttributeError: partially initialized module 'atdsApi' has no > attribute 'TestResults' (most likely due to a circular import) > > Attached the api.py for reference. > > Thanks, > Daniel DeWitte > > -----Original Message----- > From: Dave Kuhlman <dku...@da...> > Sent: Wednesday, June 1, 2022 7:08 PM > To: Dewitte, Daniel W (US) <dan...@ba...> > Cc: Gen...@li... > Subject: Re: [Generateds-discuss] How to create/ generate new xml from > generated API > > External Email Alert > > This email has been sent from an account outside of the BAE Systems network. > > Please treat the email with caution, especially if you are requested > to click on a link, decrypt/open an attachment, or enable macros. > For further information on how to spot phishing, access “Cybersecurity > OneSpace Page” and report phishing by clicking the button “Report > Phishing” on the Outlook toolbar. > > > Daniel, > > Thanks for taking the time to report this. > > Here is what I've tried so far: > > 1. I copied TestResults.xml and Common.xsd to a directory. > > 2. I ran the following command: > > ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super > tmp01sup --member-specs=dict '--export=write etree validate' > TestResults.xsd > > That generated a module `tmp01sup.py`. > > 3. So, I started IPython (the plain Python interactive prompt would > also work) and did this: > > [ins] In [1]: import tmp01sup as lib > > Which also worked. > > 4. Then I created an instance of a class in the generated module: > > [ins] In [5]: testresults01 = lib.TestResult() > [ins] In [6]: testresults01.export(sys.stdout, 0) > <c:TestResult > xmlns:None="http://www.ieee.org/ATML/2006/Common" > xmlns:c="http://www.ieee.org/ATML/2006/Common" /> > > Which also seemed to work. > > So, now I've got to read your message a bit more carefully so that I > can figure out what is wrong for your case and how to fix it. > > I do not have the file `collect_schema_locations.py`, which you show > in your message as generating an exception. So, I can't test that. > Did you generate that file with `generateDS.py`? > > However the exception "TypeError: empty namespace prefix is not > supported in XPath" seems to occur when you have a key in the nsmap > that is `None`. Try removing it. Then do the `xpath` search: > > [ins] In [23]: nsmap = root.nsmap > [ins] In [24]: nsmap > Out[24]: > {'xs': 'http://www.w3.org/2001/XMLSchema', > 'c': 'http://www.ieee.org/ATML/2006/Common', > None: 'http://www.ieee.org/ATML/2007/TestResults'} > [ins] In [25]: nsmap.pop(None) > Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' > [ins] In [26]: nsmap > Out[26]: > {'xs': 'http://www.w3.org/2001/XMLSchema', > 'c': 'http://www.ieee.org/ATML/2006/Common'} > [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) > Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at > 0x7faf573acac0>] > > Does that help with your work? > > By the way, the "--one-file-per-xsd" capability is something that I > do not understand, myself. That code was contributed by someone else. > > Dave > > > Quoting "Dewitte, Daniel W (US) via Generateds-discuss" > <gen...@li...>: > >> Here are the actual files this time :) >> >> Daniel DeWitte >> >> From: Dewitte, Daniel W (US) >> Sent: Wednesday, June 1, 2022 1:06 PM >> To: 'Gen...@li...' >> <Gen...@li...> >> Subject: How to create/ generate new xml from generated API >> >> Hi Dave, >> >> This is one of the better tools that I have found and I think the only >> one that can work the way I need it too, I just need some help with >> getting to the finish line. >> >> My goal is generate a new .xml using a known schema .xsd with Python >> as the base language (required). I have 13 different reports that all >> have a different format that are generated on a constant basis that I >> need to import into a known .xsd format for upload to a database. >> >> I believe I either want to use the build methods, or the "one-per" >> options, but I can't get either of them to work. I asked about the >> build method problem on stackover flow here: Stackoverflow - Creating >> a new XML from an XSD using GenerateDS and >> Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml >> -from-an-xsd-using-generateds-and-python> >> >> But I am not sure that the build method is what I want to use. >> >> When I try the "one-per" I get the following errors: >> Command line method, I believe this error is due to that the .xsd is >> in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >>> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >>> --one-file-per-xsd --output-directory="OnePer" >>> --module-suffix="One" TestResults.xsd Traceback (most recent call >>> last): >>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >>> in <module> >> main() >>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, >>> in main >> parseAndGenerate( >>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >>> in parseAndGenerate >> parser.parse(rootFile) >>> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in >>> parse >> xmlreader.IncrementalParser.parse(self, source) >>> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in >>> parse >> buffer = file.read(self._bufsize) >>> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in >>> decode >> return codecs.charmap_decode(input,self.errors,decoding_table)[0] >>> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >>> position 4013: character maps to <undefined> >> >> Approach 2 - Extraction and Generation Utilities: >>> (base) h:\Python Training\GenerateDS_Docs\utils>python >>> collect_schema_locations.py --force --verbose TestResults.xsd >>> testJson.txt Traceback (most recent call last): >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>> 157, in <module> >> main() >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>> 148, in main >> extract_and_generate(infile, outfile, extract_locations_fn, >> options) >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, >>> in extract_and_generate >> locations = extract_locations(infile, options) >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, >>> in extract_locations >> elements = root.xpath('xs:import', namespaces=root.nsmap) >>> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >>> File "src/lxml/xpath.pxi", line 259, in >>> lxml.etree.XPathElementEvaluator.__init__ >>> File "src/lxml/xpath.pxi", line 131, in >>> lxml.etree._XPathEvaluatorBase.__init__ >>> File "src/lxml/xpath.pxi", line 55, in >>> lxml.etree._XPathContext.__init__ File "src/lxml/extensions.pxi", >>> line 81, in lxml.etree._BaseContext.__init__ >>> TypeError: empty namespace prefix is not supported in XPath >> >> Attached are the .xsd that I am using. TestResults.xsd references >> Common.xsd. >> >> Any help on getting started with my overall goal will be much >> appreciated. I am looking to create a new XML from a known .xsd and >> add in the elements and child elements and attributes as needed. >> >> Thank you for the help, >> Daniel DeWitte > > > > -- > Dave Kuhlman > http://www.davekuhlman.org -- Dave Kuhlman http://www.davekuhlman.org |
From: Dave K. <dku...@da...> - 2022-06-02 19:56:40
|
Daniel, Thanks for providing that info, command line, generated module, etc. When you run `generateDS.py`, the "-o" option means generate a main module, and the "-s" option says generate a subclass module. The "--super=xxxx" tells `generateDS.py` to generate an `import` statement in the subclass module that imports module `atdsApi.py`. Since you have given the same name for "-o" and "-s" that module imports itself. And, maybe, the main module is over-written by the subclass module. I don't know what your needs are, but it's likely that you do not want to use either "-s" or "--super" command line options. If you do not need the subclass module, do not generate it. My example command line does not make this very clear because, although it does use different names for "-o" and "-s", there is only one letter difference. Sorry for the confusion. Try running the following instead: $ python generateDS.py -f -o atdsApi.py --member-specs=dict --export="write etree validate" TestResults.xsd Then the following Python code should work: import sys import atdsApi as lib testResults01 = lib.TestResult() testResults01.export(sys.stdout, 0) Notice that I added "sys." in front of "stdout". Hope this helps. Let me know if/when you have more questions. Dave Quoting "Dewitte, Daniel W (US)" <dan...@ba...>: > Hi Dave, > > Thank you for your quick response, so far the first part of what you > showed me is working, I do get some warnings though. It looks like > because the TestResults.xsd names multiple namespace prefix's it is > not carrying the "c:" prefix. > > 1) H:\Python Training\GenerateDS_Docs>python generateDS.py -f -o > atdsApi.py -s atdsApi.py --super=atdsApi --member-specs=dict > --export="write etree validate" TestResults.xsd > *** No suitable ns prefix for element "<XschemaElement name: > "Outcome" type: "Outcome">". > WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - > _lookupNS - Unknown namespace prefix "c" used. > WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - > _lookupNS - Unknown namespace prefix "c" used. > WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - > _lookupNS - Unknown namespace prefix "c" used. > > 2) I created a new .py file and have the following: > # -*- coding: utf-8 -*- > """ > Created on Wed May 25 11:02:45 2022 > > @author: daniel.dewitte > """ > import sys > import atdsApi as lib > > testResults01 = lib.TestResult() > testResults01.export(stdout, 0) > > When I try to run this .py I get the following errors: > runfile('H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator/generateDS_xmlCreate.py', > wdir='H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator') > Traceback (most recent call last): > > File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR > Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line > 8, in <module> > import atdsApi as lib > > File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR > Importer - Python/ATDS File Creator\atdsApi.py", line 68, in <module> > class TestResultsSub(supermod.TestResults): > > AttributeError: partially initialized module 'atdsApi' has no > attribute 'TestResults' (most likely due to a circular import) > > Attached the api.py for reference. > > Thanks, > Daniel DeWitte > > -----Original Message----- > From: Dave Kuhlman <dku...@da...> > Sent: Wednesday, June 1, 2022 7:08 PM > To: Dewitte, Daniel W (US) <dan...@ba...> > Cc: Gen...@li... > Subject: Re: [Generateds-discuss] How to create/ generate new xml > from generated API > > External Email Alert > > This email has been sent from an account outside of the BAE Systems network. > > Please treat the email with caution, especially if you are requested > to click on a link, decrypt/open an attachment, or enable macros. > For further information on how to spot phishing, access > “Cybersecurity OneSpace Page” and report phishing by clicking the > button “Report Phishing” on the Outlook toolbar. > > > Daniel, > > Thanks for taking the time to report this. > > Here is what I've tried so far: > > 1. I copied TestResults.xml and Common.xsd to a directory. > > 2. I ran the following command: > > ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super > tmp01sup --member-specs=dict '--export=write etree validate' > TestResults.xsd > > That generated a module `tmp01sup.py`. > > 3. So, I started IPython (the plain Python interactive prompt would > also work) and did this: > > [ins] In [1]: import tmp01sup as lib > > Which also worked. > > 4. Then I created an instance of a class in the generated module: > > [ins] In [5]: testresults01 = lib.TestResult() > [ins] In [6]: testresults01.export(sys.stdout, 0) > <c:TestResult > xmlns:None="http://www.ieee.org/ATML/2006/Common" > xmlns:c="http://www.ieee.org/ATML/2006/Common" /> > > Which also seemed to work. > > So, now I've got to read your message a bit more carefully so that I > can figure out what is wrong for your case and how to fix it. > > I do not have the file `collect_schema_locations.py`, which you show > in your message as generating an exception. So, I can't test that. > Did you generate that file with `generateDS.py`? > > However the exception "TypeError: empty namespace prefix is not > supported in XPath" seems to occur when you have a key in the nsmap > that is `None`. Try removing it. Then do the `xpath` search: > > [ins] In [23]: nsmap = root.nsmap > [ins] In [24]: nsmap > Out[24]: > {'xs': 'http://www.w3.org/2001/XMLSchema', > 'c': 'http://www.ieee.org/ATML/2006/Common', > None: 'http://www.ieee.org/ATML/2007/TestResults'} > [ins] In [25]: nsmap.pop(None) > Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' > [ins] In [26]: nsmap > Out[26]: > {'xs': 'http://www.w3.org/2001/XMLSchema', > 'c': 'http://www.ieee.org/ATML/2006/Common'} > [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) > Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at > 0x7faf573acac0>] > > Does that help with your work? > > By the way, the "--one-file-per-xsd" capability is something that I > do not understand, myself. That code was contributed by someone else. > > Dave > > > Quoting "Dewitte, Daniel W (US) via Generateds-discuss" > <gen...@li...>: > >> Here are the actual files this time :) >> >> Daniel DeWitte >> >> From: Dewitte, Daniel W (US) >> Sent: Wednesday, June 1, 2022 1:06 PM >> To: 'Gen...@li...' >> <Gen...@li...> >> Subject: How to create/ generate new xml from generated API >> >> Hi Dave, >> >> This is one of the better tools that I have found and I think the only >> one that can work the way I need it too, I just need some help with >> getting to the finish line. >> >> My goal is generate a new .xml using a known schema .xsd with Python >> as the base language (required). I have 13 different reports that all >> have a different format that are generated on a constant basis that I >> need to import into a known .xsd format for upload to a database. >> >> I believe I either want to use the build methods, or the "one-per" >> options, but I can't get either of them to work. I asked about the >> build method problem on stackover flow here: Stackoverflow - Creating >> a new XML from an XSD using GenerateDS and >> Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml >> -from-an-xsd-using-generateds-and-python> >> >> But I am not sure that the build method is what I want to use. >> >> When I try the "one-per" I get the following errors: >> Command line method, I believe this error is due to that the .xsd is >> in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >>> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >>> --one-file-per-xsd --output-directory="OnePer" >>> --module-suffix="One" TestResults.xsd Traceback (most recent call >>> last): >>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >>> in <module> >> main() >>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, >>> in main >> parseAndGenerate( >>> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >>> in parseAndGenerate >> parser.parse(rootFile) >>> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in >>> parse >> xmlreader.IncrementalParser.parse(self, source) >>> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in >>> parse >> buffer = file.read(self._bufsize) >>> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in >>> decode >> return codecs.charmap_decode(input,self.errors,decoding_table)[0] >>> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >>> position 4013: character maps to <undefined> >> >> Approach 2 - Extraction and Generation Utilities: >>> (base) h:\Python Training\GenerateDS_Docs\utils>python >>> collect_schema_locations.py --force --verbose TestResults.xsd >>> testJson.txt Traceback (most recent call last): >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>> 157, in <module> >> main() >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >>> 148, in main >> extract_and_generate(infile, outfile, extract_locations_fn, >> options) >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, >>> in extract_and_generate >> locations = extract_locations(infile, options) >>> File "h:\Python >>> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, >>> in extract_locations >> elements = root.xpath('xs:import', namespaces=root.nsmap) >>> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >>> File "src/lxml/xpath.pxi", line 259, in >>> lxml.etree.XPathElementEvaluator.__init__ >>> File "src/lxml/xpath.pxi", line 131, in >>> lxml.etree._XPathEvaluatorBase.__init__ >>> File "src/lxml/xpath.pxi", line 55, in >>> lxml.etree._XPathContext.__init__ File "src/lxml/extensions.pxi", >>> line 81, in lxml.etree._BaseContext.__init__ >>> TypeError: empty namespace prefix is not supported in XPath >> >> Attached are the .xsd that I am using. TestResults.xsd references >> Common.xsd. >> >> Any help on getting started with my overall goal will be much >> appreciated. I am looking to create a new XML from a known .xsd and >> add in the elements and child elements and attributes as needed. >> >> Thank you for the help, >> Daniel DeWitte > > > > -- > Dave Kuhlman > http://www.davekuhlman.org -- Dave Kuhlman http://www.davekuhlman.org |
From: Dewitte, D. W (US) <dan...@ba...> - 2022-06-02 15:03:55
|
Hi Dave, Thank you for your quick response, so far the first part of what you showed me is working, I do get some warnings though. It looks like because the TestResults.xsd names multiple namespace prefix's it is not carrying the "c:" prefix. 1) H:\Python Training\GenerateDS_Docs>python generateDS.py -f -o atdsApi.py -s atdsApi.py --super=atdsApi --member-specs=dict --export="write etree validate" TestResults.xsd *** No suitable ns prefix for element "<XschemaElement name: "Outcome" type: "Outcome">". WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS - Unknown namespace prefix "c" used. WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS - Unknown namespace prefix "c" used. WARNING - H:\Python Training\GenerateDS_Docs\generateDS.py - _lookupNS - Unknown namespace prefix "c" used. 2) I created a new .py file and have the following: # -*- coding: utf-8 -*- """ Created on Wed May 25 11:02:45 2022 @author: daniel.dewitte """ import sys import atdsApi as lib testResults01 = lib.TestResult() testResults01.export(stdout, 0) When I try to run this .py I get the following errors: runfile('H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer - Python/ATDS File Creator/generateDS_xmlCreate.py', wdir='H:/C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer - Python/ATDS File Creator') Traceback (most recent call last): File "H:\C4ISR S2 NPI\Projects\Voice of the Process\ATDS TDR Importer - Python\ATDS File Creator\generateDS_xmlCreate.py", line 8, in <module> import atdsApi as lib File "H:\C4ISR S2 NPI/Projects/Voice of the Process/ATDS TDR Importer - Python/ATDS File Creator\atdsApi.py", line 68, in <module> class TestResultsSub(supermod.TestResults): AttributeError: partially initialized module 'atdsApi' has no attribute 'TestResults' (most likely due to a circular import) Attached the api.py for reference. Thanks, Daniel DeWitte -----Original Message----- From: Dave Kuhlman <dku...@da...> Sent: Wednesday, June 1, 2022 7:08 PM To: Dewitte, Daniel W (US) <dan...@ba...> Cc: Gen...@li... Subject: Re: [Generateds-discuss] How to create/ generate new xml from generated API External Email Alert This email has been sent from an account outside of the BAE Systems network. Please treat the email with caution, especially if you are requested to click on a link, decrypt/open an attachment, or enable macros. For further information on how to spot phishing, access “Cybersecurity OneSpace Page” and report phishing by clicking the button “Report Phishing” on the Outlook toolbar. Daniel, Thanks for taking the time to report this. Here is what I've tried so far: 1. I copied TestResults.xml and Common.xsd to a directory. 2. I ran the following command: ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super tmp01sup --member-specs=dict '--export=write etree validate' TestResults.xsd That generated a module `tmp01sup.py`. 3. So, I started IPython (the plain Python interactive prompt would also work) and did this: [ins] In [1]: import tmp01sup as lib Which also worked. 4. Then I created an instance of a class in the generated module: [ins] In [5]: testresults01 = lib.TestResult() [ins] In [6]: testresults01.export(sys.stdout, 0) <c:TestResult xmlns:None="http://www.ieee.org/ATML/2006/Common" xmlns:c="http://www.ieee.org/ATML/2006/Common" /> Which also seemed to work. So, now I've got to read your message a bit more carefully so that I can figure out what is wrong for your case and how to fix it. I do not have the file `collect_schema_locations.py`, which you show in your message as generating an exception. So, I can't test that. Did you generate that file with `generateDS.py`? However the exception "TypeError: empty namespace prefix is not supported in XPath" seems to occur when you have a key in the nsmap that is `None`. Try removing it. Then do the `xpath` search: [ins] In [23]: nsmap = root.nsmap [ins] In [24]: nsmap Out[24]: {'xs': 'http://www.w3.org/2001/XMLSchema', 'c': 'http://www.ieee.org/ATML/2006/Common', None: 'http://www.ieee.org/ATML/2007/TestResults'} [ins] In [25]: nsmap.pop(None) Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' [ins] In [26]: nsmap Out[26]: {'xs': 'http://www.w3.org/2001/XMLSchema', 'c': 'http://www.ieee.org/ATML/2006/Common'} [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at 0x7faf573acac0>] Does that help with your work? By the way, the "--one-file-per-xsd" capability is something that I do not understand, myself. That code was contributed by someone else. Dave Quoting "Dewitte, Daniel W (US) via Generateds-discuss" <gen...@li...>: > Here are the actual files this time :) > > Daniel DeWitte > > From: Dewitte, Daniel W (US) > Sent: Wednesday, June 1, 2022 1:06 PM > To: 'Gen...@li...' > <Gen...@li...> > Subject: How to create/ generate new xml from generated API > > Hi Dave, > > This is one of the better tools that I have found and I think the only > one that can work the way I need it too, I just need some help with > getting to the finish line. > > My goal is generate a new .xml using a known schema .xsd with Python > as the base language (required). I have 13 different reports that all > have a different format that are generated on a constant basis that I > need to import into a known .xsd format for upload to a database. > > I believe I either want to use the build methods, or the "one-per" > options, but I can't get either of them to work. I asked about the > build method problem on stackover flow here: Stackoverflow - Creating > a new XML from an XSD using GenerateDS and > Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml > -from-an-xsd-using-generateds-and-python> > > But I am not sure that the build method is what I want to use. > > When I try the "one-per" I get the following errors: > Command line method, I believe this error is due to that the .xsd is > in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >> --one-file-per-xsd --output-directory="OnePer" >> --module-suffix="One" TestResults.xsd Traceback (most recent call >> last): >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >> in <module> > main() >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, >> in main > parseAndGenerate( >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >> in parseAndGenerate > parser.parse(rootFile) >> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in >> parse > xmlreader.IncrementalParser.parse(self, source) >> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in >> parse > buffer = file.read(self._bufsize) >> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in >> decode > return codecs.charmap_decode(input,self.errors,decoding_table)[0] >> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >> position 4013: character maps to <undefined> > > Approach 2 - Extraction and Generation Utilities: >> (base) h:\Python Training\GenerateDS_Docs\utils>python >> collect_schema_locations.py --force --verbose TestResults.xsd >> testJson.txt Traceback (most recent call last): >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 157, in <module> > main() >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 148, in main > extract_and_generate(infile, outfile, extract_locations_fn, > options) >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, >> in extract_and_generate > locations = extract_locations(infile, options) >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, >> in extract_locations > elements = root.xpath('xs:import', namespaces=root.nsmap) >> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >> File "src/lxml/xpath.pxi", line 259, in >> lxml.etree.XPathElementEvaluator.__init__ >> File "src/lxml/xpath.pxi", line 131, in >> lxml.etree._XPathEvaluatorBase.__init__ >> File "src/lxml/xpath.pxi", line 55, in >> lxml.etree._XPathContext.__init__ File "src/lxml/extensions.pxi", >> line 81, in lxml.etree._BaseContext.__init__ >> TypeError: empty namespace prefix is not supported in XPath > > Attached are the .xsd that I am using. TestResults.xsd references > Common.xsd. > > Any help on getting started with my overall goal will be much > appreciated. I am looking to create a new XML from a known .xsd and > add in the elements and child elements and attributes as needed. > > Thank you for the help, > Daniel DeWitte -- Dave Kuhlman http://www.davekuhlman.org |
From: Dave K. <dku...@da...> - 2022-06-01 23:08:26
|
Daniel, Thanks for taking the time to report this. Here is what I've tried so far: 1. I copied TestResults.xml and Common.xsd to a directory. 2. I ran the following command: ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super tmp01sup --member-specs=dict '--export=write etree validate' TestResults.xsd That generated a module `tmp01sup.py`. 3. So, I started IPython (the plain Python interactive prompt would also work) and did this: [ins] In [1]: import tmp01sup as lib Which also worked. 4. Then I created an instance of a class in the generated module: [ins] In [5]: testresults01 = lib.TestResult() [ins] In [6]: testresults01.export(sys.stdout, 0) <c:TestResult xmlns:None="http://www.ieee.org/ATML/2006/Common" xmlns:c="http://www.ieee.org/ATML/2006/Common" /> Which also seemed to work. So, now I've got to read your message a bit more carefully so that I can figure out what is wrong for your case and how to fix it. I do not have the file `collect_schema_locations.py`, which you show in your message as generating an exception. So, I can't test that. Did you generate that file with `generateDS.py`? However the exception "TypeError: empty namespace prefix is not supported in XPath" seems to occur when you have a key in the nsmap that is `None`. Try removing it. Then do the `xpath` search: [ins] In [23]: nsmap = root.nsmap [ins] In [24]: nsmap Out[24]: {'xs': 'http://www.w3.org/2001/XMLSchema', 'c': 'http://www.ieee.org/ATML/2006/Common', None: 'http://www.ieee.org/ATML/2007/TestResults'} [ins] In [25]: nsmap.pop(None) Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' [ins] In [26]: nsmap Out[26]: {'xs': 'http://www.w3.org/2001/XMLSchema', 'c': 'http://www.ieee.org/ATML/2006/Common'} [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at 0x7faf573acac0>] Does that help with your work? By the way, the "--one-file-per-xsd" capability is something that I do not understand, myself. That code was contributed by someone else. Dave Quoting "Dewitte, Daniel W (US) via Generateds-discuss" <gen...@li...>: > Here are the actual files this time :) > > Daniel DeWitte > > T: +1-603-885-2602 > M: +1-603-759-4476 > E: dan...@ba...<mailto:dan...@ba...> > > From: Dewitte, Daniel W (US) > Sent: Wednesday, June 1, 2022 1:06 PM > To: 'Gen...@li...' > <Gen...@li...> > Subject: How to create/ generate new xml from generated API > > Hi Dave, > > This is one of the better tools that I have found and I think the > only one that can work the way I need it too, I just need some help > with getting to the finish line. > > My goal is generate a new .xml using a known schema .xsd with Python > as the base language (required). I have 13 different reports that > all have a different format that are generated on a constant basis > that I need to import into a known .xsd format for upload to a > database. > > I believe I either want to use the build methods, or the "one-per" > options, but I can't get either of them to work. I asked about the > build method problem on stackover flow here: Stackoverflow - > Creating a new XML from an XSD using GenerateDS and > Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml-from-an-xsd-using-generateds-and-python> > > But I am not sure that the build method is what I want to use. > > When I try the "one-per" I get the following errors: > Command line method, I believe this error is due to that the .xsd is > in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >> --one-file-per-xsd --output-directory="OnePer" >> --module-suffix="One" TestResults.xsd >> Traceback (most recent call last): >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >> in <module> > main() >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, in main > parseAndGenerate( >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >> in parseAndGenerate > parser.parse(rootFile) >> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in parse > xmlreader.IncrementalParser.parse(self, source) >> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in parse > buffer = file.read(self._bufsize) >> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in decode > return codecs.charmap_decode(input,self.errors,decoding_table)[0] >> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >> position 4013: character maps to <undefined> > > Approach 2 - Extraction and Generation Utilities: >> (base) h:\Python Training\GenerateDS_Docs\utils>python >> collect_schema_locations.py --force --verbose TestResults.xsd >> testJson.txt >> Traceback (most recent call last): >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 157, in <module> > main() >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 148, in main > extract_and_generate(infile, outfile, extract_locations_fn, options) >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 86, in extract_and_generate > locations = extract_locations(infile, options) >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 50, in extract_locations > elements = root.xpath('xs:import', namespaces=root.nsmap) >> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >> File "src/lxml/xpath.pxi", line 259, in >> lxml.etree.XPathElementEvaluator.__init__ >> File "src/lxml/xpath.pxi", line 131, in >> lxml.etree._XPathEvaluatorBase.__init__ >> File "src/lxml/xpath.pxi", line 55, in lxml.etree._XPathContext.__init__ >> File "src/lxml/extensions.pxi", line 81, in lxml.etree._BaseContext.__init__ >> TypeError: empty namespace prefix is not supported in XPath > > Attached are the .xsd that I am using. TestResults.xsd references > Common.xsd. > > Any help on getting started with my overall goal will be much > appreciated. I am looking to create a new XML from a known .xsd and > add in the elements and child elements and attributes as needed. > > Thank you for the help, > Daniel DeWitte -- Dave Kuhlman http://www.davekuhlman.org |
From: Dewitte, D. W (US) <dan...@ba...> - 2022-06-01 17:14:22
|
Here are the actual files this time :) Daniel DeWitte T: +1-603-885-2602 M: +1-603-759-4476 E: dan...@ba...<mailto:dan...@ba...> From: Dewitte, Daniel W (US) Sent: Wednesday, June 1, 2022 1:06 PM To: 'Gen...@li...' <Gen...@li...> Subject: How to create/ generate new xml from generated API Hi Dave, This is one of the better tools that I have found and I think the only one that can work the way I need it too, I just need some help with getting to the finish line. My goal is generate a new .xml using a known schema .xsd with Python as the base language (required). I have 13 different reports that all have a different format that are generated on a constant basis that I need to import into a known .xsd format for upload to a database. I believe I either want to use the build methods, or the "one-per" options, but I can't get either of them to work. I asked about the build method problem on stackover flow here: Stackoverflow - Creating a new XML from an XSD using GenerateDS and Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml-from-an-xsd-using-generateds-and-python> But I am not sure that the build method is what I want to use. When I try the "one-per" I get the following errors: Command line method, I believe this error is due to that the .xsd is in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >(base) H:\Python Training\GenerateDS_Docs>python generateds.py --one-file-per-xsd --output-directory="OnePer" --module-suffix="One" TestResults.xsd >Traceback (most recent call last): > File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, in <module> main() > File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, in main parseAndGenerate( > File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, in parseAndGenerate parser.parse(rootFile) > File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in parse xmlreader.IncrementalParser.parse(self, source) > File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in parse buffer = file.read(self._bufsize) > File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] > UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 4013: character maps to <undefined> Approach 2 - Extraction and Generation Utilities: > (base) h:\Python Training\GenerateDS_Docs\utils>python collect_schema_locations.py --force --verbose TestResults.xsd testJson.txt > Traceback (most recent call last): > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 157, in <module> main() > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 148, in main extract_and_generate(infile, outfile, extract_locations_fn, options) > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, in extract_and_generate locations = extract_locations(infile, options) > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, in extract_locations elements = root.xpath('xs:import', namespaces=root.nsmap) > File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath > File "src/lxml/xpath.pxi", line 259, in lxml.etree.XPathElementEvaluator.__init__ > File "src/lxml/xpath.pxi", line 131, in lxml.etree._XPathEvaluatorBase.__init__ > File "src/lxml/xpath.pxi", line 55, in lxml.etree._XPathContext.__init__ > File "src/lxml/extensions.pxi", line 81, in lxml.etree._BaseContext.__init__ > TypeError: empty namespace prefix is not supported in XPath Attached are the .xsd that I am using. TestResults.xsd references Common.xsd. Any help on getting started with my overall goal will be much appreciated. I am looking to create a new XML from a known .xsd and add in the elements and child elements and attributes as needed. Thank you for the help, Daniel DeWitte |
From: Dewitte, D. W (US) <dan...@ba...> - 2022-06-01 17:06:53
|
Hi Dave, This is one of the better tools that I have found and I think the only one that can work the way I need it too, I just need some help with getting to the finish line. My goal is generate a new .xml using a known schema .xsd with Python as the base language (required). I have 13 different reports that all have a different format that are generated on a constant basis that I need to import into a known .xsd format for upload to a database. I believe I either want to use the build methods, or the "one-per" options, but I can't get either of them to work. I asked about the build method problem on stackover flow here: Stackoverflow - Creating a new XML from an XSD using GenerateDS and Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml-from-an-xsd-using-generateds-and-python> But I am not sure that the build method is what I want to use. When I try the "one-per" I get the following errors: Command line method, I believe this error is due to that the .xsd is in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >(base) H:\Python Training\GenerateDS_Docs>python generateds.py --one-file-per-xsd --output-directory="OnePer" --module-suffix="One" TestResults.xsd >Traceback (most recent call last): > File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, in <module> main() > File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, in main parseAndGenerate( > File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, in parseAndGenerate parser.parse(rootFile) > File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in parse xmlreader.IncrementalParser.parse(self, source) > File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in parse buffer = file.read(self._bufsize) > File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] > UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 4013: character maps to <undefined> Approach 2 - Extraction and Generation Utilities: > (base) h:\Python Training\GenerateDS_Docs\utils>python collect_schema_locations.py --force --verbose TestResults.xsd testJson.txt > Traceback (most recent call last): > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 157, in <module> main() > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 148, in main extract_and_generate(infile, outfile, extract_locations_fn, options) > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 86, in extract_and_generate locations = extract_locations(infile, options) > File "h:\Python Training\GenerateDS_Docs\utils\collect_schema_locations.py", line 50, in extract_locations elements = root.xpath('xs:import', namespaces=root.nsmap) > File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath > File "src/lxml/xpath.pxi", line 259, in lxml.etree.XPathElementEvaluator.__init__ > File "src/lxml/xpath.pxi", line 131, in lxml.etree._XPathEvaluatorBase.__init__ > File "src/lxml/xpath.pxi", line 55, in lxml.etree._XPathContext.__init__ > File "src/lxml/extensions.pxi", line 81, in lxml.etree._BaseContext.__init__ > TypeError: empty namespace prefix is not supported in XPath Attached are the .xsd that I am using. TestResults.xsd references Common.xsd. Any help on getting started with my overall goal will be much appreciated. I am looking to create a new XML from a known .xsd and add in the elements and child elements and attributes as needed. Thank you for the help, Daniel DeWitte |
From: Dewitte, D. W (US) <dan...@ba...> - 2022-05-25 14:59:32
|
Good Morning, I am starting to learn GenerateDS for a project and I am using your people_api.py example in the tutorial section 6.2. Everything works after some finagling, but my output looks like this in the Spyder Console: <peopleType> <person id="1"> <name>b'albert'</name> </person> <person id="2"> <name>b'betsy'</name> </person> <person id="3"> <name>b'charlie'</name> </person> </peopleType> I am trying to figure out how to remove the "b" prefix from the names. From everything that I can read is that the names are being represented as a byte object. How do I remove the prefix? I tried adding the <str>.decode("utf-8") in the for loop but that didn't work. Any help would be appreciated, Daniel DeWitte |
From: Dave K. <dku...@da...> - 2022-05-07 16:12:17
|
Pete, Great. Glad you found that. I'll apply your improved patch for stripping whitespace from around boolean values Monday. Dave Quoting Pete Forman <pe...@gm...>: > I have resolved the parseLiteral() failure by RTFM. I needed to use the > --export option to generateDS.py. > > It turns out that literals are for the XML where I was trying to extract > the expanded XSD. > > The django extract and generate scripts should help me on my way. > > -- > Pete Forman > > > > On Fri, 6 May 2022 at 11:17, Pete Forman <pe...@gm...> wrote: > >> My high level goal is to read an XML schema (XSD), expand its imports and >> extract the types. I can use XPath on my XML document to extract values as >> text. I'd like then to use XPath or similar on the XSD to correctly coerce >> the text values to their types. >> >> It looks as if parseLiteral() / exportLiteral() might do the trick. >> However I have not been able to use them. I've got the test data and this >> successfully pretty prints the XML. >> >> $ python3 -c "import people; people.parse('people.xml')" >> >> I've tried a number of things but they all are unable to resolve >> exportLiteral(). >> >> $ python3 -c "import people; people.parseLiteral('people.xml')" >> #from people import * >> >> import people as model_ >> >> rootObj = model_.rootClass( >> Traceback (most recent call last): >> File "<string>", line 1, in <module> >> File "[...]/people.py", line 3345, in parseLiteral >> rootObj.exportLiteral(sys.stdout, 0, name_=rootTag) >> AttributeError: 'people' object has no attribute 'exportLiteral' >> >> -- >> Pete Forman >> >> >> >> -- Dave Kuhlman http://www.davekuhlman.org |
From: Pete F. <pe...@gm...> - 2022-05-07 13:58:38
|
I have resolved the parseLiteral() failure by RTFM. I needed to use the --export option to generateDS.py. It turns out that literals are for the XML where I was trying to extract the expanded XSD. The django extract and generate scripts should help me on my way. -- Pete Forman On Fri, 6 May 2022 at 11:17, Pete Forman <pe...@gm...> wrote: > My high level goal is to read an XML schema (XSD), expand its imports and > extract the types. I can use XPath on my XML document to extract values as > text. I'd like then to use XPath or similar on the XSD to correctly coerce > the text values to their types. > > It looks as if parseLiteral() / exportLiteral() might do the trick. > However I have not been able to use them. I've got the test data and this > successfully pretty prints the XML. > > $ python3 -c "import people; people.parse('people.xml')" > > I've tried a number of things but they all are unable to resolve > exportLiteral(). > > $ python3 -c "import people; people.parseLiteral('people.xml')" > #from people import * > > import people as model_ > > rootObj = model_.rootClass( > Traceback (most recent call last): > File "<string>", line 1, in <module> > File "[...]/people.py", line 3345, in parseLiteral > rootObj.exportLiteral(sys.stdout, 0, name_=rootTag) > AttributeError: 'people' object has no attribute 'exportLiteral' > > -- > Pete Forman > > > > |
From: Pete F. <pe...@gm...> - 2022-05-06 10:17:25
|
My high level goal is to read an XML schema (XSD), expand its imports and extract the types. I can use XPath on my XML document to extract values as text. I'd like then to use XPath or similar on the XSD to correctly coerce the text values to their types. It looks as if parseLiteral() / exportLiteral() might do the trick. However I have not been able to use them. I've got the test data and this successfully pretty prints the XML. $ python3 -c "import people; people.parse('people.xml')" I've tried a number of things but they all are unable to resolve exportLiteral(). $ python3 -c "import people; people.parseLiteral('people.xml')" #from people import * import people as model_ rootObj = model_.rootClass( Traceback (most recent call last): File "<string>", line 1, in <module> File "[...]/people.py", line 3345, in parseLiteral rootObj.exportLiteral(sys.stdout, 0, name_=rootTag) AttributeError: 'people' object has no attribute 'exportLiteral' -- Pete Forman |
From: Dave K. <dku...@da...> - 2021-10-15 18:07:04
|
Vladan, Thanks for reporting back. Glad you solved your problem. Thanks for telling me about the `.env` file. I did not know about that and about the `dotenv` module. I'll read up on it. `generateds` is hosted at Source Forge: https://sourceforge.net/projects/generateds/ You can clone the code. If you have something that you believe needs a fix, you can create a ticket. If you have a fix, you can fork `generateds`, make your suggested changes, and send me a pull request. If you want to ask a question or discuss something, there is a mailing list. I should try to remember to CC that list myself. Dave On Fri 15 Oct 2021 01:19:07 AM PDT, Vladan Cvejic wrote: > Hello Dave, > > Just dropped by to thank you again. > I managed to fix the issue with .env file where I stated bin folder > where genererateDS is installed. > That, solved the issue without using sys.path. > > And last one from me - in case I have some other issues with > generateDS - where should I report it? > Previously I did it through Bitbucket, how should I do it now? > > Thanks a lot and regards > > Vladan > > __________________________________ > Sent from eM Client | www.emclient.com <http://www.emclient.com/get> > > ------ Original Message ------ > From: "Dave Kuhlman" <dku...@da...> > To: "Vladan Cvejic" <vla...@gm...> > Sent: 13/10/2021 00:10:31 > Subject: Re: Re[2]: generateDS question > >> Vladan, >> >> Glad you found a solution. >> >> By the way, remember that there is also `PYTHONPATH`. >> You can learn about it by doing `$ python --help1`, which >> will tell you: >> >> PYTHONPATH : ':'-separated list of directories prefixed to the >> default module search path. The result is sys.path. >> >> If you are on MS Windows, it will say "';"-separated", I believe. >> >> With respect to your questions about where the generateds `.py` >> files are installed -- I do not use `pipenv` and I do not understand >> it very much. >> I typically use `virtualenv` and `virtualenvwrapper` >> and `pip`. >> When I use those tools, `generateDS.py`, `process_includes.py`, etc. >> get installed in the `bin` directory of my active `virtualenv` >> environment. >> >> The `pipenv` documentation leaves me a bit mystified about where >> `pipenv install` installs files. >> I'm guessing that it installs them in the same locations that `pip` >> would. >> >> If that location is not in `sys.path`, then you will have to >> explicitly add it using the `PYTHONPATH` environment variable or >> `sys.path.append(...) or some other method. >> >> If there is something I can do about this to make the use of >> `pipenv` more convenient, please tell me about it. >> >> Dave >> >> >> >> On Sun 10 Oct 2021 02:24:49 PM PDT, Vladan Cvejic wrote: >> >>> Hello again, >>> >>> thank you for helping. >>> I manage to do it with "not recommended" solution:) >>> by adding a sys.path entry that is showing to bin directory >>> created by pipenv (where is generateDS). >>> >>> I have another question here: >>> - While comparing with other packages I installed in my virtual >>> environment (like flake8, pylint etc) - they all have py files in >>> another directory site-packages (/lib/python3.8/site-packages/) >>> but generateDS.py is not there (there is no /generateDS directory >>> there while for example flake8 is there with py files) >>> And pipenv is only adding the other one >>> (~/,virtualenvs/packages-o_VHYWw_/lib/python3.8/site-packages/) to >>> sys.path. >>> Do you think it is something to do with pipenv and how generateDS >>> is instructing it where should be all files copied etc? >>> >>> That is why, when I added bin path to sys.path it managed to find it. >>> >>> Once more, thank you very much for your help and suggestions (I >>> will know what to study more :) >>> >>> All the best >>> >>> Vladan >>> >>> >>> __________________________________ >>> Sent from eM Client | www.emclient.com <http://www.emclient.com/get> >>> >>> ------ Original Message ------ >>> From: "Dave Kuhlman" <dku...@da...> >>> To: "Vladan Cvejic" <vla...@gm...> >>> Sent: 09/10/2021 00:40:19 >>> Subject: Re: generateDS question >>> >>>> Vladan, >>>> >>>> Good to hear from you. >>>> >>>> It's because `generateDS.py` is not in a location from which it can >>>> be imported, or because it is in a location from which it cannot be >>>> imported. >>>> >>>> Here are a few ideas that might help. >>>> >>>> There are several things you can try: >>>> >>>> - Since you are using `pipenv`, check to make sure that the Python >>>> environment in which you installed `generateds` has been >>>> activated. >>>> >>>> - Find out where the Python executable is located. That will tell >>>> you which installation is active. Looks like you are on Linux, so >>>> you find out by doing this: >>>> >>>> $ which python >>>> >>>> - Find out where `generateDS.py` has been installed and make sure >>>> that directory is in `sys.path`. You can learn where those >>>> locations are at the Python interactive prompt by typing: >>>> >>>> >>> import sys >>>> >>> sys.path >>>> ['', '/usr/lib/python39.zip', '/usr/lib/python3.9', ... ] >>>> >>> >>>> >>>> The '' means current working directory, I believe. >>>> >>>> - Copy `generateDS.py` to a location from which it can be imported, >>>> or make a symbolic link there.rectory, I believe. >>>> >>>> - Modify `sys.path` in your application. This is not a recommended >>>> solution, however. For example: >>>> >>>> >>> import sys >>>> >>> sys.path.append("/path/to/generateds/directory") >>>> >>>> Hope this helps. Let me know if I can answer any questions. >>>> >>>> Dave >>>> >>>> >>>> >>>> On Thu 07 Oct 2021 01:14:27 PM PDT, Vladan Cvejic wrote: >>>> >>>>> Hello Dave, >>>>> >>>>> I apologize for being direct, but I'm in need of your help. >>>>> I am trying to setup a project that is using your program >>>>> generateDS.py on linux (ubuntu). >>>>> Simulation of my workflow is (creating folder, installing >>>>> pipenv, going to pipenv shell, installing pipenv install >>>>> generateDS, creating simple main.py with import generatedDS): >>>>> >>>>> user@test:~/gits$ mkdir test >>>>> user@test:~/gits$ cd test/ >>>>> user@test:~/gits/test$ pipenv install >>>>> Creating a virtualenv for this project… >>>>> Using /usr/bin/python3 (3.8.10) to create virtualenv… >>>>> ⠋created virtual environment CPython3.8.10.final.0-64 in 211ms >>>>> creator >>>>> CPython3Posix(dest=/home/user/.local/share/virtualenvs/test-lQXnKvl3, >>>>> clear=False, global=False) >>>>> seeder FromAppData(download=False, pip=latest, >>>>> setuptools=latest, wheel=latest, pkg_resources=latest, >>>>> via=copy, >>>>> app_data_dir=/home/user/.local/share/virtualenv/seed-app-data/v1.0.1.debian.1) >>>>> activators >>>>> BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator >>>>> >>>>> Virtualenv location: /home/user/.local/share/virtualenvs/test-lQXnKvl3 >>>>> Creating a Pipfile for this project… >>>>> Pipfile.lock not found, creating… >>>>> Locking [dev-packages] dependencies… >>>>> Locking [packages] dependencies… >>>>> Updated Pipfile.lock (9e16cc)! >>>>> Installing dependencies >>>>> from Pipfile.lock (9e16cc)… >>>>> 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00 >>>>> To activate this project's virtualenv, run the following: >>>>> $ pipenv shell >>>>> user@test:~/gits/test$ pipenv shell >>>>> Spawning environment shell (/bin/bash). Use 'exit' to leave. >>>>> . /home/user/.local/share/virtualenvs/test-lQXnKvl3/bin/activate >>>>> user@test:~/gits/test$ . >>>>> /home/user/.local/share/virtualenvs/test-lQXnKvl3/bin/activate >>>>> (test-lQXnKvl3) user@test:~/gits/test$ pipenv install generateDS >>>>> Installing generateDS… >>>>> Looking in indexes: https://pypi.python.org/simple >>>>> Collecting generateDS >>>>> Using cached generateDS-2.40.3-py3-none-any.whl (141 kB) >>>>> Collecting requests>=2.21.0 >>>>> Using cached requests-2.26.0-py2.py3-none-any.whl (62 kB) >>>>> Collecting lxml >>>>> Using cached lxml-4.6.3-cp38-cp38-manylinux2014_x86_64.whl (6.8 MB) >>>>> Collecting six >>>>> Using cached six-1.16.0-py2.py3-none-any.whl (11 kB) >>>>> Collecting idna<4,>=2.5; python_version >= "3" >>>>> Using cached idna-3.2-py3-none-any.whl (59 kB) >>>>> Collecting certifi>=2017.4.17 >>>>> Using cached certifi-2021.5.30-py2.py3-none-any.whl (145 kB) >>>>> Collecting urllib3<1.27,>=1.21.1 >>>>> Using cached urllib3-1.26.7-py2.py3-none-any.whl (138 kB) >>>>> Collecting charset-normalizer~=2.0.0; python_version >= "3" >>>>> Using cached charset_normalizer-2.0.6-py3-none-any.whl (37 kB) >>>>> Installing collected packages: idna, certifi, urllib3, >>>>> charset-normalizer, requests, lxml, six, generateDS >>>>> Successfully installed certifi-2021.5.30 >>>>> charset-normalizer-2.0.6 generateDS-2.40.3 idna-3.2 lxml-4.6.3 >>>>> requests-2.26.0 six-1.16.0 urllib3-1.26.7 >>>>> >>>>> Adding generateDS to Pipfile's [packages]… >>>>> Pipfile.lock (9e16cc) out of date, updating to (30a375)… >>>>> Locking [dev-packages] dependencies… >>>>> Locking [packages] dependencies… >>>>> Updated Pipfile.lock (30a375)! >>>>> Installing dependencies from Pipfile.lock (30a375)… >>>>> 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 8/8 — 00:00:11 >>>>> >>>>> >>>>> Then creating simple file: >>>>> (test-lQXnKvl3) user@test:~/gits/test$vim main.py >>>>> >>>>> With following content: >>>>> import generateDS >>>>> print("test") >>>>> generateDS.main() >>>>> >>>>> When trying to run main.py I am getting error - No module named >>>>> 'generateDS': >>>>> (test-lQXnKvl3) user@test:~/gits/test$ python main.py >>>>> Traceback (most recent call last): >>>>> File "main.py", line 1, in <module> >>>>> import generateDS >>>>> ModuleNotFoundError: No module named 'generateDS' >>>>> >>>>> On the other hand when trying following command, I am getting >>>>> response from generateDS, so if I understand correctly it is >>>>> working: >>>>> (test-lQXnKvl3) user@test:~/gits/test$ pipenv run generateDS .py >>>>> >>>>> Synopsis: >>>>> Generate Python classes from XML schema definition. >>>>> Input is read from in_xsd_file or, if "-" (dash) arg, from stdin. >>>>> Output is written to files named in "-o" and "-s" options. >>>>> Usage: >>>>> python generateDS.py [ options ] <xsd_file> >>>>> python generateDS.py [ options ] - >>>>> Options: >>>>> -h, --help Display this help information. >>>>> >>>>> To summarize, I am trying to use generateDS in my project as a >>>>> module (I remember that I did it on Windows, but somehow it is >>>>> not working in Linux). >>>>> Do you have any suggestion? >>>>> >>>>> Kind regards >>>>> >>>>> Vladan >>>>> >>>>> >>>>> __________________________________ >>>>> Sent from eM Client | www.emclient.com <http://www.emclient.com/get> >>>> >>>> >>>> >>>> -- Dave Kuhlman >>>> http://www.davekuhlman.org >>>> >> >> >> >> -- Dave Kuhlman >> http://www.davekuhlman.org >> -- Dave Kuhlman http://www.davekuhlman.org |
From: Dave K. <dku...@da...> - 2021-09-13 19:49:45
|
Jon, Were you able to resolve your problem. Here is another idea -- Post-process the exported XML output. Here is a bit of code (found on the Web) that will strip namespece definitions and namespace prefixes: doc = etree.parse("myfile.xml") root = doc.getroot() for elem in root.iter(): elem.tag = etree.QName(elem).localname etree.cleanup_namespaces(root) with open("mynewfile.xml", 'wb') as outfile: doc.write(outfile) I've attached a more complete script that does this. By the way, opening the output file in binary mode seems weird to me. Alternatively, you could write to `outfile.buffer` or convert to string with `etree.tostring(r, encoding='utf-8')`. They all seem about the same. Take your pick. Dave On Thu 09 Sep 2021 01:36:41 PM PDT, Falcon Programmer wrote: > Hi, > > Is it possible to remove the namespace generated when exporting via a > generateds? > > I tried setting the 'namespaceprefix_' on the export, and also tried > '.set_ns_prefix_' on the object itself and it doesn't seem to have > made a difference. > > I need to do this as I'm submitting an XML that needs to be a specific > format without the namespace specified. > > Thanks, > > Jon > > > _______________________________________________ > Generateds-discuss mailing list > Gen...@li... > https://lists.sourceforge.net/lists/listinfo/generateds-discuss -- Dave Kuhlman http://www.davekuhlman.org |
From: Dave K. <dku...@da...> - 2021-09-09 23:34:01
|
Jon, In each generated module (result of the -o command line parameter) there is a global variable "UseCapturedNS_". Try setting that to `False`. Perhaps that will produce what you want. Or, have you already tried that? Hope this helps. If not give me a few more details, and I'll look into it next week. Dave On Thu 09 Sep 2021 01:36:41 PM PDT, Falcon Programmer wrote: > Hi, > > Is it possible to remove the namespace generated when exporting via a > generateds? > > I tried setting the 'namespaceprefix_' on the export, and also tried > '.set_ns_prefix_' on the object itself and it doesn't seem to have > made a difference. > > I need to do this as I'm submitting an XML that needs to be a specific > format without the namespace specified. > > Thanks, > > Jon > > > _______________________________________________ > Generateds-discuss mailing list > Gen...@li... > https://lists.sourceforge.net/lists/listinfo/generateds-discuss -- Dave Kuhlman http://www.davekuhlman.org |
From: Falcon P. <pro...@gm...> - 2021-09-09 20:36:58
|
Hi, Is it possible to remove the namespace generated when exporting via a generateds? I tried setting the 'namespaceprefix_' on the export, and also tried '.set_ns_prefix_' on the object itself and it doesn't seem to have made a difference. I need to do this as I'm submitting an XML that needs to be a specific format without the namespace specified. Thanks, Jon |
From: Dave K. <dku...@da...> - 2021-08-14 00:05:17
|
Rick, Thanks again for the report. I believe I've fixed this now. We'll now generate an additional line of code to convert the Python `datetime`, `date`, or `time` object to string before doing the regular expression match for validation. I've uploaded this patched version to PyPI and SourceForge. If you get a chance to try it, please let me know whether ti fixes the issue for you. Dave On Mon 09 Aug 2021 08:03:39 AM PDT, Taube, Heinrich K wrote: > Hi, I am prepping to teach a cs+music class in the fall and Ive used > generateDS to convert the MusicXml schema > (https://www.musicxml.com/for-developers/musicxml-xsd/) into a > generateDS python file. Thank you for such an awesome program!! Ive > been trying it out on some musicxml scores, and I've found one > problem so far. If a musicxml file contains an element like this: > <encoding-date>2021-08-09</encoding-date> > > then parsing fails in gds_validate_simple_patterns() with the message > > TypeError: expected string or bytes-like object > > Here’s a partial stack trace: > […] > File "/Users/taube/Software/musx/musx/mxml/musicxml.py", line > 31792, in _buildChildren > self.validate_yyyy_mm_dd(self.encoding_date[-1]) > File "/Users/taube/Software/musx/musx/mxml/musicxml.py", line > 31707, in validate_yyyy_mm_dd > if not self.gds_validate_simple_patterns( > File "/Users/taube/Software/musx/musx/mxml/musicxml.py", line 507, > in gds_validate_simple_patterns > mo = re_.search(patterns2, target) > File > "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", > line 201, in search > return _compile(pattern, flags).search(string) > TypeError: expected string or bytes-like object > > If I delete that one line from the musicxml file, then the file is > parsed successfully. I’m wondering if you have any suggestions on > what I can do to stop the exception? > Ive tried putting a few print statements in the musicxml.py file to > try to look more closely at what is happening but for some reason I > cant get the print statements to show up in my terminal. Im not sure > why that is yet (the musicxml.py file is 3.1 megabytes and maybe > that has something to do with it…) The only other issue I could > point at is that when i create the musicxml.py file like this > > $ generateDS -o musicxml.py --root-element "score_partwise" > schema/musicxml.xsd > > I get many many of messages like this: > > *** No suitable ns prefix for element "<XschemaElement name: > "key-accidental" type: "key-accidental">”. > > and a handful with these warnings: > > *** warning. Removing child with duplicate name: “tie" > *** warning. child chord already removed > > However, despite the warnings it seems like the code is actually > parsing xml Elements correctly so I’ve not looked into this any more. > > > If you have pointers on what I might be able to do with > gds_validate_simple_patterns it would be greatly appreciated! > > > Rick Taube > ________________________________ > Chair, Composition/Theory > School of Music > University of Illinois Urbana-Champaign > Email: ta...@il...<mailto:ta...@il...> > President, Illiac Software Inc. > https://harmonia.illiacsoftware.com/ > Email: ta...@il... -- Dave Kuhlman http://www.davekuhlman.org |