Exemple #1
0
 def _LexicalToKeywords (cls, text):
     lexical_re = cls.__LexicalREMap.get(cls)
     if lexical_re is None:
         pattern = '^' + cls._Lexical_fmt + '%Z?$'
         for (k, v) in six.iteritems(cls.__PatternMap):
             pattern = pattern.replace(k, v)
         lexical_re = re.compile(pattern)
         cls.__LexicalREMap[cls] = lexical_re
     match = lexical_re.match(text)
     if match is None:
         raise SimpleTypeValueError(cls, text)
     match_map = match.groupdict()
     kw = { }
     for (k, v) in six.iteritems(match_map):
         if (k in cls.__LexicalIntegerFields) and (v is not None):
             kw[k] = six.int_type(v)
     if '-' == match_map.get('negYear'):
         kw['year'] = - kw['year']
     if match_map.get('fracsec') is not None:
         kw['microsecond'] = six.int_type(round(1000000 * six.float_type('0%s' % (match_map['fracsec'],))))
     else:
         # Discard any bogosity passed in by the caller
         kw.pop('microsecond', None)
     if match_map.get('tzinfo') is not None:
         kw['tzinfo'] = pyxb.utils.utility.UTCOffsetTimeZone(match_map['tzinfo'])
     else:
         kw.pop('tzinfo', None)
     return kw
Exemple #2
0
 def _LexicalToKeywords(cls, text):
     lexical_re = cls.__LexicalREMap.get(cls)
     if lexical_re is None:
         pattern = '^' + cls._Lexical_fmt + '%Z?$'
         for (k, v) in six.iteritems(cls.__PatternMap):
             pattern = pattern.replace(k, v)
         lexical_re = re.compile(pattern)
         cls.__LexicalREMap[cls] = lexical_re
     match = lexical_re.match(text)
     if match is None:
         raise SimpleTypeValueError(cls, text)
     match_map = match.groupdict()
     kw = {}
     for (k, v) in six.iteritems(match_map):
         if (k in cls.__LexicalIntegerFields) and (v is not None):
             kw[k] = six.int_type(v)
     if '-' == match_map.get('negYear'):
         kw['year'] = -kw['year']
     if match_map.get('fracsec') is not None:
         kw['microsecond'] = six.int_type(
             round(1000000 * six.float_type('0%s' %
                                            (match_map['fracsec'], ))))
     else:
         # Discard any bogosity passed in by the caller
         kw.pop('microsecond', None)
     if match_map.get('tzinfo') is not None:
         kw['tzinfo'] = pyxb.utils.utility.UTCOffsetTimeZone(
             match_map['tzinfo'])
     else:
         kw.pop('tzinfo', None)
     return kw
