-
Notifications
You must be signed in to change notification settings - Fork 26
/
_tupleparse.py
2369 lines (1822 loc) · 80.2 KB
/
_tupleparse.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: Martin Pool <mbp@hp.com>
# Author: Tim Potter <tpot@hp.com>
# Author: Bart Whiteley <bwhiteley@suse.de>
# Author: Ross Peoples <ross.peoples@gmail.com>
#
# pylint: disable=too-many-lines
'''Tuple parser for the XML schema representing CIM messages.
This framework is meant to add some value to the tuple-tree
representation of CIM in XML by having the following properties:
- Silently ignoring whitespace text elements
- Conversion from tuple-tree representation into a python dictionary
which can then be accessed in a readable fashion.
- Validation of the XML elements and attributes without having to
use the DTD file or any external tools.
'''
# Implementation:
#
# This works by a recursive descent down the CIM XML tupletree. As we walk
# down, we produce CIM objects representing the CIM message in digested form.
#
# For each XML node type FOO there is one function parse_foo, which
# returns the digested form by examining a tuple tree rooted at FOO.
#
# The resulting objects are constrained to the shape of the CIM XML
# tree: if one node in XML contains another, then the corresponding
# CIM object will contain the second. However, there can be local
# transformations at each node: some levels are ommitted, some are
# transformed into lists or hashes.
#
# We try to validate that the tree is well-formed too. The validation
# is more strict than the DTD, but it is forgiving of implementation
# quirks and bugs in Pegasus.
#
# Bear in mind in the parse functions that each tupletree tuple is
# structured as
#
# tup_tree[0]: name string == name(tup_tree)
# tup_tree[1]: hash of attributes == attrs(tup_tree)
# tup_tree[2]: sequence of children == kids(tup_tree)
#
# Note: This layer is inconsistent in what it returns: In some places it
# returns tupletrees, and in others Python objects. This is likely staying that
# way in the future.
#
# Note: Some attributes have defined values or formats, such as NAME of a CLASS
# or EMBEDDEDOBJECT. This layer does not check the values or formats. CIM
# names are not being checked on the client side at all (when receiving names
# that is not worthwhile at all, and when sending names the server will
# reject them if there is a problem). Enumerated values may be checked at
# higher levels.
# This module is meant to be safe for 'import *'.
from __future__ import absolute_import
import re
import warnings
import six
from ._utils import _stacklevel_above_module, _format
from ._nocasedict import NocaseDict
from ._cim_obj import CIMInstance, CIMInstanceName, CIMClass, CIMClassName, \
CIMProperty, CIMMethod, CIMParameter, CIMQualifier, \
CIMQualifierDeclaration
from ._cim_types import CIMDateTime, type_from_name
from ._tupletree import xml_to_tupletree_sax
from ._exceptions import CIMXMLParseError
from ._warnings import ToleratedServerIssueWarning
__all__ = []
CIMXML_HEX_PATTERN = re.compile(r'^(\+|\-)?0[xX][0-9a-fA-F]+$')
NUMERIC_CIMTYPE_PATTERN = re.compile(r'^([su]int(8|16|32|64)|real(32|64))$')
def name(tup_tree):
"""
Return first (name) element of tup_tree
"""
return tup_tree[0]
def attrs(tup_tree):
"""
Return second (attributes) element of tup_tree
"""
return tup_tree[1]
def kids(tup_tree):
"""
Return a list with the child elements of tup_tree.
The child elements are represented as tupletree nodes.
Child nodes that are not XML elements (e.g. text nodes) in tup_tree are
filtered out.
"""
k = tup_tree[2]
if k is None:
return []
# pylint: disable=unidiomatic-typecheck
return [x for x in k if type(x) == tuple]
class TupleParser(object):
"""
Parser for a CIM XML tupletree.
"""
def __init__(self, conn_id=None):
"""
conn_id (:term:`connection id`): Connection ID to be used in any
exceptions that may be raised.
"""
self.conn_id = conn_id
def pcdata(self, tup_tree):
"""
Return the concatenated character data within the child nodes of a
tuple tree node, as a unicode string. Whitespace is preserved.
The child nodes must be text nodes (no element nodes).
"""
try:
data = u''.join(tup_tree[2])
except TypeError:
raise CIMXMLParseError(
_format("Element {0!A} has unexpected child elements: "
"{1!A} (allowed is only text content)",
name(tup_tree), tup_tree[2]),
conn_id=self.conn_id)
return data
# pylint: disable=too-many-arguments
def check_node(self, tup_tree, nodename, required_attrs=None,
optional_attrs=None, allowed_children=None,
allow_pcdata=False):
# pylint: disable=too-many-branches
"""
Check static local constraints on a tuple tree node.
The node must have the given nodename.
Required_attrs is a list/tuple of attribute names that must be present.
None means the same as an empty list: No attributes are required.
Optional_attrs is a list/tuple of attribute names that may be present.
None means the same as an empty list: No attributes are optional.
Present attributes is a list/tuple of attributes that are neither
required nor optional, are rejected.
If allowed_children is not None, it is a list/tuple where the node may
have children of the given types. It can be [] for nodes that may not
have any children. If it's None, no validation of the children is
performed.
If allow_pcdata is True, then non-whitespace text nodes are allowed as
children. (Whitespace text nodes are always allowed as children.)
"""
if name(tup_tree) != nodename:
raise CIMXMLParseError(
_format("Unexpected element {0!A} (expecting element {1!A})",
name(tup_tree), nodename),
conn_id=self.conn_id)
# Check we have all the required attributes, and no unexpected ones
tt_attrs = {}
if attrs(tup_tree) is not None:
tt_attrs = attrs(tup_tree).copy()
if required_attrs:
for attr in required_attrs:
if attr not in tt_attrs:
raise CIMXMLParseError(
_format("Element {0!A} missing required attribute "
"{1!A} (only has attributes {2!A})",
name(tup_tree), attr, attrs(tup_tree).keys()),
conn_id=self.conn_id)
del tt_attrs[attr]
if optional_attrs:
for attr in optional_attrs:
if attr in tt_attrs:
del tt_attrs[attr]
if tt_attrs:
raise CIMXMLParseError(
_format("Element {0!A} has invalid attribute(s) {1!A}",
name(tup_tree), tt_attrs.keys()),
conn_id=self.conn_id)
if allowed_children is not None:
invalid_children = []
for child in kids(tup_tree):
if name(child) not in allowed_children:
invalid_children.append(name(child))
if invalid_children:
if not allowed_children:
allow_txt = "no child elements are allowed"
else:
allow_txt = _format("allowed are child elements {0!A}",
allowed_children)
raise CIMXMLParseError(
_format("Element {0!A} has invalid child element(s) "
"{1!A} ({2})",
name(tup_tree), set(invalid_children), allow_txt),
conn_id=self.conn_id)
if not allow_pcdata:
for child in tup_tree[2]:
if isinstance(child, six.string_types):
if child.lstrip(' \t\n') != '':
raise CIMXMLParseError(
_format("Element {0!A} has unexpected non-blank "
"text content {1!A}",
name(tup_tree), child),
conn_id=self.conn_id)
def one_child(self, tup_tree, acceptable):
"""
Parse children of a node with exactly one child node.
acceptable is a list/tuple of acceptable child nodes
PCData is ignored.
"""
k = kids(tup_tree)
if not k:
raise CIMXMLParseError(
_format("Element {0!A} missing required child element {1!A}",
name(tup_tree), acceptable),
conn_id=self.conn_id)
if len(k) > 1:
raise CIMXMLParseError(
_format("Element {0!A} has too many child elements {1!A} "
"(allowed is one child element {2!A})",
name(tup_tree), [name(t) for t in k], acceptable),
conn_id=self.conn_id)
child = k[0]
if name(child) not in acceptable:
raise CIMXMLParseError(
_format("Element {0!A} has invalid child element {1!A} "
"(allowed is one child element {2!A})",
name(tup_tree), name(child), acceptable),
conn_id=self.conn_id)
return self.parse_any(child)
def optional_child(self, tup_tree, allowed):
"""
Parse exactly zero or one of a list/tuple of elements from the
child nodes.
"""
k = kids(tup_tree)
if not k:
return None
if len(k) == 1:
return self.one_child(tup_tree, allowed)
# len(k) > 1
raise CIMXMLParseError(
_format("Element {0!A} has too many child elements {1!A} "
"(allowed is one optional child element {2!A})",
name(tup_tree), [name(t) for t in k], allowed),
conn_id=self.conn_id)
def list_of_various(self, tup_tree, acceptable):
"""
Parse zero or more of a list/tuple of elements from the child nodes.
Each element of the list can be any type from the list of acceptable
nodes.
"""
result = []
for child in kids(tup_tree):
if name(child) not in acceptable:
raise CIMXMLParseError(
_format("Element {0!A} has invalid child element {1!A} "
"(allowed are child elements {2!A})",
name(tup_tree), name(child), acceptable),
conn_id=self.conn_id)
result.append(self.parse_any(child))
return result
def list_of_matching(self, tup_tree, matched):
"""
Parse only the children of particular types defined in the list/tuple
matched under tup_tree.
Other children are ignored rather than giving an error.
"""
result = []
for child in kids(tup_tree):
if name(child) not in matched:
continue
result.append(self.parse_any(child))
return result
def list_of_same(self, tup_tree, acceptable):
"""
Parse a list/tuple of elements from child nodes.
The children can be any of the listed acceptable types, but they
must all be the same.
"""
k = kids(tup_tree)
if not k: # empty list, consistent with list_of_various
return []
a_child = name(k[0])
if a_child not in acceptable:
raise CIMXMLParseError(
_format("Element {0!A} has invalid child element {1!A} "
"(allowed is a sequence of like elements from {2!A})",
name(tup_tree), a_child, acceptable),
conn_id=self.conn_id)
result = []
for child in k:
if name(child) != a_child:
raise CIMXMLParseError(
_format("Element {0!A} has invalid child element {1!A} "
"(sequence must have like elements {2!A})",
name(tup_tree), name(child), a_child),
conn_id=self.conn_id)
result.append(self.parse_any(child))
return result
def notimplemented(self, tup_tree):
"""
Raise exception for not implemented function.
"""
raise CIMXMLParseError(
_format("Internal Error: Parsing support for element {0!A} is "
"not implemented", name(tup_tree)),
conn_id=self.conn_id)
#
# Root element
#
def parse_cim(self, tup_tree):
"""
Parse the top level element of CIM/XML message
::
<!ELEMENT CIM (MESSAGE | DECLARATION)>
<!ATTLIST CIM
CIMVERSION CDATA #REQUIRED
DTDVERSION CDATA #REQUIRED>
"""
self.check_node(tup_tree, 'CIM', ('CIMVERSION', 'DTDVERSION'))
if not attrs(tup_tree)['CIMVERSION'].startswith('2.'):
raise CIMXMLParseError(
_format("CIMVERSION is {0}, expected 2.x.y",
attrs(tup_tree)['CIMVERSION']),
conn_id=self.conn_id)
child = self.one_child(tup_tree, ('MESSAGE', 'DECLARATION'))
return name(tup_tree), attrs(tup_tree), child
#
# Declaration elements
#
def parse_declaration(self, tup_tree):
"""
::
<!ELEMENT DECLARATION ( DECLGROUP | DECLGROUP.WITHNAME |
DECLGROUP.WITHPATH )+>
Note: We only support the DECLGROUP child, at this point.
"""
self.check_node(tup_tree, 'DECLARATION')
child = self.one_child(tup_tree, ('DECLGROUP',))
return name(tup_tree), attrs(tup_tree), child
def parse_declgroup(self, tup_tree):
"""
::
<!ELEMENT DECLGROUP ( (LOCALNAMESPACEPATH|NAMESPACEPATH)?,
QUALIFIER.DECLARATION*, VALUE.OBJECT* )>
Note: We only support the QUALIFIER.DECLARATION and VALUE.OBJECT
children, and with a multiplicity of 1, at this point.
"""
self.check_node(tup_tree, 'DECLGROUP')
child = self.one_child(tup_tree,
('QUALIFIER.DECLARATION', 'VALUE.OBJECT'))
return name(tup_tree), attrs(tup_tree), child
#
# Object value elements
#
def parse_value(self, tup_tree):
"""
Parse a VALUE element and return its text content as a unicode string.
Whitespace is preserved.
The conversion of the text representation of the value to a CIM data
type object requires CIM type information which is not available on the
VALUE element and therefore will be done when parsing higher level
elements that have that information.
::
<!ELEMENT VALUE (#PCDATA)>
"""
self.check_node(tup_tree, 'VALUE', (), (), (), allow_pcdata=True)
return self.pcdata(tup_tree)
def parse_value_array(self, tup_tree):
"""
Parse a VALUE.ARRAY element and return the items in the array as a list
of unicode strings, or None for NULL items. Whitespace is preserved.
::
<!ELEMENT VALUE.ARRAY (VALUE | VALUE.NULL)*>
"""
self.check_node(tup_tree, 'VALUE.ARRAY')
children = self.list_of_various(tup_tree, ('VALUE', 'VALUE.NULL'))
return children
def parse_value_reference(self, tup_tree):
"""
Parse a VALUE.REFERENCE element and return the instance path or class
path it represents as a CIMInstanceName or CIMClassName object,
respectively.
::
<!ELEMENT VALUE.REFERENCE (CLASSPATH | LOCALCLASSPATH | CLASSNAME |
INSTANCEPATH | LOCALINSTANCEPATH |
INSTANCENAME)>
"""
self.check_node(tup_tree, 'VALUE.REFERENCE')
child = self.one_child(tup_tree,
('CLASSPATH', 'LOCALCLASSPATH', 'CLASSNAME',
'INSTANCEPATH', 'LOCALINSTANCEPATH',
'INSTANCENAME'))
return child
def parse_value_refarray(self, tup_tree):
"""
Parse a VALUE.REFARRAY element and return the array of instance paths
or class paths it represents as a list of CIMInstanceName or
CIMClassName objects, respectively.
::
<!ELEMENT VALUE.REFARRAY (VALUE.REFERENCE | VALUE.NULL)*>
"""
self.check_node(tup_tree, 'VALUE.REFARRAY')
children = self.list_of_various(tup_tree,
('VALUE.REFERENCE', 'VALUE.NULL'))
return children
def parse_value_object(self, tup_tree):
"""
::
<!ELEMENT VALUE.OBJECT (CLASS | INSTANCE)>
"""
self.check_node(tup_tree, 'VALUE.OBJECT')
child = self.one_child(tup_tree, ('CLASS', 'INSTANCE'))
return (name(tup_tree), attrs(tup_tree), child)
def parse_value_namedinstance(self, tup_tree):
"""
::
<!ELEMENT VALUE.NAMEDINSTANCE (INSTANCENAME, INSTANCE)>
Returns:
CIMInstance object with path set (without host or namespace).
"""
self.check_node(tup_tree, 'VALUE.NAMEDINSTANCE')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"(INSTANCENAME, INSTANCE))",
name(tup_tree), k),
conn_id=self.conn_id)
inst_path = self.parse_instancename(k[0])
instance = self.parse_instance(k[1])
instance.path = inst_path
return instance
def parse_value_instancewithpath(self, tup_tree):
"""
The VALUE.INSTANCEWITHPATH is used to define a value that comprises
a single CIMInstance with additional information that defines the
absolute path to that object.
::
<!ELEMENT VALUE.INSTANCEWITHPATH (INSTANCEPATH, INSTANCE)>
"""
self.check_node(tup_tree, 'VALUE.INSTANCEWITHPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"(INSTANCEPATH, INSTANCE))",
name(tup_tree), k),
conn_id=self.conn_id)
inst_path = self.parse_instancepath(k[0])
instance = self.parse_instance(k[1])
instance.path = inst_path
return instance
def parse_value_namedobject(self, tup_tree):
"""
::
<!ELEMENT VALUE.NAMEDOBJECT (CLASS | (INSTANCENAME, INSTANCE))>
"""
self.check_node(tup_tree, 'VALUE.NAMEDOBJECT')
k = kids(tup_tree)
len_k = len(k)
if len_k == 2:
inst_path = self.parse_instancename(k[0])
_object = self.parse_instance(k[1])
_object.path = inst_path
return (name(tup_tree), attrs(tup_tree), _object)
if len_k == 1:
_object = self.parse_class(k[0])
return (name(tup_tree), attrs(tup_tree), _object)
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting one or two child elements "
"(CLASS | (INSTANCENAME, INSTANCE)))",
name(tup_tree), k),
conn_id=self.conn_id)
# pylint: disable=invalid-name
def parse_value_objectwithlocalpath(self, tup_tree):
"""
::
<!ELEMENT VALUE.OBJECTWITHLOCALPATH ((LOCALCLASSPATH, CLASS) |
(LOCALINSTANCEPATH, INSTANCE))>
Returns:
tupletree with child item that is:
- for class-level use: a tuple(CIMClassName, CIMClass) where the
path of the CIMClass object is set (with namespace).
- for class-level use: a CIMInstance object with its path set
(with namespace).
"""
self.check_node(tup_tree, 'VALUE.OBJECTWITHLOCALPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"((LOCALCLASSPATH, CLASS) | (LOCALINSTANCEPATH, "
"INSTANCE)))", name(tup_tree), k),
conn_id=self.conn_id)
if name(k[0]) == 'LOCALCLASSPATH':
# Note: Before pywbem 0.12, CIMClass did not have a path, therefore
# classpath and class were returned as a tuple. In pywbem 0.12,
# CIMClass got a path, but they are still returned as a tuple.
class_path = self.parse_localclasspath(k[0])
klass = self.parse_class(k[1])
klass.path = class_path
_object = (class_path, klass)
else: # LOCALINSTANCEPATH
# convert tuple to CIMInstance object with path set
inst_path = self.parse_localinstancepath(k[0])
_object = self.parse_instance(k[1])
_object.path = inst_path
return (name(tup_tree), attrs(tup_tree), _object)
def parse_value_objectwithpath(self, tup_tree):
"""
::
<!ELEMENT VALUE.OBJECTWITHPATH ((CLASSPATH, CLASS) |
(INSTANCEPATH, INSTANCE))>
Returns:
tupletree with child item that is:
- for class-level use: a tuple(CIMClassName, CIMClass) where the
path of the CIMClass object is set (with namespace and host).
- for class-level use: a CIMInstance object with its path set
(with namespace and host).
"""
self.check_node(tup_tree, 'VALUE.OBJECTWITHPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"((CLASSPATH, CLASS) | (INSTANCEPATH, INSTANCE)))",
name(tup_tree), k),
conn_id=self.conn_id)
if name(k[0]) == 'CLASSPATH':
# Note: Before pywbem 0.12, CIMClass did not have a path, therefore
# classpath and class were returned as a tuple. In pywbem 0.12,
# CIMClass got a path, but they are still returned as a tuple.
class_path = self.parse_classpath(k[0])
klass = self.parse_class(k[1])
klass.path = class_path
_object = (class_path, klass)
else: # INSTANCEPATH
# convert tuple to CIMInstance object with path set
inst_path = self.parse_instancepath(k[0])
_object = self.parse_instance(k[1])
_object.path = inst_path
return (name(tup_tree), attrs(tup_tree), _object)
def parse_value_null(self, tup_tree):
"""
Parse a VALUE.NULL element and return None.
::
<!ELEMENT VALUE.NULL EMPTY>
"""
self.check_node(tup_tree, 'VALUE.NULL', (), (), ())
return None
#
# Object naming and locating elements
#
def parse_namespacepath(self, tup_tree):
"""
Parse a NAMESPACEPATH element and return the host and namespace it
represents as a tuple (host, namespace).
::
<!ELEMENT NAMESPACEPATH (HOST, LOCALNAMESPACEPATH)>
"""
self.check_node(tup_tree, 'NAMESPACEPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"(HOST, LOCALNAMESPACEPATH))", name(tup_tree), k),
conn_id=self.conn_id)
host = self.parse_host(k[0])
namespace = self.parse_localnamespacepath(k[1])
return (host, namespace)
def parse_localnamespacepath(self, tup_tree):
"""
Parse a LOCALNAMESPACEPATH element and return the namespace it
represents as a unicode string.
The namespace is formed by joining the namespace components (one from
each NAMESPACE child element) with a slash (e.g. to "root/cimv2").
::
<!ELEMENT LOCALNAMESPACEPATH (NAMESPACE+)>
"""
self.check_node(tup_tree, 'LOCALNAMESPACEPATH', (), (), ('NAMESPACE',))
if not kids(tup_tree):
raise CIMXMLParseError(
_format("Element {0!A} missing child elements (expecting one "
"or more child elements 'NAMESPACE')", name(tup_tree)),
conn_id=self.conn_id)
# self.list_of_various() has the same effect as self.list_of_same()
# when used with a single allowed child element, but is a little
# faster.
ns_list = self.list_of_various(tup_tree, ('NAMESPACE',))
return u'/'.join(ns_list)
def parse_host(self, tup_tree):
"""
Parse a HOST element and return its text content as a unicode string.
::
<!ELEMENT HOST (#PCDATA)>
"""
self.check_node(tup_tree, 'HOST', (), (), (), allow_pcdata=True)
return self.pcdata(tup_tree)
def parse_namespace(self, tup_tree):
"""
Parse a NAMESPACE element and return the namespace component it
represents (e.g. "root") as a unicode string.
::
<!ELEMENT NAMESPACE EMPTY>
<!ATTLIST NAMESPACE
%CIMName;>
"""
self.check_node(tup_tree, 'NAMESPACE', ('NAME',), (), ())
return attrs(tup_tree)['NAME']
def parse_classpath(self, tup_tree):
"""
Parse a CLASSPATH element and return the class path it represents as a
CIMClassName object.
::
<!ELEMENT CLASSPATH (NAMESPACEPATH, CLASSNAME)>
"""
self.check_node(tup_tree, 'CLASSPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"(NAMESPACEPATH, CLASSNAME))", name(tup_tree), k),
conn_id=self.conn_id)
host, namespace = self.parse_namespacepath(k[0])
class_path = self.parse_classname(k[1])
class_path.host = host
class_path.namespace = namespace
return class_path
def parse_localclasspath(self, tup_tree):
"""
Parse a LOCALCLASSPATH element and return the class path it represents
as a CIMClassName object.
::
<!ELEMENT LOCALCLASSPATH (LOCALNAMESPACEPATH, CLASSNAME)>
"""
self.check_node(tup_tree, 'LOCALCLASSPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"(LOCALNAMESPACEPATH, CLASSNAME))", name(tup_tree), k),
conn_id=self.conn_id)
namespace = self.parse_localnamespacepath(k[0])
class_path = self.parse_classname(k[1])
class_path.namespace = namespace
return class_path
def parse_classname(self, tup_tree):
"""
Parse a CLASSNAME element and return the class path it represents as a
CIMClassName object.
::
<!ELEMENT CLASSNAME EMPTY>
<!ATTLIST CLASSNAME
%CIMName;>
Returns:
CIMClassName object (without namespace or host)
"""
self.check_node(tup_tree, 'CLASSNAME', ('NAME',), (), ())
classname = attrs(tup_tree)['NAME']
class_path = CIMClassName(classname)
return class_path
def parse_instancepath(self, tup_tree):
"""
Parse an INSTANCEPATH element and return the instance path it
represents as a CIMInstanceName object.
::
<!ELEMENT INSTANCEPATH (NAMESPACEPATH, INSTANCENAME)>
"""
self.check_node(tup_tree, 'INSTANCEPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"(NAMESPACEPATH, INSTANCENAME))", name(tup_tree), k),
conn_id=self.conn_id)
host, namespace = self.parse_namespacepath(k[0])
inst_path = self.parse_instancename(k[1])
inst_path.host = host
inst_path.namespace = namespace
return inst_path
def parse_localinstancepath(self, tup_tree):
"""
Parse a LOCALINSTANCEPATH element and return the instance path it
represents as a CIMInstanceName object.
::
<!ELEMENT LOCALINSTANCEPATH (LOCALNAMESPACEPATH, INSTANCENAME)>
"""
self.check_node(tup_tree, 'LOCALINSTANCEPATH')
k = kids(tup_tree)
if len(k) != 2:
raise CIMXMLParseError(
_format("Element {0!A} has invalid number of child elements "
"{1!A} (expecting two child elements "
"(LOCALNAMESPACEPATH, INSTANCENAME))",
name(tup_tree), k),
conn_id=self.conn_id)
namespace = self.parse_localnamespacepath(k[0])
inst_path = self.parse_instancename(k[1])
inst_path.namespace = namespace
return inst_path
def parse_instancename(self, tup_tree):
"""
Parse an INSTANCENAME element and return the instance path it
represents as a CIMInstanceName object.
::
<!ELEMENT INSTANCENAME (KEYBINDING* | KEYVALUE? |
VALUE.REFERENCE?)>
<!ATTLIST INSTANCENAME
%ClassName;>
"""
self.check_node(tup_tree, 'INSTANCENAME', ('CLASSNAME',))
k = kids(tup_tree)
if not k:
# probably not ever going to see this, but it's valid
# according to the grammar
return CIMInstanceName(attrs(tup_tree)['CLASSNAME'], {})
kid0 = k[0]
k0_name = name(kid0)
classname = attrs(tup_tree)['CLASSNAME']
if k0_name in ('KEYVALUE', 'VALUE.REFERENCE'):
if len(k) != 1:
raise CIMXMLParseError(
_format("Element {0!A} has more than one child element "
"{1!A} (expecting child elements "
"(KEYBINDING* | KEYVALUE? | VALUE.REFERENCE?))",
name(tup_tree), k0_name),
conn_id=self.conn_id)
val = self.parse_any(kid0)
return CIMInstanceName(classname, {None: val})
if k0_name == 'KEYBINDING':
kbs = {}
# self.list_of_various() has the same effect as self.list_of_same()
# when used with a single allowed child element, but is a little
# faster.
for key_bind in self.list_of_various(tup_tree, ('KEYBINDING',)):
kbs.update(key_bind)
return CIMInstanceName(classname, kbs)
raise CIMXMLParseError(
_format("Element {0!A} has invalid child elements {1!A} "
"(expecting child elements "
"(KEYBINDING* | KEYVALUE? | VALUE.REFERENCE?))",
name(tup_tree), k),
conn_id=self.conn_id)
def parse_objectpath(self, tup_tree):