def run_window(self): """ Retrieve the window controller, ready it and run the mainloop """ from window import WindowController window_title = self.cli_flags.get("window-title", "Dragbox") if self.identifier: window_title += (" (%s)" % self.identifier) self.wc = WindowController(self.data_controller, window_title) self.wc.ready_window() try: from gtk import main main() except KeyboardInterrupt: # catch ctrl-c and exit nicely pass self.prepare_quit()
class dragbox(object): """ Dragbox application object """ def __init__(self): """ Entrance point for the dragbox application + Read commandline + Try dbus connection + Add items + Run """ # Read commandline switches (files, texts, cli_flags) = self.read_opts(argv[1:]) # Read from stdin from sys import stdin if not stdin.isatty(): pipe_text = stdin.read() if pipe_text: texts += [pipe_text] self.cli_flags = cli_flags get = self.cli_flags.get("get") list_names = self.cli_flags.get("list-names") nofork = self.cli_flags.get("no-fork") self.identifier = self.cli_flags.get("identifier") success = True quit_done = False if nofork: self.dbus_connection = None elif list_names: self.get_dbus_names() quit_done = True else: self.dbus_connection = self.get_dbus_connection() if get: success = self.try_get_data() quit_done = True elif not nofork and self.dbus_connection: self.dbus_send(files, texts) quit_done = True else: self.dbus_service = self.start_dbus_service() # Fork to disconnect from terminal pid = fork() if pid: quit_done = True if quit_done: raise SystemExit(not success) (self.data_controller, self.writer) = self.init_data() self.add_files(None, files) self.add_texts(None, texts) self.run_window() def init_data(self): """ Create the data controller and a connected plugins.Writer return a tuple of data controller and writer """ from data import DataController data_controller = DataController() quiet = self.cli_flags.get("quiet", True) onexit = self.cli_flags.get("write-on-exit", False) if onexit or not quiet: writer = self.make_writer() if not quiet: data_controller.connect("item-added", writer.print_item) else: writer = None return (data_controller, writer) def make_writer(self): """ Return a plugins.Writer object Initialized with current null termination and paths/uris settings """ from plugins import Writer null_term = self.cli_flags.get("null", False) paths = self.cli_flags.get("paths", True) writer = Writer(paths, null_term) return writer def run_window(self): """ Retrieve the window controller, ready it and run the mainloop """ from window import WindowController window_title = self.cli_flags.get("window-title", "Dragbox") if self.identifier: window_title += (" (%s)" % self.identifier) self.wc = WindowController(self.data_controller, window_title) self.wc.ready_window() try: from gtk import main main() except KeyboardInterrupt: # catch ctrl-c and exit nicely pass self.prepare_quit() # quit def prepare_quit(self): exit = self.cli_flags.get("write-on-exit") if self.writer and exit: self.writer.print_all(self.data_controller) def read_opts(self, argv): """ Uses gnu getopt to read command line arguments returns a tuple of (files, texts, options) """ from getopt import gnu_getopt, GetoptError short_opts = "hf:t:nvpu0axm:" long_opts = [ 'help', 'file=', 'text=', 'no-fork', 'version', 'paths', 'uris', 'null-terminate', 'window-title=', 'write-on-exit', 'write-async', 'get', 'name=', 'list' ] try: (options, arguments) = gnu_getopt(argv, short_opts, long_opts) except GetoptError, exception: print_error("%s" % exception) self.print_usage(usage_error=True) prefs = {} # store command line flags in `prefs` # quiet: if True, output async # list-names: if True, list running instances for (o,a) in options: if o in ("-h", "--help"): self.print_usage() elif o in ("-v", "--version"): self.print_version() elif o in ("-n", "--no-fork"): prefs["no-fork"] = True prefs["quiet"] = True prefs["write-on-exit"] = True elif o in ("-p", "--paths"): prefs["paths"] = True elif o in ("-u", "--uris"): prefs["paths"] = False elif o in ("-0", "--null-terminate"): prefs["null"] = True elif o in ("--window-title"): prefs["window-title"] = a elif o in ("-x", "--write-on-exit"): # don't write _asyncronously_ prefs["quiet"] = True prefs["write-on-exit"] = True elif o in ("-a", "--write-async"): # don't write _asyncronously_ prefs["quiet"] = False prefs["write-on-exit"] = False elif o in ("--get"): prefs["get"] = True elif o in ("-m", "--name"): prefs["identifier"] = a elif o in ("--list"): prefs["list-names"] = True # Handle strongly typed items files = [] texts = [] for (o, a) in options: if o in ("-f", "--file"): path = self.check_file_exists(a, warn=True) if path: files.append(path) elif o in ("-t", "--text"): texts.append(a) # Handle rest # Try if they are files text = "" for item in arguments : path = self.check_file_exists(item, warn=False) if path: files.append(path) else: text = text + " " + item # Take rest as one text block if text: texts.append(text) return (files, texts, prefs)