Skip to content

Commit

Permalink
Fix some issues by pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaleino committed Nov 17, 2012
1 parent b386b37 commit b5a4d70
Show file tree
Hide file tree
Showing 19 changed files with 733 additions and 452 deletions.
113 changes: 69 additions & 44 deletions cli/wicd-cli.py
@@ -1,5 +1,6 @@
#!/usr/bin/python

""" Scriptable command-line interface. """
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
Expand Down Expand Up @@ -32,20 +33,30 @@

bus = dbus.SystemBus()
try:
daemon = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon'),
'org.wicd.daemon')
wireless = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wireless'),
'org.wicd.daemon.wireless')
wired = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wired'),
'org.wicd.daemon.wired')
config = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/config'),
'org.wicd.daemon.config')
daemon = dbus.Interface(
bus.get_object('org.wicd.daemon', '/org/wicd/daemon'),
'org.wicd.daemon'
)
wireless = dbus.Interface(
bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wireless'),
'org.wicd.daemon.wireless'
)
wired = dbus.Interface(
bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wired'),
'org.wicd.daemon.wired'
)
config = dbus.Interface(
bus.get_object('org.wicd.daemon', '/org/wicd/daemon/config'),
'org.wicd.daemon.config'
)
except dbus.DBusException:
print 'Error: Could not connect to the daemon. Please make sure it is running.'
print 'Error: Could not connect to the daemon. ' + \
'Please make sure it is running.'
sys.exit(3)

if not daemon:
print 'Error connecting to wicd via D-Bus. Please make sure the wicd service is running.'
print 'Error connecting to wicd via D-Bus. ' + \
'Please make sure the wicd service is running.'
sys.exit(3)

parser = optparse.OptionParser()
Expand All @@ -61,12 +72,14 @@
parser.add_option('--network-details', '-d', default=False, action='store_true')
parser.add_option('--disconnect', '-x', default=False, action='store_true')
parser.add_option('--connect', '-c', default=False, action='store_true')
parser.add_option('--list-encryption-types', '-e', default=False, action='store_true')
parser.add_option('--list-encryption-types', '-e', default=False,
action='store_true')
# short options for these aren't great.
parser.add_option('--wireless', '-y', default=False, action='store_true')
parser.add_option('--wired', '-z', default=False, action='store_true')
parser.add_option('--load-profile', '-o', default=False, action='store_true')
parser.add_option('--status', '-i', default=False, action='store_true') # -i(nfo)
parser.add_option('--status', '-i', default=False,
action='store_true') # -i(nfo)

options, arguments = parser.parse_args()

Expand Down Expand Up @@ -114,19 +127,22 @@

# functions
def is_valid_wireless_network_id(network_id):
""" Check if it's a valid wireless network. '"""
if not (network_id >= 0 \
and network_id < wireless.GetNumberOfNetworks()):
print 'Invalid wireless network identifier.'
sys.exit(1)

def is_valid_wired_network_id(network_id):
""" Check if it's a valid wired network. '"""
num = len(wired.GetWiredProfileList())
if not (network_id < num and \
network_id >= 0):
print 'Invalid wired network identifier.'
sys.exit(4)

def is_valid_wired_network_profile(profile_name):
""" Check if it's a valid wired network profile. '"""
if not profile_name in wired.GetWiredProfileList():
print 'Profile of that name does not exist.'
sys.exit(5)
Expand All @@ -151,10 +167,10 @@ def is_valid_wired_network_profile(profile_name):
wireless.GetWirelessProperty(network_id, 'essid'))
elif options.wired:
print '#\tProfile name'
id = 0
i = 0
for profile in wired.GetWiredProfileList():
print '%s\t%s' % (id, profile)
id += 1
print '%s\t%s' % (i, profile)
i += 1
op_performed = True

if options.network_details:
Expand All @@ -173,13 +189,17 @@ def is_valid_wired_network_profile(profile_name):
if wireless.GetWirelessProperty(network_id, "encryption"):
print "Encryption: On"
print "Encryption Method: %s" % \
wireless.GetWirelessProperty(network_id, "encryption_method")
wireless.GetWirelessProperty(network_id, "encryption_method")
else:
print "Encryption: Off"
print "Quality: %s" % wireless.GetWirelessProperty(network_id, "quality")
print "Mode: %s" % wireless.GetWirelessProperty(network_id, "mode")
print "Channel: %s" % wireless.GetWirelessProperty(network_id, "channel")
print "Bit Rates: %s" % wireless.GetWirelessProperty(network_id, "bitrates")
print "Quality: %s" % \
wireless.GetWirelessProperty(network_id, "quality")
print "Mode: %s" % \
wireless.GetWirelessProperty(network_id, "mode")
print "Channel: %s" % \
wireless.GetWirelessProperty(network_id, "channel")
print "Bit Rates: %s" % \
wireless.GetWirelessProperty(network_id, "bitrates")
op_performed = True

