Exemplo n.º 1
0
    def _run_cycle(self):
        """
        Executes a \"cycle\" of this screen.
            * The concept of \"cycle\" is no longer accurate, and is misleading.
              this function will not return.
        New threaded implementation:
            * Checks if self.path is a valid path, using `os.path.exists`
            * Assigns a new Queue to `queue_of_valid_files`
            * Appends a new `FileScannerThread` object to a list of threads
            * `start()`s the `FileScannerThread`
                * `FileScannerThread` will put paths in the queue as valid
                   file paths are found
            * `clear_screen()`s
            * Gets a file from `queue_of_valid_files`, removing item from queue
            * While nextFile (empty sequences are false)
                * As long as there is something in the queue - that is, as long
                  as `queue.queue.get()` is able to get an object from (the)
                  `queue_of_valid_files`, this test evaluates True.
                * I imagine that this behaves unpredictably given a computer
                  with __REALLY__ slow I/O
            * Opens `nextFile` with handle-auto-closing `with` statement and
              `typing_print()`s it
            * Clears screen if `self.cleanup_per_file`
            * Puts `nextFile` ON the queue
                * Because `queue_of_valid_files.get()` REMOVES a file path
                  from the queue, `_run_cycle()` will never reach that path
                  again, and eventually will exhaust the queue
                  (failing silently, with a blank screen)
                    * A static blank screen is the antithesis of a screensaver
                * Therefore, `queue_of_valid_files.put(nextFile)` puts the file
                  path at the last spot in the queue
            * Finally, another call to `queue_of_valid_files.get()` sets up
              the next iteration in the while loop.
        """
        # validate path
        if not os.path.exists(self.path) and self.path[0:4] != 'http':
            raise exception.PathNotFoundException(self.path)

        queue_of_valid_files = queue.Queue()

        threads = [
            self.FileScannerThread(self, queue_of_valid_files, self.path)
        ]
        threads[-1].daemon = True
        threads[-1].start()

        self.clear_screen()
        nextFile = queue_of_valid_files.get()
        while nextFile:
            self.get_terminal_size()
            imgconv = ImageConverter()
            file_data = imgconv.convert_image(nextFile, self.geometry['x'],
                                              self.geometry['y'], self.options)
            self.typing_print(file_data)
            time.sleep(self.frame_delay)
            if self.cleanup_per_file:
                self.clear_screen()
            queue_of_valid_files.put(nextFile)
            nextFile = queue_of_valid_files.get()
Exemplo n.º 2
0
    def _parse_args(self, prepared_args):
        for o, a in prepared_args[0]:  # optlist, args
            if o in ("-h", "--help"):
                self.usage()
                self.screen_exit()
            elif o in ("-i", "--invert"):
                self.options['invert'] = True
            elif o in ("-w", "--wide"):
                try:
                    # makes sure this is a valid integer
                    self.options['wide'] = int(a)
                except:
                    raise exception.InvalidOptionException("wide")
            elif o in ("-c", "--contrast"):
                self.options['contrast'] = True
            elif o in ("-s", "--set"):
                self.options['customcharset'] = a
            elif o in ("-f", "--framedelay"):
                try:
                    self.option['framedelay'] = int(a)
                except:
                    raise exception.InvalidOptionException("framedelay")
            elif o in ("-z", "--scale"):
                try:
                    self.options['scale'] = int(a)
                except:
                    raise exception.InvalidOptionException("scale")
            elif o in ("-d", "--delay"):
                try:
                    # make sure argument is a valid value (float)
                    self.delay = float(a)
                except:
                    raise exception.InvalidOptionException("delay")
            elif o in ("-p", "--path"):
                # make sure argument is a valid value (existing path)
                self.path = a
                if not os.path.exists(
                        self.path) and self.path[0:4].lower() != 'http':
                    raise exception.PathNotFoundException(
                        self.path,
                        _("Make sure the file or directory exists."))
            else:
                # this should never happen!
                raise Exception(_("Unhandled option. See --help for details."))

        # last validations
        if self.path in (None, ''):
            raise exception.InvalidOptionException(
                "path",
                _("It is mandatory option"),
                help=self._message_no_path())
Exemplo n.º 3
0
    def doc_xml_file(self, path):
        """
        Parses a specified file into a xml.dom.minidom document object, to be
        used by `parse_data` method later on. This method here will store the
        result in the private `__doc` property.

        Arguments:

            * path: the XML file path that will be parsed into a dom
              document object.
        """
        if not os.path.exists(path):
            raise exception.PathNotFoundException(path)

        try:
            self.__doc = parse(path)
        except:
            raise exception.XmlException(path)
