lucy-python-script-2030 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pypi/Scripts/wmiweb.py ADDED
@@ -0,0 +1,255 @@
1
+ from cgi import escape
2
+ import threading
3
+ try:
4
+ from urllib import quote
5
+ except ImportError:
6
+ from urllib.parse import quote
7
+ from wsgiref.simple_server import make_server
8
+ from wsgiref.util import request_uri, application_uri, shift_path_info
9
+
10
+ import wmi
11
+
12
+ try:
13
+ unicode
14
+ except NameError:
15
+ unicode = str
16
+
17
+ doc = []
18
+
19
+ def link(text, computer, namespace=None, wmi_class=None):
20
+ link = '<a href="/' + quote(computer)
21
+ if namespace:
22
+ link += "/" + quote(namespace)
23
+ if wmi_class:
24
+ link += "/" + quote(wmi_class)
25
+ return link + '">' + escape(text) + '</a>'
26
+
27
+ def start_doc(title):
28
+ doc[:] = []
29
+ doc.append("""
30
+ <html>
31
+ <head>
32
+ <title>%(title)s</title>
33
+ <style>
34
+ body {font-family : Verdana, sans-serif; font-size : 84%%; margin : 3em;}
35
+ table.items {padding-left : 30px;}
36
+ li, td {font-family : "Courier New", monospace;}
37
+ td {padding-right : 1em; font-size : 84%%;}
38
+ h1, h2, h3 {font-family : Tahoma, sans-serif;}
39
+ h2 a {text-decoration : none;}
40
+ span.tooltip {border-bottom : 1px dotted #777;}
41
+ </style>
42
+ </head>
43
+ <body>
44
+ """ % locals())
45
+
46
+ def finish_doc():
47
+ doc.append("""
48
+ </body>
49
+ </html>
50
+ """ % locals())
51
+
52
+ def doc_table(items, n_cols=3, callback=None):
53
+ n_rows, n_spare_cols = divmod(len(items), n_cols)
54
+ doc.append('<table cellspacing=0 class="items">')
55
+
56
+ for n_row in range(n_rows):
57
+ doc.append("<tr>")
58
+ for n_col in range(n_cols):
59
+ item = items[n_cols * n_col + n_row]
60
+ if callback:
61
+ item = callback(item)
62
+ doc.append("<td><li>%s</li></td>" % item)
63
+ doc.append("</tr>")
64
+
65
+ if n_spare_cols:
66
+ doc.append("<tr>")
67
+ for n_col in reversed(range(n_spare_cols)):
68
+ item = items[len(items) - 1 - n_col]
69
+ if callback:
70
+ item = callback(item)
71
+ doc.append("<td><li>%s</li></td>" % item)
72
+ doc.append("</tr>")
73
+
74
+ doc.append("</table>")
75
+
76
+ def doc_breadcrumbs(computer, namespace, wmi_class=None):
77
+ doc.append('<p class="breadcrumbs">')
78
+ doc.append('%s &rarr; %s' % (link(computer, computer), link(namespace, computer, namespace)))
79
+ if wmi_class:
80
+ doc.append(' &rarr; %s' % (link(wmi_class, computer, namespace, wmi_class)))
81
+ doc.append('</p>')
82
+
83
+
84
+ def doc_wmi_class(computer, namespace, wmi_class, wmi_connection):
85
+ start_doc("WMI: Class %(wmi_class)s in namespace %(namespace)s on %(computer)s" % locals())
86
+ doc_breadcrumbs(computer, namespace, wmi_class)
87
+ doc.append("<h2>%(wmi_class)s</h2>" % locals())
88
+ klass = getattr(wmi_connection, wmi_class)
89
+
90
+ def property_callback(property_name):
91
+ property = klass.wmi_property(property_name)
92
+ mapping = property.qualifiers.get("MappingStrings")
93
+ if mapping is None:
94
+ return property_name
95
+ else:
96
+ return '<span class="tooltip" title="%s">%s</span>' % (property.provenance, property_name)
97
+
98
+ doc.append("<hr>")
99
+ doc.append("<h3>Ancestors</h3>")
100
+ ancestors = klass.derivation()
101
+ if ancestors:
102
+ doc.append(" <b>:</b> ".join(link(ancestor, computer, namespace, ancestor) for ancestor in reversed(ancestors)))
103
+ else:
104
+ doc.append("<p>No ancestors</p>")
105
+
106
+ doc.append("<hr>")
107
+ doc.append("<h3>Children</h3>")
108
+ children = sorted(c.Path_.Class for c in klass._namespace.SubclassesOf(wmi_class))
109
+ if children:
110
+ doc.append('<ul>')
111
+ for child in children:
112
+ doc.append('<li>%s</li>' % link(child, computer, namespace, child))
113
+ doc.append('</ul>')
114
+ else:
115
+ doc.append('<p>No children</p>')
116
+
117
+ doc.append("<hr>")
118
+ doc.append("<h3>Associated classes</h3>")
119
+ associations = sorted(klass.associated_classes)
120
+ if associations:
121
+ doc.append("<ul>")
122
+ for association in associations:
123
+ doc.append('<li>%s</li>' % link(association, computer, namespace, association))
124
+ doc.append("</ul>")
125
+ else:
126
+ doc.append("<p>No associated classes</p>")
127
+
128
+ doc.append("<hr>")
129
+ doc.append("<h3>Properties</h3>")
130
+ properties = sorted(klass._properties)
131
+ n_properties = len(properties)
132
+ if n_properties == 0:
133
+ doc.append("<p>No properties</p>")
134
+ if 1 <= n_properties <= 10:
135
+ doc_table(properties, 1, property_callback)
136
+ elif 10 < n_properties <= 20:
137
+ doc_table(properties, 2, property_callback)
138
+ elif 20 < n_properties <= 30:
139
+ doc_table(properties, 3, property_callback)
140
+ else:
141
+ doc_table(properties, 4, property_callback)
142
+
143
+ doc.append("<hr>")
144
+ doc.append("<h3>Keys</h3>")
145
+ keys = sorted(klass.keys)
146
+ if keys:
147
+ doc.append("<ul>")
148
+ for key in keys:
149
+ doc.append("<li>%s</li>" % key)
150
+ doc.append("</ul>")
151
+ else:
152
+ doc.append("<p>No keys</p>")
153
+
154
+ doc.append("<hr>")
155
+ doc.append("<h3>Methods</h3>")
156
+ methods = sorted(klass._methods)
157
+ if methods:
158
+ doc.append("<ul>")
159
+ for m in methods:
160
+ doc.append('<li><b>%s</b><br>%s</li>' % (m, escape(str(getattr(klass, m)))))
161
+ doc.append("</ul>")
162
+ else:
163
+ doc.append("<p>No methods</p>")
164
+
165
+ doc.append("<hr>")
166
+ doc.append("<h3>Qualifiers</h3>")
167
+ qualifiers = sorted(klass.qualifiers.items())
168
+ if qualifiers:
169
+ doc.append("<ul>")
170
+ for q in qualifiers:
171
+ doc.append("<li><b>%s</b>: %s</li>" % q)
172
+ doc.append("</ul>")
173
+ else:
174
+ doc.append("<p>No qualifiers</p>")
175
+
176
+ finish_doc()
177
+
178
+ def doc_namespace(computer, namespace, wmi_connection):
179
+ start_doc("WMI: Namespace %(namespace)s on %(computer)s" % locals())
180
+ doc_breadcrumbs(computer, namespace)
181
+
182
+ namespaces = namespace.split("\\")
183
+ namespace_links = []
184
+ for i, n in enumerate(namespaces):
185
+ namespace_links.append(link(n, computer, "\\".join(namespaces[:i+1])))
186
+ doc.append("<h2>%s</h2>" % "\\".join(namespace_links))
187
+
188
+ doc.append("<hr>")
189
+ subnamespaces = sorted(wmi_connection.__NAMESPACE())
190
+ doc.append("<h3>Namespaces:</h3>")
191
+ if subnamespaces:
192
+ doc.append("<ul>")
193
+ for subnamespace in subnamespaces:
194
+ name = subnamespace.Name
195
+ doc.append('<li>%s</li>' % link(name, computer, namespace + "\\" + name))
196
+ doc.append("</ul>")
197
+ else:
198
+ doc.append("<p>No namespaces</p>")
199
+
200
+ doc.append("<hr>")
201
+ subclasses = sorted(wmi_connection.classes)
202
+ doc.append("<h3>Classes:</h3>")
203
+ if subclasses:
204
+ doc.append("<ul>")
205
+ for subclass in subclasses:
206
+ doc.append('<li>%s</li>' % link(subclass, computer, namespace, subclass))
207
+ doc.append("</ul>")
208
+ else:
209
+ doc.append("<p>No classes</p>")
210
+
211
+ finish_doc()
212
+
213
+ def handle_namespace(environ, computer, namespace):
214
+ if not namespace:
215
+ wmi_connection = wmi.WMI(computer, namespace="root/cimv2")
216
+ for setting in wmi_connection.Win32_WMISetting():
217
+ namespace=setting.ASPScriptDefaultNamespace
218
+ break
219
+
220
+ wmi_connection = wmi.WMI(computer, namespace=namespace, find_classes=True)
221
+ wmi_class = shift_path_info(environ)
222
+ if wmi_class:
223
+ doc_wmi_class(computer, namespace, wmi_class, wmi_connection)
224
+ else:
225
+ doc_namespace(computer, namespace, wmi_connection)
226
+
227
+ def handle_computer(environ, computer):
228
+ handle_namespace(environ, computer, shift_path_info(environ))
229
+
230
+ def app(environ, start_response):
231
+ computer = shift_path_info(environ)
232
+ if computer == "favicon.ico":
233
+ start_response("404 Not Found", [("Content-Type", "text/plain")])
234
+ return []
235
+ elif computer:
236
+ start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")])
237
+ handle_computer(environ, computer)
238
+ return(unicode(d).encode("utf8") + unicode("\n").encode("utf8") for d in doc)
239
+ else:
240
+ start_response("301 Moved Permanently", [("Location", "/localhost"),("Content-Type", "text/plain")])
241
+ return ["Redirected to /localhost"]
242
+
243
+ PORT = 8010
244
+
245
+ def run_browser():
246
+ import os
247
+ os.startfile("http://localhost:%d" % PORT)
248
+
249
+ if __name__ == '__main__':
250
+ threading.Timer(3.0, run_browser).start()
251
+ httpd = make_server('', PORT, app)
252
+ try:
253
+ httpd.serve_forever()
254
+ except KeyboardInterrupt:
255
+ print("Shutting down gracefully...")