mirror of
https://github.com/gryf/wicd.git
synced 2025-12-19 12:28:08 +01:00
Style changes for python files
This commit is contained in:
136
cli/wicd-cli.py
136
cli/wicd-cli.py
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
""" Scriptable command-line interface. """
|
||||
"""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
|
||||
@@ -17,9 +17,11 @@
|
||||
# MA 02110-1301, USA.
|
||||
|
||||
import optparse
|
||||
import sys
|
||||
|
||||
import dbus
|
||||
import dbus.service
|
||||
import sys
|
||||
|
||||
from wicd import misc
|
||||
from wicd.translations import _
|
||||
|
||||
@@ -52,13 +54,13 @@ try:
|
||||
'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()
|
||||
@@ -71,25 +73,26 @@ parser.add_option('--name', '-m')
|
||||
parser.add_option('--scan', '-S', default=False, action='store_true')
|
||||
parser.add_option('--save', '-w', default=False, action='store_true')
|
||||
parser.add_option('--list-networks', '-l', default=False, action='store_true')
|
||||
parser.add_option('--network-details', '-d', default=False, action='store_true')
|
||||
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')
|
||||
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)
|
||||
action='store_true') # -i(nfo)
|
||||
|
||||
options, arguments = parser.parse_args()
|
||||
|
||||
op_performed = False
|
||||
|
||||
if not (options.wireless or options.wired) and not options.status:
|
||||
print(("Please use --wireless or --wired to specify " + \
|
||||
"the type of connection to operate on."))
|
||||
print("Please use --wireless or --wired to specify the type of "
|
||||
"connection to operate on.")
|
||||
|
||||
if options.status:
|
||||
status, info = daemon.GetConnectionStatus()
|
||||
@@ -109,46 +112,47 @@ if options.status:
|
||||
print((_('Connection type') + ': ' + conn_type))
|
||||
if status == misc.WIRELESS:
|
||||
strength = daemon.FormatSignalForPrinting(info[2])
|
||||
print((_('Connected to $A at $B (IP: $C)') \
|
||||
.replace('$A', info[1]) \
|
||||
.replace('$B', strength) \
|
||||
.replace('$C', info[0])))
|
||||
print((_('Network ID: $A') \
|
||||
.replace('$A', info[3])))
|
||||
print(_('Connected to $A at $B (IP: $C)')
|
||||
.replace('$A', info[1])
|
||||
.replace('$B', strength)
|
||||
.replace('$C', info[0]))
|
||||
print(_('Network ID: $A').replace('$A', info[3]))
|
||||
else:
|
||||
print((_('Connected to wired network (IP: $A)') \
|
||||
.replace('$A', info[0])))
|
||||
print(_('Connected to wired network (IP: $A)')
|
||||
.replace('$A', info[0]))
|
||||
else:
|
||||
if status == misc.CONNECTING:
|
||||
if info[0] == 'wired':
|
||||
print((_('Connecting to wired network.')))
|
||||
print(_('Connecting to wired network.'))
|
||||
elif info[0] == 'wireless':
|
||||
print((_('Connecting to wireless network "$A".') \
|
||||
.replace('$A', info[1])))
|
||||
print(_('Connecting to wireless network "$A".')
|
||||
.replace('$A', info[1]))
|
||||
op_performed = True
|
||||
|
||||
# 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()):
|
||||
"""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. '"""
|
||||
"""Check if it's a valid wired network. """
|
||||
num = len(wired.GetWiredProfileList())
|
||||
if not (network_id < num and \
|
||||
network_id >= 0):
|
||||
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():
|
||||
"""Check if it's a valid wired network profile. """
|
||||
if profile_name not in wired.GetWiredProfileList():
|
||||
print('Profile of that name does not exist.')
|
||||
sys.exit(5)
|
||||
|
||||
|
||||
if options.scan and options.wireless:
|
||||
# synchronized scan
|
||||
wireless.Scan(True)
|
||||
@@ -163,10 +167,11 @@ if options.list_networks:
|
||||
if options.wireless:
|
||||
print('#\tBSSID\t\t\tChannel\tESSID')
|
||||
for network_id in range(0, wireless.GetNumberOfNetworks()):
|
||||
print(('%s\t%s\t%s\t%s' % (network_id,
|
||||
wireless.GetWirelessProperty(network_id, 'bssid'),
|
||||
wireless.GetWirelessProperty(network_id, 'channel'),
|
||||
wireless.GetWirelessProperty(network_id, 'essid'))))
|
||||
print('%s\t%s\t%s\t%s' %
|
||||
(network_id,
|
||||
wireless.GetWirelessProperty(network_id, 'bssid'),
|
||||
wireless.GetWirelessProperty(network_id, 'channel'),
|
||||
wireless.GetWirelessProperty(network_id, 'essid')))
|
||||
elif options.wired:
|
||||
print('#\tProfile name')
|
||||
i = 0
|
||||
@@ -186,22 +191,22 @@ if options.network_details:
|
||||
# we're connected to a network, print IP
|
||||
print(("IP: %s" % wireless.GetWirelessIP(0)))
|
||||
|
||||
print(("Essid: %s" % wireless.GetWirelessProperty(network_id, "essid")))
|
||||
print(("Bssid: %s" % wireless.GetWirelessProperty(network_id, "bssid")))
|
||||
print("Essid: %s" % wireless.GetWirelessProperty(network_id, "essid"))
|
||||
print("Bssid: %s" % wireless.GetWirelessProperty(network_id, "bssid"))
|
||||
if wireless.GetWirelessProperty(network_id, "encryption"):
|
||||
print("Encryption: On")
|
||||
print(("Encryption Method: %s" % \
|
||||
wireless.GetWirelessProperty(network_id, "encryption_method")))
|
||||
print("Encryption Method: %s" %
|
||||
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
|
||||
@@ -216,11 +221,11 @@ if options.network_property:
|
||||
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)
|
||||
wireless.SetWirelessProperty(network_id, options.network_property,
|
||||
options.set_to)
|
||||
elif options.wired:
|
||||
if not options.set_to:
|
||||
print((wired.GetWiredProperty(options.network_property)))
|
||||
@@ -232,13 +237,13 @@ if options.disconnect:
|
||||
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:
|
||||
@@ -247,16 +252,16 @@ if options.connect:
|
||||
is_valid_wireless_network_id(options.network)
|
||||
name = wireless.GetWirelessProperty(options.network, 'essid')
|
||||
encryption = wireless.GetWirelessProperty(options.network, 'enctype')
|
||||
print(("Connecting to %s with %s on %s" % (name, encryption,
|
||||
wireless.DetectWirelessInterface())))
|
||||
print("Connecting to %s with %s on %s" %
|
||||
(name, encryption, wireless.DetectWirelessInterface()))
|
||||
wireless.ConnectWireless(options.network)
|
||||
|
||||
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 = wired.CheckIfWiredConnecting
|
||||
@@ -273,8 +278,8 @@ if options.connect:
|
||||
while check():
|
||||
next_ = status()
|
||||
if next_ != last:
|
||||
# avoid a race condition where status is updated to "done" after
|
||||
# the loop check
|
||||
# avoid a race condition where status is updated to "done"
|
||||
# after the loop check
|
||||
if next_ == "done":
|
||||
break
|
||||
print((message()))
|
||||
@@ -284,14 +289,16 @@ if options.connect:
|
||||
exit_status = 6
|
||||
op_performed = True
|
||||
|
||||
|
||||
def str_properties(prop):
|
||||
""" Pretty print optional and required properties. """
|
||||
"""Pretty print optional and required properties."""
|
||||
if len(prop) == 0:
|
||||
return "None"
|
||||
else:
|
||||
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:'
|
||||
@@ -302,7 +309,7 @@ if options.wireless and options.list_encryption_types:
|
||||
print((' Req: %s' % str_properties(t['required'])))
|
||||
print('---')
|
||||
# don't print optionals (yet)
|
||||
#print ' Opt: %s' % str_properties(type['optional'])
|
||||
# print ' Opt: %s' % str_properties(type['optional'])
|
||||
i += 1
|
||||
op_performed = True
|
||||
|
||||
@@ -318,4 +325,3 @@ if not op_performed:
|
||||
print("No operations performed.")
|
||||
|
||||
sys.exit(exit_status)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user