Example #1
0
    def run(self, stdscr):
        """
        This method is called by the curses.wrapper to start the mainloop and
        screen.

        :param stdscr: curses screen passed in from curses.wrapper

        """
        # We want to do an interactive session, so start up the curses screen and
        # pass it the function that handles commands
        colors.init_colors()
        self.statusbars = StatusBars()
        from modes.connectionmanager import ConnectionManager
        self.stdscr = stdscr
        self.screen = ConnectionManager(stdscr, self.encoding)
        self.eventlog = EventLog()

        self.screen.topbar = "{!status!}Deluge " + deluge.common.get_version(
        ) + " Console"
        self.screen.bottombar = "{!status!}"
        self.screen.refresh()

        # The Screen object is designed to run as a twisted reader so that it
        # can use twisted's select poll for non-blocking user input.
        reactor.addReader(self.screen)

        # Start the twisted mainloop
        reactor.run()
Example #2
0
    def run(self, stdscr):
        """
        This method is called by the curses.wrapper to start the mainloop and
        screen.

        :param stdscr: curses screen passed in from curses.wrapper

        """
        # We want to do an interactive session, so start up the curses screen and
        # pass it the function that handles commands
        colors.init_colors()
        self.statusbars = StatusBars()
        from modes.connectionmanager import ConnectionManager
        self.stdscr = stdscr
        self.screen = ConnectionManager(stdscr, self.encoding)
        self.eventlog = EventLog()

        self.screen.topbar = "{!status!}Deluge " + deluge.common.get_version() + " Console"
        self.screen.bottombar = "{!status!}"
        self.screen.refresh()

        # The Screen object is designed to run as a twisted reader so that it
        # can use twisted's select poll for non-blocking user input.
        reactor.addReader(self.screen)

        # Start the twisted mainloop
        reactor.run()
