def opt_table(table): opt_list = [] for r in table: opt = "--" + r[1] if r[0]: opt = "-" + r[0] + ", " + opt if r[2]: opt = opt + "=<" + r[2] + ">" opt_list.append([opt + " ", r[3]]) # By appending [0,0], we insure that this will work even if # opt_list is empty (which it never should be) max_len = apply(max, map(lambda x:len(x[0]), opt_list) + [0,0]) for opt, desc_str in opt_list: if 79 - max_len > 10: desc = linebreak(desc_str, 79 - max_len) else: desc = [desc_str] desc_first = desc.pop(0) nmtalk.message(string.ljust(opt, max_len) + desc_first) for d in desc: nmtalk.message(" " * max_len + d)
def execute(self, options_dict, non_option_args): manager = self.Manager() current_state = manager.get_wireless_state() if len(non_option_args) < 1: if current_state: print "Enabled" else: print "Disabled" return 0 new_state = non_option_args[0].lower() if new_state == "on" or new_state == "enable" or new_state == "yes" or new_state == "1": if not current_state: if not manager.get_wireless_hw_state(): nmtalk.message("Wireless hardware is disabled, can not enable wireless") return 1 nmtalk.message("Enabling wireless") manager.set_wireless_state(True) else: nmtalk.message("Wireless is already enabled") elif new_state == "off" or new_state == "disable" or new_state == "no" or new_state == 0: if current_state: nmtalk.message("Disabling wireless") manager.set_wireless_state(False) else: nmtalk.message("Wireless is already disabled") else: nmtalk.fatal("Invalid argument '%s'. Please use 'on' or 'off'" % non_option_args[0]) return 0
def monitor(state): nmtalk.message("'%s': %s" % (device_iface, nmdevice.device_state_to_str(state))) if state == nmdevice.DEVICE_STATE_ACTIVATED: loop.quit() elif state == nmdevice.DEVICE_STATE_FAILED or state == nmdevice.DEVICE_STATE_DISCONNECTED or \ state == nmdevice.DEVICE_STATE_CANCELLED: loop.quit() self.exit_status = 1
def execute(self, options_dict, non_option_args): manager = self.Manager() devices = [] if len(non_option_args) < 1: # Get all wireless devices for device in manager.get_devices(): if isinstance(device, nmwirelessdevicecmds.WirelessDevice): devices.append(device) else: for device in manager.get_devices(): for arg in non_option_args: if device.get_interface() == arg: if isinstance(device, nmwirelessdevicecmds.WirelessDevice): devices.append(device) else: nmtalk.error("Wireless device expected, got %s" % str(device)) got_devices = len(devices) if got_devices < 1: nmtalk.error("No wireless devices found.") return 1 for device in devices: if got_devices > 1: nmtalk.message("Access Points for device '%s':" % device.get_interface()) aps = device.get_access_points() if not aps: nmtalk.message("No access points found.") continue rows = [] active_ap = device.get_active_access_point() if active_ap: active_ssid = active_ap.get_ssid() else: active_ssid = None for ap in aps: if active_ssid == ap.get_ssid(): active = "*" else: active = "" rows.append ((str(ap), active)) rows.sort(lambda x,y:cmp(x[0].lower(), y[0].lower())) nmformat.tabular(("SSID", "Active"), rows) return 0
def monitor(state, reason): msg = "'%s': %s" % (connection_name, nmvpn.vpn_connection_state_to_str(state)) if reason != nmvpn.VPN_CONNECTION_STATE_REASON_NONE: msg += " (%s)" % nmvpn.vpn_connection_state_reason_to_str(reason) nmtalk.message(msg) if state == nmvpn.VPN_CONNECTION_STATE_ACTIVATED: loop.quit() elif state == nmvpn.VPN_CONNECTION_STATE_FAILED or state == nmvpn.VPN_CONNECTION_STATE_DISCONNECTED: loop.quit() self.exit_status = 1
def execute(self, options_dict, non_option_args): manager = self.Manager() current_state = manager.get_state() if len(non_option_args) < 1: if current_state == nm.MANAGER_STATE_SLEEPING: print "Disabled" else: print "Enabled" return 0 new_state = non_option_args[0].lower() if new_state == "on" or new_state == "enable" or new_state == "yes" or new_state == "1": if current_state == nm.MANAGER_STATE_SLEEPING: nmtalk.message("Enabling networking") manager.sleep(False) else: nmtalk.message("Networking is already enabled") elif new_state == "off" or new_state == "disable" or new_state == "no" or new_state == 0: if current_state != nm.MANAGER_STATE_SLEEPING: nmtalk.message("Disabling networking") manager.sleep(True) else: nmtalk.message("Networking is already disabled") else: nmtalk.fatal("Invalid argument '%s'. Please use 'on' or 'off'" % non_option_args[0]) return 0
def monitor(state, reason): msg = "'%s': %s" % (connection_name, nmvpn.vpn_connection_state_to_str(state)) if reason != nmvpn.VPN_CONNECTION_STATE_REASON_NONE: msg += " (%s)" % nmvpn.vpn_connection_state_reason_to_str( reason) nmtalk.message(msg) if state == nmvpn.VPN_CONNECTION_STATE_ACTIVATED: loop.quit() elif state == nmvpn.VPN_CONNECTION_STATE_FAILED or state == nmvpn.VPN_CONNECTION_STATE_DISCONNECTED: loop.quit() self.exit_status = 1
def execute(self, options_dict, non_option_args): manager = nmsettings.ConnectionManager() if options_dict.has_key("system"): connections = manager.get_connections( nmsettings.DBUS_SERVICE_SYSTEM_SETTINGS, "vpn") print_service = False elif options_dict.has_key("user"): connections = manager.get_connections( nmsettings.DBUS_SERVICE_USER_SETTINGS, "vpn") print_service = False else: connections = manager.get_connections( nmsettings.DBUS_SERVICE_SYSTEM_SETTINGS, "vpn") connections += manager.get_connections( nmsettings.DBUS_SERVICE_USER_SETTINGS, "vpn") print_service = True vpn_manager = nmvpn.VpnManager() active_map = {} for c in vpn_manager.list_active_connections(): active_map[c.get_name()] = nmvpn.vpn_connection_state_to_str( c.get_state()) rows = [] for connection in connections: name = connection.get_id() r = [name, active_map.get(name, "")] if print_service: if connection.service == nmsettings.DBUS_SERVICE_USER_SETTINGS: r.append("User") else: r.append("System") rows.append(r) if rows: headers = ["Name", "Status"] if print_service: headers.append("Service type") rows.sort(lambda x, y: cmp(x[0].lower(), y[0].lower())) nmformat.tabular(headers, rows) else: nmtalk.message("No VPN connections found.") return 0
def execute(self, options_dict, non_option_args): manager = self.Manager() device_list = manager.get_devices() if len(device_list) < 1: nmtalk.message("No devices found.") return 0 table_rows = [] for device in device_list: row = (device.get_interface(), str(device), nmdevice.device_state_to_str(device.get_state())) table_rows.append(row) nmformat.tabular(("Interface", "Type", "State"), table_rows) return 0
def execute(self, options_dict, non_option_args): manager = nmsettings.ConnectionManager() if options_dict.has_key("system"): connections = manager.get_connections(nmsettings.DBUS_SERVICE_SYSTEM_SETTINGS, "vpn") print_service = False elif options_dict.has_key("user"): connections = manager.get_connections(nmsettings.DBUS_SERVICE_USER_SETTINGS, "vpn") print_service = False else: connections = manager.get_connections(nmsettings.DBUS_SERVICE_SYSTEM_SETTINGS, "vpn") connections += manager.get_connections(nmsettings.DBUS_SERVICE_USER_SETTINGS, "vpn") print_service = True vpn_manager = nmvpn.VpnManager() active_map = {} for c in vpn_manager.list_active_connections(): active_map[c.get_name()] = nmvpn.vpn_connection_state_to_str(c.get_state()) rows = [] for connection in connections: name = connection.get_id() r = [name, active_map.get(name, "")] if print_service: if connection.service == nmsettings.DBUS_SERVICE_USER_SETTINGS: r.append("User") else: r.append("System") rows.append(r) if rows: headers = ["Name", "Status"] if print_service: headers.append("Service type") rows.sort(lambda x,y:cmp(x[0].lower(), y[0].lower())) nmformat.tabular(headers, rows) else: nmtalk.message("No VPN connections found.") return 0
def execute(self, options_dict, non_option_args): manager = nmsettings.ConnectionManager() if options_dict.has_key("system"): connections = manager.get_connections(nmsettings.DBUS_SERVICE_SYSTEM_SETTINGS) print_service = False elif options_dict.has_key("user"): connections = manager.get_connections(nmsettings.DBUS_SERVICE_USER_SETTINGS) print_service = False else: connections = manager.get_connections(nmsettings.DBUS_SERVICE_SYSTEM_SETTINGS) connections += manager.get_connections(nmsettings.DBUS_SERVICE_USER_SETTINGS) print_service = True rows = [] for connection in connections: r = [connection.get_id(), connection.get_type()] if print_service: if connection.service == nmsettings.DBUS_SERVICE_USER_SETTINGS: r.append("User") else: r.append("System") rows.append(r) if rows: if options_dict.has_key("sort-by-type"): rows.sort(lambda x,y:cmp(x[1].lower(), y[1].lower())) else: rows.sort(lambda x,y:cmp(x[0].lower(), y[0].lower())) headers = ["Name", "Type"] if print_service: headers.append("Service type") nmformat.tabular(headers, rows) else: nmtalk.message("No connections found.") return 0
def print_command_list(commands, with_categories=0): max_len = 0 cmd_list = [] for c in commands: name, aliases, description, category = c if aliases: name = name + " (" + string.join(aliases, ", ") + ")" cmd_list.append([name, description, category]) max_len = max(max_len, len(name)) desc_len = max_len + 4 cmd_list.sort(command_sort) previous_category = "we will banish all dwarves from the love kingdom" for c in cmd_list: name, description, category = c if with_categories and category != previous_category: if category_data.has_key(category): category_name = category_data[category][0] else: category_name = string.upper(category[0]) + category[1:] nmtalk.message("\n" + category_name + " commands:") previous_category = category # If, for some reason, the command list is *really* wide (which it never should # be), don't do something stupid. if 79 - desc_len > 10: desc = nmformat.linebreak(description, 79-desc_len) else: desc = [description] desc_first = desc.pop(0) nmtalk.message(" " + string.ljust(name, max_len) + " " + desc_first) for d in desc: nmtalk.message(" " * desc_len + d)
def tabular(headers, table): def row_to_string(row, col_sizes): if nmtalk.be_terse: return string.join(row, "|") else: return string.join(pad_row(row, col_sizes), " | ") col_sizes = max_col_widths(table) if headers and not nmtalk.be_terse: col_sizes = map(max, map(len, headers), col_sizes) # print headers nmtalk.message(string.join(pad_row(headers, col_sizes), " | ")) # print head/body separator nmtalk.message( string.join(map(lambda x: stutter("-", x), col_sizes), "-+-")) # print table body for r in table: nmtalk.message(row_to_string(r, col_sizes))
def tabular(headers, table): def row_to_string(row, col_sizes): if nmtalk.be_terse: return string.join(row, "|") else: return string.join(pad_row(row, col_sizes), " | ") col_sizes = max_col_widths(table) if headers and not nmtalk.be_terse: col_sizes = map(max, map(len,headers), col_sizes) # print headers nmtalk.message(string.join(pad_row(headers, col_sizes), " | ")) # print head/body separator nmtalk.message(string.join (map(lambda x:stutter("-",x), col_sizes), "-+-")) # print table body for r in table: nmtalk.message(row_to_string(r, col_sizes))
def usage_basic(): nmtalk.message("Usage: nm <command> <options> ...") nmtalk.message("") keys = command_dict.keys() if keys: keys.sort() command_list = [] for k in keys: description, constructor, aliases, hidden, basic, category = command_dict[k] if not hidden and basic: command_list.append([k, aliases, description, category]) nmtalk.message("Some basic commands are:") print_command_list(command_list) nmtalk.message("") nmtalk.message("For a more complete list of commands and important options,") nmtalk.message("run \"nm help\".") nmtalk.message("") else: nmtalk.error("<< No commands found --- something is wrong! >>")
def aligned(table): col_sizes = max_col_widths(table) for r in table: nmtalk.message(string.join(pad_row(r, col_sizes), " "))
def separated(table, separator): for r in table: nmtalk.message(string.join(clean_row(r, separator), separator + " "))
nmtalk.show_messages = 0 nmtalk.show_warnings = 0 if opt_dict.has_key("verbose"): nmtalk.show_verbose = 1 ### Whitespace is nice, so we always print a blank line before ### executing the command if not nmtalk.be_terse: nmtalk.message("") try: command.execute(opt_dict, args) except IOError, e: if e.errno == 13: nmtalk.error("You must be root to execute this command") else: show_exception(e) sys.exit(1) except Exception, e: show_exception(e) sys.exit(1) ### Whitespace is nice, so we always print a blank line after ### executing the command if not nmtalk.be_terse: nmtalk.message("")
def main(ver, nm_dir): global local global nm_version nm_version = ver if os.environ.has_key("NM_DEBUG"): nmtalk.show_debug = 1 import_commands(nm_dir) ### ### Grab the option list and extract the first non-option argument that ### looks like a command. This could get weird if someone passes the name ### of a command as the argument to an option. ### argv = sys.argv[1:] argv = nmcommand.expand_synthetic_args(argv) if "--version" in argv: print print nm_name + " " + nm_version print nm_copyright print sys.exit(0) command = nmcommand.extract_command_from_argv(argv) if "-?" in argv or "--help" in argv: command.usage() sys.exit(0) # A hack to suppress extra whitespace when dumping. if command.name() == "dump": nmtalk.be_terse = 1 argv = nmcommand.get_user_default_args(argv, command) opt_dict, args = command.process_argv(argv) ### ### Control verbosity ### if opt_dict.has_key("terse"): nmtalk.be_terse = 1 if opt_dict.has_key("quiet"): nmtalk.show_messages = 0 nmtalk.show_warnings = 0 if opt_dict.has_key("verbose"): nmtalk.show_verbose = 1 ### Whitespace is nice, so we always print a blank line before ### executing the command if not nmtalk.be_terse: nmtalk.message("") try: command.execute(opt_dict, args) except IOError, e: if e.errno == 13: nmtalk.error("You must be root to execute this command") else: show_exception(e) sys.exit(1)
def usage(self): nmtalk.message("") nmtalk.message("Usage: nm " + self.name() + " <options> " + \ self.arguments()) nmtalk.message("") description = self.description_long() or self.description_short() if description: description = "'" + self.name() + "': " + description for l in nmformat.linebreak(description, 72): nmtalk.message(l) nmtalk.message("") opts = self.local_opt_table() if opts: nmtalk.message("'" + self.name() + "' Options:") nmformat.opt_table(opts) nmtalk.message("") opts = self.default_opt_table() if opts: nmtalk.message("General Options:") nmformat.opt_table(opts) nmtalk.message("") exit(1)
if opt_dict.has_key("verbose"): nmtalk.show_verbose = 1 ### Whitespace is nice, so we always print a blank line before ### executing the command if not nmtalk.be_terse: nmtalk.message("") try: command.execute(opt_dict, args) except IOError, e: if e.errno == 13: nmtalk.error("You must be root to execute this command") else: show_exception(e) sys.exit(1) except Exception, e: show_exception(e) sys.exit(1) ### Whitespace is nice, so we always print a blank line after ### executing the command if not nmtalk.be_terse: nmtalk.message("")
def usage_full(): nmtalk.message("Usage: nm <command> <options> ...") nmtalk.message("") nmtalk.message("The following options are understood by all commands:") nmformat.opt_table(default_opt_table) keys = command_dict.keys() if keys: command_list = [] for k in keys: description, constructor, aliases, hidden, basic, category = command_dict[k] if not hidden: command_list.append([k, aliases, description, category]) print_command_list(command_list, with_categories=1) nmtalk.message("") nmtalk.message("For more detailed information about a specific command,") nmtalk.message("run 'nm <command name> --help'.") nmtalk.message("") else: nmtalk.error("<< No commands found --- something is wrong! >>")