Exemplo n.º 4
0
    def _parse_args(self, prepared_args):
        """
        Handles the special command-line arguments available for this screen.
        Although this is a base screen, having these options prepared here
        can save coding for screens that will not change the default options.

        See `_usage_options_example` method for documentation on each of the
        options being parsed here.

        Additionally, this is dependent on the values exposed in `cli_opts`,
        passed to this class during its instantiation. Only values properly
        configured there will be accepted here.
        """
        for o, a in prepared_args[0]:  # optlist, args
            if o in ("-h", "--help"):
                self.usage()
                self.screen_exit()
            elif o in ("-n", "--no-adjust"):
                self.adjust = False
            elif o in ("-p", "--path"):
                # make sure argument is a valid value (existing path)
                self.path = a
                if not os.path.exists(self.path):
                    raise exception.PathNotFoundException(
                        self.path, _("Make sure the file exists."))
                if not os.path.isfile(self.path):
                    raise exception.InvalidOptionException(
                        "--path", _("Make sure it is a file"))

            elif o in ("-d", "--delay"):
                try:
                    # make sure argument is a valid value (float)
                    self.delay = float(a)
                except:
                    raise exception.InvalidOptionException("delay")
            else:
                # this should never happen!
                raise Exception(_("Unhandled option. See --help for details."))
Exemplo n.º 5
0
    def _run_cycle(self):
        if self.is_initalized is False:
            #if 'pygments' in sys.modules:
            pygments_spec = importlib.util.find_spec('pygments')
            found = pygments_spec is not None
            if found is True:
                from pygments import highlight
                from pygments.lexers import guess_lexer
                from pygments.formatters import TerminalFormatter
                self.pygments_installed = True
            self.is_initalized = True
        """
        Executes a \"cycle\" of this screen.
            * The concept of \"cycle\" is no longer accurate, and is misleading.
              this function will not return.
        New threaded implementation:
            * Checks if self.path is a valid path, using `os.path.exists`
            * Assigns a new Queue to `queue_of_valid_files`
            * Appends a new `FileScannerThread` object to a list of threads
            * `start()`s the `FileScannerThread`
                * `FileScannerThread` will put paths in the queue as valid
                   file paths are found
            * `clear_screen()`s
            * Gets a file from `queue_of_valid_files`, removing item from queue
            * While nextFile (empty sequences are false)
                * As long as there is something in the queue - that is, as long
                  as `queue.queue.get()` is able to get an object from (the)
                  `queue_of_valid_files`, this test evaluates True.
                * I imagine that this behaves unpredictably given a computer
                  with __REALLY__ slow I/O
            * Opens `nextFile` with handle-auto-closing `with` statement and
              `typing_print()`s it
            * Clears screen if `self.cleanup_per_file`
            * Puts `nextFile` ON the queue
                * Because `queue_of_valid_files.get()` REMOVES a file path
                  from the queue, `_run_cycle()` will never reach that path
                  again, and eventually will exhaust the queue
                  (failing silently, with a blank screen)
                    * A static blank screen is the antithesis of a screensaver
                * Therefore, `queue_of_valid_files.put(nextFile)` puts the file
                  path at the last spot in the queue
            * Finally, another call to `queue_of_valid_files.get()` sets up
              the next iteration in the while loop.
        """
        # validate path
        if not os.path.exists(self.path):
            raise exception.PathNotFoundException(self.path)

        queue_of_valid_files = queue.Queue()

        threads = [FileReaderBase.FileScannerThread(self, queue_of_valid_files, self.path)]
        threads[-1].daemon = True
        threads[-1].start()
        
        print(_("""
Scanning path for supported files.
If this message does not disappear then there are no supported file types in the given path."""))

        nextFile = queue_of_valid_files.get()
    
        #self.clear_screen() hides any error message produced before it!
        self.clear_screen()

        while nextFile:
            with open(nextFile, 'r') as f:
                file_data = f.read()
                if self.pygments_installed is True:
                    if self.colorize is True:
                        lexer = guess_lexer(file_data)
                        formatter = TerminalFormatter
                        file_data = highlight(file_data,lexer,formatter())
                self.typing_print(file_data)
            if self.cleanup_per_file:
                self.clear_screen()
            queue_of_valid_files.put(nextFile)
            nextFile = queue_of_valid_files.get()