Example #3
0
class ConsoleUI(component.Component):
    def __init__(self, args=None, cmds = None, daemon = None):
        component.Component.__init__(self, "ConsoleUI", 2)

        # keep track of events for the log view
        self.events = []

        try:
            locale.setlocale(locale.LC_ALL, '')
            self.encoding = locale.getpreferredencoding()
        except:
            self.encoding = sys.getdefaultencoding()

        log.debug("Using encoding: %s", self.encoding)


        # start up the session proxy
        self.sessionproxy = SessionProxy()

        client.set_disconnect_callback(self.on_client_disconnect)

        # Set the interactive flag to indicate where we should print the output
        self.interactive = True
        self._commands = cmds
        if args:
            args = ' '.join(args)
            self.interactive = False
            if not cmds:
                print "Sorry, couldn't find any commands"
                return
            else:
                from commander import Commander
                cmdr = Commander(cmds)
                if daemon:
                    cmdr.exec_args(args,*daemon)
                else:
                    cmdr.exec_args(args,None,None,None,None)


        self.coreconfig = CoreConfig()
        if self.interactive and not deluge.common.windows_check():
            # We use the curses.wrapper function to prevent the console from getting
            # messed up if an uncaught exception is experienced.
            import curses.wrapper
            curses.wrapper(self.run)
        elif self.interactive and deluge.common.windows_check():
            print """\nDeluge-console does not run in interactive mode on Windows. \n
Please use commands from the command line, eg:\n
    deluge-console.exe help
    deluge-console.exe info
    deluge-console.exe "add --help"
    deluge-console.exe "add -p c:\\mytorrents c:\\new.torrent"
            """
        else:
            reactor.run()

    def run(self, stdscr):
        """
        This method is called by the curses.wrapper to start the mainloop and
        screen.

        :param stdscr: curses screen passed in from curses.wrapper

        """
        # We want to do an interactive session, so start up the curses screen and
        # pass it the function that handles commands
        colors.init_colors()
        self.statusbars = StatusBars()
        from modes.connectionmanager import ConnectionManager
        self.stdscr = stdscr
        self.screen = ConnectionManager(stdscr, self.encoding)
        self.eventlog = EventLog()

        self.screen.topbar = "{!status!}Deluge " + deluge.common.get_version() + " Console"
        self.screen.bottombar = "{!status!}"
        self.screen.refresh()

        # The Screen object is designed to run as a twisted reader so that it
        # can use twisted's select poll for non-blocking user input.
        reactor.addReader(self.screen)

        # Start the twisted mainloop
        reactor.run()


    def start(self):
        # Maintain a list of (torrent_id, name) for use in tab completion
        self.torrents = []
        if not self.interactive:
            self.started_deferred = defer.Deferred()
            def on_session_state(result):
                def on_torrents_status(torrents):
                    for torrent_id, status in torrents.items():
                        self.torrents.append((torrent_id, status["name"]))
                    self.started_deferred.callback(True)

                client.core.get_torrents_status({"id": result}, ["name"]).addCallback(on_torrents_status)
            client.core.get_session_state().addCallback(on_session_state)


    def match_torrent(self, string):
        """
        Returns a list of torrent_id matches for the string.  It will search both
        torrent_ids and torrent names, but will only return torrent_ids.

        :param string: str, the string to match on

        :returns: list of matching torrent_ids. Will return an empty list if
            no matches are found.

        """
        if self.interactive and isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy):
            return self.screen.match_torrent(string)
        matches  = []

        string = string.decode(self.encoding)
        for tid, name in self.torrents:
            if tid.startswith(string) or name.startswith(string):
                matches.append(tid)

        return matches


    def get_torrent_name(self, torrent_id):
        if self.interactive and hasattr(self.screen,"get_torrent_name"):
            return self.screen.get_torrent_name(torrent_id)

        for tid, name in self.torrents:
            if torrent_id == tid:
                return name

        return None


    def set_batch_write(self, batch):
        if self.interactive and isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy):
            return self.screen.set_batch_write(batch)

    def tab_complete_torrent(self, line):
        if self.interactive and isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy):
            return self.screen.tab_complete_torrent(line)

    def tab_complete_path(self, line, type="file", ext="", sort="name", dirs_first=True):
        if self.interactive and isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy):
            return self.screen.tab_complete_path(line, type=type, ext=ext, sort=sort, dirs_first=dirs_first)

    def set_mode(self, mode):
        reactor.removeReader(self.screen)
        self.screen = mode
        self.statusbars.screen = self.screen
        reactor.addReader(self.screen)
        mode.refresh()

    def on_client_disconnect(self):
        component.stop()

    def write(self, s):
        if self.interactive:
            if isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy):
                self.screen.write(s)
            else:
                component.get("LegacyUI").add_line(s, False)
                self.events.append(s)
        else:
            print colors.strip_colors(s.encode(self.encoding))

    def write_event(self, s):
        if self.interactive:
            if isinstance(self.screen,deluge.ui.console.modes.legacy.Legacy):
                self.events.append(s)
                self.screen.write(s)
            else:
                component.get("LegacyUI").add_line(s, False)
                self.events.append(s)
        else:
            print colors.strip_colors(s.encode(self.encoding))
