示例#1
0
def main(args=None, catch_all=False, parsed_args=None, log=None):
    parser, subparsers = get_parser_and_subparsers('ldh')
    register_subcommands(subparsers, ldh.commands)

    args = parsed_args or parser.parse_args(args=args)

    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        if not log:  # pragma: no cover
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:
            print(e)
            return main([args._command, '-h'])
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#2
0
def main(args=None, catch_all=False, parsed_args=None, log=None):
    parser, subparsers = get_parser_and_subparsers('georoc')
    parser.add_argument('--repos',
                        type=PathType(type='dir'),
                        default=pathlib.Path('.'),
                        help='Location of the data repository')
    register_subcommands(subparsers, pygeoroc.commands)

    args = parsed_args or parser.parse_args(args=args)

    if not hasattr(args, "main"):  # pragma: no cover
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        if not log:  # pragma: no cover
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        args.repos = GEOROC(args.repos)
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#3
0
def main(args=None, catch_all=False, parsed_args=None, log=None):
    parser, subparsers = get_parser_and_subparsers('acc')
    parser.add_argument('--repos',
                        default='.',
                        type=PathType(type='dir'),
                        help='acc-data')
    register_subcommands(subparsers, pyacc.commands)

    args = parsed_args or parser.parse_args(args=args)

    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        args.api = ACC(args.repos)
        if not log:  # pragma: no cover
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:
            print(e)
            return main([args._command, '-h'])
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#4
0
def main(args=None, catch_all=False, parsed_args=None):
    import pygrambank.commands

    parser, subparsers = get_parser_and_subparsers('grambank')
    parser.add_argument('--repos',
                        help="clone of glottobank/Grambank",
                        default=Path('.'),
                        type=Path)
    register_subcommands(subparsers, pygrambank.commands)

    args = parsed_args or parser.parse_args(args=args)
    if not hasattr(args, "main"):  # pragma: no cover
        parser.print_help()
        return 1

    args.repos = Grambank(args.repos, wiki=getattr(args, 'wiki_repos', None))
    with Logging(args.log, level=args.log_level):
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:  # pragma: no cover
            print(e)
            return main([args._command, '-h'])
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#5
0
def main(args=None, catch_all=False, parsed_args=None, log=None):
    parser, subparsers = get_parser_and_subparsers('cdstarcat')
    envvar = 'CDSTAR_CATALOG'
    parser.add_argument(
        '--catalog',
        help="defaults to ${0}".format(envvar),
        default=os.environ.get(envvar))
    add_cdstar(parser)
    register_subcommands(subparsers, cdstarcat.commands)

    args = parsed_args or parser.parse_args(args=args)

    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        args.catalog = stack.enter_context(Catalog(
            args.catalog, cdstar_url=args.url, cdstar_user=args.user, cdstar_pwd=args.pwd))
        if not log:  # pragma: no cover
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#6
0
def _main(commands,
          args=None,
          catch_all=False,
          parsed_args=None,
          log=None,
          test=False):
    try:
        repos = Config.from_file().get_clone('glottolog')
    except KeyError:  # pragma: no cover
        repos = pathlib.Path('.')
    parser, subparsers = get_parser_and_subparsers('glottolog')
    parser.add_argument('--repos',
                        help="clone of glottolog/glottolog",
                        default=repos,
                        type=pathlib.Path)
    parser.add_argument(
        '--repos-version',
        help="version of repository data. Requires a git clone!",
        default=None)
    parser.add_argument('--pkg-dir',
                        help=argparse.SUPPRESS,
                        default=pathlib.Path(__file__).parent)
    register_subcommands(subparsers, commands)

    args = parsed_args or parser.parse_args(args=args)
    args.test = test

    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        if not log:  # pragma: no cover
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        if args.repos_version:  # pragma: no cover
            # If a specific version of the data is to be used, we make
            # use of a Catalog as context manager:
            stack.enter_context(Catalog(args.repos, tag=args.repos_version))
        try:
            args.repos = Glottolog(args.repos)
        except Exception as e:
            print(e)
            return _main(commands, args=[args._command, '-h'])
        args.log.info('glottolog/glottolog at {0}'.format(args.repos.repos))
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:
            print(e)
            return _main(commands, args=[args._command, '-h'])
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#7
0
    def run():
        logger = logging.getLogger(__name__)
        logger.addHandler(logging.StreamHandler(sys.stdout))
        logger.setLevel(logging.WARN)

        logger.warn('warn')
        logger.debug('debug1')

        with Logging(logger):
            logger.debug('debug2')
