def print_styles_list(self): """Print list of Pygments styles.""" # print(*sorted(get_all_styles()), sep='\n') # Run the corresponding command from pygment's CLI, also prints descriptions from pygments.cmdline import main main(['pygmentize', '-L', 'styles'])
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): 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() #self.clear_screen() hides any error message produced before it! self.clear_screen() nextFile = queue_of_valid_files.get() while nextFile: if(self.colorize == True): tmpFile = "/tmp/pygged" cmdline.main(['pygmentize', '-f', 'terminal', '-o', tmpFile, nextFile]) else: tmpFile = nextFile with open(tmpFile, 'r') as f: file_data = f.read() 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()
def run_cmdline(*args, **kwds): saved_stdin = sys.stdin saved_stdout = sys.stdout saved_stderr = sys.stderr if sys.version_info > (3, ): stdin_buffer = BytesIO() stdout_buffer = BytesIO() stderr_buffer = BytesIO() new_stdin = sys.stdin = io.TextIOWrapper(stdin_buffer, 'utf-8') new_stdout = sys.stdout = io.TextIOWrapper(stdout_buffer, 'utf-8') new_stderr = sys.stderr = io.TextIOWrapper(stderr_buffer, 'utf-8') else: stdin_buffer = new_stdin = sys.stdin = StringIO() stdout_buffer = new_stdout = sys.stdout = StringIO() stderr_buffer = new_stderr = sys.stderr = StringIO() new_stdin.write(kwds.get('stdin', '')) new_stdin.seek(0, 0) try: ret = cmdline.main(['pygmentize'] + list(args)) finally: sys.stdin = saved_stdin sys.stdout = saved_stdout sys.stderr = saved_stderr new_stdout.flush() new_stderr.flush() out, err = stdout_buffer.getvalue().decode('utf-8'), \ stderr_buffer.getvalue().decode('utf-8') return (ret, out, err)
def run_cmdline(*args, **kwds): saved_stdin = sys.stdin saved_stdout = sys.stdout saved_stderr = sys.stderr if sys.version_info > (3,): stdin_buffer = BytesIO() stdout_buffer = BytesIO() stderr_buffer = BytesIO() new_stdin = sys.stdin = io.TextIOWrapper(stdin_buffer, 'utf-8') new_stdout = sys.stdout = io.TextIOWrapper(stdout_buffer, 'utf-8') new_stderr = sys.stderr = io.TextIOWrapper(stderr_buffer, 'utf-8') else: stdin_buffer = new_stdin = sys.stdin = StringIO() stdout_buffer = new_stdout = sys.stdout = StringIO() stderr_buffer = new_stderr = sys.stderr = StringIO() new_stdin.write(kwds.get('stdin', '')) new_stdin.seek(0, 0) try: ret = cmdline.main(['pygmentize'] + list(args)) finally: sys.stdin = saved_stdin sys.stdout = saved_stdout sys.stderr = saved_stderr new_stdout.flush() new_stderr.flush() out, err = stdout_buffer.getvalue().decode('utf-8'), \ stderr_buffer.getvalue().decode('utf-8') return (ret, out, err)
def run_cmdline(*args): saved_stdout = sys.stdout saved_stderr = sys.stderr if sys.version_info > (3,): stdout_buffer = BytesIO() stderr_buffer = BytesIO() new_stdout = sys.stdout = io.TextIOWrapper(stdout_buffer, 'utf-8') new_stderr = sys.stderr = io.TextIOWrapper(stderr_buffer, 'utf-8') else: stdout_buffer = new_stdout = sys.stdout = StringIO() stderr_buffer = new_stderr = sys.stderr = StringIO() try: ret = cmdline.main(["pygmentize"] + list(args)) finally: sys.stdout = saved_stdout sys.stderr = saved_stderr new_stdout.flush() new_stderr.flush() out, err = stdout_buffer.getvalue().decode('utf-8'), \ stderr_buffer.getvalue().decode('utf-8') return (ret, out, err)
def run_cmdline(*args, **kwds): saved_stdin = sys.stdin saved_stdout = sys.stdout saved_stderr = sys.stderr stdin_buffer = BytesIO() stdout_buffer = BytesIO() stderr_buffer = BytesIO() new_stdin = sys.stdin = io.TextIOWrapper(stdin_buffer, 'utf-8') new_stdout = sys.stdout = io.TextIOWrapper(stdout_buffer, 'utf-8') new_stderr = sys.stderr = io.TextIOWrapper(stderr_buffer, 'utf-8') new_stdin.write(kwds.get('stdin', '')) new_stdin.seek(0, 0) try: ret = cmdline.main(['pygmentize'] + list(args)) finally: sys.stdin = saved_stdin sys.stdout = saved_stdout sys.stderr = saved_stderr new_stdout.flush() new_stderr.flush() out, err = stdout_buffer.getvalue(), \ stderr_buffer.getvalue() return (ret, _decode_output(out), _decode_output(err))
# -*- coding: utf-8 -*- import re import sys from pygments.cmdline import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(main())
try: if not outfile: #print formatter, 'using', formatter.encoding realoutfile = formatter.encoding and BytesIO() or StringIO() formatter.format(tokens, realoutfile) return realoutfile.getvalue() else: formatter.format(tokens, outfile) except TypeError, err: if isinstance(err.args[0], str) and \ 'unbound method format' in err.args[0]: raise TypeError('format() argument must be a formatter instance, ' 'not a class') raise def highlight(code, lexer, formatter, outfile=None): """ Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``. If ``outfile`` is given and a valid file object (an object with a ``write`` method), the result will be written to it, otherwise it is returned as a string. """ return format(lex(code, lexer), formatter, outfile) if __name__ == '__main__': from pygments.cmdline import main sys.exit(main(sys.argv))
if __name__ == '__main__': import sys from pygments.cmdline import main sys.exit(main())