Example #4
0
class ConsoleUI(component.Component):
    def __init__(self, args=None, cmds=None, daemon=None):
        component.Component.__init__(self, "ConsoleUI", 2)

        # keep track of events for the log view
        self.events = []

        try:
            locale.setlocale(locale.LC_ALL, '')
            self.encoding = locale.getpreferredencoding()
        except:
            self.encoding = sys.getdefaultencoding()

        log.debug("Using encoding: %s", self.encoding)

        # start up the session proxy
        self.sessionproxy = SessionProxy()

        client.set_disconnect_callback(self.on_client_disconnect)

        # Set the interactive flag to indicate where we should print the output
        self.interactive = True
        self._commands = cmds
        if args:
            args = ' '.join(args)
            self.interactive = False
            if not cmds:
                print "Sorry, couldn't find any commands"
                return
            else:
                from commander import Commander
                cmdr = Commander(cmds)
                if daemon:
                    cmdr.exec_args(args, *daemon)
                else:
                    cmdr.exec_args(args, None, None, None, None)

        self.coreconfig = CoreConfig()
        if self.interactive and not deluge.common.windows_check():
            # We use the curses.wrapper function to prevent the console from getting
            # messed up if an uncaught exception is experienced.
            import curses.wrapper
            curses.wrapper(self.run)
        elif self.interactive and deluge.common.windows_check():
            print """\nDeluge-console does not run in interactive mode on Windows. \n
Please use commands from the command line, eg:\n
    deluge-console.exe help
    deluge-console.exe info
    deluge-console.exe "add --help"
    deluge-console.exe "add -p c:\\mytorrents c:\\new.torrent"
            """
        else:
            reactor.run()

    def run(self, stdscr):
        """
        This method is called by the curses.wrapper to start the mainloop and
        screen.

        :param stdscr: curses screen passed in from curses.wrapper

        """
        # We want to do an interactive session, so start up the curses screen and
        # pass it the function that handles commands
        colors.init_colors()
        self.statusbars = StatusBars()
        from modes.connectionmanager import ConnectionManager
        self.stdscr = stdscr
        self.screen = ConnectionManager(stdscr, self.encoding)
        self.eventlog = EventLog()

        self.screen.topbar = "{!status!}Deluge " + deluge.common.get_version(
        ) + " Console"
        self.screen.bottombar = "{!status!}"
        self.screen.refresh()

        # The Screen object is designed to run as a twisted reader so that it
        # can use twisted's select poll for non-blocking user input.
        reactor.addReader(self.screen)

        # Start the twisted mainloop
        reactor.run()

    def start(self):
        # Maintain a list of (torrent_id, name) for use in tab completion
        self.torrents = []
        if not self.interactive:
            self.started_deferred = defer.Deferred()

            def on_session_state(result):
                def on_torrents_status(torrents):
                    for torrent_id, status in torrents.items():
                        self.torrents.append((torrent_id, status["name"]))
                    self.started_deferred.callback(True)

                client.core.get_torrents_status({
                    "id": result
                }, ["name"]).addCallback(on_torrents_status)

            client.core.get_session_state().addCallback(on_session_state)

    def match_torrent(self, string):
        """
        Returns a list of torrent_id matches for the string.  It will search both
        torrent_ids and torrent names, but will only return torrent_ids.

        :param string: str, the string to match on

        :returns: list of matching torrent_ids. Will return an empty list if
            no matches are found.

        """
        if self.interactive and isinstance(
                self.screen, deluge.ui.console.modes.legacy.Legacy):
            return self.screen.match_torrent(string)
        matches = []

        string = string.decode(self.encoding)
        for tid, name in self.torrents:
            if tid.startswith(string) or name.startswith(string):
                matches.append(tid)

        return matches

    def get_torrent_name(self, torrent_id):
        if self.interactive and hasattr(self.screen, "get_torrent_name"):
            return self.screen.get_torrent_name(torrent_id)

        for tid, name in self.torrents:
            if torrent_id == tid:
                return name

        return None

    def set_batch_write(self, batch):
        if self.interactive and isinstance(
                self.screen, deluge.ui.console.modes.legacy.Legacy):
            return self.screen.set_batch_write(batch)

    def tab_complete_torrent(self, line):
        if self.interactive and isinstance(
                self.screen, deluge.ui.console.modes.legacy.Legacy):
            return self.screen.tab_complete_torrent(line)

    def tab_complete_path(self,
                          line,
                          type="file",
                          ext="",
                          sort="name",
                          dirs_first=True):
        if self.interactive and isinstance(
                self.screen, deluge.ui.console.modes.legacy.Legacy):
            return self.screen.tab_complete_path(line,
                                                 type=type,
                                                 ext=ext,
                                                 sort=sort,
                                                 dirs_first=dirs_first)

    def set_mode(self, mode):
        reactor.removeReader(self.screen)
        self.screen = mode
        self.statusbars.screen = self.screen
        reactor.addReader(self.screen)
        mode.refresh()

    def on_client_disconnect(self):
        component.stop()

    def write(self, s):
        if self.interactive:
            if isinstance(self.screen, deluge.ui.console.modes.legacy.Legacy):
                self.screen.write(s)
            else:
                component.get("LegacyUI").add_line(s, False)
                self.events.append(s)
        else:
            print colors.strip_colors(s.encode(self.encoding))

    def write_event(self, s):
        if self.interactive:
            if isinstance(self.screen, deluge.ui.console.modes.legacy.Legacy):
                self.events.append(s)
                self.screen.write(s)
            else:
                component.get("LegacyUI").add_line(s, False)
                self.events.append(s)
        else:
            print colors.strip_colors(s.encode(self.encoding))