Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cirucular imports after creating binding for ReqIf schema #206

Closed
ansFourtyTwo opened this issue Jul 9, 2020 · 6 comments
Closed

Cirucular imports after creating binding for ReqIf schema #206

ansFourtyTwo opened this issue Jul 9, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@ansFourtyTwo
Copy link

After succesfully creating bindings for the ReqIf schema with
xsdata https://www.omg.org/spec/ReqIF/20110401/reqif.xsd --package reqif_model

I tried to parse an actual *.reqif file into the Dataclasses with following code snippet:

from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.parsers.config import ParserConfig
from reqif_model.reqif import ReqIf

config  = ParserConfig(fail_on_unknown_properties=True)
parser = Parser = XmlParser(config=config)
reqif = parser.from_file('./KuFu_Testfunktion_00270cf8.reqif', ReqIf)

However, the import of the ReqIfdataclass from reqif_model.reqifmodule throws an ImportError:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/xsdata_test/reqif_model/__init__.py", line 1, in <module>
    from reqif_model.reqif import AlternativeId
  File "/tmp/xsdata_test/reqif_model/reqif.py", line 4, in <module>
    from reqif_model.www.w3.org.tr.xhtml_modularization.schema.xhtml_blkstruct_1 import (
  File "/tmp/xsdata_test/reqif_model/www/w3/org/tr/xhtml_modularization/schema/__init__.py", line 3, in <module>
    from reqif_model.www.w3.org.tr.xhtml_modularization.schema.xhtml_inlpres_1 import XhtmlInlPresAttlistValue
  File "/tmp/xsdata_test/reqif_model/www/w3/org/tr/xhtml_modularization/schema/xhtml_inlpres_1.py", line 4, in <module>
    from reqif_model.www.w3.org.tr.xhtml_modularization.schema.xhtml_hypertext_1 import (
  File "/tmp/xsdata_test/reqif_model/www/w3/org/tr/xhtml_modularization/schema/xhtml_hypertext_1.py", line 4, in <module>
    from reqif_model.www.w3.org.tr.xhtml_modularization.schema.xhtml_inlphras_1 import (
  File "/tmp/xsdata_test/reqif_model/www/w3/org/tr/xhtml_modularization/schema/xhtml_inlphras_1.py", line 4, in <module>
    from reqif_model.www.w3.org.tr.xhtml_modularization.schema.xhtml_inlpres_1 import (
ImportError: cannot import name 'XhtmlInlPresType' from 'reqif_model.www.w3.org.tr.xhtml_modularization.schema.xhtml_inlpres_1' (/tmp/xsdata_test/reqif_model/www/w3/org/tr/xhtml_modularization/schema/xhtml_inlpres_1.py)

It seems like there is an circular import, as the error occures during the second import from reqif_model.www.w3.org.tr.xhtml_modularization.schema.xhtml_inlpres_1.

@tefra
Copy link
Owner

tefra commented Jul 9, 2020

I am afraid that's trickier to resolve @ansFourtyTwo, although xml schema allows for circular imports, it's really difficult to tackle especially in python. BTW I also tried the xjc compiler (java) also fails.

I will leave this issue open since I definitely want to revisit it but right now you either have to sort the imports manually, which I am not optimistic it's even possible, or to find some sort of tool that cleans up xml schema definitions.

@tefra
Copy link
Owner

tefra commented Jul 9, 2020

If your xml files are relatively simple and dont use the full specification you could try to write the @datalcass models on your own.

I am sorry, currently I can't offer you a better solution

@tefra
Copy link
Owner

tefra commented Jul 10, 2020

I had another idea, maybe offer a way to organize classes per namespace that could do the trick instead of relying to the filepaths of the schemas.

@tefra tefra added the enhancement New feature or request label Jul 10, 2020
tefra added a commit that referenced this issue Jul 10, 2020
Notes:
By default the cli is using the original schemas structure
to assign pacakges and modules to classes.

The new option will group all classes by the target namespace
they were originally defined and write them in the same module
inside the user provided package name.
@tefra
Copy link
Owner

tefra commented Jul 10, 2020

Hi @ansFourtyTwo

I wanted to try grouping the classes by the target namespace for some time now, the end result is a more flat structure will less packages and modules but it works like a charm against circular imports 😄

I added a new cli flag --ns-struct to bypass the default behavior, give it a try and let me know how you like it?

pip install git+https://github.com/tefra/xsdata@master

xsdata https://www.omg.org/spec/ReqIF/20110401/reqif.xsd --package here.models --ns-struct

tefra added a commit that referenced this issue Jul 10, 2020
Notes:
By default the cli is using the original schemas structure
to assign classes to packages and modules

The new option will group all classes by the target namespace
they were originally defined and write them in the same module
inside the user provided package name.
tefra added a commit that referenced this issue Jul 11, 2020
Notes:
Clean attribute names from prefixes earlier
in the SchemaMapper.

Bonus:
Clean namespaces in wildcard names.
@tefra tefra closed this as completed in c383193 Jul 11, 2020
@ansFourtyTwo
Copy link
Author

Hi @tefra

This solutions works fine. I was able to create the DataClasses and parse a *.reqif file into it. However I ran into some other issues searializing from the DataClasses back to xml.

If you don't mind, I'll open a new issue for this, although I already feel a bit bad opening issues all the time.

Regarding you apologize: You don't have to! Never! xsData does already an amazing job and seeing you working and improving it is really inspiring. Keep the good work going! Really appriciate it.

@tefra
Copy link
Owner

tefra commented Jul 13, 2020

Awesome @ansFourtyTwo

yes keep the issues separate please and keep them coming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants