-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui_tkinter.py
More file actions
251 lines (208 loc) · 8.61 KB
/
gui_tkinter.py
File metadata and controls
251 lines (208 loc) · 8.61 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
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
"""
WBEM GUI for Python 2.7+ and 3.3+
------------------------------------
This module contains the functionality to create CIM-XML requests and parse CIM-XML responses. In addition,
HTTP(S) is used to provide the communications portion of WBEM.
License
-------
The MIT License (MIT)
Copyright (c) 2015 Ross Peoples <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import sys
if sys.version[0] == '2':
from Tkinter import *
import tkMessageBox as messagebox
import ttk
else:
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from wbem.cim import InvalidClass
from wbem.client import WBEMClient
# make sure to get the file's current path, even when compiled
try:
file_path = os.path.abspath(__file__)
except NameError:
file_path = os.path.abspath(sys.argv[0])
class MainApplication(ttk.Frame, object):
@staticmethod
def _pad_widgets(frame, padx=5, pady=5):
for c in frame.winfo_children():
if c.widgetName.endswith('label'):
continue
c.grid_configure(padx=padx, pady=pady)
@staticmethod
def parse_uri(uri):
if not uri or '://' not in uri:
raise ValueError('Full http(s) URI is required')
protocol, url = uri.split('://', 1)
try:
hostinfo, namespace = url.split('/', 1)
except ValueError:
hostinfo = url
namespace = 'root/cimv2'
try:
host, port = hostinfo.split(':', 1)
except ValueError:
host = hostinfo
port = 80
return dict(
https=protocol.lower() == 'https',
default_namespace=namespace.strip('/'),
hostname=host,
port=port
)
def __init__(self, parent, *args, **kwargs):
super(MainApplication, self).__init__(parent, *args, **kwargs)
# set some global options
parent.title('WBEM GUI')
parent.columnconfigure(0, weight=1)
parent.rowconfigure(0, weight=1)
# grid self
self.grid(column=0, row=0, sticky=(N, S, E, W))
# initialize container frames
self._initialize_connect_frame()
self._initialize_operations_frame()
self._initialize_results_frame()
# configure grid
self.columnconfigure(0, weight=1)
self.rowconfigure(2, weight=1)
def _initialize_connect_frame(self, row=0):
# initialize widgets
self.frm_connect = ttk.Frame(self)
self.txt_uri = ttk.Entry(self.frm_connect)
self.txt_username = ttk.Entry(self.frm_connect)
self.txt_password = ttk.Entry(self.frm_connect, show='*')
# grid widgets and labels
self.frm_connect.grid(column=0, row=row, sticky=(N, S, E, W))
ttk.Label(self.frm_connect, text='URI').grid(column=0, row=0, sticky=W)
ttk.Label(self.frm_connect, text='Username').grid(column=1, row=0, sticky=W)
ttk.Label(self.frm_connect, text='Password').grid(column=2, row=0, sticky=W)
self.txt_uri.grid(column=0, row=1, sticky=(E, W))
self.txt_username.grid(column=1, row=1, sticky=(E, W))
self.txt_password.grid(column=2, row=1, sticky=(E, W))
# configure grid
self.frm_connect.columnconfigure(0, weight=3)
self.frm_connect.columnconfigure(1, weight=1)
self.frm_connect.columnconfigure(2, weight=1)
self._pad_widgets(self.frm_connect)
def _initialize_operations_frame(self, row=1):
# initialize widgets
self.frm_operations = ttk.Frame(self)
self.val_operation = StringVar(value='EnumerateInstances')
self.cbo_operation = ttk.Combobox(
self.frm_operations, state='readonly', textvariable=self.val_operation,
values=('EnumerateInstances', 'EnumerateInstanceNames', 'GetInstance')
)
self.txt_class_or_instance = ttk.Entry(self.frm_operations)
self.btn_execute = ttk.Button(self.frm_operations, text='Execute', command=self.btn_execute_evt)
# grid widgets
self.frm_operations.grid(column=0, row=row, sticky=(N, S, E, W))
ttk.Label(self.frm_operations, text='Operation').grid(column=0, row=0, sticky=W)
ttk.Label(self.frm_operations, text='Class or instance').grid(column=0, row=2, sticky=W)
self.cbo_operation.grid(column=0, row=1, sticky=W)
self.txt_class_or_instance.grid(column=0, row=3, sticky=(E, W))
self.btn_execute.grid(column=0, row=4, sticky=E)
# configure grid
self.frm_operations.columnconfigure(0, weight=1)
self._pad_widgets(self.frm_operations)
def _initialize_results_frame(self, row=2):
# initialize widgets
self.frm_results = ttk.Frame(self)
self.txt_results = Text(self.frm_results)
self.txt_results.insert(END, 'Waiting for execution...')
# grid widgets
self.frm_results.grid(column=0, row=row, sticky=(N, S, E, W))
self.txt_results.grid(column=0, row=0, sticky=(N, S, E, W))
# configure grid
self.frm_results.columnconfigure(0, weight=1)
self.frm_results.rowconfigure(0, weight=1)
self._pad_widgets(self.frm_results)
def btn_execute_evt(self, *args):
try:
self.execute_query()
except ValueError as ex:
messagebox.showerror(message=str(ex))
raise
def execute_query(self):
class_or_instance = self.txt_class_or_instance.get()
if not class_or_instance:
raise ValueError('Requires class or instance name')
connect_info = self.parse_uri(self.txt_uri.get())
connect_info['username'] = self.txt_username.get() or None
connect_info['password'] = self.txt_password.get() or None
c = WBEMClient(**connect_info)
operation = self.cbo_operation.get()
if operation == 'GetInstance':
obj = c.Instance(class_or_instance)
else:
obj = c.Class(class_or_instance)
try:
result = getattr(c, operation)(obj)
except InvalidClass as ex:
raise ValueError(str(ex))
self.txt_results.delete('1.0', END)
if result.instances:
self.txt_results.insert(END, 'Instance names:\n')
for i in result.instances:
self.txt_results.insert(END, ' ' + str(i) + '\n')
if i.properties:
self.txt_results.insert(END, ' Properties:\n')
for k, v in sorted(i.properties.items()):
self.txt_results.insert(END, ' %s: %s\n' % (k, v))
elif result.properties:
self.txt_results.insert(END, 'Properties:\n')
for k, v in sorted(result.properties.items()):
self.txt_results.insert(END, ' %s: %s\n' % (k, v))
else:
self.txt_results.insert(END, 'No results')
def compile_exe():
"""
Creates a standalone EXE file.
"""
print('\nRunning compile using distutils and py2exe:\n')
from distutils.core import setup
import py2exe # required for proper inclusion
typelibs = []
com_server = []
dll_excludes = ['w9xpopen.exe']
sys.argv[1] = 'py2exe'
setup(
console=[file_path],
com_server=com_server,
options=dict(
py2exe=dict(
typelibs=typelibs,
includes=[],
excludes=[],
dll_excludes=dll_excludes,
bundle_files=2,
compressed=True
),
),
zipfile=None
)
sys.exit(0)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '-c':
compile_exe()
root = Tk()
MainApplication(root)
root.mainloop()
sys.exit(0)