-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed-datatypes.py
1317 lines (1114 loc) · 52.3 KB
/
fixed-datatypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright 2009-2013, Peter A. Bigot
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain a
# copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Classes supporting U{XMLSchema Part 2: Datatypes<http://www.w3.org/TR/xmlschema-2/>}.
Each L{simple type definition<pyxb.xmlschema.structures.SimpleTypeDefinition>} component
instance is paired with at most one L{basis.simpleTypeDefinition}
class, which is a subclass of a Python type augmented with facets and
other constraining information. This file contains the definitions of
these types.
We want the simple datatypes to be efficient Python values, but to
also hold specific constraints that don't apply to the Python types.
To do this, we subclass each PST. Primitive PSTs inherit from the
Python type that represents them, and from a
pyxb.binding.basis.simpleTypeDefinition class which adds in the
constraint infrastructure. Derived PSTs inherit from the parent PST.
There is an exception to this when the Python type best suited for a
derived SimpleTypeDefinition differs from the type associated with its
parent STD: for example, L{xsd:integer<integer>} has a value range
that requires it be represented by a Python C{long}, but
L{xsd:int<int>} allows representation by a Python C{int}. In this
case, the derived PST class is structured like a primitive type, but
the PST associated with the STD superclass is recorded in a class
variable C{_XsdBaseType}.
Note the strict terminology: "datatype" refers to a class which is a
subclass of a Python type, while "type definition" refers to an
instance of either SimpleTypeDefinition or ComplexTypeDefinition.
"""
import logging
import re
import binascii
import base64
import math
import numbers
import decimal as python_decimal
from pyxb.exceptions_ import *
import pyxb.namespace
import pyxb.utils.unicode
from pyxb.utils import six
from pyxb.utils.typeutils import make_base_dt
from . import basis
_log = logging.getLogger(__name__)
_PrimitiveDatatypes = []
_DerivedDatatypes = []
_ListDatatypes = []
# We use unicode as the Python type for anything that isn't a normal
# primitive type. Presumably, only enumeration and pattern facets
# will be applied.
class anySimpleType (basis.simpleTypeDefinition, six.text_type):
"""XMLSchema datatype U{anySimpleType<http://www.w3.org/TR/xmlschema-2/#dt-anySimpleType>}."""
_XsdBaseType = None
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('anySimpleType')
@classmethod
def XsdLiteral (cls, value):
return value
# anySimpleType is not treated as a primitive, because its variety
# must be absent (not atomic).
class string (basis.simpleTypeDefinition, six.text_type):
"""XMLSchema datatype U{string<http://www.w3.org/TR/xmlschema-2/#string>}."""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('string')
@classmethod
def XsdLiteral (cls, value):
assert isinstance(value, cls)
return value
@classmethod
def XsdValueLength (cls, value):
return len(value)
_PrimitiveDatatypes.append(string)
# It is illegal to subclass the bool type in Python, so we subclass
# int instead.
@six.python_2_unicode_compatible
class boolean (basis.simpleTypeDefinition, six.int_type, basis._NoNullaryNonNillableNew_mixin):
"""XMLSchema datatype U{boolean<http://www.w3.org/TR/xmlschema-2/#boolean>}."""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('boolean')
@classmethod
def XsdLiteral (cls, value):
if value:
return 'true'
return 'false'
def __str__ (self):
if self:
return six.u('true')
return six.u('false')
def __new__ (cls, *args, **kw):
args = cls._ConvertArguments(args, kw)
if 0 < len(args):
value = args[0]
args = args[1:]
if value in (1, 0, '1', '0', 'true', 'false'):
if value in (1, '1', 'true'):
iv = True
else:
iv = False
return super(boolean, cls).__new__(cls, iv, *args, **kw)
raise SimpleTypeValueError(cls, value)
return super(boolean, cls).__new__(cls, *args, **kw)
_PrimitiveDatatypes.append(boolean)
class decimal (basis.simpleTypeDefinition, python_decimal.Decimal, basis._RepresentAsXsdLiteral_mixin, basis._NoNullaryNonNillableNew_mixin):
"""XMLSchema datatype U{decimal<http://www.w3.org/TR/xmlschema-2/#decimal>}.
This class uses Python's L{decimal.Decimal} class to support (by
default) 28 significant digits. Only normal and zero values are
valid; this means C{NaN} and C{Infinity} may be created during
calculations, but cannot be expressed in XML documents.
"""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('decimal')
def __new__ (cls, *args, **kw):
args = cls._ConvertArguments(args, kw)
# Pre Python 2.7 can't construct from float values
if (1 <= len(args)) and isinstance(args[0], six.float_type):
args = (str(args[0]),) + args[1:]
try:
rv = super(decimal, cls).__new__(cls, *args, **kw)
except python_decimal.DecimalException:
raise SimpleTypeValueError(cls, *args)
cls._CheckValidValue(rv)
return rv
@classmethod
def _CheckValidValue (cls, value):
if not (value.is_normal() or value.is_zero()):
raise SimpleTypeValueError(cls, value)
return super(decimal, cls)._CheckValidValue(value)
@classmethod
def XsdLiteral (cls, value):
(sign, digits, exponent) = value.normalize().as_tuple()
if (0 < len(digits)) and (0 == digits[0]):
digits = ()
rchars = []
if sign:
rchars.append('-')
digits_before = len(digits) + exponent
if 0 < digits_before:
rchars.extend(map(str, digits[:digits_before]))
digits = digits[digits_before:]
if (0 == len(digits)) and (0 < exponent):
rchars.extend(['0'] * exponent)
exponent = 0
else:
rchars.append('0')
rchars.append('.')
digits_after = -exponent
assert(0 <= digits_after)
if 0 < digits_after:
rchars.extend(['0'] * (digits_after - len(digits)))
rchars.extend(map(str, digits))
else:
rchars.append('0')
return six.u('').join(rchars)
_PrimitiveDatatypes.append(decimal)
class _fp (basis.simpleTypeDefinition, six.float_type, basis._NoNullaryNonNillableNew_mixin):
_XsdBaseType = anySimpleType
@classmethod
def XsdLiteral (cls, value):
if math.isinf(value):
if (0 > value):
return '-INF'
return 'INF'
if math.isnan(value):
return 'NaN'
return '%s' % (value,)
class float (_fp):
"""XMLSchema datatype U{float<http://www.w3.org/TR/xmlschema-2/#float>}."""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('float')
_PrimitiveDatatypes.append(float)
class double (_fp):
"""XMLSchema datatype U{double<http://www.w3.org/TR/xmlschema-2/#double>}."""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('double')
_PrimitiveDatatypes.append(double)
import datetime
class duration (basis.simpleTypeDefinition, datetime.timedelta, basis._RepresentAsXsdLiteral_mixin):
"""XMLSchema datatype U{duration<http://www.w3.org/TR/xmlschema-2/#duration>}.
This class uses the Python C{datetime.timedelta} class as its
underlying representation. This works fine as long as no months
or years are involved, and no negative durations are involved.
Because the XML Schema value space is so much larger, it is kept
distinct from the Python value space, which reduces to integral
days, seconds, and microseconds.
In other words, the implementation of this type is a little
shakey.
"""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('duration')
__Lexical_re = re.compile('^(?P<neg>-?)P((?P<years>\d+)Y)?((?P<months>\d+)M)?((?P<days>\d+)D)?(?P<Time>T((?P<hours>\d+)H)?((?P<minutes>\d+)M)?(((?P<seconds>\d+)(?P<fracsec>\.\d+)?)S)?)?$')
# We do not use weeks
__XSDFields = ( 'years', 'months', 'days', 'hours', 'minutes', 'seconds' )
__PythonFields = ( 'days', 'seconds', 'microseconds', 'minutes', 'hours' )
def negativeDuration (self):
return self.__negativeDuration
__negativeDuration = None
def durationData (self):
return self.__durationData
__durationData = None
def __new__ (cls, *args, **kw):
args = cls._ConvertArguments(args, kw)
have_kw_update = False
negative_duration = False
if kw.get('_nil'):
data = dict(zip(cls.__PythonFields, len(cls.__PythonFields) * [0,]))
elif 0 == len(args):
if kw.get('_from_xml'):
raise SimpleTypeValueError(cls, args)
data = dict(zip(cls.__PythonFields, len(cls.__PythonFields) * [0,]))
elif 1 < len(args):
if kw.get('_from_xml'):
raise SimpleTypeValueError(cls, args)
# Apply the arguments as in the underlying Python constructor
data = dict(zip(cls.__PythonFields[:len(args)], args))
else:
text = args[0];
if 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 kw.get('_from_xml'):
raise SimpleTypeValueError(cls, args)
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):
# 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
@classmethod
def XsdLiteral (cls, value):
elts = []
if value.negativeDuration():
elts.append('-')
elts.append('P')
for k in ( 'years', 'months', 'days' ):
v = value.__durationData.get(k, 0)
if 0 != v:
elts.append('%d%s' % (v, k[0].upper()))
time_elts = []
for k in ( 'hours', 'minutes' ):
v = value.__durationData.get(k, 0)
if 0 != v:
time_elts.append('%d%s' % (v, k[0].upper()))
v = value.__durationData.get('seconds', 0)
if 0 != v:
time_elts.append(('%f' % (v,)).rstrip('0').rstrip('.') + 'S')
if 0 < len(time_elts):
elts.append('T')
elts.extend(time_elts)
if 1 == len(elts):
# Value must have zero duration. Pick something short.
elts.append('0D')
return ''.join(elts)
_PrimitiveDatatypes.append(duration)
class _PyXBDateTime_base (basis.simpleTypeDefinition, basis._RepresentAsXsdLiteral_mixin):
_Lexical_fmt = None
"""Format for the lexical representation of a date-related instance, excluding timezone.
Subclasses must define this."""
# Map from strptime/strftime formats to the regular expressions we
# use to extract them. We're more strict than strptime, so not
# trying to use that.
__PatternMap = { '%Y' : '(?P<negYear>-?)(?P<year>\d{4,})'
, '%m' : '(?P<month>\d{2})'
, '%d' : '(?P<day>\d{2})'
, '%H' : '(?P<hour>\d{2})'
, '%M' : '(?P<minute>\d{2})'
, '%S' : '(?P<second>\d{2})(?P<fracsec>\.\d+)?'
, '%Z' : '(?P<tzinfo>Z|[-+]\d\d:\d\d)' }
# Cache of compiled regular expressions to parse lexical space of
# a subclass.
__LexicalREMap = { }
# Fields extracted by parsing that have an integer value
__LexicalIntegerFields = ( 'year', 'month', 'day', 'hour', 'minute', 'second' )
_UTCTimeZone = pyxb.utils.utility.UTCOffsetTimeZone(0)
"""A L{datetime.tzinfo} instance representing UTC."""
_LocalTimeZone = pyxb.utils.utility.LocalTimeZone()
"""A L{datetime.tzinfo} instance representing the local time zone."""
_DefaultYear = 1900
_DefaultMonth = 1
_DefaultDay = 1
@classmethod
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
@classmethod
def _SetKeysFromPython_csc (cls, python_value, kw, fields):
for f in fields:
kw[f] = getattr(python_value, f)
return getattr(super(_PyXBDateTime_base, cls), '_SetKeysFromPython_csc', lambda *a,**kw: None)(python_value, kw, fields)
@classmethod
def _SetKeysFromPython (cls, python_value, kw, fields):
return cls._SetKeysFromPython_csc(python_value, kw, fields)
# Several datetime classes are extension classes, and the PyXB
# subclasses won't recognize the packed values. Use the lexical
# representation instead.
def __reduce__ (self):
return (self.__class__, (self.xsdLiteral(),))
# In Python 3.6 datetime started using __reduce_ex__ which is
# higher priority. Override it too.
def __reduce_ex__ (self, protocol):
return (self.__class__, (self.xsdLiteral(),))
@classmethod
def _AdjustForTimezone (cls, kw):
"""Update datetime keywords to account for timezone effects.
All XML schema timezoned times are in UTC, with the time "in
its timezone". If the keywords indicate a non-UTC timezone is
in force, and L{pyxb.PreserveInputTimeZone()} has not been
set, adjust the values to account for the zone by subtracting
the corresponding UTC offset and mark explicitly that the time
is in UTC by leaving a C{tzinfo} attribute identifying the UTC
time zone.
@param kw: A dictionary of keywords relevant for a date or
time instance. The dictionary is updated by this call.
"""
if pyxb.PreserveInputTimeZone():
return
tzoffs = kw.pop('tzinfo', None)
if tzoffs is not None:
use_kw = kw.copy()
# Ensure ctor requirements of datetime.datetime are met
use_kw.setdefault('year', cls._DefaultYear)
use_kw.setdefault('month', cls._DefaultMonth)
use_kw.setdefault('day', cls._DefaultDay)
dt = datetime.datetime(tzinfo=tzoffs, **use_kw)
dt -= tzoffs.utcoffset(dt)
for k in six.iterkeys(kw):
kw[k] = getattr(dt, k)
kw['tzinfo'] = cls._UTCTimeZone
@classmethod
def XsdLiteral (cls, value):
iso = value.replace(tzinfo=None).isoformat()
if 0 <= iso.find('.'):
iso = iso.rstrip('0')
if value.tzinfo is not None:
iso += value.tzinfo.tzname(value)
return iso
class dateTime (_PyXBDateTime_base, datetime.datetime):
"""XMLSchema datatype U{dateTime<http://www.w3.org/TR/xmlschema-2/#dateTime>}.
This class uses the Python C{datetime.datetime} class as its
underlying representation. Unless L{pyxb.PreserveInputTimeZone()}
is used, all timezoned dateTime objects are in UTC. Presence of
time zone information in the lexical space is preserved by a
non-empty tzinfo field, which should always be zero minutes offset
from UTC unless the input time zone was preserved.
@warning: The value space of Python's C{datetime.datetime} class
is more restricted than that of C{xs:datetime}. As a specific
example, Python does not support negative years or years with more
than four digits. For now, the convenience of having an object
that is compatible with Python is more important than supporting
the full value space. In the future, the choice may be left up to
the developer.
"""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('dateTime')
_Lexical_fmt = '%Y-%m-%dT%H:%M:%S'
__CtorFields = ( 'year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond', 'tzinfo' )
def __new__ (cls, *args, **kw):
args = cls._ConvertArguments(args, kw)
ctor_kw = { }
if kw.get('_nil'):
ctor_kw = { 'year': 1900, 'month': 1, 'day': 1 }
elif 0 == len(args):
if kw.get('_from_xml'):
raise SimpleTypeValueError(cls, args)
ctor_kw = { 'year': 1900, 'month': 1, 'day': 1 }
elif 1 == len(args):
value = args[0]
if isinstance(value, six.string_types):
ctor_kw.update(cls._LexicalToKeywords(value))
elif isinstance(value, datetime.datetime):
cls._SetKeysFromPython(value, ctor_kw, cls.__CtorFields)
elif isinstance(value, six.integer_types):
raise TypeError('function takes at least 3 arguments (%d given)' % (len(args),))
else:
raise SimpleTypeValueError(cls, value)
elif 3 <= len(args):
for fi in range(len(cls.__CtorFields)):
fn = cls.__CtorFields[fi]
if fi < len(args):
ctor_kw[fn] = args[fi]
elif fn in kw:
ctor_kw[fn] = kw[fn]
kw.pop(fn, None)
else:
raise TypeError('function takes at least 3 arguments (%d given)' % (len(args),))
cls._AdjustForTimezone(ctor_kw)
kw.update(ctor_kw)
year = kw.pop('year')
month = kw.pop('month')
day = kw.pop('day')
rv = super(dateTime, cls).__new__(cls, year, month, day, **kw)
return rv
@classmethod
def today (cls):
"""Return today.
Just like datetime.datetime.today(), except this one sets a
tzinfo field so it's clear the value is UTC."""
return cls(datetime.datetime.now(cls._UTCTimeZone))
def aslocal (self):
"""Returns a C{datetime.datetime} instance denoting the same
time as this instance but adjusted to be in the local time
zone.
@rtype: C{datetime.datetime} (B{NOT} C{xsd.dateTime})
"""
dt = make_base_dt(self)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=self._UTCTimeZone)
return dt.astimezone(self._LocalTimeZone)
_PrimitiveDatatypes.append(dateTime)
class time (_PyXBDateTime_base, datetime.time):
"""XMLSchema datatype U{time<http://www.w3.org/TR/xmlschema-2/#time>}.
This class uses the Python C{datetime.time} class as its
underlying representation. Note that per the XMLSchema spec, all
dateTime objects are in UTC, and that timezone information in the
string representation in XML is an indication of the local time
zone's offset from UTC. Presence of time zone information in the
lexical space is indicated by the tzinfo field.
@note: C{pyxb.PreserveInputTimeZone()} can be used to bypass the
normalization to UTC.
"""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('time')
_Lexical_fmt = '%H:%M:%S'
__CtorFields = ( 'hour', 'minute', 'second', 'microsecond', 'tzinfo' )
def __new__ (cls, *args, **kw):
args = cls._ConvertArguments(args, kw)
ctor_kw = { }
if kw.get('_nil'):
pass
elif 0 == len(args):
if kw.get('_from_xml'):
raise SimpleTypeValueError(cls, args)
else:
value = args[0]
if isinstance(value, six.string_types):
ctor_kw.update(cls._LexicalToKeywords(value))
elif isinstance(value, (datetime.time, datetime.datetime)):
cls._SetKeysFromPython(value, ctor_kw, cls.__CtorFields)
elif isinstance(value, six.integer_types):
for fi in range(len(cls.__CtorFields)):
fn = cls.__CtorFields[fi]
if fi < len(args):
ctor_kw[fn] = args[fi]
elif fn in kw:
ctor_kw[fn] = kw[fn]
kw.pop(fn, None)
else:
raise SimpleTypeValueError(cls, value)
cls._AdjustForTimezone(ctor_kw)
kw.update(ctor_kw)
return super(time, cls).__new__(cls, **kw)
_PrimitiveDatatypes.append(time)
class _PyXBDateOnly_base (_PyXBDateTime_base, datetime.datetime):
_XsdBaseType = anySimpleType
_ValidFields = ( 'year', 'month', 'day' )
def __new__ (cls, *args, **kw):
args = cls._ConvertArguments(args, kw)
ctor_kw = { }
ctor_kw['year'] = cls._DefaultYear
ctor_kw['month'] = cls._DefaultMonth
ctor_kw['day'] = cls._DefaultDay
ctor_kw['hour'] = 0
ctor_kw['minute'] = 0
ctor_kw['second'] = 0
if kw.get('_nil'):
pass
elif 1 <= len(args):
value = args[0]
if isinstance(value, six.string_types):
if 1 != len(args):
raise TypeError('construction from string requires exactly 1 argument')
ctor_kw.update(cls._LexicalToKeywords(value))
elif isinstance(value, (datetime.date, datetime.datetime)):
if 1 != len(args):
raise TypeError('construction from instance requires exactly 1 argument')
cls._SetKeysFromPython(value, ctor_kw, cls._ValidFields)
try:
tzinfo = value.tzinfo
if tzinfo is not None:
ctor_kw['tzinfo'] = tzinfo
except AttributeError:
pass
else:
fi = 0
while fi < len(cls._ValidFields):
fn = cls._ValidFields[fi]
if fi < len(args):
ctor_kw[fn] = args[fi]
elif fn in kw:
ctor_kw[fn] = kw[fn]
kw.pop(fn, None)
fi += 1
# https://github.com/renalreg/PyXB-X/issues/8
# Because PyXB dates are datetime not date underneath, Python 3.8+ correctly crashes as a result of
# the extra parameters for a datetime (but not in a date) being present. We'll silently discard them if
# they are of value "zero", as they're not included in the resulting argv array below, and zero is
# set as the default value above in any case.
for extra_arg in args[fi:]:
if isinstance(extra_arg, numbers.Integral) and extra_arg == 0:
fi += 1
if fi < len(args):
ctor_kw['tzinfo'] = args[fi]
fi += 1
if fi != len(args):
raise TypeError('function takes %d arguments plus optional tzinfo (%d given)' % (len(cls._ValidFields), len(args)))
else:
raise TypeError('function takes %d arguments plus optional tzinfo' % (len(cls._ValidFields),))
# Do not adjust for the timezone here. Only xsd:date provides
# a recoverable timezone, so just preserve the as-supplied
# timezone, and we'll canonicalize the date one if/when it's
# converted back to lexical form.
kw.update(ctor_kw)
argv = []
argv.append(kw.pop('year'))
argv.append(kw.pop('month'))
argv.append(kw.pop('day'))
return super(_PyXBDateOnly_base, cls).__new__(cls, *argv, **kw)
@classmethod
def XsdLiteral (cls, value):
# Work around strftime year restriction
fmt = cls._Lexical_fmt
if value.year < 1900:
fmt = fmt.replace('%Y', '%04d' % (value.year,))
value = value.replace(year=1900)
if value.tzinfo is not None:
fmt += value.tzinfo.tzname(value)
return value.strftime(fmt)
class date (_PyXBDateOnly_base):
"""XMLSchema datatype U{date<http://www.w3.org/TR/xmlschema-2/#date>}.
This class uses the Python C{datetime.datetime} class as its
underlying representation; fields not relevant to this type are
derived from 1900-01-01T00:00:00.
@note: Unlike L{dateTime}, timezoned date values are not converted
to UTC. The provided timezone information is retained along with
the instance; however, the lexical representation generated for
output is canonicalized (timezones no more than 12 hours off UTC).
"""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('date')
_Lexical_fmt = '%Y-%m-%d'
_Fields = ( 'year', 'month', 'day' )
__SecondsPerMinute = 60
__MinutesPerHalfDay = 12 * 60
__MinutesPerDay = 24 * 60
def xsdRecoverableTzinfo (self):
"""Return the recoverable tzinfo for the date.
Return a L{pyxb.utils.utility.UTCOffsetTimeZone} instance
reflecting the timezone associated with the date, or C{None}
if the date is not timezoned.
@note: This is not the recoverable timezone, because timezones are
represented as timedeltas which get normalized in ways that
don't match what we expect for a tzinfo.
"""
if self.tzinfo is None:
return None
sdt = self.replace(hour=0, minute=0, second=0, tzinfo=self._UTCTimeZone)
utc_offset = (sdt - self).seconds // self.__SecondsPerMinute
if utc_offset > self.__MinutesPerHalfDay:
utc_offset -= self.__MinutesPerDay
return pyxb.utils.utility.UTCOffsetTimeZone(utc_offset)
@classmethod
def XsdLiteral (cls, value):
# Create a copy of `value` using datetime baseclass
# This allows us to do datetime arithmetic on Python 3.8+
base_value = make_base_dt(value)
# Work around strftime year restriction
fmt = cls._Lexical_fmt
rtz = value.xsdRecoverableTzinfo()
if rtz is not None:
# If the date is timezoned, convert it to UTC
base_value -= base_value.tzinfo.utcoffset(value)
base_value = base_value.replace(tzinfo=cls._UTCTimeZone)
# Use the midpoint of the one-day interval to get the correct
# month/day.
base_value += datetime.timedelta(minutes=cls.__MinutesPerHalfDay)
if base_value.year < 1900:
fmt = fmt.replace('%Y', '%04d' % (base_value.year,))
base_value = base_value.replace(year=1900)
if rtz is not None:
fmt += rtz.tzname(base_value)
return base_value.strftime(fmt)
_PrimitiveDatatypes.append(date)
class gYearMonth (_PyXBDateOnly_base):
"""XMLSchema datatype U{gYearMonth<http://www.w3.org/TR/xmlschema-2/#gYearMonth>}.
This class uses the Python C{datetime.datetime} class as its
underlying representation; fields not relevant to this type are
derived from 1900-01-01T00:00:00.
"""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('gYearMonth')
_Lexical_fmt = '%Y-%m'
_ValidFields = ( 'year', 'month' )
_PrimitiveDatatypes.append(gYearMonth)
class gYear (_PyXBDateOnly_base):
"""XMLSchema datatype U{gYear<http://www.w3.org/TR/xmlschema-2/#gYear>}.
This class uses the Python C{datetime.datetime} class as its
underlying representation; fields not relevant to this type are
derived from 1900-01-01T00:00:00.
"""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('gYear')
_Lexical_fmt = '%Y'
_ValidFields = ( 'year', )
_PrimitiveDatatypes.append(gYear)
class gMonthDay (_PyXBDateOnly_base):
"""XMLSchema datatype U{gMonthDay<http://www.w3.org/TR/xmlschema-2/#gMonthDay>}.
This class uses the Python C{datetime.datetime} class as its
underlying representation; fields not relevant to this type are
derived from 1900-01-01T00:00:00.
"""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('gMonthDay')
_Lexical_fmt = '--%m-%d'
_ValidFields = ( 'month', 'day' )
_PrimitiveDatatypes.append(gMonthDay)
class gDay (_PyXBDateOnly_base):
"""XMLSchema datatype U{gDay<http://www.w3.org/TR/xmlschema-2/#gDay>}.
This class uses the Python C{datetime.datetime} class as its
underlying representation; fields not relevant to this type are
derived from 1900-01-01T00:00:00.
"""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('gDay')
_Lexical_fmt = '---%d'
_ValidFields = ( 'day', )
_PrimitiveDatatypes.append(gDay)
class gMonth (_PyXBDateOnly_base):
"""XMLSchema datatype U{gMonth<http://www.w3.org/TR/xmlschema-2/#gMonth>}.
This class uses the Python C{datetime.datetime} class as its
underlying representation; fields not relevant to this type are
derived from 1900-01-01T00:00:00.
"""
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('gMonth')
_Lexical_fmt = '--%m'
_ValidFields = ( 'month', )
_PrimitiveDatatypes.append(gMonth)
class hexBinary (basis.simpleTypeDefinition, six.binary_type):
"""XMLSchema datatype U{hexBinary<http://www.w3.org/TR/xmlschema-2/#hexBinary>}."""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('hexBinary')
@classmethod
def _ConvertArguments_vx (cls, args, kw):
if (1 <= len(args)) and kw.get('_from_xml', False):
xmlt = args[0]
try:
xmld = xmlt.encode('utf-8')
arg0 = binascii.unhexlify(xmld)
args = (arg0,) + args[1:]
except (TypeError, binascii.Error):
raise SimpleTypeValueError(cls, args[0])
return args
@classmethod
def XsdLiteral (cls, value):
if isinstance(value, six.text_type):
value = value.encode('utf-8')
rvd = binascii.hexlify(value)
rvt = rvd.decode('utf-8')
return rvt.upper()
@classmethod
def XsdValueLength (cls, value):
return len(value)
_PrimitiveDatatypes.append(hexBinary)
class base64Binary (basis.simpleTypeDefinition, six.binary_type):
"""XMLSchema datatype U{base64Binary<http://www.w3.org/TR/xmlschema-2/#base64Binary>}.
See also U{RFC2045<http://tools.ietf.org/html/rfc2045>} and U{RFC4648<http://tools.ietf.org/html/rfc4648>}.
"""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('base64Binary')
# base64 is too lenient: it accepts 'ZZZ=' as an encoding of
# 'e\x96', while the required XML Schema production requires
# 'ZZY='. Define a regular expression per section 3.2.16.
_B04 = '[AQgw]'
_B04S = '(%s ?)' % (_B04,)
_B16 = '[AEIMQUYcgkosw048]'
_B16S = '(%s ?)' % (_B16,)
_B64 = '[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/]'
_B64S = '(%s ?)' % (_B64,)
__Pattern = '^((' + _B64S + '{4})*((' + _B64S + '{3}' + _B64 + ')|(' + _B64S + '{2}' + _B16S + '=)|(' + _B64S + _B04S + '= ?=)))?$'
__Lexical_re = re.compile(__Pattern)
__ValidateLength = None
@classmethod
def XsdValidateLength (cls, length):
"""Control the maximum encoded size that is checked for XML literal validity.
Python's base64 module allows some literals that are invalid
according to XML rules. PyXB verifies the validity using a
regular expression, which is costly for something that is
unlikely to occur. Use this function to inhibit checks for
validity based on the length of the XML literal.
@param length: C{None} (default) to check all literals,
otherwise the maximum length literal that will be checked.
Pass C{-1} to disable the validity check.
@return: the previous validation length
"""
rv = cls.__ValidateLength
if (length is None) or isinstance(length, six.integer_types):
cls.__ValidateLength = length
return rv
raise TypeError('must provide None or integer length')
@classmethod
def _ConvertArguments_vx (cls, args, kw):
if (1 <= len(args)) and kw.get('_from_xml', False):
xmlt = args[0]
try:
xmld = xmlt.encode('utf-8')
arg0 = base64.standard_b64decode(xmld)
args = (arg0,) + args[1:]
except (TypeError, binascii.Error):
raise SimpleTypeValueError(cls, xmlt)
if (cls.__ValidateLength is None) or (cls.__ValidateLength >= len(xmlt)):
# This is what it costs to try to be a validating processor.
if cls.__Lexical_re.match(xmlt) is None:
raise SimpleTypeValueError(cls, xmlt)
return args
@classmethod
def XsdLiteral (cls, value):
if isinstance(value, six.text_type):
value = value.encode('utf-8')
rvd = base64.standard_b64encode(value)
rvt = rvd.decode('utf-8')
return rvt
@classmethod
def XsdValueLength (cls, value):
return len(value)
_PrimitiveDatatypes.append(base64Binary)
class anyURI (basis.simpleTypeDefinition, six.text_type):
"""XMLSchema datatype U{anyURI<http://www.w3.org/TR/xmlschema-2/#anyURI>}."""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('anyURI')
@classmethod
def XsdValueLength (cls, value):
return len(value)
@classmethod
def XsdLiteral (cls, value):
return six.text_type(value)
_PrimitiveDatatypes.append(anyURI)
class QName (basis.simpleTypeDefinition, pyxb.namespace.ExpandedName):
"""XMLSchema datatype U{QName<http://www.w3.org/TR/xmlschema-2/#QName>}."""
_XsdBaseType = anySimpleType
_ExpandedName = pyxb.namespace.XMLSchema.createExpandedName('QName')
@classmethod
def XsdValueLength (cls, value):
"""Section 4.3.1.3: Legacy length return None to indicate no check"""
return None
@classmethod
def _ConvertIf (cls, value, xmlns_context):
if isinstance(value, pyxb.namespace.ExpandedName):
assert 0 > value.localName().find(':')
return value
if not isinstance(value, six.string_types):
raise SimpleTypeValueError(cls, value)
if 0 <= value.find(':'):
(prefix, local) = value.split(':', 1)
if (NCName._ValidRE.match(prefix) is None) or (NCName._ValidRE.match(local) is None):
raise SimpleTypeValueError(cls, value)
if xmlns_context is None:
raise pyxb.QNameResolutionError('QName resolution requires namespace context', value, xmlns_context)
return xmlns_context.interpretQName(value, default_no_namespace=True)
if NCName._ValidRE.match(value) is None:
raise SimpleTypeValueError(cls, value)
if xmlns_context is not None:
return xmlns_context.interpretQName(value, default_no_namespace=True)
return pyxb.namespace.ExpandedName(value)
@classmethod