From be8b3fd63dd323bcee4c03a46820c086d65e34b7 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Sat, 15 Dec 2007 13:22:14 +0100 Subject: [PATCH] Clean up coding style and small issues in main.py --- Dragbox/main.py | 119 +++++++++++++++++++++++++++----------------------------- 1 file changed, 57 insertions(+), 62 deletions(-) diff --git a/Dragbox/main.py b/Dragbox/main.py index 087c25b..6001585 100644 --- a/Dragbox/main.py +++ b/Dragbox/main.py @@ -23,19 +23,18 @@ from os import path from utils import print_error class dragbox(object): - u""" - dragbox controller object - """ - + Dragbox application object + """ def __init__(self): - """ + """ This class runs one instance of dragbox + + Read commandline + Try dbus connection + Add items - + Run - """ + + Run + """ # Read commandline switches (files, texts, cli_flags) = self.read_opts(argv[1:]) @@ -65,6 +64,7 @@ class dragbox(object): window_title = cli_flags.get("window-title", "Dragbox") self.wc = WindowController(self.data, window_title) self.wc.ready_window() + try: from gtk import main main() @@ -73,11 +73,11 @@ class dragbox(object): raise SystemExit def read_opts(self, argv): - """ + """ Uses gnu getopt to read command line arguments - returns (files, texts, options) - - """ + + returns a tuple of (files, texts, options) + """ from getopt import gnu_getopt, GetoptError short_opts = "nhf:t:vqpu0" long_opts = [ @@ -88,11 +88,11 @@ class dragbox(object): try: (options, arguments) = gnu_getopt(argv, short_opts, long_opts) except GetoptError, exception: - print_error("Sorry, %s" % exception) # prints "option * not recognized" - self.print_usage() + print_error("Sorry, %s" % exception) + self.print_usage(usage_error=True) prefs = {} # Search for preferences - for (o,a) in options: + for (o,a) in options: if o in ("-h", "--help"): self.print_usage() elif o in ("-v", "--version"): @@ -118,7 +118,7 @@ class dragbox(object): for (o, a) in options: if o in ("-f", "--file"): path = self.check_file_exists(a, warn=True) - if path: files.append(path) + if path: files.append(path) elif o in ("-t", "--text"): texts.append(a) # Handle rest @@ -135,24 +135,24 @@ class dragbox(object): if text: texts.append(text) return (files, texts, prefs) - def init_gnome(self): - """ + def init_gnome(self): + """ we have to init gnome properly - """ + """ import globals from gnome import init, PARAM_APP_DATADIR, PARAM_ENABLE_SOUND - properties = { + properties = { PARAM_APP_DATADIR : globals.data_dir, - PARAM_ENABLE_SOUND : False + PARAM_ENABLE_SOUND : False } init(u"dragbox", globals.version, properties=properties) - def try_dbus_send(self, files, texts): - """ - Tries to connect ot already running instance, - if that fails starts a dbus service - - """ + def try_dbus_send(self, files, texts): + """ + Tries to connect ot already running instance + + If none found, start a new dbus service + """ from utils import print_debug, print_error from globals import dbus_enabled if dbus_enabled != "yes": @@ -164,35 +164,32 @@ class dragbox(object): print_error(info) return False - should_die = False - + send_success = False try: dclient = dbuscontrol.client() iface = dclient.get_interface() if files: iface.send_files(files) if texts: iface.send_texts(texts) - should_die = True + send_success = True print_debug("Sent files, quitting") except Exception, info: print_debug(info) - print_debug("Trying to start dbus server") + print_debug("Trying to start dbus service") try: dserver = dbuscontrol.make_server() dserver.set_delegate(self) except Exception, info: print_debug(info) - print_error("Failed to start dbus, might not be available") - return should_die + print_error("Failed to use dbus, might not be available") + return send_success def check_file_exists(self, f, warn=True): - """ - Checks if a file exists - @param f: the local path to the file - @type f: a string object. + """ + Check if a file exists - @param warn: If True, this method prints an error message and exits - @type warn: a bool object. - """ + f: a local path + warn: If True, print an error message and exit if nonexistant + """ p = path.abspath(f) if path.exists(p): return p @@ -202,39 +199,35 @@ class dragbox(object): raise SystemExit(1) def add_files(self, flist): - """ + """ Adds a list of files to the dragbox - @param flist: List of absolute paths - @type flist: a list object. - - """ + + flist: a list of absolute paths + """ from dragitem import FILE_TYPE - + for f in flist: fileloc = path.abspath(f) self.data.add_item(fileloc, FILE_TYPE) def add_texts(self, tlist): - """ + """ Adds a list of text to the dragbox - @param tlist: List of strings - @type tlist: a list object. - - """ + + tlist: a list of strings + """ from dragitem import TEXT_TYPE - + for text in tlist: self.data.add_item(text, TEXT_TYPE) def activate_application(self): - """ - Activates, brings to front - For dbus use - - """ - self.wc.show_window() #make sure it's in front + """ + Activate and bring to front + """ + self.wc.show_window() - def print_usage(self): + def print_usage(self, usage_error=False): print """ Usage: %s [-nq] item [...] @@ -247,19 +240,21 @@ Usage: %s [-nq] item [...] --window-title name set window title to name -n, --no-fork don't fork (also disables use of dbus) - + The options below only apply with --no-fork: -q, --quiet don't print dragged-in items -p, --paths print paths of dragged-in files (default) -u, --uris print uris of dragged-in files - -0, --null-terminate separate items' output with \\0, not \\n - + -0, --null-terminate separate items' output with \\0, not \\n + -h, --help display this help -v, --version display version information """ % "dragbox" - raise SystemExit(1) + raise SystemExit(usage_error) + def print_version(self): import globals print "%s %s\n\n%s" % (globals.package_name, globals.version, globals.copyright_info) raise SystemExit + -- 2.11.4.GIT