# network properties
Expand All @@ -194,7 +214,8 @@ def is_valid_wired_network_profile(profile_name):
network_id = wireless.GetCurrentNetworkID(0)
is_valid_wireless_network_id(network_id)
if not options.set_to:
print wireless.GetWirelessProperty(network_id, options.network_property)
print wireless.GetWirelessProperty(network_id,
options.network_property)
else:
wireless.SetWirelessProperty(network_id, \
options.network_property, options.set_to)
Expand All @@ -209,11 +230,13 @@ def is_valid_wired_network_profile(profile_name):
daemon.Disconnect()
if options.wireless:
if wireless.GetCurrentNetworkID(0) > -1:
print "Disconnecting from %s on %s" % (wireless.GetCurrentNetwork(0),
wireless.DetectWirelessInterface())
print "Disconnecting from %s on %s" % \
(wireless.GetCurrentNetwork(0),
wireless.DetectWirelessInterface())
elif options.wired:
if wired.CheckPluggedIn():
print "Disconnecting from wired connection on %s" % wired.DetectWiredInterface()
print "Disconnecting from wired connection on %s" % \
wired.DetectWiredInterface()
op_performed = True

if options.connect:
Expand All @@ -226,16 +249,17 @@ def is_valid_wired_network_profile(profile_name):
wireless.DetectWirelessInterface())
wireless.ConnectWireless(options.network)

check = lambda: wireless.CheckIfWirelessConnecting()
status = lambda: wireless.CheckWirelessConnectingStatus()
message = lambda: wireless.CheckWirelessConnectingMessage()
check = wireless.CheckIfWirelessConnecting
status = wireless.CheckWirelessConnectingStatus
message = wireless.CheckWirelessConnectingMessage
elif options.wired:
print "Connecting to wired connection on %s" % wired.DetectWiredInterface()
print "Connecting to wired connection on %s" % \
wired.DetectWiredInterface()
wired.ConnectWired()

check = lambda: wired.CheckIfWiredConnecting()
status = lambda: wired.CheckWiredConnectingStatus()
message = lambda: wired.CheckWiredConnectingMessage()
check = wired.CheckIfWiredConnecting
status = wired.CheckWiredConnectingStatus
message = wired.CheckWiredConnectingMessage
else:
check = lambda: False
status = lambda: False
Expand All @@ -245,36 +269,37 @@ def is_valid_wired_network_profile(profile_name):
last = None
if check:
while check():
next = status()
if next != last:
# avoid a race condition where status is updated to "done" after the
# loop check
if next == "done":
next_ = status()
if next_ != last:
# avoid a race condition where status is updated to "done" after
# the loop check
if next_ == "done":
break
print message()
last = next
last = next_
print "done!"
op_performed = True

# pretty print optional and required properties
def str_properties(prop):
""" Pretty print optional and required properties. """
if len(prop) == 0:
return "None"
else:
return ', '.join("%s (%s)" % (x[0], x[1].replace("_", " ")) for x in type['required'])
tmp = [(x[0], x[1].replace('_', ' ')) for x in type['required']]
return ', '.join("%s (%s)" % (x, y) for x, y in tmp)

if options.wireless and options.list_encryption_types:
et = misc.LoadEncryptionMethods()
# print 'Installed encryption templates:'
print '%s\t%-20s\t%s' % ('#', 'Name', 'Description')
id = 0
for type in et:
print '%s\t%-20s\t%s' % (id, type['type'], type['name'])
print ' Req: %s' % str_properties(type['required'])
i = 0
for t in et:
print '%s\t%-20s\t%s' % (i, t['type'], t['name'])
print ' Req: %s' % str_properties(t['required'])
print '---'
# don't print optionals (yet)
#print ' Opt: %s' % str_properties(type['optional'])
id += 1
i += 1
op_performed = True

if options.save and options.network > -1:
Expand Down
98 changes: 59 additions & 39 deletions curses/configscript_curses.py
Expand Up @@ -26,61 +26,81 @@
from wicd import misc
from wicd.translations import _
import configscript
from configscript import write_scripts,get_script_info,get_val,none_to_blank,blank_to_none
from configscript import write_scripts, get_script_info, get_val
from configscript import none_to_blank, blank_to_none

import urwid
import urwid.curses_display
import sys
import os

ui = None
frame = None
pre_entry = None
post_entry = None
pre_disconnect_entry = None
post_disconnect_entry = None

def main(argv):
global ui,frame
""" Main function. """
global ui, frame
if len(argv) < 2:
print 'Network id to configure is missing, aborting.'
sys.exit(1)

ui = urwid.curses_display.Screen()
ui.register_palette( [
('body','default','default'),
('focus','dark magenta','light gray'),
ui.register_palette([
('body', 'default', 'default'),
('focus', 'dark magenta', 'light gray'),
('editcp', 'default', 'default', 'standout'),
('editbx', 'light gray', 'dark blue'),
('editfc', 'white','dark blue', 'bold')] )
('editfc', 'white','dark blue', 'bold'),
])

network = argv[1]
network_type = argv[2]

script_info = get_script_info(network, network_type)

blank = urwid.Text('')
pre_entry_t = ('body',_('Pre-connection Script')+': ')
post_entry_t = ('body',_('Post-connection Script')+': ')
pre_disconnect_entry_t = ('body',_('Pre-disconnection Script')+': ')
post_disconnect_entry_t = ('body',_('Post-disconnection Script')+': ')

