~ubuntu-branches/ubuntu/saucy/wicd/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/03-fix_typo_wicd-cli.patch/cli/wicd-cli.py

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-03-04 10:59:30 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100304105930-jihcz2fn8e77uqbb
Tags: 1.7.0+ds1-1
* New repacked tarball, contains translations .po files
  (Closes: #572352)
* debian/bug-control added, let's add more info to the bugreports!
* debian/control:
  - debhelper dependency bumped to >= 7.2.3~, to use dh_bugfiles
  - added dependency on python-iniparse to wicd-daemon
* debian/rules:
  - make dh_bugfiles act on all packages
  - add get-orig-source target, to build a package containing the
    .po files
* debian/patches/:
  - 20-use_iniparse.patch added, uses python-iniparse instead of
    standard library's ConfigParser (Closes: #568326)
  - 21-fix_ESSID_setting.patch added, use correct command to set
    ESSID's (useful when a network is named "off" or "any")
    (Closes: #571100)
  - 22-fix_deprecation.patch added, fixes some deprecated usage of
    gtk.Statusbar (Closes: #571314)
* debian/watch updated to mangle Debian version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
#       This program is free software; you can redistribute it and/or modify
 
4
#       it under the terms of the GNU General Public License as published by
 
5
#       the Free Software Foundation; either version 2 of the License, or
 
6
#       (at your option) any later version.
 
7
#
 
8
#       This program is distributed in the hope that it will be useful,
 
9
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#       GNU General Public License for more details.
 
12
#
 
13
#       You should have received a copy of the GNU General Public License
 
14
#       along with this program; if not, write to the Free Software
 
15
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
16
#       MA 02110-1301, USA.
 
17
 
 
18
import optparse
 
19
import dbus
 
20
import dbus.service
 
21
import sys
 
22
from wicd import misc
 
23
 
 
24
misc.RenameProcess('wicd-cli')
 
25
 
 
26
if getattr(dbus, 'version', (0, 0, 0)) < (0, 80, 0):
 
27
    import dbus.glib
 
28
else:
 
29
    from dbus.mainloop.glib import DBusGMainLoop
 
30
    DBusGMainLoop(set_as_default=True)
 
31
 
 
32
bus = dbus.SystemBus()
 
33
try:
 
34
        daemon = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon'),
 
35
                        'org.wicd.daemon')
 
36
        wireless = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wireless'),
 
37
                        'org.wicd.daemon.wireless')
 
38
        wired = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wired'),
 
39
                        'org.wicd.daemon.wired')
 
40
        config = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/config'),
 
41
                        'org.wicd.daemon.config')
 
42
except dbus.DBusException:
 
43
        print 'Error: Could not connect to the daemon. Please make sure it is running.'
 
44
        sys.exit(3)
 
45
 
 
46
parser = optparse.OptionParser()
 
47
 
 
48
parser.add_option('--network', '-n', type='int', default=-1)
 
49
parser.add_option('--network-property', '-p')
 
50
parser.add_option('--set-to', '-s')
 
51
parser.add_option('--name', '-m')
 
52
 
 
53
parser.add_option('--scan', '-S', default=False, action='store_true')
 
54
parser.add_option('--save', '-w', default=False, action='store_true')
 
55
parser.add_option('--list-networks', '-l', default=False, action='store_true')
 
56
parser.add_option('--network-details', '-d', default=False, action='store_true')
 
57
parser.add_option('--disconnect', '-x', default=False, action='store_true')
 
58
parser.add_option('--connect', '-c', default=False, action='store_true')
 
59
parser.add_option('--list-encryption-types', '-e', default=False, action='store_true')
 
60
# short options for these two aren't great.
 
61
parser.add_option('--wireless', '-y', default=False, action='store_true')
 
62
parser.add_option('--wired', '-z', default=False, action='store_true')
 
63
parser.add_option('--load-profile', '-o', default=False, action='store_true')
 
64
 
 
65
options, arguments = parser.parse_args()
 
66
 
 
67
op_performed = False
 
