-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.py
More file actions
executable file
·282 lines (233 loc) · 9.51 KB
/
gui.py
File metadata and controls
executable file
·282 lines (233 loc) · 9.51 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
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
import PySimpleGUI as Sg
from net_async import MgmtIPAddresses
from parsers import cucm_export_parse
from exceptions import NoPhoneReportFound
from os import path
f = {
'font': 'Helvetica',
'size': {
'small': '12',
'medium': '14',
'large': '16'
}}
s_font = f'{f["font"]} {f["size"]["small"]}'
m_font = f'{f["font"]} {f["size"]["medium"]}'
l_font = f'{f["font"]} {f["size"]["large"]}'
window_title = 'NetInventory'
def gui_print(string, font=m_font):
return [Sg.Text(str(string), font=font)]
def gui_print_box(string, font=m_font, size=(20, 100)):
return [Sg.Multiline(str(string), font=font, size=size)]
def gui_checkbox(string, key=None, font=m_font, size=(20, 100)):
return [Sg.Checkbox(str(string), font=font, size=size, key=key)]
def button(string, font=m_font):
return [Sg.Button(str(string), font=font, bind_return_key=True)]
def dropdown(input_list, font=m_font):
return [Sg.Combo(input_list, font=font, bind_return_key=True)]
def file_browse_botton(string, font=m_font):
return [
Sg.Input(Sg.user_settings_get_entry('-filename-', ''), key='file'),
Sg.FileBrowse(str(string), initial_folder='./', font=font)]
def folder_browse_botton(string, font=m_font):
return [
Sg.Input(Sg.user_settings_get_entry('-folder-', ''), key='folder'),
Sg.FolderBrowse(str(string), initial_folder='./', font=font)]
def cucm_file_browse_botton(string, font=m_font):
return [
Sg.Input(Sg.user_settings_get_entry('-cucm_file-', ''), key='cucm_file'),
Sg.FileBrowse(str(string), initial_folder='./', font=font)]
def gui_user_input(font=m_font):
return [Sg.Input(Sg.user_settings_get_entry('-username-', ''), key='user', font=font)]
def gui_password_input(default_string='', font=m_font):
return [Sg.InputText(str(default_string), key='pass', password_char='*', font=font)]
def gui_enable_password_input(default_string='', font=m_font):
return [Sg.InputText(str(default_string), key='enable_pw', password_char='*', font=font)]
def w_mgmt_file_main(current_window=None):
"""Main / Home Window"""
if current_window is not None:
current_window.close()
layout = [
gui_print('Select file containing device management IP addresses'),
file_browse_botton('Browse'),
gui_checkbox('Use CUCM Export', key='cucm_export'),
button('Next')
]
return Sg.Window(window_title, layout, margins=(100, 100))
def w_save_folder(current_window=None):
"""Folder Save Window"""
if current_window is not None:
current_window.close()
layout = [
gui_print('Select folder to save Inventory Export'),
folder_browse_botton('Browse'),
button('Save File')
]
return Sg.Window(window_title, layout, margins=(100, 100))
def w_credential(current_window=None):
"""Get Network Credential Window"""
if current_window is not None:
current_window.close()
layout = [
gui_print('Network Username'),
gui_user_input(),
gui_print('Network Password'),
gui_password_input(''),
gui_print('Enable Password(If Applicable)'),
gui_enable_password_input(''),
button('Run Inventory Discovery')
]
return Sg.Window(window_title, layout, margins=(100, 100))
def w_cucm_file_main(current_window=None):
"""Window for CUCM export file selection"""
if current_window is not None:
current_window.close()
layout = [
gui_print('Select CUCM export CSV file'),
cucm_file_browse_botton('Browse'),
button('Next')
]
return Sg.Window(window_title, layout, margins=(100, 100))
def w_file_not_found(current_window, cucm=False, folder=False):
"""File Not Found Window and Retry"""
current_window.close()
if cucm:
layout = [
gui_print('File or directory not found.'),
gui_print('Select CUCM export CSV file'),
cucm_file_browse_botton('Browse'),
button('Retry')
]
elif folder:
layout = [
gui_print('Directory not found.'),
gui_print('Select folder to save Inventory Export'),
folder_browse_botton('Browse'),
button('Retry')
]
else:
layout = [
gui_print('File or directory not found.'),
gui_print('Select file containing device management IP addresses'),
file_browse_botton('Browse'),
button('Retry')
]
return Sg.Window(window_title, layout, margins=(100, 100))
def w_invalid_file_entry(current_window, mgmt_file):
"""Invalid File Entry Window and Retry"""
invalid_lines = 'Line | Value\n'
line_nums = mgmt_file.invalid_line_nums
ip_addresses = mgmt_file.invalid_ip_addresses
for (line_n, ip_addr) in zip(
line_nums, ip_addresses):
blank_space = ''
for num1 in range(0, 10 - len(line_n)):
blank_space += ' '
invalid_lines += f'{line_n}{blank_space} {ip_addr}'
current_window.close()
layout = [
gui_print('Invalid File Entries'),
gui_print_box(invalid_lines, size=(30, 15)),
gui_print('Select file containing device management IP addresses'),
file_browse_botton('Browse'),
button('Retry')
]
return Sg.Window(window_title, layout, margins=(100, 100))
def management_file_browse():
"""Window for selecting management file
:return: Management file location"""
current_window = w_mgmt_file_main()
while True:
event, values = current_window.read(timeout=10)
if event == 'Check File' or event == 'Retry':
try:
Sg.user_settings_set_entry('-filename-', values['file'])
file = MgmtIPAddresses(values['file'])
if file.valid:
current_window.close()
return file.mgmt_ips
else:
current_window = w_invalid_file_entry(current_window, file)
except FileNotFoundError:
current_window = w_file_not_found(current_window)
if event == Sg.WIN_CLOSED:
current_window.close()
return None
def inventory_save_folder_browse():
"""Window for selecting inventory save location
:return: Inventory file save folder location"""
current_window = w_save_folder()
while True:
event, values = current_window.read(timeout=10)
if event == 'Save File' or event == 'Retry':
Sg.user_settings_set_entry('-folder-', values['folder'])
if path.isdir(values['folder']):
return values['folder']
else:
current_window = w_file_not_found(current_window, folder=True)
if event == Sg.WIN_CLOSED:
current_window.close()
return None
class InventoryGui:
"""
Front-end GUI for collecting inputs.
Attributes:
mgmt_ips - List of management IP addresses\n
parsed_cucm_phones - Output from 'cucm_export_parse()'\n
username - Network Username\n
password - Network Password\n
enable_pw - Enable Password
"""
def __init__(self):
current_window = w_mgmt_file_main()
mgmt = False
cucm = False
while True:
event, values = current_window.read(timeout=10)
if (event == 'Next' or event == 'Retry') and not mgmt:
try:
Sg.user_settings_set_entry('-filename-', values['file'])
file = MgmtIPAddresses(values['file'])
if file.valid:
self.mgmt_ips = file.mgmt_ips
"""List of management IP addresses"""
mgmt = True
if values['cucm_export']:
current_window = w_cucm_file_main(current_window)
cucm = True
else:
current_window = w_credential(current_window)
else:
current_window = w_invalid_file_entry(current_window, file)
except FileNotFoundError:
current_window = w_file_not_found(current_window)
elif event == 'Next' and cucm:
try:
Sg.user_settings_set_entry('-cucm_file-', values['cucm_file'])
self.parsed_cucm_phones = cucm_export_parse(values['cucm_file'])
"""Output from 'cucm_export_parse()'"""
current_window = w_credential(current_window)
except NoPhoneReportFound:
current_window = w_file_not_found(current_window, True)
elif event == 'Run Inventory Discovery':
self.username = values['user']
"""Network Username"""
self.password = values['pass']
"""Network Password"""
self.enable_pw = values['enable_pw']
"""Enable Password"""
current_window.close()
break
if event == Sg.WIN_CLOSED:
current_window.close()
break
# def w_template(current_window, mgmt_file):
# """Template Window"""
# current_window.close()
# layout = [
# gui_print('Invalid File Entries'),
# gui_print_box(invalid_lines, size=(30, 15)),
# gui_print('Select file containing switch management IP addresses'),
# file_browse_botton('Browse'),
# button('Retry')
# ]
# return Sg.Window(window_title, layout, margins=(100, 100))