1
- # -*- coding: utf-8 -*-
2
- ##############################################################################
3
- #
4
- # Module: devServer.py
5
- #
6
- # Description:
7
- # Server socket module, which listens for the client
8
- # Based on the command from Clinet, control device which is connected
9
- # Send the response back to Clinet in JSON format (JSON Object)
10
- #
11
- # Copyright notice:
12
- # This file copyright (c) 2020 by
13
- #
14
- # MCCI Corporation
15
- # 3520 Krums Corners Road
16
- # Ithaca, NY 14850
17
- #
18
- # Released under the MCCI Corporation.
19
- #
20
- # Author:
21
- # Seenivasan V, MCCI Corporation June 2021
22
- #
23
- # Revision history:
24
- # V2.6.0 Wed Apr 20 2022 17:00:00 Seenivasan V
25
- # Module created
26
- ##############################################################################
27
- # Built-in imports
28
1
import json
29
-
30
- import sys
31
2
import os
32
3
33
- # Own modules
34
- import xml .dom .minidom
35
-
36
- DOM_ELEMENT = 1
37
- NODE_TEXT = 3
38
- DOCUMENT_TYPE = 10
39
-
40
- # Key Macros
41
- BUSNAME = "_name"
42
- DEVNAME = "device_name_key"
43
- UID = "switch_uid_key"
44
- VENDORNAME = "vendor_name_key"
45
-
46
- NODEKEY = "_items"
47
-
48
- ##############################################################################
49
- # Utilities
50
- ##############################################################################
51
-
52
- def filterTextNodes (inpDE ):
53
- fnodes = []
54
- for elem in inpDE .childNodes :
55
- if elem .nodeType == DOM_ELEMENT :
56
- fnodes .append (elem )
57
- return fnodes
58
-
59
- def checkDataType (inpDE ):
60
- dtypeIdx = None
61
- for idx , elem in enumerate (inpDE ):
62
- if (elem .nodeName == "key" ):
63
- if (elem .firstChild .nodeValue == "_dataType" ):
64
- dtypeIdx = idx + 1
4
+ speed_tag = ['receptacle_1_tag' , 'receptacle_2_tag' , 'receptacle_3_tag' , 'receptacle_4_tag' ]
5
+ swuid_tag = 'switch_uid_key'
6
+ tbdata_tag = 'SPThunderboltDataType'
65
7
66
- dataType = inpDE [dtypeIdx ].firstChild .nodeValue
67
-
68
- if (dataType == "SPThunderboltDataType" ):
69
- return True
70
- return False
71
-
72
- def pickArrayElementFromItem (inpDE ):
73
- dtypeIdx = None
74
- for idx , elem in enumerate (inpDE ):
75
- if (elem .nodeName == "key" ):
76
- if (elem .firstChild .nodeValue == "_items" ):
77
- dtypeIdx = idx + 1
8
+ tbbus = []
78
9
79
- dataType = inpDE [dtypeIdx ].nodeName
80
- if (dataType == "array" ):
81
- return inpDE [dtypeIdx ]
82
- else :
83
- return None
10
+ finalDict = {}
84
11
85
- # Function to calculate number of buses
86
- # Number of buses = number of Dict elements in the imAE
87
- def getTotalBuses (imAE ):
88
- fnodes = filterTextNodes (imAE )
89
- rnodes = []
90
- for ie in fnodes :
91
- if ie .nodeName == "dict" :
92
- rnodes .append (ie )
93
- return rnodes
94
12
95
- def getBusProperties (sbe ):
96
- busparam = []
97
- busidx = []
98
- rnodes = filterTextNodes (sbe )
99
- for idx , elem in enumerate (rnodes ):
100
- if (elem .nodeName == "key" ):
101
- if (elem .firstChild .nodeValue == BUSNAME ):
102
- busidx .append (idx + 1 )
103
- elif (elem .firstChild .nodeValue == DEVNAME ):
104
- busidx .append (idx + 1 )
105
- elif (elem .firstChild .nodeValue == UID ):
106
- busidx .append (idx + 1 )
107
- elif (elem .firstChild .nodeValue == VENDORNAME ):
108
- busidx .append (idx + 1 )
109
- for idx in busidx :
110
- busparam .append (rnodes [idx ].firstChild .nodeValue )
111
- return busparam
13
+ def grabData (gbus , finalDict ):
14
+ childs = None
15
+ if swuid_tag in gbus :
16
+ bdict = {}
17
+ bdict ['name' ] = gbus ['_name' ]
18
+ bdict ['deviceName' ] = gbus ['device_name_key' ]
19
+ bdict ['vendorName' ] = gbus ['vendor_name_key' ]
112
20
113
- def getTotalBusNames (tbe ):
114
- busparam = []
115
- for ide in tbe :
116
- busparam .append (getBusProperties (ide ))
117
- return busparam
118
-
119
- def getBusHubPtrs (sbe ):
120
- sbushubptrs = []
121
- busidx = None
122
- hae = None
123
- rnodes = filterTextNodes (sbe )
124
- for idx , elem in enumerate (rnodes ):
125
- if (elem .nodeName == "key" ):
126
- if (elem .firstChild .nodeValue == NODEKEY ):
127
- busidx = idx + 1
128
- if (busidx != None ):
129
- dataType = rnodes [busidx ].nodeName
130
- if (dataType == "array" ):
131
- hae = rnodes [busidx ]
132
- if (hae != None ):
133
- hrnodes = filterTextNodes (hae )
134
- for elem in hrnodes :
135
- if elem .nodeName == "dict" :
136
- sbushubptrs .append (elem )
21
+ for stag in speed_tag :
22
+ if stag in gbus :
23
+ bdict ['speed' ] = gbus [stag ]['current_speed_key' ]
24
+ # print(stag)
25
+ break
26
+
27
+ if '_items' in gbus .keys ():
28
+ childs = gbus ['_items' ]
29
+ nchild = []
30
+ for ielem in childs :
31
+ nchild .append (ielem [swuid_tag ])
32
+ bdict ['child' ] = nchild
33
+ else :
34
+ bdict ['child' ] = []
35
+ childs = []
36
+ finalDict [gbus [swuid_tag ]] = bdict
137
37
138
- return sbushubptrs
139
-
140
- def getTotalBusHubPtrs (tbe ):
141
- tbushubptrs = []
142
- for ide in tbe :
143
- tbushubptrs .append (getBusHubPtrs (ide ))
144
- return tbushubptrs
145
-
146
- def fetchNameAndItem (she ):
147
- nhe = None
148
- chn = None
149
- nameidx = None
150
- itemidx = None
151
- for idx , elem in enumerate (she ):
152
- if (elem .nodeName == "key" ):
153
- if (elem .firstChild .nodeValue == BUSNAME ):
154
- nameidx = idx + 1
155
- elif (elem .firstChild .nodeValue == NODEKEY ):
156
- itemidx = idx + 1
157
- elif (elem .firstChild .nodeValue == UID ):
158
- uididx = idx + 1
159
-
160
- chn = she [nameidx ].firstChild .nodeValue
161
- uid = she [uididx ].firstChild .nodeValue
162
- nodename = chn + "," + uid
163
- if (itemidx != None ):
164
- nhe = she [itemidx ]
165
- return nodename , nhe
166
-
167
- def getDictNodeFromArray (inpDN ):
168
- dnode = None
169
- rnodes = filterTextNodes (inpDN )
170
- for idx , elem in enumerate (rnodes ):
171
- if (elem .nodeName == "dict" ):
172
- dnode = elem
173
- return dnode
174
-
175
- def getBusHubs (sbhptr ):
176
- sbushubs = []
177
- if (len (sbhptr ) == 0 ):
178
- return sbushubs
38
+ kilist = list (gbus .keys ())
39
+ for ikey in kilist :
40
+ if ikey != "_items" :
41
+ del gbus [ikey ]
179
42
180
- # Hub pointer is a Dict Element which may contains nested hubs and ports
181
- # In which there should be a name key and item key which is optional
182
- # if name key found, append the name string to the list
183
- # then if item key found do the nested operation until no item key found
43
+ return childs
184
44
185
- # Remove the Text Nodes
186
- trhptr = []
187
- for elem in sbhptr :
188
- trhptr .append (filterTextNodes (elem ))
189
45
190
- hname , nitem = fetchNameAndItem (trhptr [0 ])
191
-
192
- # Fetch Name and Item
193
- for she in trhptr :
194
- hname , nitem = fetchNameAndItem (she )
195
- sbushubs .append (hname )
196
- while (nitem != None ):
197
- rnodes = getDictNodeFromArray (nitem )
198
- rrnodes = filterTextNodes (rnodes )
199
- hname , nitem = fetchNameAndItem (rrnodes )
200
- sbushubs .append (hname )
201
- return sbushubs
46
+ def handleBusTree (gbus , tbdict ):
47
+ doflg = True
48
+ while doflg :
49
+ gchild = grabData (gbus , tbdict )
50
+ if gchild == None :
51
+ doflg = False
52
+ else :
53
+ # print(len(gchild))
54
+ if len (gchild ) > 0 :
55
+ gbus = gchild [0 ]
56
+ else :
57
+ doflg = False
58
+
202
59
203
60
def scan_tb ():
204
- buses = [] # contains Dict Elements
205
- busNames = [] # [["bus0 busname", "deviceName", "UID", "Vendor Name"],["bus1 busname", "deviceName", "UID", "Vendor Name"]]
206
- busHubPtrs = [] # [[bus_0 hub1, hub2,...,hubn],[bus_1 hub1, hub2,...,hubn]]
207
- busHubs = []
208
-
61
+ tbbus = []
209
62
tbdict = {}
210
-
211
- xmldoc = os .popen ("system_profiler -xml SPThunderboltDataType" )
212
- # Use the parse() function to load and parse an XML file
213
- xmlobj = xml .dom .minidom .parseString (xmldoc .read ())
214
63
215
- childobj = None
216
- child = xmlobj .childNodes
217
- childobj = child [1 ]
64
+ # f = open('myspeed.json')
65
+ # mytb = json.load(f)
218
66
219
- # Plist should contains 2 DOM text nodes and one array element
220
- fnodes = filterTextNodes (childobj )
221
- mAE = fnodes [0 ]
222
-
223
- # This DOM Element (array) contains 2 DOM text nodes and one DOM Element (dict)
224
- fnodes = filterTextNodes (mAE )
225
- mDE = fnodes [0 ]
226
-
227
- fmDE = filterTextNodes (mDE )
228
- if checkDataType (fmDE ):
229
-
230
- imAE = pickArrayElementFromItem (fmDE )
231
-
232
- buses = getTotalBuses (imAE )
233
- busNames = getTotalBusNames (buses )
234
-
235
- busHubPtrs = getTotalBusHubPtrs (buses )
236
-
237
- for hptr in busHubPtrs :
238
- busHubs .append (getBusHubs (hptr ))
67
+ xmldoc = os .popen ("system_profiler -json SPThunderboltDataType" )
68
+ mytb = json .load (xmldoc )
69
+ # Use the parse() function to load and parse an XML file
70
+ # xmlobj = xml.dom.minidom.parseString(xmldoc.read())
239
71
240
- fbnames = []
241
- for bus in busNames :
242
- try :
243
- fbnames .append (bus [0 ])
244
- except :
245
- pass
72
+ if tbdata_tag in mytb .keys ():
73
+ tbbuses = mytb [tbdata_tag ]
246
74
247
- for idx , hubs in enumerate (busHubs ):
248
- try :
249
- tbdict [fbnames [idx ]] = hubs
250
- except :
251
- pass
252
- else :
253
- print ("Thunderbolt Data type fail" )
75
+ for tbus in tbbuses :
76
+ tbbus .append (tbus )
254
77
78
+ for i in range (len (tbbus )):
79
+ handleBusTree (tbbus [i ], tbdict )
80
+ # print(tbdict)
255
81
return tbdict
0 commit comments