def entry_point(argv=None): """ Actual entry point for execution, wrapped in a function for testing. """ try: mainret = main( docopt( USAGESTR, argv=argv or sys.argv[1:], version=VERSIONSTR, script=SCRIPT, )) except InvalidArg as ex: print_err(ex) mainret = 1 except FileNotFoundError as ex: print_err('File not found: {}'.format(ex.filename)) mainret = 2 except (EOFError, KeyboardInterrupt): print_err('\nUser cancelled.\n') mainret = 2 except BrokenPipeError: print_err('\nBroken pipe, input/output was interrupted.\n') mainret = 3 finally: lock_release() sys.exit(mainret)
def entry_point(): """ The actual entry point for this script, created to handle setuptools console scripts. Module-level error handling goes here. This function is responsible for starting `main()` and exiting the program. """ try: mainret = main(docopt(USAGESTR, version=VERSIONSTR, script=SCRIPT)) except (KeyboardInterrupt, UserCancelled) as ex: if not isinstance(ex, UserCancelled): ex = UserCancelled() print_err('\n{}'.format(ex)) mainret = 2 except (FatalError, HTTPError, UnicodeDecodeError, ValueError) as ex: if DEBUG: print_err('\n{}\n'.format(traceback.format_exc())) else: print_err('\n{}\n'.format(ex)) mainret = 1 except EnvironmentError as ex: if DEBUG: print_err(traceback.format_exc()) else: print_err(format_env_err(exc=ex)) mainret = 1 sys.exit(mainret)
def main(): """ Main entry point, expects doctopt arg dict as argd. """ global DEBUG argd = docopt(USAGESTR, version=VERSIONSTR, script=SCRIPT) DEBUG = argd['--debug'] width = parse_int(argd['--width'] or DEFAULT_WIDTH) or 1 indent = parse_int(argd['--indent'] or (argd['--INDENT'] or 0)) prepend = ' ' * (indent * 4) if prepend and argd['--indent']: # Smart indent, change max width based on indention. width -= len(prepend) userprepend = argd['--prepend'] or (argd['--PREPEND'] or '') prepend = ''.join((prepend, userprepend)) if argd['--prepend']: # Smart indent, change max width based on prepended text. width -= len(userprepend) userappend = argd['--append'] or (argd['--APPEND'] or '') if argd['--append']: width -= len(userappend) if argd['WORDS']: # Try each argument as a file name. argd['WORDS'] = ((try_read_file(w) if len(w) < 256 else w) for w in argd['WORDS']) words = ' '.join((w for w in argd['WORDS'] if w)) else: # No text/filenames provided, use stdin for input. words = read_stdin() block = FormatBlock(words).iter_format_block( chars=argd['--chars'], fill=argd['--fill'], prepend=prepend, strip_first=argd['--stripfirst'], append=userappend, strip_last=argd['--striplast'], width=width, newlines=argd['--newlines'], lstrip=argd['--lstrip'], ) for i, line in enumerate(block): if argd['--enumerate']: # Current line number format supports up to 999 lines before # messing up. Who would format 1000 lines like this anyway? print('{: >3}: {}'.format(i + 1, line)) else: print(line) return 0
def entry_point(): """ Main entry point for setuptools. Handles module-level exceptions, and exiting with the correct status code. """ try: mainret = main(docopt(USAGESTR, version=VERSIONSTR, script=SCRIPT)) except (InvalidArg, EnvironmentError) as ex: print_err(ex) mainret = 1 except (EOFError, KeyboardInterrupt): print_err('\nUser cancelled.\n') mainret = 2 except BrokenPipeError: print_err('\nBroken pipe, input/output was interrupted.\n') mainret = 3 sys.exit(mainret)
if self.msg: return 'Invalid argument, {}'.format(self.msg) return 'Invalid argument!' class MissingDependency(EnvironmentError): def __init__(self, msg=None): self.msg = msg or '' def __str__(self): if self.msg: return 'Missing dependency, {}'.format(self.msg) return 'Missing a dependency!' TESTNAMES = reversed(list(get_test_names())) if __name__ == '__main__': try: mainret = main(docopt(USAGESTR, version=VERSIONSTR, script=SCRIPT)) except (InvalidArg, MissingDependency) as ex: print_err(ex) mainret = 1 except (EOFError, KeyboardInterrupt): print_err('\nUser cancelled.\n') mainret = 2 except BrokenPipeError: print_err('\nBroken pipe, input/output was interrupted.\n') mainret = 3 sys.exit(mainret)
**kwargs ) class InvalidArg(ValueError): """ Raised when the user has used an invalid argument. """ def __init__(self, msg=None): self.msg = msg or '' def __str__(self): if self.msg: return 'Invalid argument, {}'.format(self.msg) return 'Invalid argument!' UPDATEABLE_APPS = get_updateable() if __name__ == '__main__': try: mainret = main(docopt(USAGESTR, version=VERSIONSTR, script=SCRIPT)) except (ValueError, InvalidArg) as ex: print_err(ex) mainret = 1 except (EOFError, KeyboardInterrupt): print_err('\nUser cancelled.\n') mainret = 2 except BrokenPipeError: print_err('\nBroken pipe, input/output was interrupted.\n') mainret = 3 sys.exit(mainret)
] if line_nums: pcs = [[ xs[0], C('').join(C(self.lineno, 'red' if i == 0 else 'green'), ':'), xs[1] ] for i, xs in enumerate(pcs)] return '{}\n'.format(C('\n').join(C(' ').join(pc) for pc in pcs)) line = self._highlighted_match if matched else self._highlighted if line_nums: s = str(C(': ').join(C(self.lineno, 'blue'), line)) else: s = line return '{}\n'.format(''.join((' ' * indent, s.rstrip()))) if __name__ == '__main__': try: mainret = main(docopt(USAGESTR, version=VERSIONSTR)) except InvalidArg as ex: print_err('\n{}'.format(ex)) mainret = 1 except (EOFError, KeyboardInterrupt): print_err(C('\nUser cancelled.\n', 'red')) mainret = 2 except BrokenPipeError: print_err(C('\nBroken pipe, input/output was interrupted.\n', 'red')) mainret = 3 sys.exit(mainret)
def set_tags(self, taglist): """ Set the tags for this file. `taglist` should be an iterable of strings (tags). Removes any duplicate tags before setting. Returns the tags on success. Possibly raises AttrError. """ tagstr = self.parse_taglist(taglist) newvalue = self.set_attr(self.attr_tags, tagstr) self.tags = self.parse_tagstr(newvalue) return self.tags if __name__ == '__main__': ARGD = docopt(USAGESTR, version=VERSIONSTR, script=SCRIPT) DEBUG = ARGD['--debug'] QUIET = ARGD['--quiet'] if ARGD['--nocolor']: # Override automatic detection. colr_disable() try: MAINRET = main(ARGD) except KeyboardInterrupt: print_err('User cancelled.') MAINRET = 2 except BrokenPipeError: print_err('Broken pipe, operation may have been interrupted.') MAINRET = 3 sys.exit(MAINRET)