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

WIP: Support for simpleType restrictions/facets #1303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ch3pjw
Copy link

@ch3pjw ch3pjw commented Mar 18, 2022

After talking to @mvantellingen via email I've sketched out the first part of restriction/facet handling.

Currently this MR doesn't include a huge amount of testing - I'd like to verify things are heading in the right direction structure-wise first, and get some guidance on what areas are of most concern. Furthermore, this MR doesn't (yet?) include any validation/enforcement if the parsed facets/restrictions - I'm hoping its both desirable and possible to incorporate raising validation failures both on reading XML or building instances of types/elements.

NB: I've disable some tests that were already failing on master 😬

These need sorting by a maintainer, but I want a clear test run against
which to verify my forthcoming changes.
...and include in a `facets` class attribute on generated types. This
means that the result of parsing something like

```
<xs:simpleType name="MyEnum">
  <xs:restriction base="xs:string">
    <xs:enumeration value="a"/>
    <xs:enumeration value="b"/>
  </xs:restriction>
</xs:simpleType>
```

should yield something like

```
>>> MyEnum = client.get_type('{namespace}MyEnum')
>>> MyEnum.facets.enumeration
["a", "b"]

We do not (yet) attempt to verify the facets hold when parsing XML or
accepting Python data.
@EmpireofKings
Copy link

This should resolve your errors.

There are two issues in the file facets.py under the function parse_xml().

1). You misspelled 'whitespace'.
2). The re.compile(facet.get('value')) generates an invalid regex. I have provided a fix for this.

## should be outside the Facets class.

import regex as re

def fix(st):
    _delim = str('\\\/')
    strlist = list(st)
    for i in range(0,len(strlist)):
        if strlist[i] == '\\':
                if strlist[i+1] == 'i' or strlist[i+1] == 'c':
                        strlist[i] = _delim[:2]
    return ''.join(strlist)

## inside Facets class
    @classmethod
    def parse_xml(cls, restriction_elem: etree._Element):
        kwargs = {}
        enumeration = []
        patterns = []
        for facet in restriction_elem:
            if facet.tag == xsd_ns('enumeration'):
                enumeration.append(facet.get('value'))
            elif facet.tag == xsd_ns('fractionDigits'):
                kwargs['fraction_digits'] = int(facet.get('value'))
            elif facet.tag == xsd_ns('length'):
                kwargs['length'] = int(facet.get('value'))
            elif facet.tag == xsd_ns('maxExclusive'):
                kwargs['max_exclusive'] = facet.get('value')
            elif facet.tag == xsd_ns('maxInclusive'):
                kwargs['max_inclusive'] = facet.get('value')
            elif facet.tag == xsd_ns('maxLength'):
                kwargs['max_length'] = int(facet.get('value'))
            elif facet.tag == xsd_ns('minExclusive'):
                kwargs['min_exclusive'] = facet.get('value')
            elif facet.tag == xsd_ns('minInclusive'):
                kwargs['min_inclusive'] = facet.get('value')
            elif facet.tag == xsd_ns('minLength'):
                kwargs['min_length'] = int(facet.get('value'))
            elif facet.tag == xsd_ns('pattern'):
                fac = facet.get('value').replace('-','')
                patterns.append(re.compile(fix(fac)))
            elif facet.tag == xsd_ns('totalDigits'):
                kwargs['total_digits'] = int(facet.get('value'))
            elif facet.tag == xsd_ns('whiteSpace'):
                kwargs['whitespace'] = Whitespace[facet.get('value')]
            elif facet.tag == xsd_ns('whiteSpace'):
                kwargs['whitespace'] = Whitespace[facet.get('value')]

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

Successfully merging this pull request may close these issues.

2 participants