示例#8
0
def main(args=None, catch_all=False, parsed_args=None, log=None):
    repos = None
    if not NO_CAT:
        try:
            repos = cldfcatalog.Config.from_file().get_clone('concepticon')
        except KeyError:  # pragma: no cover
            pass
    repos = repos or Path('.')
    parser, subparsers = get_parser_and_subparsers('concepticon')
    parser.add_argument('--repos',
                        help="clone of concepticon/concepticon-data",
                        default=repos,
                        type=Path)
    parser.add_argument(
        '--repos-version',
        help="version of repository data. Requires a git clone!",
        default=None)
    register_subcommands(subparsers, pyconcepticon.commands)

    args = parsed_args or parser.parse_args(args=args)

    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        if not log:  # pragma: no cover
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        if args.repos_version:  # pragma: no cover
            # If a specific version of the data is to be used, we make
            # use of a Catalog as context manager:
            if NO_CAT:
                print(NO_CAT)
                return 1
            stack.enter_context(
                cldfcatalog.Catalog(args.repos, tag=args.repos_version))
        args.repos = Concepticon(args.repos)
        args.log.info('concepticon/concepticon-data at {0}'.format(
            args.repos.repos))
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:
            print(e)
            return main([args._command, '-h'])
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#9
0
def main(args=None, catch_all=False, parsed_args=None):
    try:  # pragma: no cover
        repos = Config.from_file().get_clone('concepticon')
    except KeyError:  # pragma: no cover
        repos = pathlib.Path('.')

    parser, subparsers = get_parser_and_subparsers('norare')
    parser.add_argument('--repos',
                        help="clone of concepticon/concepticon-data",
                        default=repos,
                        type=PathType(type='dir'))
    parser.add_argument(
        '--repos-version',
        help="version of repository data. Requires a git clone!",
        default=None)
    parser.add_argument('--norarepo',
                        default=pathlib.Path('.'),
                        type=PathType(type='dir'))

    register_subcommands(subparsers, pynorare.commands)

    args = parsed_args or parser.parse_args(args=args)
    if not hasattr(args, "main"):  # pragma: no cover
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        stack.enter_context(Logging(args.log, level=args.log_level))
        if args.repos_version:  # pragma: no cover
            # If a specific version of the data is to be used, we make
            # use of a Catalog as context manager:
            stack.enter_context(Catalog(args.repos, tag=args.repos_version))
        args.repos = Concepticon(args.repos)
        args.api = NoRaRe(args.norarepo, concepticon=args.repos)
        args.log.info('norare at {0}'.format(args.repos.repos))
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:  # pragma: no cover
            print(e)
            return main([args._command, '-h'])
        except Exception as e:  # pragma: no cover
            if catch_all:  # pragma: no cover
                print(e)
                return 1
            raise
示例#10
0
def main(args=None, catch_all=False, parsed_args=None, log=None):
    parser, subparsers = get_parser_and_subparsers('clics')
    parser.add_argument('--repos',
                        help="Repository or output directory",
                        default=Clics('.'),
                        type=Clics)

    parser.add_argument('-s', '--seed', type=setseed, default=None)
    parser.add_argument('-t', '--threshold', type=int, default='1')
    parser.add_argument('-f', '--edgefilter', default='families')
    parser.add_argument('-g', '--graphname', default='network')

    register_subcommands(subparsers, pyclics.commands)

    args = parsed_args or parser.parse_args(args=args)

    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    args.api = args.repos
    with contextlib.ExitStack() as stack:
        if not log:
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except argparse.ArgumentError as e:
            print(e)
            return main([args._command, '-h'])
        except Exception as e:  # pragma: no cover
            if catch_all:
                print(e)
                return 1
            raise
