-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick_Subnet_Calculator.py
134 lines (106 loc) · 5.01 KB
/
Quick_Subnet_Calculator.py
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Copyright (C) 2018, 2019 Brandon M. Pace
#
# This file is part of Quick Subnet Calculator
#
# Quick Subnet Calculator is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Quick Subnet Calculator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Quick Subnet Calculator.
# If not, see <https://www.gnu.org/licenses/>.
import GUI
import libIPconv as conv
import wx
class MainFrame(GUI.SubnetCalcFrame):
def __init__(self, *args, **kwds):
GUI.SubnetCalcFrame.__init__(self, *args, **kwds)
self.text_ctrl_dotted.addr_type = conv.ADDRTYPE.DOTTED
self.text_ctrl_dotted.is_mask = False
self.text_ctrl_mask.addr_type = conv.ADDRTYPE.DOTTED
self.text_ctrl_mask.mask_type = conv.MASKTYPE.DOTTED
self.text_ctrl_mask.is_mask = True
main_calculator.set_value(f'{self.text_ctrl_dotted.GetValue()}/{self.spin_ctrl_mask.GetValue()}')
self.text_ctrl_mask.SetValue(conv.cidrToDottedQuadStr(self.slider_mask.GetValue()))
self.update()
def on_char(self, event):
super().on_char(event)
if event.GetSkipped():
return # return if the key was already allowed
event_control = event.GetEventObject()
event_key = event.GetKeyCode()
if conv.filters.isAllowedASCII(event_key, event_control.addr_type):
event.Skip()
if event_control.addr_type == conv.ADDRTYPE.DOTTED:
control_content = event_control.GetValue()
control_selection = event_control.GetSelection()
# Insert the new character at the insertion point, over-writing any selected characters
first = control_content[0:control_selection[0]]
last = control_content[control_selection[1]:]
check_value = first + chr(event_key) + last
if event.GetSkipped():
check_value = conv.augment.pad_dotted_right(check_value)
if not conv.isValidIPv4(check_value, conv.ADDRTYPE.DOTTED):
event.Skip(False)
else:
event.Skip()
def on_paste(self, event):
success, pasted_string = super().on_paste(event)
event_object = event.GetEventObject()
filtered_string = conv.filters.filterChars(pasted_string, event_object.addr_type)
if not filtered_string:
return # nothing to insert
control_content = event_object.GetValue()
control_selection = event_object.GetSelection()
# Insert the new string at the insertion point, over-writing any selected characters
first = control_content[0:control_selection[0]]
last = control_content[control_selection[1]:]
final_value = first + filtered_string + last
event_object.SetValue(final_value)
event_object.SetInsertionPoint(len(first + filtered_string))
def on_slider(self, event):
super().on_slider(event)
self.update(self.slider_mask)
def on_spinctrl(self, event):
super().on_spinctrl(event)
self.update(self.spin_ctrl_mask)
def on_text(self, event):
event_object = event.GetEventObject()
self.update(trigger_object=event_object)
def update(self, trigger_object=None):
if trigger_object in [self.slider_mask, self.spin_ctrl_mask, self.text_ctrl_mask]:
mask_object = trigger_object
else:
mask_object = self.spin_ctrl_mask
if main_calculator.set_value(f'{self.text_ctrl_dotted.GetValue()}/{mask_object.GetValue()}'):
new_info = main_calculator.subnet_info()
self.text_ctrl_network.SetValue(new_info['network_addr'])
self.text_ctrl_broadcast.SetValue(new_info['broadcast_addr'])
self.text_ctrl_first_addr.SetValue(new_info['first_addr'])
self.text_ctrl_last_addr.SetValue(new_info['last_addr'])
self.text_ctrl_usable.SetValue(new_info['usable'])
if mask_object is self.text_ctrl_mask:
self.slider_mask.SetValue(int(new_info['prefix']))
self.spin_ctrl_mask.SetValue(int(new_info['prefix']))
else:
self.text_ctrl_mask.SetValue(new_info['netmask'])
else:
self.reset_results()
class MainApp(wx.App):
def OnInit(self):
self.frame_main = MainFrame(None, wx.ID_ANY, "", name='MainFrame')
self.SetTopWindow(self.frame_main)
self.frame_main.Show()
return True
if __name__ == "__main__":
main_calculator = conv.SubnetCalculator()
app = MainApp(0)
app.MainLoop()