-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbase.py
More file actions
136 lines (103 loc) · 3.37 KB
/
base.py
File metadata and controls
136 lines (103 loc) · 3.37 KB
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
# Copyright contributors to the python-itoolkit project
# SPDX-License-Identifier: MIT
import xml.dom.minidom
class iBase: # noqa N801
"""IBM i XMLSERVICE call addable operation(s).
Args:
iopt (dict): user options (see descendents)
idft (dict): default options (see descendents)
Example:
Calling a CL command and shell script::
itransport = DirectTransport()
itool = iToolKit()
itool.add(iCmd('chglibl', 'CHGLIBL LIBL(XMLSERVICE)'))
itool.add(iSh('ps', 'ps -ef'))
... so on ...
itool.call(itransport)
Notes:
iopt (dict): XMLSERVICE elements, attributes and values
'_tag' - element <x>
'value' - value <x>value</x>
'id' - attribute <x var='ikey'>
'children' - iBase children
... many more idft + iopt ...
'error' - <x 'error'='fast'>
"""
def __init__(self, iopt={}, idft={}):
# xml defaults
self.opt = idft.copy()
self.opt.update(iopt)
# my children objects
self.opt['_children'] = []
def add(self, obj):
"""Additional mini dom xml child nodes.
Args:
obj (iBase) : additional child object
Example:
Adding a program::
itool = iToolKit()
itool.add(
iPgm('zzcall','ZZCALL') <--- child of iToolkit
.addParm(iData('INCHARA','1a','a')) <--- child of iPgm
)
"""
self.opt['_children'].append(obj)
def xml_in(self):
"""Return XML string of collected mini dom xml child nodes.
Returns:
XML (str)
"""
return self.make().toxml()
def make(self, doc=None):
"""Assemble coherent mini dom xml, including child nodes.
Returns:
xml.dom.minidom (obj)
"""
if not doc:
doc = xml.dom.minidom.Document()
return self._make(doc)
def _make(self, doc):
tag = doc.createElement(self.opt['_tag'])
tag.setAttribute('var', self.opt['_id'])
for attr, value in self.opt.items():
if not attr.startswith('_'):
tag.setAttribute(attr, str(value))
if len(self.opt['_value']):
value = doc.createCDATASection(self.opt['_value'])
tag.appendChild(value)
for child in self.opt['_children']:
tag.appendChild(child.make(doc))
return tag
class iXml(iBase): # noqa N801
"""
IBM i XMLSERVICE raw xml input.
Args:
ixml (str): custom XML for XMLSERVICE operation.
Example:
iXml("<cmd>CHGLIBL LIBL(XMLSERVICE)</cmd>")
iXml("<sh>ls /tmp</sh>")
Returns:
iXml (obj)
Notes:
Not commonly used, but ok when other classes fall short.
"""
def __init__(self, ixml):
super().__init__()
self.xml_body = ixml
def add(self, obj):
"""add input not allowed.
Returns:
raise except
"""
raise
def _make(self, _doc):
"""Assemble coherent mini dom xml.
Returns:
xml.dom.minidom (obj)
"""
try:
node = xml.dom.minidom.parseString(self.xml_body).firstChild
except xml.parsers.expat.ExpatError as e:
e.args += (self.xml_body,)
raise
return node