/
_cim_types.py
1371 lines (1099 loc) · 49.6 KB
/
_cim_types.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
#
# (C) Copyright 2003,2004 Hewlett-Packard Development Company, L.P.
# (C) Copyright 2006,2007 Novell, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Tim Potter <tpot@hp.com>
# Author: Bart Whiteley <bwhiteley@suse.de>
# Author: Ross Peoples <ross.peoples@gmail.com>
#
# pylint: disable=line-too-long
"""
Python classes for representing values of CIM data types, and related
conversion functions.
The following table shows how CIM data types are represented in Python.
Note that some basic CIM data types are represented with built-in Python
types.
======================================== =====================================
CIM data type Python type
======================================== =====================================
boolean :class:`py:bool`
char16 :term:`string`
or :class:`~pywbem.Char16`
string :term:`string`
string (EmbeddedInstance) :class:`~pywbem.CIMInstance`
string (EmbeddedObject) :class:`~pywbem.CIMInstance`
or :class:`~pywbem.CIMClass`
datetime :class:`~pywbem.CIMDateTime`
reference :class:`~pywbem.CIMInstanceName`
uint8 :class:`~pywbem.Uint8`
uint16 :class:`~pywbem.Uint16`
uint32 :class:`~pywbem.Uint32`
uint64 :class:`~pywbem.Uint64`
sint8 :class:`~pywbem.Sint8`
sint16 :class:`~pywbem.Sint16`
sint32 :class:`~pywbem.Sint32`
sint64 :class:`~pywbem.Sint64`
real32 :class:`~pywbem.Real32`
real64 :class:`~pywbem.Real64`
[] (array) :class:`py:list`
======================================== =====================================
The CIM NULL value is represented with Python `None` which can be used for any
CIM typed value to represent NULL.
Note that init methods of pywbem classes that take CIM typed values as input
may support Python types in addition to those shown above. For example, the
:class:`~pywbem.CIMProperty` class represents property values of CIM datetime
type internally as :class:`~pywbem.CIMDateTime` objects, but its init method
accepts :class:`py:datetime.timedelta` objects, :class:`py:datetime.datetime`
objects, :term:`string`, in addition to
:class:`~pywbem.CIMDateTime` objects.
"""
# pylint: enable=line-too-long
# Note: When used before module docstrings, Pylint scopes the disable statement
# to the whole rest of the file, so we need an enable statement.
# This module is meant to be safe for 'import *'.
from __future__ import absolute_import
from datetime import tzinfo, datetime, timedelta
import re
import copy
import six
from .config import ENFORCE_INTEGER_RANGE
from ._utils import _ensure_unicode, _hash_item, _format, _to_unicode, \
_eq_item
if six.PY2:
# pylint: disable=invalid-name,undefined-variable
_Longint = long # noqa: F821
else:
# pylint: disable=invalid-name
_Longint = int
__all__ = ['cimtype', 'type_from_name', 'MinutesFromUTC', 'CIMType',
'CIMDateTime', 'CIMInt', 'Uint8', 'Sint8', 'Uint16', 'Sint16',
'Uint32', 'Sint32', 'Uint64', 'Sint64', 'CIMFloat', 'Real32',
'Real64', 'Char16']
class _CIMComparisonMixin(object): # pylint: disable=too-few-public-methods
"""
Mixin class providing default implementations for equality test
operators and hash function.
The implementations of ordering tests are also provided and raise an
exception because ordering of CIM objects is not supported.
In Python 2, the rich comparison operators (e.g. `__eq__()`) have
precedence over the traditional comparator method (`__cmp__()`).
In Python 3, the comparator method (`__cmp__()`) no longer exists.
Therefore, implementing the rich comparison operators works in both.
"""
__slots__ = []
def __eq__(self, other):
"""
Equality test for two CIM objects.
The comparison must be implemented in the derived class.
"""
raise NotImplementedError
def __ne__(self, other):
"""
Non-equality test for two CIM objects.
The comparison is delegated to the `__eq__()` method, because we
require it to be implemented.
See https://stackoverflow.com/a/30676267/1424462 for discussion about
delegating to == vs. __eq__().
"""
return not self.__eq__(other)
def __raise_ordering_not_supported(self, other, op):
"""
Function to raise a TypeError indicating that ordering of this class
is not supported.
"""
raise TypeError(
"'{}' not supported between instances of '{}' and '{}'".
format(op, type(self), type(other)))
def __lt__(self, other):
"" # Avoids docstring to be inherited
self.__raise_ordering_not_supported(other, '<')
def __gt__(self, other):
"" # Avoids docstring to be inherited
self.__raise_ordering_not_supported(other, '>')
def __ge__(self, other):
"" # Avoids docstring to be inherited
self.__raise_ordering_not_supported(other, '>=')
def __le__(self, other):
"" # Avoids docstring to be inherited
self.__raise_ordering_not_supported(other, '<=')
def __hash__(self):
"""
Interface definition for hash function to be provided by subclasses.
Background: In order to behave as expected in sets and other hash-based
collections, the hash values of objects must be equal when the objects
themselves are considered equal. The default hash function for classes
is based on `id()` and therefore does not satisfy that requirement.
Therefore, the CIM objects need to implement a hash function that
satisfies that requirement.
"""
raise NotImplementedError
class SlottedPickleMixin(object):
"""
On Python 2, the built-in 'pickle' module uses pickle protocol 0 by default.
Using protocol 0 causes pickle to raise TypeError for objects with slots
that do not define methods __getstate__() and __setstate__().
In Python 3.0, the default pickle protocol changed to protocol 3 and in
Python 3.8 to protocol 4, both of which support objects with slots by
default, without requiring that they define methods __getstate__() and
__setstate__().
That led to the idea to provide these methods only when running on
Python 2. However, it turns out that objects pickled before this change
(using the built-in methods) save all attributes in one shot by saving
their __dict__ value, so the default unpickling after introduction of
slots attempts to set the __dict__ which fails with AttributeError.
Therefore, these methods need to be provided also for Python 3.
Also, because code in pywbem_mock/_resolvermixin.py dynamically added
the non-existing attributes 'classorigin' and 'propagated' to CIMClass
objects (that code has been removed meanwhile), the unpickling may
encounter CIMClass objects that still have these attributes set. That
is handled by ignoring these attributes during unpickling.
These methods support classes with both slotted and non-slotted attributes.
Support for non-slottted attributes is needed for example when users
define classes derived from CIM object or CIM type classes.
"""
__slots__ = []
def __getstate__(self):
"" # Avoids docstring to be inherited
dct = {}
for attr in self.__slots__:
dct[attr] = getattr(self, attr)
# Support for objects that also have __dict__, e.g. user defined
# derived classes that did not define __slots__:
if hasattr(self, '__dict__'):
for attr in self.__dict__:
dct[attr] = getattr(self, attr)
return dct
def __setstate__(self, dct):
"" # Avoids docstring to be inherited
# Defer import due to circular import dependencies:
# pylint: disable=import-outside-toplevel
from pywbem._cim_obj import CIMClass as class_type
for attr in dct:
# Compatibility support for CIMClass objects that were pickled
# before pywbem 1.2.0:
if isinstance(self, class_type) and \
attr in ('classorigin', 'propagated'):
continue
setattr(self, attr, dct[attr])
class MinutesFromUTC(SlottedPickleMixin, tzinfo):
"""
Timezone information (an implementation of :class:`py:datetime.tzinfo`)
that represents a fixed offset in +/- minutes from UTC and is thus suitable
for the CIM datetime data type.
Objects of this class are needed in order to make
:class:`py:datetime.datetime` objects timezone-aware, in order to be
useable as input data to the timezone-aware :class:`~pywbem.CIMDateTime`
type.
They are also used to provide timezone information to
:meth:`~pywbem.CIMDateTime.now` and
:meth:`~pywbem.CIMDateTime.fromtimestamp`
Example:
::
from datetime import datetime
from time import time
import pywbem
# Create a timezone-aware datetime object (for CEDT = UTC+2h), and
# convert that to CIM datetime:
dt = datetime(year=2016, month=3, day=31, hour=19, minute=30,
second=40, microsecond=654321,
tzinfo=pywbem.MinutesFromUTC(120))
cim_dt = pywbem.CIMDateTime(dt)
# Convert a POSIX timestamp value to CIM datetime (for EST = UTC-5h):
posix_ts = time() # seconds since the epoch, not timezone-aware
cim_dt = pywbem.CIMDateTime.fromtimestamp(posix_ts,
pywbem.MinutesFromUTC(-300))
"""
__slots__ = ['_offset']
def __init__(self, offset): # pylint: disable=super-init-not-called
"""
Parameters:
offset (:term:`integer`):
Timezone offset to be represented in the CIM datetime value in +/-
minutes from UTC.
This is the offset of local time to UTC (including DST offset),
where a positive value indicates minutes east of UTC, and a
negative value indicates minutes west of UTC.
"""
self._offset = offset
def __repr__(self):
"""
Return a string representation suitable for debugging.
"""
return _format(
"MinutesFromUTC("
"offset={s._offset!A})",
s=self)
def utcoffset(self, dt): # pylint: disable=unused-argument
"""
An implementation of the corresponding base class method
(see :meth:`py:datetime.tzinfo.utcoffset` for its description),
which needs
to return the offset of local time to UTC (including DST offset), as a
:class:`py:datetime.timedelta` object. This method is called by the
Python datetime classes, and a pywbem user normally does not have
to deal with it.
This implementation returns the offset used to initialize the object,
for any specified `dt` parameter.
"""
return timedelta(minutes=self._offset)
def dst(self, dt): # pylint: disable=unused-argument
"""
An implementation of the corresponding base class method,
(see :meth:`py:datetime.tzinfo.dst` for its description),
which needs
to return the offset caused by DST, as a :class:`py:datetime.timedelta`
object. This method is called by the Python datetime classes, and a
pywbem user normally does not have to deal with it.
This implementation returns an offset of 0 (indicating that DST is not
in effect), for any specified `dt` parameter, because CIM datetime
values do not represent DST information.
"""
return timedelta(0)
def tzname(self, dt): # pylint: disable=unused-argument
"""
An implementation of the corresponding base class method,
(see :meth:`py:datetime.tzinfo.tzname` for its description),
which needs to return the name of the timezone of the
specified datetime object.
This implementation returns the timezone offset formatted as a
signed HH:MM string, where positive values are east of UTC.
"""
# Note that divmod() and // return one less for negative numbers
# assuming the remainder would be added on it. For example,
# divmod(-5, 60) = -1, 55
# That is mathematically consistent, but is not what we want
# since we want both return values to be negative, for negative
# input. Therefore we handle the sign separately.
sign = '-' if self._offset < 0 else ''
hh, mm = divmod(abs(self._offset), 60)
return "{sign}{hh:02d}:{mm:02d}".format(sign=sign, hh=hh, mm=mm)
class CIMType(SlottedPickleMixin): # pylint: disable=too-few-public-methods
"""Base type for all CIM data types defined in this package."""
__slots__ = []
#: The name of the CIM datatype, as a :term:`string`. See
#: :ref:`CIM data types` for details.
cimtype = None
class Char16(CIMType, six.text_type):
"""
A value of CIM data type char16.
This class is derived from :term:`unicode string`.
Normally, values of CIM data type char16 are represented using
:term:`unicode string` objects. This class can be used to represent values
of CIM data type char16 when it matters to distinguish them from values of
CIM data type string. The only situation where that matters is for
keybindings, because that allows properly setting the TYPE attribute on
KEYVALUE elements when creating the CIM-XML representation for a keybinding.
"""
__slots__ = []
#: The name of the CIM datatype ``"char16"``
cimtype = 'char16'
# Changing the string content requires using __new__() instead of __init__()
def __new__(cls, content=''):
"" # Avoids docstring to be inherited
return super(Char16, cls).__new__(cls, _ensure_unicode(content))
class CIMDateTime(_CIMComparisonMixin, CIMType):
"""
A value of CIM data type datetime.
The object represents either a timezone-aware point in time, or a time
interval.
Two objects of this class compare equal if their public attributes compare
equal. Objects of this class are immutable and :term:`hashable`, with the
hash value being based on their public attributes.
"""
__slots__ = ['__timedelta', '__datetime', '__precision']
#: The name of the CIM datatype ``"datetime"``
cimtype = 'datetime'
_timestamp_pattern = re.compile(
r'^([\d\*]{4})([\d\*]{2})([\d\*]{2})'
r'([\d\*]{2})([\d\*]{2})([\d\*]{2})\.([\d\*]{6})'
r'([+|-])(\d{3})')
_interval_pattern = re.compile(
r'^([\d\*]{8})([\d\*]{2})([\d\*]{2})([\d\*]{2})\.([\d\*]{6})'
r'(:)(000)')
def __init__(self, dtarg):
"""
Parameters:
dtarg:
The value from which the object is initialized, as one of the
following types:
* A :term:`string` object will be
interpreted as CIM datetime format (see :term:`DSP0004`) and
will result in a point in time or a time interval. The use
of asterisk characters in the value is supported according to
the rules defined in :term:`DSP0004` (e.g.
"20180911124613.128***:000").
* A :class:`py:datetime.datetime` object will result in a point
in time. If the :class:`py:datetime.datetime` object is
timezone-aware (see :class:`~pywbem.MinutesFromUTC`), the
specified timezone will be used. Otherwise, a default timezone
of UTC will be assumed.
* A :class:`py:datetime.timedelta` object will result in a time
interval.
* Another :class:`~pywbem.CIMDateTime` object will be copied.
"""
self.__timedelta = None # timedelta value, if interval
self.__datetime = None # datetime value, if timestamp
self.__precision = None # 0-based index of first asterisk, or None
dtarg = _ensure_unicode(dtarg)
if isinstance(dtarg, six.text_type):
m = self._timestamp_pattern.search(dtarg)
if m is not None:
# timestamp format
parts = m.groups()
offset = int(parts[8])
if parts[7] == '-':
offset = -offset
if '*' in dtarg:
first = dtarg.index('*')
after = dtarg.rindex('*') + 1
if not re.match(r'^[\*\.]+$', dtarg[first:after]):
raise ValueError(
_format("Asterisks in CIM datetime timestamp "
"value are not consecutive: {0!A}", dtarg))
if after != 21: # end of microseconds field
raise ValueError(
_format("Asterisks in CIM datetime timestamp "
"value do not include least significant "
"field: {0!A}", dtarg))
self.__precision = first
year = self._to_int(parts[0], 0, None, 'year', dtarg)
month = self._to_int(parts[1], 1, None, 'month', dtarg)
day = self._to_int(parts[2], 1, None, 'day', dtarg)
hour = self._to_int(parts[3], 0, None, 'hour', dtarg)
minute = self._to_int(parts[4], 0, None, 'minute', dtarg)
second = self._to_int(parts[5], 0, None, 'second', dtarg)
microsec = self._to_int(parts[6], 0, '0', 'microsecond', dtarg)
try:
# Possible errors are e.g. field out of range
self.__datetime = datetime(
year, month, day, hour, minute, second, microsec,
MinutesFromUTC(offset))
except ValueError as exc:
raise ValueError(
_format("Invalid datetime() input from CIM datetime "
"timestamp value {0!A}: {1}", dtarg, exc))
else:
m = self._interval_pattern.search(dtarg)
if m is not None:
# interval format
parts = m.groups()
if '*' in dtarg:
first = dtarg.index('*')
after = dtarg.rindex('*') + 1
if not re.match(r'^[\*\.]+$', dtarg[first:after]):
raise ValueError(
_format("Asterisks in CIM datetime interval "
"value are not consecutive: {0!A}",
dtarg))
if after != 21: # end of microseconds field
raise ValueError(
_format("Asterisks in CIM datetime interval "
"value do not include least "
"significant field: {0!A}", dtarg))
self.__precision = first
days = self._to_int(parts[0], 0, None, 'days', dtarg)
hours = self._to_int(parts[1], 0, None, 'hours', dtarg)
minutes = self._to_int(parts[2], 0, None, 'minutes', dtarg)
seconds = self._to_int(parts[3], 0, None, 'seconds', dtarg)
microsecs = self._to_int(parts[4], 0, '0', 'microseconds',
dtarg)
try:
# Possible errors are e.g. field out of range
self.__timedelta = timedelta(
days=days, hours=hours, minutes=minutes,
seconds=seconds, microseconds=microsecs)
except ValueError as exc:
raise ValueError(
_format("Invalid timedelta() input from CIM "
"datetime interval value {0!A}: {1}",
dtarg, exc))
else:
raise ValueError(
_format("Invalid format of CIM datetime value: {0!A}",
dtarg))
elif isinstance(dtarg, datetime):
if dtarg.tzinfo is None:
self.__datetime = dtarg.replace(tzinfo=MinutesFromUTC(0))
else:
self.__datetime = copy.copy(dtarg)
elif isinstance(dtarg, timedelta):
self.__timedelta = copy.copy(dtarg)
elif isinstance(dtarg, CIMDateTime):
self.__datetime = copy.copy(dtarg.datetime)
self.__timedelta = copy.copy(dtarg.timedelta)
else:
raise TypeError(
_format("dtarg argument {0!A} has an invalid type: {1} "
"(expected datetime, timedelta, string, or "
"CIMDateTime)", dtarg, type(dtarg)))
@staticmethod
def _to_int(value_str, min_value, rep_digit, field_name, dtarg):
"""
Convert value_str into an integer, replacing right-consecutive
asterisks with rep_digit, and an all-asterisk value with min_value.
field_name and dtarg are passed only for informational purposes.
"""
if '*' in value_str:
first = value_str.index('*')
after = value_str.rindex('*') + 1
if value_str[first:after] != '*' * (after - first):
raise ValueError(
_format("Asterisks in {0} field of CIM datetime value "
"{1!A} are not consecutive: {2!A}",
field_name, dtarg, value_str))
if after != len(value_str):
raise ValueError(
_format("Asterisks in {0} field of CIM datetime value "
"{1!A} do not end at end of field: {2!A}",
field_name, dtarg, value_str))
if rep_digit is None: # pylint: disable=no-else-return
# Must be an all-asterisk field
if first != 0:
raise ValueError(
_format("Asterisks in {0} field of CIM datetime value "
"{1!A} do not start at begin of field: {2!A}",
field_name, dtarg, value_str))
return min_value
else:
value_str = value_str.replace('*', rep_digit)
# Because the pattern and the asterisk replacement mechanism already
# ensure only decimal digits, we expect the integer conversion to
# always succeed.
value = int(value_str)
return value
def _to_str(self, value, field_begin, field_len):
"""
Convert value (int) to a field string, considering precision.
"""
value_str = '{0:0{1}d}'.format(value, field_len)
if self.precision is not None and \
self.precision < field_begin + field_len:
# field is partly or completely affected by precision
# -> replace insignificant digits with asterisks
precision_index = max(0, self.precision - field_begin)
value_str = value_str[:precision_index] + \
'*' * (field_len - precision_index)
return value_str
@property
def minutes_from_utc(self):
"""
The timezone offset of this point in time object as +/- minutes from
UTC.
A positive value of the timezone offset indicates minutes east of UTC,
and a negative value indicates minutes west of UTC.
0, if this object represents a time interval.
"""
offset = 0
if self.__datetime is not None and \
self.__datetime.utcoffset() is not None:
offset = self.__datetime.utcoffset().seconds / 60
if self.__datetime.utcoffset().days == -1:
offset = -((60 * 24) - offset)
return int(offset)
@property
def datetime(self):
"""
The point in time represented by this object, as a
:class:`py:datetime.datetime` object.
`None` if this object represents a time interval.
"""
return self.__datetime
@property
def timedelta(self):
"""
The time interval represented by this object, as a
:class:`py:datetime.timedelta` object.
`None` if this object represents a point in time.
"""
return self.__timedelta
@property
def precision(self):
"""
Precision of the time interval or point in time represented by this
object, if the datetime input string contained asterisk characters.
The precision is the 0-based index of the first asterisk character in
the datetime input string, or `None` if there were no asterisk
characters. For example, the precision of the timestamp value
"201809121230**.******+000" is 12.
"""
return self.__precision
@property
def is_interval(self):
"""
A boolean indicating whether this object represents a time interval
(`True`) or a point in time (`False`).
"""
return self.__timedelta is not None
@staticmethod
def get_local_utcoffset():
"""
Return the timezone offset of the current local timezone as +/- minutes
from UTC.
A positive value indicates minutes east of UTC, and a negative
value indicates minutes west of UTC.
"""
utc = datetime.utcnow()
local = datetime.now()
if local < utc:
return - int(float((utc - local).seconds) / 60 + .5)
return int(float((local - utc).seconds) / 60 + .5)
@classmethod
def now(cls, tzi=None):
"""
Factory method that returns a new :class:`~pywbem.CIMDateTime` object
representing the current date and time.
The optional timezone information is used to convert the CIM datetime
value into the desired timezone. That does not change the point in time
that is represented by the value, but it changes the value of the
``hhmmss`` components of the CIM datetime value to compensate for
changes in the timezone offset component.
Parameters:
tzi (:class:`~pywbem.MinutesFromUTC`):
Timezone information. `None` means that the current local timezone
is used.
Returns:
A new :class:`~pywbem.CIMDateTime` object representing the current
date and time.
"""
if tzi is None:
tzi = MinutesFromUTC(cls.get_local_utcoffset())
return cls(datetime.now(tzi))
@classmethod
def fromtimestamp(cls, ts, tzi=None):
# pylint: disable=invalid-name
"""
Factory method that returns a new :class:`~pywbem.CIMDateTime` object
from a POSIX timestamp value and optional timezone information.
A POSIX timestamp value is the number of seconds since "the epoch",
i.e. 1970-01-01 00:00:00 UTC. Thus, a POSIX timestamp value is
unambiguous w.r.t. the timezone, but it is not timezone-aware.
The optional timezone information is used to convert the CIM datetime
value into the desired timezone. That does not change the point in time
that is represented by the value, but it changes the value of the
``hhmmss`` components of the CIM datetime value to compensate for
changes in the timezone offset component.
Parameters:
ts (:term:`integer`):
POSIX timestamp value.
tzi (:class:`~pywbem.MinutesFromUTC`):
Timezone information. `None` means that the current local timezone
is used.
Returns:
A new :class:`~pywbem.CIMDateTime` object representing the
specified point in time.
"""
if tzi is None:
tzi = MinutesFromUTC(cls.get_local_utcoffset())
return cls(datetime.fromtimestamp(ts, tzi))
def __str__(self):
"""
Return a string representing the object in CIM datetime format.
"""
if self.is_interval: # pylint: disable=no-else-return
days = self.timedelta.days
hours = self.timedelta.seconds // 3600
sec_in_hour = self.timedelta.seconds - hours * 3600
minutes = sec_in_hour // 60
seconds = sec_in_hour - minutes * 60
microsecs = self.timedelta.microseconds
days_str = self._to_str(days, 0, 8)
hours_str = self._to_str(hours, 8, 2)
minutes_str = self._to_str(minutes, 10, 2)
seconds_str = self._to_str(seconds, 12, 2)
microsecs_str = self._to_str(microsecs, 15, 6)
ret_str = '{0}{1}{2}{3}.{4}:000'.format(
days_str, hours_str, minutes_str, seconds_str,
microsecs_str)
return ret_str
else: # timestamp
offset = self.minutes_from_utc
sign = '+'
if offset < 0:
sign = '-'
offset = -offset
year = self.datetime.year
month = self.datetime.month
day = self.datetime.day
hour = self.datetime.hour
minute = self.datetime.minute
second = self.datetime.second
microsec = self.datetime.microsecond
year_str = self._to_str(year, 0, 4)
month_str = self._to_str(month, 4, 2)
day_str = self._to_str(day, 6, 2)
hour_str = self._to_str(hour, 8, 2)
minute_str = self._to_str(minute, 10, 2)
second_str = self._to_str(second, 12, 2)
microsec_str = self._to_str(microsec, 15, 6)
ret_str = '{0}{1}{2}{3}{4}{5}.{6}{7}{8:03d}'.format(
year_str, month_str, day_str, hour_str, minute_str,
second_str, microsec_str, sign, offset)
return ret_str
def __repr__(self):
"""
Return a string representation suitable for debugging.
"""
return _format(
"CIMDateTime("
"cimtype={s.cimtype!A}, "
"datetime={s.datetime!A}, "
"timedelta={s.timedelta!A}, "
"precision={s.precision!A}), "
"minutes_from_utc={s.minutes_from_utc!A})",
s=self)
def __getstate__(self):
"" # Avoids docstring to be inherited
return str(self)
def __setstate__(self, arg):
"" # Avoids docstring to be inherited
self.__init__(arg)
def __eq__(self, other):
"""
Equality test function for two :class:`~pywbem.CIMDateTime` objects.
The equality is based on their public attributes.
Note that a :class:`~pywbem.CIMDateTime` object represents either a
time interval or a point in time but never both. Therefore, an object
representing an interval is never equal to an object representing a
point in time.
Returns `False` if the `other` object is not a
:class:`~pywbem.CIMDateTime` object.
"""
if self is other:
return True
if not isinstance(other, CIMDateTime):
return False
return (_eq_item(self.datetime, other.datetime) and
_eq_item(self.timedelta, other.timedelta))
def __hash__(self):
"""
Return a hash value based on the public attributes of this class.
Because these attributes are not modifiable, objects of this class are
:term:`hashable` (and not just :term:`unchanged-hashable`).
"""
hashes = (
_hash_item(self.datetime),
_hash_item(self.timedelta),
# The 'is_interval' and 'minutes_from_utc' attributes are not used
# for hash value calculation because they are derived attributes.
)
return hash(hashes)
# CIM integer types
class CIMInt(CIMType, _Longint):
"""
Base type for CIM integer data types. Derived from :class:`~pywbem.CIMType`
and :class:`py:int` (for Python 3) or :class:`py:long` (for Python 2).
This class has a concept of a valid range for the represented integer,
based upon the capability of the CIM data type as defined in
:term:`DSP0004`. The additional constraints defined by possible MinValue
or MaxValue qualifiers are not taken into account at this level.
The valid value range is enforced when an instance of a subclass of this
class (e.g. :class:`~pywbem.Uint8`) is created. Values outside of the
valid range raise a :exc:`ValueError`.
The enforcement of the valid value range can be disabled via the
configuration variable :data:`~pywbem.config.ENFORCE_INTEGER_RANGE`.
Two objects of subclasses of this base class compare equal if their numeric
values compare equal. Objects of this class are immutable and
:term:`hashable`, with the hash value being based on its numeric value.
Instances of subclasses of this class can be initialized with the usual
input arguments supported by :term:`integer`, for example:
::
>>> pywbem.Uint8(42)
Uint8(cimtype='uint8', 42)
>>> pywbem.Uint8('42')
Uint8(cimtype='uint8', 42)
>>> pywbem.Uint8('2A', 16)
Uint8(cimtype='uint8', 42)
>>> pywbem.Uint8('100', 16)
Traceback (most recent call last):
. . .
ValueError: Integer value 256 is out of range for CIM datatype uint8
>>> pywbem.Uint8(100, 10)
Traceback (most recent call last):
. . .
TypeError: int() can't convert non-string with explicit base
"""
__slots__ = []
#: The minimum valid value for the integer, according to the capabilities
#: of its CIM data type. See :ref:`CIM data types` for a list of CIM
#: integer data types.
minvalue = None
#: The maximum valid value for the integer, according to the capabilities
#: of its CIM data type. See :ref:`CIM data types` for a list of CIM
#: integer data types.
maxvalue = None
def __new__(cls, *args, **kwargs):
"" # Avoids docstring to be inherited
# Python 3.7 removed support for passing the value for int() as a
# keyword argument named 'x'. It now needs to be passed as a positional
# argument. The testclient test case definitions rely on a keyword
# argument, so we now transform the keyword arg into a positional
# arg.
if 'x' in kwargs:
args = list(*args) # args is passed as a tuple
args.append(kwargs.pop('x'))
value = _Longint(*args, **kwargs)
if ENFORCE_INTEGER_RANGE:
if value > cls.maxvalue or value < cls.minvalue:
raise ValueError(
_format("Integer value {0} is out of range for CIM "
"datatype {1}", value, cls.cimtype))
# The value needs to be processed here, because int/long is immutable
return super(CIMInt, cls).__new__(cls, *args, **kwargs)
# Note: __str__() is added later, for Python 3.
def __repr__(self):
"""
Return a string representation suitable for debugging.
"""
return _format(
"{s.__class__.__name__}("
"cimtype={s.cimtype!A}, "
"minvalue={s.minvalue}, " # Avoid long indicator 'L' in Python 2
"maxvalue={s.maxvalue}, " # Avoid long indicator 'L' in Python 2
"{s})",
s=self)
class Uint8(CIMInt):
"""
A value of CIM data type uint8. Derived from :class:`~pywbem.CIMInt`.
For details on CIM integer data types, see :class:`~pywbem.CIMInt`.
"""
__slots__ = []
#: The name of the CIM datatype
cimtype = 'uint8'
#: The minimum valid value for the CIM datatype
minvalue = 0
#: The maximum valid value for the CIM datatype
maxvalue = 2**8 - 1
class Sint8(CIMInt):
"""
A value of CIM data type sint8. Derived from :class:`~pywbem.CIMInt`.
For details on CIM integer data types, see :class:`~pywbem.CIMInt`.
"""
__slots__ = []
#: The name of the CIM datatype
cimtype = 'sint8'
#: The minimum valid value for the CIM datatype
minvalue = -2 ** (8 - 1)
#: The maximum valid value for the CIM datatype
maxvalue = 2 ** (8 - 1) - 1
class Uint16(CIMInt):
"""
A value of CIM data type uint16. Derived from :class:`~pywbem.CIMInt`.
For details on CIM integer data types, see :class:`~pywbem.CIMInt`.
"""
__slots__ = []
#: The name of the CIM datatype
cimtype = 'uint16'
#: The minimum valid value for the CIM datatype
minvalue = 0
#: The maximum valid value for the CIM datatype
maxvalue = 2**16 - 1
class Sint16(CIMInt):
"""
A value of CIM data type sint16. Derived from :class:`~pywbem.CIMInt`.
For details on CIM integer data types, see :class:`~pywbem.CIMInt`.
"""
__slots__ = []
#: The name of the CIM datatype
cimtype = 'sint16'
#: The minimum valid value for the CIM datatype
minvalue = -2 ** (16 - 1)
#: The maximum valid value for the CIM datatype
maxvalue = 2 ** (16 - 1) - 1
class Uint32(CIMInt):
"""
A value of CIM data type uint32. Derived from :class:`~pywbem.CIMInt`.