68
 
 
69
if not (options.wireless or options.wired):
 
70
        print "Please use --wireless or --wired to specify " + \
 
71
        "the type of connection to operate on."
 
72
 
 
73
# functions
 
74
def is_valid_wireless_network_id(network_id):
 
75
        if not (network_id >= 0 \
 
76
                        and network_id < wireless.GetNumberOfNetworks()):
 
77
                print 'Invalid wireless network identifier.'
 
78
                sys.exit(1)
 
79
 
 
80
def is_valid_wired_network_id(network_id):
 
81
        num = len(wired.GetWiredProfileList())
 
82
        if not (network_id < num and \
 
83
                        network_id >= 0):
 
84
                print 'Invalid wired network identifier.'
 
85
                sys.exit(4)
 
86
 
 
87
def is_valid_wired_network_profile(profile_name):
 
88
        if not profile_name in wired.GetWiredProfileList():
 
89
                print 'Profile of that name does not exist.'
 
90
                sys.exit(5)
 
91
 
 
92
if options.scan and options.wireless:
 
93
        # synchronized scan
 
94
        wireless.Scan(True)
 
95
        op_performed = True
 
96
 
 
97
if options.load_profile and options.wired:
 
98
        is_valid_wired_network_profile(options.name)
 
99
        config.ReadWiredNetworkProfile(options.name)
 
100
        op_performed = True
 
101
 
 
102
if options.list_networks:
 
103
        if options.wireless:
 
104
                print '#\tBSSID\t\t\tChannel\tESSID'
 
105
                for network_id in range(0, wireless.GetNumberOfNetworks()):
 
106
                        print '%s\t%s\t%s\t%s' % (network_id,
 
107
                                wireless.GetWirelessProperty(network_id, 'bssid'),
 
108
                                wireless.GetWirelessProperty(network_id, 'channel'),
 
109
                                wireless.GetWirelessProperty(network_id, 'essid'))
 
110
        elif options.wired:
 
111
                print '#\tProfile name'
 
112
                id = 0
 
113
                for profile in wired.GetWiredProfileList():
 
114
                        print '%s\t%s' % (id, profile)
 
115
                        id += 1
 
116
        op_performed = True
 
117
 
 
118
if options.network_details:
 
119
        if options.wireless:
 
120
                if options.network >= 0:
 
121
                        is_valid_wireless_network_id(options.network)
 
122
                        network_id = options.network
 
123
                else:
 
124
                        network_id = wireless.GetCurrentNetworkID(0)
 
125
                        is_valid_wireless_network_id(network_id)
 
126
                        # we're connected to a network, print IP
 
127
                        print "IP: %s" % wireless.GetWirelessIP(0)
 
128
 
 
129
                print "Essid: %s" % wireless.GetWirelessProperty(network_id, "essid")
 
130
                print "Bssid: %s" % wireless.GetWirelessProperty(network_id, "bssid")
 
131
                if wireless.GetWirelessProperty(network_id, "encryption"):
 
132
                        print "Encryption: On"
 
133
                        print "Encryption Method: %s" % \
 
134
                                        wireless.GetWirelessProperty(network_id, "encryption_method")
 
135
                else:
 
136
                        print "Encryption: Off"
 
137
                print "Quality: %s" % wireless.GetWirelessProperty(network_id, "quality")
 
138
                print "Mode: %s" % wireless.GetWirelessProperty(network_id, "mode")
 
139
                print "Channel: %s" % wireless.GetWirelessProperty(network_id, "channel")
 
140
                print "Bit Rates: %s" % wireless.GetWirelessProperty(network_id, "bitrates")
 
141
        op_performed = True
 
142
 
 
143
# network properties
 
144
 
 
145
if options.network_property:
 
146
        options.network_property = option.network_property.lower()
 
147
        if options.wireless:
 
148
                if options.network >= 0:
 
149
                        is_valid_wireless_network_id(options.network)
 
150
                        network_id = options.network
 
151
                else:
 
152
                        network_id = wireless.GetCurrentNetworkID(0)
 
