tinc-gui: Reformat codebase according to PEP8

This commit is contained in:
Martin Weinelt 2015-09-28 05:20:03 +02:00
parent 7306823843
commit 0c7e0210d9

View file

@ -51,8 +51,9 @@ ID = 0
ACK = 4 ACK = 4
CONTROL = 18 CONTROL = 18
class Node:
def parse(self, args): class Node(object):
def __init__(self, args):
self.name = args[0] self.name = args[0]
self.address = args[1] self.address = args[1]
self.port = args[3] self.port = args[3]
@ -72,8 +73,9 @@ class Node:
self.subnets = {} self.subnets = {}
class Edge:
def parse(self, args): class Edge(object):
def __init__(self, args):
self.fr = args[0] self.fr = args[0]
self.to = args[1] self.to = args[1]
self.address = args[2] self.address = args[2]
@ -81,8 +83,9 @@ class Edge:
self.options = int(args[-2], 16) self.options = int(args[-2], 16)
self.weight = int(args[-1]) self.weight = int(args[-1])
class Subnet:
def parse(self, args): class Subnet(object):
def __init__(self, args):
if args[0].find('#') >= 0: if args[0].find('#') >= 0:
(address, self.weight) = args[0].split('#', 1) (address, self.weight) = args[0].split('#', 1)
else: else:
@ -97,8 +100,9 @@ class Subnet:
self.owner = args[1] self.owner = args[1]
class Connection:
def parse(self, args): class Connection(object):
def __init__(self, args):
self.name = args[0] self.name = args[0]
self.address = args[1] self.address = args[1]
self.port = args[3] self.port = args[3]
@ -107,9 +111,10 @@ class Connection:
self.status = int(args[6], 0x10) self.status = int(args[6], 0x10)
self.weight = 123 self.weight = 123
class VPN: class VPN:
confdir = '/etc/tinc' confdir = '/etc/tinc'
piddir = '/var/run/' piddir = '/var/run'
def connect(self): def connect(self):
# read the pidfile # read the pidfile
@ -118,10 +123,10 @@ class VPN:
f.close() f.close()
# check if there is a UNIX socket as well # check if there is a UNIX socket as well
if self.pidfile.endswith(".pid"): if self.pidfile.endswith('.pid'):
unixfile = self.pidfile.replace(".pid", ".socket"); unixfile = self.pidfile.replace('.pid', '.socket');
else: else:
unixfile = self.pidfile + ".socket"; unixfile = self.pidfile + '.socket';
if os.path.exists(unixfile): if os.path.exists(unixfile):
# use it if it exists # use it if it exists
@ -174,22 +179,19 @@ class VPN:
if resp[1] == '3': if resp[1] == '3':
if len(resp) < 19: if len(resp) < 19:
continue continue
node = self.nodes.get(resp[2]) or Node() node = self.nodes.get(resp[2]) or Node(resp[2:])
node.parse(resp[2:])
node.visited = True node.visited = True
self.nodes[resp[2]] = node self.nodes[resp[2]] = node
elif resp[1] == '4': elif resp[1] == '4':
if len(resp) < 9: if len(resp) < 9:
continue continue
edge = self.nodes.get((resp[2], resp[3])) or Edge() edge = self.nodes.get((resp[2], resp[3])) or Edge(resp[2:])
edge.parse(resp[2:])
edge.visited = True edge.visited = True
self.edges[(resp[2], resp[3])] = edge self.edges[(resp[2], resp[3])] = edge
elif resp[1] == '5': elif resp[1] == '5':
if len(resp) < 4: if len(resp) < 4:
continue continue
subnet = self.subnets.get((resp[2], resp[3])) or Subnet() subnet = self.subnets.get((resp[2], resp[3])) or Subnet(resp[2:])
subnet.parse(resp[2:])
subnet.visited = True subnet.visited = True
self.subnets[(resp[2], resp[3])] = subnet self.subnets[(resp[2], resp[3])] = subnet
if subnet.owner == "(broadcast)": if subnet.owner == "(broadcast)":
@ -198,8 +200,7 @@ class VPN:
elif resp[1] == '6': elif resp[1] == '6':
if len(resp) < 9: if len(resp) < 9:
break break
connection = self.connections.get((resp[2], resp[3], resp[5])) or Connection() connection = self.connections.get((resp[2], resp[3], resp[5])) or Connection(resp[2:])
connection.parse(resp[2:])
connection.visited = True connection.visited = True
self.connections[(resp[2], resp[3], resp[5])] = connection self.connections[(resp[2], resp[3], resp[5])] = connection
else: else:
@ -262,7 +263,7 @@ class VPN:
self.tincconf = os.path.join(self.confbase, 'tinc.conf') self.tincconf = os.path.join(self.confbase, 'tinc.conf')
if pidfile != None: if pidfile is not None:
self.pidfile = pidfile self.pidfile = pidfile
else: else:
if platform.system() == 'Windows': if platform.system() == 'Windows':
@ -273,6 +274,7 @@ class VPN:
else: else:
self.pidfile = os.path.join(VPN.piddir, 'tinc.pid') self.pidfile = os.path.join(VPN.piddir, 'tinc.pid')
# GUI starts here # GUI starts here
argv0 = sys.argv[0] argv0 = sys.argv[0]
@ -280,6 +282,7 @@ del sys.argv[0]
netname = None netname = None
pidfile = None pidfile = None
def usage(exitcode=0): def usage(exitcode=0):
print('Usage: ' + argv0 + ' [options]') print('Usage: ' + argv0 + ' [options]')
print('\nValid options are:') print('\nValid options are:')
@ -289,14 +292,15 @@ def usage(exitcode = 0):
print('\nReport bugs to tinc@tinc-vpn.org.') print('\nReport bugs to tinc@tinc-vpn.org.')
sys.exit(exitcode) sys.exit(exitcode)
while sys.argv: while sys.argv:
if sys.argv[0] in ('-n', '--net'): if sys.argv[0] in ('-n', '--net'):
del sys.argv[0] del sys.argv[0]
netname = sys.argv[0] netname = sys.argv[0]
elif sys.argv[0] in ('--pidfile'): elif sys.argv[0] in '--pidfile':
del sys.argv[0] del sys.argv[0]
pidfile = sys.argv[0] pidfile = sys.argv[0]
elif sys.argv[0] in ('--help'): elif sys.argv[0] in '--help':
usage(0) usage(0)
else: else:
print(argv0 + ': unrecognized option \'' + sys.argv[0] + '\'') print(argv0 + ': unrecognized option \'' + sys.argv[0] + '\'')
@ -304,27 +308,27 @@ while sys.argv:
del sys.argv[0] del sys.argv[0]
if netname == None: if netname is None:
netname = os.getenv("NETNAME") netname = os.getenv('NETNAME')
elif netname == '.':
if netname == ".":
netname = None netname = None
vpn = VPN(netname, pidfile) vpn = VPN(netname, pidfile)
vpn.connect() vpn.connect()
class SuperListCtrl(wx.ListCtrl, ColumnSorterMixin, ListCtrlAutoWidthMixin): class SuperListCtrl(wx.ListCtrl, ColumnSorterMixin, ListCtrlAutoWidthMixin):
def __init__(self, parent, style): def __init__(self, parent, style):
wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES) wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES)
ListCtrlAutoWidthMixin.__init__(self) ListCtrlAutoWidthMixin.__init__(self)
ColumnSorterMixin.__init__(self, 16) ColumnSorterMixin.__init__(self, 16)
def GetListCtrl(self): def get_list_ctrl(self):
return self return self
class SettingsPage(wx.Panel): class SettingsPage(wx.Panel):
def OnDebugLevel(self, event): def on_debug_level(self, event):
vpn.debug(self.debug.GetValue()) vpn.debug(self.debug.GetValue())
def __init__(self, parent, id): def __init__(self, parent, id):
@ -344,7 +348,7 @@ class SettingsPage(wx.Panel):
debuglabel = wx.StaticText(self, -1, 'Debug level:') debuglabel = wx.StaticText(self, -1, 'Debug level:')
self.debug = wx.SpinCtrl(self, min=0, max=5, initial=vpn.debug()) self.debug = wx.SpinCtrl(self, min=0, max=5, initial=vpn.debug())
self.debug.Bind(wx.EVT_SPINCTRL, self.OnDebugLevel) self.debug.Bind(wx.EVT_SPINCTRL, self.on_debug_level)
grid.Add(debuglabel) grid.Add(debuglabel)
grid.Add(self.debug) grid.Add(self.debug)
@ -355,6 +359,7 @@ class SettingsPage(wx.Panel):
self.SetSizer(grid) self.SetSizer(grid)
class ConnectionsPage(wx.Panel): class ConnectionsPage(wx.Panel):
def __init__(self, parent, id): def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id) wx.Panel.__init__(self, parent, id)
@ -378,13 +383,13 @@ class ConnectionsPage(wx.Panel):
disconnect = wx.MenuItem(self, -1, 'Disconnect') disconnect = wx.MenuItem(self, -1, 'Disconnect')
self.AppendItem(disconnect) self.AppendItem(disconnect)
self.Bind(wx.EVT_MENU, self.OnDisconnect, id=disconnect.GetId()) self.Bind(wx.EVT_MENU, self.on_disconnect, id=disconnect.GetId())
def OnDisconnect(self, event): def on_disconnect(self, event):
vpn.disconnect(self.item[0]) vpn.disconnect(self.item[0])
def OnContext(self, event): def on_context(self, event):
i = event.GetIndex() idx = event.GetIndex()
self.PopupMenu(self.ContextMenu(self.list.itemDataMap[event.GetIndex()]), event.GetPosition()) self.PopupMenu(self.ContextMenu(self.list.itemDataMap[event.GetIndex()]), event.GetPosition())
def refresh(self): def refresh(self):
@ -401,8 +406,9 @@ class ConnectionsPage(wx.Panel):
self.list.SetStringItem(i, 2, connection.port) self.list.SetStringItem(i, 2, connection.port)
self.list.SetStringItem(i, 3, str(connection.options)) self.list.SetStringItem(i, 3, str(connection.options))
self.list.SetStringItem(i, 4, str(connection.weight)) self.list.SetStringItem(i, 4, str(connection.weight))
self.list.itemDataMap[i] = (connection.name, connection.address, connection.port, connection.options, connection.weight) self.list.itemDataMap[i] = (connection.name, connection.address, connection.port, connection.options,
self.list.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.OnContext) connection.weight)
self.list.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.on_context)
self.list.SetItemData(i, i) self.list.SetItemData(i, i)
i += 1 i += 1
@ -411,6 +417,7 @@ class ConnectionsPage(wx.Panel):
self.list.SortListItems(sortstate[0], sortstate[1]) self.list.SortListItems(sortstate[0], sortstate[1])
class NodesPage(wx.Panel): class NodesPage(wx.Panel):
def __init__(self, parent, id): def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id) wx.Panel.__init__(self, parent, id)
@ -466,7 +473,9 @@ class NodesPage(wx.Panel):
else: else:
since = "never" since = "never"
self.list.SetStringItem(i, 15, since) self.list.SetStringItem(i, 15, since)
self.list.itemDataMap[i] = (node.name, node.address, node.port, node.cipher, node.digest, node.maclength, node.compression, node.options, node.status, node.nexthop, node.via, node.distance, node.pmtu, node.minmtu, node.maxmtu, since) self.list.itemDataMap[i] = (node.name, node.address, node.port, node.cipher, node.digest, node.maclength,
node.compression, node.options, node.status, node.nexthop, node.via,
node.distance, node.pmtu, node.minmtu, node.maxmtu, since)
self.list.SetItemData(i, i) self.list.SetItemData(i, i)
i += 1 i += 1
@ -475,6 +484,7 @@ class NodesPage(wx.Panel):
self.list.SortListItems(sortstate[0], sortstate[1]) self.list.SortListItems(sortstate[0], sortstate[1])
class EdgesPage(wx.Panel): class EdgesPage(wx.Panel):
def __init__(self, parent, id): def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id) wx.Panel.__init__(self, parent, id)
@ -515,6 +525,7 @@ class EdgesPage(wx.Panel):
self.list.SortListItems(sortstate[0], sortstate[1]) self.list.SortListItems(sortstate[0], sortstate[1])
class SubnetsPage(wx.Panel): class SubnetsPage(wx.Panel):
def __init__(self, parent, id): def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id) wx.Panel.__init__(self, parent, id)
@ -548,14 +559,17 @@ class SubnetsPage(wx.Panel):
self.list.SortListItems(sortstate[0], sortstate[1]) self.list.SortListItems(sortstate[0], sortstate[1])
class StatusPage(wx.Panel): class StatusPage(wx.Panel):
def __init__(self, parent, id): def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id) wx.Panel.__init__(self, parent, id)
class GraphPage(wx.Window): class GraphPage(wx.Window):
def __init__(self, parent, id): def __init__(self, parent, id):
wx.Window.__init__(self, parent, id) wx.Window.__init__(self, parent, id)
class NetPage(wx.Notebook): class NetPage(wx.Notebook):
def __init__(self, parent, id): def __init__(self, parent, id):
wx.Notebook.__init__(self, parent) wx.Notebook.__init__(self, parent)
@ -573,14 +587,15 @@ class NetPage(wx.Notebook):
self.AddPage(self.nodes, 'Nodes') self.AddPage(self.nodes, 'Nodes')
self.AddPage(self.edges, 'Edges') self.AddPage(self.edges, 'Edges')
self.AddPage(self.subnets, 'Subnets') self.AddPage(self.subnets, 'Subnets')
# self.AddPage(self.graph, 'Graph') # self.AddPage(self.graph, 'Graph')
class MainWindow(wx.Frame): class MainWindow(wx.Frame):
def OnQuit(self, event): def on_quit(self, event):
app.ExitMainLoop() app.ExitMainLoop()
def OnTimer(self, event): def on_timer(self, event):
vpn.refresh() vpn.refresh()
self.np.nodes.refresh() self.np.nodes.refresh()
self.np.subnets.refresh() self.np.subnets.refresh()
@ -591,9 +606,10 @@ class MainWindow(wx.Frame):
wx.Frame.__init__(self, parent, id, title) wx.Frame.__init__(self, parent, id, title)
menubar = wx.MenuBar() menubar = wx.MenuBar()
file = wx.Menu()
file.Append(1, '&Quit\tCtrl-X', 'Quit tinc GUI') menu = wx.Menu()
menubar.Append(file, '&File') menu.Append(1, '&Quit\tCtrl-X', 'Quit tinc GUI')
menubar.Append(menu, '&File')
# nb = wx.Notebook(self, -1) # nb = wx.Notebook(self, -1)
# nb.SetPadding((0, 0)) # nb.SetPadding((0, 0))
@ -601,22 +617,27 @@ class MainWindow(wx.Frame):
# nb.AddPage(np, 'VPN') # nb.AddPage(np, 'VPN')
self.timer = wx.Timer(self, -1) self.timer = wx.Timer(self, -1)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(1000) self.timer.Start(1000)
self.Bind(wx.EVT_MENU, self.OnQuit, id=1) self.Bind(wx.EVT_MENU, self.on_quit, id=1)
self.SetMenuBar(menubar) self.SetMenuBar(menubar)
self.Show() self.Show()
app = wx.App() app = wx.App()
mw = MainWindow(None, -1, 'Tinc GUI') mw = MainWindow(None, -1, 'Tinc GUI')
#def OnTaskBarIcon(event): """
# mw.Raise() def OnTaskBarIcon(event):
# mw.Raise()
#icon = wx.Icon("tincgui.ico", wx.BITMAP_TYPE_PNG) """
#taskbaricon = wx.TaskBarIcon()
#taskbaricon.SetIcon(icon, 'Tinc GUI') """
#wx.EVT_TASKBAR_RIGHT_UP(taskbaricon, OnTaskBarIcon) icon = wx.Icon("tincgui.ico", wx.BITMAP_TYPE_PNG)
taskbaricon = wx.TaskBarIcon()
taskbaricon.SetIcon(icon, 'Tinc GUI')
wx.EVT_TASKBAR_RIGHT_UP(taskbaricon, OnTaskBarIcon)
"""
app.MainLoop() app.MainLoop()
vpn.close() vpn.close()