Exemple #3
0
    def __new__ (cls, *args, **kw):
        args = cls._ConvertArguments(args, kw)
        have_kw_update = False
        if not kw.get('_nil'):
            if 0 == len(args):
                raise SimpleTypeValueError(cls, args)
            text = args[0]
        if kw.get('_nil'):
            data = dict(zip(cls.__PythonFields, len(cls.__PythonFields) * [0,]))
            negative_duration = False
        elif isinstance(text, six.string_types):
            match = cls.__Lexical_re.match(text)
            if match is None:
                raise SimpleTypeValueError(cls, text)
            match_map = match.groupdict()
            if 'T' == match_map.get('Time'):
                # Can't have T without additional time information
                raise SimpleTypeValueError(cls, text)

            negative_duration = ('-' == match_map.get('neg'))

            fractional_seconds = 0.0
            if match_map.get('fracsec') is not None:
                fractional_seconds = six.float_type('0%s' % (match_map['fracsec'],))
                usec = six.int_type(1000000 * fractional_seconds)
                if negative_duration:
                    kw['microseconds'] = - usec
                else:
                    kw['microseconds'] = usec
            else:
                # Discard any bogosity passed in by the caller
                kw.pop('microsecond', None)

            data = { }
            for fn in cls.__XSDFields:
                v = match_map.get(fn, 0)
                if v is None:
                    v = 0
                data[fn] = six.int_type(v)
                if fn in cls.__PythonFields:
                    if negative_duration:
                        kw[fn] = - data[fn]
                    else:
                        kw[fn] = data[fn]
            data['seconds'] += fractional_seconds
            have_kw_update = True
        elif isinstance(text, cls):
            data = text.durationData().copy()
            negative_duration = text.negativeDuration()
        elif isinstance(text, datetime.timedelta):
            data = { 'days' : text.days,
                     'seconds' : text.seconds + (text.microseconds / 1000000.0) }
            negative_duration = (0 > data['days'])
            if negative_duration:
                if 0.0 == data['seconds']:
                    data['days'] = - data['days']
                else:
                    data['days'] = 1 - data['days']
                    data['seconds'] = 24 * 60 * 60.0 - data['seconds']
            data['minutes'] = 0
            data['hours'] = 0
        elif isinstance(text, six.integer_types) and (1 < len(args)):
            # Apply the arguments as in the underlying Python constructor
            data = dict(zip(cls.__PythonFields[:len(args)], args))
            negative_duration = False
        else:
            raise SimpleTypeValueError(cls, text)
        if not have_kw_update:
            rem_time = data.pop('seconds', 0)
            if (0 != (rem_time % 1)):
                data['microseconds'] = data.pop('microseconds', 0) + six.int_type(1000000 * (rem_time % 1))
                rem_time = rem_time // 1
            data['seconds'] = rem_time % 60
            rem_time = data.pop('minutes', 0) + (rem_time // 60)
            data['minutes'] = rem_time % 60
            rem_time = data.pop('hours', 0) + (rem_time // 60)
            data['hours'] = rem_time % 24
            data['days'] += (rem_time // 24)
            for fn in cls.__PythonFields:
                if fn in data:
                    if negative_duration:
                        kw[fn] = - data[fn]
                    else:
                        kw[fn] = data[fn]
                else:
                    kw.pop(fn, None)
            kw['microseconds'] = data.pop('microseconds', 0)
            data['seconds'] += kw['microseconds'] / 1000000.0

        rv = super(duration, cls).__new__(cls, **kw)
        rv.__durationData = data
        rv.__negativeDuration = negative_duration
        return rv
Exemple #4
0
    def __new__(cls, *args, **kw):
        args = cls._ConvertArguments(args, kw)
        have_kw_update = False
        if not kw.get('_nil'):
            if 0 == len(args):
                raise SimpleTypeValueError(cls, args)
            text = args[0]
        if kw.get('_nil'):
            data = dict(
                zip(cls.__PythonFields,
                    len(cls.__PythonFields) * [
                        0,
                    ]))
            negative_duration = False
        elif isinstance(text, six.string_types):
            match = cls.__Lexical_re.match(text)
            if match is None:
                raise SimpleTypeValueError(cls, text)
            match_map = match.groupdict()
            if 'T' == match_map.get('Time'):
                # Can't have T without additional time information
                raise SimpleTypeValueError(cls, text)

            negative_duration = ('-' == match_map.get('neg'))

            fractional_seconds = 0.0
            if match_map.get('fracsec') is not None:
                fractional_seconds = six.float_type('0%s' %
                                                    (match_map['fracsec'], ))
                usec = six.int_type(1000000 * fractional_seconds)
                if negative_duration:
                    kw['microseconds'] = -usec
                else:
                    kw['microseconds'] = usec
            else:
                # Discard any bogosity passed in by the caller
                kw.pop('microsecond', None)

            data = {}
            for fn in cls.__XSDFields:
                v = match_map.get(fn, 0)
                if v is None:
                    v = 0
                data[fn] = six.int_type(v)
                if fn in cls.__PythonFields:
                    if negative_duration:
                        kw[fn] = -data[fn]
                    else:
                        kw[fn] = data[fn]
            data['seconds'] += fractional_seconds
            have_kw_update = True
        elif isinstance(text, cls):
            data = text.durationData().copy()
            negative_duration = text.negativeDuration()
        elif isinstance(text, datetime.timedelta):
            data = {
                'days': text.days,
                'seconds': text.seconds + (text.microseconds / 1000000.0)
            }
            negative_duration = (0 > data['days'])
            if negative_duration:
                if 0.0 == data['seconds']:
                    data['days'] = -data['days']
                else:
                    data['days'] = 1 - data['days']
                    data['seconds'] = 24 * 60 * 60.0 - data['seconds']
            data['minutes'] = 0
            data['hours'] = 0
        elif isinstance(text, six.integer_types) and (1 < len(args)):
            # Apply the arguments as in the underlying Python constructor
            data = dict(zip(cls.__PythonFields[:len(args)], args))
            negative_duration = False
        else:
            raise SimpleTypeValueError(cls, text)
        if not have_kw_update:
            rem_time = data.pop('seconds', 0)
            if (0 != (rem_time % 1)):
                data['microseconds'] = data.pop(
                    'microseconds', 0) + six.int_type(1000000 * (rem_time % 1))
                rem_time = rem_time // 1
            data['seconds'] = rem_time % 60
            rem_time = data.pop('minutes', 0) + (rem_time // 60)
            data['minutes'] = rem_time % 60
            rem_time = data.pop('hours', 0) + (rem_time // 60)
            data['hours'] = rem_time % 24
            data['days'] += (rem_time // 24)
            for fn in cls.__PythonFields:
                if fn in data:
                    if negative_duration:
                        kw[fn] = -data[fn]
                    else:
                        kw[fn] = data[fn]
                else:
                    kw.pop(fn, None)
            kw['microseconds'] = data.pop('microseconds', 0)
            data['seconds'] += kw['microseconds'] / 1000000.0

        rv = super(duration, cls).__new__(cls, **kw)
        rv.__durationData = data
        rv.__negativeDuration = negative_duration
        return rv