153
                        is_valid_wireless_network_id(network_id)
 
154
                if not options.set_to:
 
155
                        print wireless.GetWirelessProperty(network_id, options.network_property)
 
156
                else:
 
157
                        wireless.SetWirelessProperty(network_id, \
 
158
                                        options.network_property, options.set_to)
 
159
        elif options.wired:
 
160
                if not options.set_to:
 
161
                        print wired.GetWiredProperty(options.network_property)
 
162
                else:
 
163
                        wired.SetWiredProperty(options.network_property, options.set_to)
 
164
        op_performed = True
 
165
 
 
166
if options.disconnect:
 
167
        daemon.Disconnect()
 
168
        if options.wireless:
 
169
                if wireless.GetCurrentNetworkID(0) > -1:
 
170
                        print "Disconnecting from %s on %s" % (wireless.GetCurrentNetwork(0),
 
171
                                        wireless.DetectWirelessInterface())
 
172
        elif options.wired:
 
173
                if wired.CheckPluggedIn():
 
174
                        print "Disconnecting from wired connection on %s" % wired.DetectWiredInterface()
 
175
        op_performed = True
 
176
 
 
177
if options.connect:
 
178
        if options.wireless and options.network > -1:
 
179
                is_valid_wireless_network_id(options.network)
 
180
                name = wireless.GetWirelessProperty(options.network, 'essid')
 
181
                encryption = wireless.GetWirelessProperty(options.network, 'enctype')
 
182
                print "Connecting to %s with %s on %s" % (name, encryption,
 
183
                                wireless.DetectWirelessInterface())
 
184
                wireless.ConnectWireless(options.network)
 
185
 
 
186
                check = lambda: wireless.CheckIfWirelessConnecting()
 
187
                message = lambda: wireless.CheckWirelessConnectingMessage()
 
188
        elif options.wired:
 
189
                print "Connecting to wired connection on %s" % wired.DetectWiredInterface()
 
190
                wired.ConnectWired()
 
191
 
 
192
                check = lambda: wired.CheckIfWiredConnecting()
 
193
                message = lambda: wired.CheckWiredConnectingMessage()
 
194
 
 
195
        # update user on what the daemon is doing
 
196
        last = None
 
197
        while check():
 
198
                next = message()
 
199
                if next != last:
 
200
                        # avoid a race condition where status is updated to "done" after the
 
201
                        # loop check
 
202
                        if next == "done":
 
203
                                break
 
204
                        print "%s..." % next.replace("_", " ")
 
205
                        last = next
 
206
        print "done!"
 
207
        op_performed = True
 
208
 
 
209
# pretty print optional and required properties
 
210
def str_properties(prop):
 
211
        if len(prop) == 0:
 
212
                return "None"
 
213
        else:
 
214
                return ', '.join("%s (%s)" % (x[0], x[1].replace("_", " ")) for x in type['required'])
 
215
 
 
216
if options.wireless and options.list_encryption_types:
 
217
        et = misc.LoadEncryptionMethods()
 
218
        # print 'Installed encryption templates:'
 
219
        print '%s\t%-20s\t%s' % ('#', 'Name', 'Description')
 
220
        id = 0
 
221
        for type in et:
 
222
                print '%s\t%-20s\t%s' % (id, type['type'], type['name'])
 
223
                print '  Req: %s' % str_properties(type['required'])
 
224
                print '---'
 
225
                # don't print optionals (yet)
 
226
                #print '  Opt: %s' % str_properties(type['optional'])
 
227
                id += 1
 
228
        op_performed = True
 
229
 
 
230
if options.save and options.network > -1:
 
231
        if options.wireless:
 
232
                is_valid_wireless_network_id(options.network)
 
233
                config.SaveWirelessNetworkProfile(options.network)
 
234
        elif options.wired:
 
235
                config.SaveWiredNetworkProfile(options.name)
 
236
        op_performed = True
 
237
 
 
238
if not op_performed:
 
239
        print "No operations performed."
 
240