Beispiel #1
0
class SelectPydir(Pydir):

    def __init__(self, _list, header=None, paths=False):
        """
        Override completely is easier
        """
        verbose = False
        self.ask_pound()
        self.header=header
        self.reset_pages()
        self.user_sorter = 'name'
        self._clear_screen = None
        self.user_command = PydirShell(self)
        self.command = Shell(avoid_recursion="projects")
        self.menu = SelectMenu(self)
        self.last_mod_time = None
        self.output = Output()
        self.initialize()
        self.paths = paths
        self._list = SelectList(_list, paths=paths)
        verbose and print("SelectDir _list is {}".format(self._list))

    def loop_iteration(self):
        """
        Call whenever a new loop should be indicated
        """
        self.output.set_list(self.list_current_directory())

    def ask_pound(self):
        self.ask = '-> '

    def initialize(self):
        self._selection = None

    def list_current_directory(self):
        return self._list

    def pre_process(self, file_name):
        if self.paths:
            return Pydir.pre_process(self, file_name)
        else:
            return True

    def print_header(self):
        print(Fore.BLACK + Back.WHITE + " {} ".format(self.header) + Style.RESET_ALL)        

    def post_process(self, file_name, num, max_length):
        if self.paths:
            return Pydir.post_process(self, file_name, num, max_length)
        else:
            return (num, file_name)
        
    def parse_input(self, user_input):
        # specialized code given the fact we're working with an abstract list rather than
        # a list based on the directory

        # TODO: I shouldn't need to call this, find out what is throwing off the indexing
        self.list_current_directory().do_indexing()

        if not user_input.strip():
            user_input = str(self.list_current_directory().default.index)

        this = self.list_current_directory().which_equals('name', user_input, first_only=True)
            
        if user_input.isdigit() and not user_input.startswith('name '):
            # "make it so" feature: user types in number and we
            # figure out if it's a cd or an open statement
            which = int(user_input)
            if which < len(self.list_current_directory()):
                this = self.list_current_directory().index(which)
        try:
            handled = self.menu._parse(user_input)
        except BadArguments:
            handled = True

        if not handled:
            if user_input == self.quit_request:
                self.stop()
            elif user_input.startswith('+'):
                if self.pages:
                    self.not_done()
                    return self.pages['page'] + user_input.count('+')
                else:
                    input("No page to turn")
            elif user_input.startswith('-'):
                if self.pages:
                    self.not_done()
                    return self.pages['page'] - user_input.count('-')
                else:
                    input("No page to turn")

        if this:
                self.got_good_input(self.derive_selectable_from_this(this))

    def derive_selectable_from_this(self, _this):
        """
        You can override me
        """
        raise NotImplemented

    def got_good_input(self, _input):
        """
        Sets _selection
        """
        self._selection = _input

    def execute(self):
        """
        
        """
        self.clear_screen()

        # Send control to list current directory, or the currage page, if available
        if not self.pages:
            self.list_items()
        else:
            self.list_current_page()

        try:
            page = self.parse_input(input(self.ask))
        except KeyboardInterrupt:
            print()
            page = None

        # Clean up pages state as appropriate
        if page is None: 
            # If parse_input returns None (page 0 is legitimate number)
            self.reset_pages()
        else:
            self.turn_page(page)

    def start(self):
        """ Instead of looping, just take input once """
        self.not_done()
        while not self._done:
            self.done()  # force someone else to say the opposite
            self.loop_iteration()
            self.execute()
        return self.stop()

    def done(self):
        self._done = True

    def not_done(self):
        self._done = False

    def stop(self):
        return self._selection

    def select(self, using):
        """
        Returns the item selected by the user
        """
        self.derive_selectable_from_this = using
        return self.start()

    def walk(self, _):
        """
        Overwrite to return nothing, as not used
        """
        return []