python-zeep icon indicating copy to clipboard operation
python-zeep copied to clipboard

WIP: Support for simpleType restrictions/facets

Open ch3pjw opened this issue 3 years ago • 1 comments

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 :grimacing:

ch3pjw avatar Mar 18 '22 14:03 ch3pjw

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')]

EmpireofKings avatar Nov 11 '22 01:11 EmpireofKings