示例#11
0
def main(args=None, catch_all=False, parsed_args=None):
    import grambank.commands

    parser, subparsers = get_parser_and_subparsers('grambank-app')
    register_subcommands(subparsers, grambank.commands)

    args = parsed_args or parser.parse_args(args=args)
    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    with Logging(args.log, level=args.log_level):
        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:
            print(e)
            return main([args._command, '-h'])
        except Exception as e:
            if catch_all:  # pragma: no cover
                print(e)
                return 1
            raise
示例#12
0
 def main(self, args=None, catch_all=False, parsed_args=None):
     args = parsed_args or self.parse_args(args=args)
     with Logging(args.log, level=args.log_level):
         return super(ArgumentParserWithLogging,
                      self).main(catch_all=catch_all, parsed_args=args)
示例#13
0
def main(args=None, catch_all=False, parsed_args=None, log=None):
    parser, subparsers = get_parser_and_subparsers(cldfbench.__name__)

    # We add a "hidden" option to turn-off config file reading in tests:
    parser.add_argument('--no-config',
                        default=False,
                        action='store_true',
                        help=argparse.SUPPRESS)
    add_csv_field_size_limit(parser, default=csv.field_size_limit())

    # Discover available commands:
    # Commands are identified by (<entry point name>).<module name>
    register_subcommands(subparsers,
                         cldfbench.commands,
                         entry_point='cldfbench.commands')

    args = parsed_args or parser.parse_args(args=args)
    if not hasattr(args, "main"):
        parser.print_help()
        return 1

    with contextlib.ExitStack() as stack:
        if not log:  # pragma: no cover
            stack.enter_context(Logging(args.log, level=args.log_level))
        else:
            args.log = log
        # args.no_catalogs is set by the `config` command, because this command specifies
        # catalog options **optionally**, and prompts for user input only in its `run` function.
        if not getattr(args, "no_catalogs", False):
            cfg = Config.from_file()
            for cls in BUILTIN_CATALOGS:
                # Now we loop over known catalogs, see whether they are used by the command,
                # and if so, "enter" the catalog.
                name, from_cfg = cls.cli_name(), False
                if hasattr(args, name):
                    # If no path was passed on the command line, we look up the config:
                    path = getattr(args, name)
                    if (not path) and (not args.no_config):
                        try:
                            path = cfg.get_clone(name)
                            from_cfg = True
                        except KeyError as e:  # pragma: no cover
                            print(termcolor.colored(str(e) + '\n', 'red'))
                            return main([args._command, '-h'])
                    try:
                        setattr(
                            args,
                            name,
                            stack.enter_context(
                                cls(path, getattr(args, name + '_version',
                                                  None))),
                        )
                    except ValueError as e:
                        print(
                            termcolor.colored(
                                '\nError initializing catalog {0}'.format(
                                    name), 'red'))
                        if from_cfg:
                            print(
                                termcolor.colored(
                                    'from config {0}'.format(cfg.fname()),
                                    'red'))
                        print(termcolor.colored(str(e) + '\n', 'red'))
                        return main([args._command, '-h'])

        try:
            return args.main(args) or 0
        except KeyboardInterrupt:  # pragma: no cover
            return 0
        except ParserError as e:
            print(
                termcolor.colored('ERROR: {}\n'.format(e),
                                  'red',
                                  attrs={'bold'}))
            return main([args._command, '-h'])
        except Exception as e:
            if catch_all:  # pragma: no cover
                print(
                    termcolor.colored('ERROR: {}\n'.format(e),
                                      'red',
                                      attrs={'bold'}))
                return 1
            raise