global pre_entry,post_entry,pre_disconnect_entry,post_disconnect_entry
pre_entry = urwid.AttrWrap(urwid.Edit(pre_entry_t,
none_to_blank(script_info.get('pre_entry'))),'editbx','editfc' )
post_entry = urwid.AttrWrap(urwid.Edit(post_entry_t,
none_to_blank(script_info.get('post_entry'))),'editbx','editfc' )

pre_disconnect_entry = urwid.AttrWrap(urwid.Edit(pre_disconnect_entry_t,
none_to_blank(script_info.get('pre_disconnect_entry'))),'editbx','editfc' )
post_disconnect_entry = urwid.AttrWrap(urwid.Edit(post_disconnect_entry_t,
none_to_blank(script_info.get('post_disconnect_entry'))),'editbx','editfc' )
pre_entry_t = ('body', _('Pre-connection Script') + ': ')
post_entry_t = ('body', _('Post-connection Script') + ': ')
pre_disconnect_entry_t = ('body', _('Pre-disconnection Script') + ': ')
post_disconnect_entry_t = ('body', _('Post-disconnection Script') + ': ')

global pre_entry, post_entry, pre_disconnect_entry, post_disconnect_entry
pre_entry = urwid.AttrWrap(urwid.Edit(pre_entry_t,
none_to_blank(script_info.get('pre_entry'))),
'editbx', 'editfc')
post_entry = urwid.AttrWrap(urwid.Edit(post_entry_t,
none_to_blank(script_info.get('post_entry'))),
'editbx','editfc')

pre_disconnect_entry = urwid.AttrWrap(urwid.Edit(pre_disconnect_entry_t,
none_to_blank(script_info.get('pre_disconnect_entry'))),
'editbx', 'editfc')
post_disconnect_entry = urwid.AttrWrap(urwid.Edit(post_disconnect_entry_t,
none_to_blank(script_info.get('post_disconnect_entry'))),
'editbx','editfc')

# The buttons
ok_button = urwid.AttrWrap(urwid.Button(_('OK'),ok_callback),'body','focus')
cancel_button = urwid.AttrWrap(urwid.Button(_('Cancel'),cancel_callback),'body','focus')

button_cols = urwid.Columns([ok_button,cancel_button],dividechars=1)

lbox = urwid.Pile([('fixed',2,urwid.Filler(pre_entry)),
#('fixed',urwid.Filler(blank),1),
('fixed',2,urwid.Filler(post_entry)),
('fixed',2,urwid.Filler(pre_disconnect_entry)),
('fixed',2,urwid.Filler(post_disconnect_entry)),
#blank,blank,blank,blank,blank,
ok_button = urwid.AttrWrap(
urwid.Button(_('OK'), ok_callback),
'body','focus'
)
cancel_button = urwid.AttrWrap(
urwid.Button(_('Cancel'), cancel_callback),
'body','focus'
)

button_cols = urwid.Columns([ok_button, cancel_button], dividechars=1)

lbox = urwid.Pile([('fixed', 2, urwid.Filler(pre_entry)),
#('fixed', urwid.Filler(blank), 1),
('fixed', 2, urwid.Filler(post_entry)),
('fixed', 2, urwid.Filler(pre_disconnect_entry)),
('fixed', 2, urwid.Filler(post_disconnect_entry)),
#blank, blank, blank, blank, blank,
urwid.Filler(button_cols,'bottom')
])
frame = urwid.Frame(lbox)
Expand All @@ -89,16 +109,18 @@ def main(argv):
if result == True:
script_info["pre_entry"] = blank_to_none(pre_entry.get_edit_text())
script_info["post_entry"] = blank_to_none(post_entry.get_edit_text())
script_info["pre_disconnect_entry"] = blank_to_none(pre_disconnect_entry.get_edit_text())
script_info["post_disconnect_entry"] = blank_to_none(post_disconnect_entry.get_edit_text())
script_info["pre_disconnect_entry"] = \
blank_to_none(pre_disconnect_entry.get_edit_text())
script_info["post_disconnect_entry"] = \
blank_to_none(post_disconnect_entry.get_edit_text())
write_scripts(network, network_type, script_info)

OK_PRESSED = False
CANCEL_PRESSED = False
def ok_callback(button_object,user_data=None):
def ok_callback(button_object, user_data=None):
global OK_PRESSED
OK_PRESSED = True
def cancel_callback(button_object,user_data=None):
def cancel_callback(button_object, user_data=None):
global CANCEL_PRESSED
CANCEL_PRESSED = True
def run():
Expand All @@ -119,9 +141,7 @@ def run():
#Send key to underlying widget:
if urwid.is_mouse_event(k):
event, button, col, row = k
frame.mouse_event( dim,
event, button, col, row,
focus=True)
frame.mouse_event(dim, event, button, col, row, focus=True)
else:
frame.keypress(dim, k)
# Check if buttons are pressed.
Expand Down

0 comments on commit b5a4d70

Please sign in to comment.