Пример #1
0
    def test_collectworkspaces_add_arguments(self):
        from argparse import ArgumentParser
        parser = ArgumentParser()
        cmd = collectworkspaces.Command()
        cmd.add_arguments(parser)

        self.assertIn('[-f]', parser.format_usage())
        self.assertIn('--force', parser.format_help())
        self.assertIn('Force the overwrite the app directory', parser.format_help())
Пример #2
0
 def test_syncstores_add_arguments(self):
     parser = ArgumentParser()
     cmd = syncstores.Command()
     cmd.add_arguments(parser)
     self.assertIn('app_name', parser.format_usage())
     self.assertIn('[-r]', parser.format_usage())
     self.assertIn('[-f]', parser.format_usage())
     self.assertIn('[-d DATABASE]', parser.format_usage())
     self.assertIn('--refresh', parser.format_help())
     self.assertIn('--firsttime', parser.format_help())
     self.assertIn('--database DATABASE', parser.format_help())
Пример #3
0
def main():
    '''Main method. Make the database from the given file and run the problem solutions.'''

    problems = (prob1, prob2, prob3, prob4, prob5, prob6)

    from argparse import ArgumentParser

    argparser = ArgumentParser()
    argparser.add_argument(
        'fname',
        default='planets.csv',
        help=
        'Name of the file from which to read the database (default: planets.csv).'
    )
    argparser.add_argument(
        'problems',
        nargs='*',
        type=int,
        help='Which problems to run (1-{0}, default: all).'.format(
            len(problems)),
        default=range(1,
                      len(problems) + 1))

    args = argparser.parse_args()
    db = make_db(args.fname)

    for prob in args.problems:
        if prob not in argparser.get_default('problems'):
            raise IndexError('Problems must be in the range ' +
                             str(argparser.get_default('problems')) + '\n' +
                             argparser.format_help())
        problems[prob - 1](db)

    return locals()
Пример #4
0
def parse_args() -> None:
    parser = ArgumentParser(
        description="Cryptocurrency command-line tool",
        epilog="https://github.com/bonjoursoftware/cryptocli",
        prog="docker run bonjoursoftware/cryptocli",
    )
    parser.set_defaults(func=lambda _: print(parser.format_help()))

    subparsers = parser.add_subparsers()

    find_parser = subparsers.add_parser("find")
    find_parser.add_argument("-s",
                             "--symbol",
                             type=str,
                             required=True,
                             help="cryptocurrency symbol")
    find_parser.set_defaults(func=find_symbols)

    price_parser = subparsers.add_parser("price")
    price_parser.add_argument("-s",
                              "--symbol",
                              type=str,
                              help="cryptocurrency symbol",
                              default="BTC-GBP")
    price_parser.add_argument("-t",
                              "--ticker",
                              type=int,
                              help="ticker interval in seconds")
    price_parser.set_defaults(func=watch_last_trade_price)

    args = parser.parse_args()
    args.func(args)
Пример #5
0
def initArgs():
    parser = ArgumentParser()
    parser.add_argument("fileList",
                        nargs='*',
                        help="filename list to check, default is stdin / -.",
                        default="-")
    parser.add_argument("-a",
                        "--algorithm",
                        type=int,
                        help="1 (default), 224, 256, 384, 512",
                        default=1)
    parser.add_argument(
        "-b",
        "--binary",
        help="read files in binary mode (default on DOS/Windows)",
        action='store_true',
        default=True)
    parser.add_argument("-t",
                        "--text",
                        help="read files in text mode (default on Unix/Linux)",
                        action='store_true',
                        default=False)
    parser.add_argument("-c",
                        "--check",
                        help="check SHA sums against given list")

    global args
    try:
        args = parser.parse_args()
    except:
        print >> stderr, "\n" + parser.format_help()
        exit(-1)

    if args.text: args.binary = False
    if args.binary: args.text = False
Пример #6
0
 def _main():
     p = ArgumentParser(description=descr, formatter_class=RawTextHelpFormatter, add_help=False)
     p.format_help = MethodType(lambda s: s.description, p)
     p.add_argument("file", nargs="?")
     p.add_argument("-d", "--decode", action="store_true")
     p.add_argument("-i", "--ignore-garbage", action="store_true")
     if inv:
         p.add_argument("-I", "--invert", action="store_true")
     if swap:
         p.add_argument("-s", "--swapcase", action="store_true")
     if wrap:
         p.add_argument("-w", "--wrap", type=int, default=76)
     p.add_argument("--help", action="help")
     p.add_argument("--version", action="version")
     p.version = "CodExt " + __version__
     args = p.parse_args()
     if args.decode:
         args.wrap = 0
     args.invert = getattr(args, "invert", False)
     c, f = _input(args.file), [encode, decode][args.decode]
     if swap and args.swapcase and args.decode:
         c = codecs.decode(c, "swapcase")
     c = b(c).rstrip(b"\r\n")
     try:
         c = f(c, "base" + base + ["", "-inv"][getattr(args, "invert", False)],
               ["strict", "ignore"][args.ignore_garbage])
     except Exception as err:
         print("%sbase%s: invalid input" % (getattr(err, "output", ""), base))
         return 1
     c = ensure_str(c)
     if swap and args.swapcase and not args.decode:
         c = codecs.encode(c, "swapcase")
     for l in (wraptext(c, args.wrap) if args.wrap > 0 else [c]) if wrap else c.split("\n"):
         print(l)
     return 0
Пример #7
0
def main():
    descr = """Usage: unbase [OPTION]... [FILE]
Decode multi-layer base encoded FILE, or standard input, to standard output.

With no FILE, or when FILE is -, read standard input.

Optional arguments:
  -E, --extended        also consider generic base codecs while guess-decoding
  -f, --stop-function   set the result chceking function (default: text)
                         format: printables|text|flag|lang_[bigram]
  -M, --max-depth       maximum codec search depth (default: 5)
  -m, --min-depth       minimum codec search depth (default: 0)
  -p, --pattern         pattern to be matched while searching
  -s, --show            show the decoding chain

      --help     display this help and exit
      --verbose  show guessing information and steps
      --version  output version information and exit

Report unbase bugs to <https://github.com/dhondta/python-codext/issues/new>
Full documentation at: <https://python-codext.readthedocs.io/en/latest/enc/base.html>
"""
    parser = ArgumentParser(description=descr,
                            formatter_class=RawTextHelpFormatter,
                            add_help=False)
    parser.format_help = MethodType(lambda s: s.description, parser)
    group = parser.add_mutually_exclusive_group()
    parser.add_argument("file", nargs="?")
    parser.add_argument("-E", "--extended", action="store_true")
    group.add_argument("-f", "--stop-function", default="text")
    parser.add_argument("-M", "--max-depth", type=int, default=10)
    parser.add_argument("-m", "--min-depth", type=int, default=0)
    group.add_argument("-p", "--pattern")
    parser.add_argument("-s", "--show", action="store_true")
    parser.add_argument("--help", action="help")
    parser.add_argument("--version", action="version")
    parser.add_argument("--verbose", action="store_true")
    parser.version = "CodExt " + __version__
    args = parser.parse_args()
    c, e = _input(args.file), [["base%d-generic" % i for i in range(2, 256)],
                               []][args.extended]
    c = c.rstrip("\r\n") if isinstance(c, str) else c.rstrip(b"\r\n")
    r = codecs.guess(c,
                     stopfunc._validate(args.stop_function),
                     0,
                     args.max_depth,
                     "base",
                     tuple(e),
                     stop=False,
                     show=args.verbose,
                     debug=args.verbose)
    if len(r) == 0:
        print("Could not decode :-(")
        return 0
    ans = max(r.items(), key=lambda x: len(x[0]))
    if args.show:
        print(" - ".join(ans[0]))
    print(ensure_str(ans[1]))
    return 0
Пример #8
0
 def test_tethys_app_uninstall_add_arguments(self):
     parser = ArgumentParser('foo_parser')
     cmd = tethys_app_uninstall.Command()
     cmd.add_arguments(parser)
     self.assertIn('foo_parser', parser.format_usage())
     self.assertIn('app_or_extension', parser.format_usage())
     self.assertIn('[-e]', parser.format_usage())
     self.assertIn('--extension', parser.format_help())
 def test_tethys_app_uninstall_add_arguments(self):
     parser = ArgumentParser('foo_parser')
     cmd = tethys_app_uninstall.Command()
     cmd.add_arguments(parser)
     self.assertIn('foo_parser', parser.format_usage())
     self.assertIn('app_or_extension', parser.format_usage())
     self.assertIn('[-e]', parser.format_usage())
     self.assertIn('--extension', parser.format_help())
Пример #10
0
def parse_arguments(argv: List[str]) -> Namespace:
    """
    Parses !advent arguments from the given list.

    Returns namespace with argument values or throws UsageSyntaxException.
    If an exception is thrown, its message should be shown to the user and
    execution should NOT continue.
    """
    parser = ArgumentParser("!advent", add_help=False)

    parser.add_argument("day",
                        type=int,
                        default=0,
                        nargs="?",
                        help="Show leaderboard for specific day" +
                        " (default: all days)")
    parser.add_argument("-g",
                        "--global",
                        action="store_true",
                        dest="global_",
                        help="Show global points")
    parser.add_argument("-y",
                        "--year",
                        type=int,
                        default=datetime.now().year,
                        help="Year of leaderboard (default: current year)")
    parser.add_argument("-c",
                        "--code",
                        type=int,
                        default=UQCS_LEADERBOARD,
                        help="Leaderboard code (default: UQCS leaderboard)")
    parser.add_argument("-s",
                        "--sort",
                        default=SortMode.PART_2,
                        type=SortMode,
                        choices=(SortMode.PART_1, SortMode.PART_2,
                                 SortMode.DELTA),
                        help="Sorting method when displaying one day" +
                        " (default: part 2 completion time)")
    parser.add_argument("-h",
                        "--help",
                        action="store_true",
                        help="Prints this help message")

    # used to propagate usage errors out.
    # somewhat hacky. typically, this should be done by subclassing ArgumentParser
    def usage_error(message, *args, **kwargs):
        raise UsageSyntaxException(message)

    parser.error = usage_error  # type: ignore

    args = parser.parse_args(argv)

    if args.help:
        raise UsageSyntaxException("```\n" + parser.format_help() + "\n```")

    return args
Пример #11
0
class ArgumentCommand(Command):
    """A base command with argparse support rolled in."""
    def __init__(self, manager):
        """Initialize the command with an argument parser."""
        super(ArgumentCommand, self).__init__(manager)
        prog = "{} {}".format(os.path.basename(sys.argv[0]), self.name)
        self.argparser = ArgumentParser(add_help=False, prog=prog)
        self.options = None
        self.args = None
        self.add_arguments()

    def add_arguments(self):
        """Build the argument parser here. It's in self.argparser."""

    def parse_args(self, args, known=False):
        """
        Parse arguments into self.options. Flatten out any action=store values with nargs=1 or
        None. If known is True then only parse known args and place the remaining args into
        self.args.
        """
        def get_valid_action(dest):
            for action in self.argparser._actions:
                if isinstance(action, _StoreAction) and action.dest == dest:
                    if action.nargs is None or action.nargs == 1:
                        return action
                    break
            return None

        if known:
            self.options, self.args = self.argparser.parse_known_args(args[1:])
        else:
            self.options = self.argparser.parse_args(args[1:])

        for name, value in self.options.__dict__.iteritems():
            if hasattr(value, '__iter__'):
                action = get_valid_action(name)
                if action:
                    setattr(self.options, name, value[0])

    def get_usage(self):
        """Print usage text generated from argparse."""
        usage = self.argparser.format_usage().strip()
        return re.sub(r'^usage:\s[^\s]+\s[^\s]+\s', '', usage)

    def get_help(self):
        """Print help text generated from argparse."""
        text = self.argparser.format_help()
        regex = re.compile(r'^optional arguments')
        while text and not regex.match(text):
            start = text.find('\n') + 1
            text = text[start:]
        return text

    def run(self, args):
        """Parse the commandline arguments."""
        self.parse_args(args)
Пример #12
0
def argument_parser():
    parser = ArgumentParser()
    parser.format_help = lambda: __doc__.lstrip()
    add_argument = parser.add_argument
    add_argument('-o', '--output', type=str)
    add_argument('-t', '--timezone', type=str)
    add_argument('-p', '--period', type=str)
    add_argument('-w', '--width', type=int)
    add_argument('--title', type=str)
    add_argument('-g', '--grid', action='store_true')
    add_argument('-v', '--version', action='version', version=__version__)
    return parser
Пример #13
0
def initArgs():
    parser = ArgumentParser()
    parser.add_argument("fileList",
                        nargs='*',
                        help="filename list to check, default is stdin / -.",
                        default='*')

    global args
    try:
        args = parser.parse_args()
    except:
        print >> stderr, "\n" + parser.format_help()
        exit(-1)
Пример #14
0
def main():
    top_parser = ArgumentParser(description='Testing', add_help=False)
    top_parser.add_argument("--name",
                            default=IMPLEMENTED_TRAIN_ENGINE[0],
                            type=str,
                            choices=IMPLEMENTED_TRAIN_ENGINE,
                            help="test what?")
    top_parser.add_argument("-h", "--help",
                            action='store_true',
                            help="""
                                show this message. if name is specified, 
                                help message for test arg parser also will be show.
                                """)
    top_opt, other_args = top_parser.parse_known_args()

    have_specified_name = "--name" in sys.argv

    if top_opt.help and not have_specified_name:
        top_parser.print_help()
        print("\nSupported test engine for now:\n")
        for e in IMPLEMENTED_TRAIN_ENGINE:
            print("* " + e)
        print("\tYou must choose one of above as engine!")
        return

    test = select_test(top_opt.name)

    parser = ArgumentParser(description='Testing {}'.format(top_opt.name), add_help=False)
    parser.add_argument('--gpu_id', default=0, type=int, help='gpu_id: e.g. 0')
    parser.add_argument('--batch_size', default=8, type=int, help='batch size')
    parser.add_argument("--output_dir", type=str, default="ckp/")
    test.add_new_arg_for_parser(parser, top_opt.name)

    opt = parser.parse_args(other_args)
    opt.name = top_opt.name

    if top_opt.help and have_specified_name:
        print(parser.format_help())
        return

    torch.cuda.set_device(opt.gpu_id)
    print("Begin to test {}, using GPU {}".format(top_opt.name, opt.gpu_id))
    cudnn.benchmark = True
    device = "cuda"

    test_engine = test.get_tester(opt, device)
    data_loader = test.get_data_loader(opt)
    test_engine.run(data_loader, max_epochs=1)
Пример #15
0
def parse_args(args):
    import os
    from argparse import ArgumentParser

    home_path = os.environ['HOME']

    parser = ArgumentParser()
    subparsers = parser.add_subparsers(dest='subcommand')
    actions = {}

    install_name = 'install'
    install_command = subparsers.add_parser(
        name=install_name,
        help='create links to dotfile under ${HOME}.',
    )
    actions[install_name] = install

    install_command.add_argument(
        '--dest',
        '-d',
        type=str,
        default=home_path,
        help='the path which stores links to dotfile: {}'.format(home_path),
    )

    clean_name = 'clean'
    clean_command = subparsers.add_parser(
        name=clean_name,
        help='clean up links to dotfile contained in ${HOME}.',
    )
    actions[clean_name] = clean

    clean_command.add_argument(
        '--dest',
        '-d',
        type=str,
        default=home_path,
        help='the path which stores links to dotfile: {}'.format(home_path),
    )

    context = parser.parse_args(args)
    if context.subcommand is None:
        return None, parser.format_help()

    context.subcommand = actions[context.subcommand]

    return context, None
Пример #16
0
 def run(self):
     tree = self._parse_commands()
     parser = ArgumentParser(description="Bolinette Web Framework")
     sub_parsers = parser.add_subparsers()
     self._build_parsers(tree, sub_parsers, [])
     parsed = vars(parser.parse_args())
     if "__blnt_cmd__" in parsed:
         cmd = parsed.pop("__blnt_cmd__")
         self._run_command(cmd, parsed)
     elif "__blnt_path__" in parsed:
         self._console.error(
             self._sub_commands[parsed["__blnt_path__"]].format_help()
         )
         sys.exit(1)
     else:
         self._console.error(parser.format_help())
         sys.exit(1)
Пример #17
0
def main():
    '''Main method. Make the database from the given file and run the problem solutions.'''

    problems = (
        prob1,
        prob2,
        prob3,
        prob4,
    )

    from argparse import ArgumentParser

    argparser = ArgumentParser()
    argparser.add_argument(
        'fname',
        nargs='?',
        default='oecd-gdp-pc-change-1997-2017.csv',
        help='Name of the file from which to read the database\
 (default: oecd-gdp-pc-change-1997-2017.csv).')
    argparser.add_argument(
        'problems',
        nargs='*',
        type=int,
        help='Which problems to run (1-{0}, default: all).'.format(
            len(problems)),
        default=range(1,
                      len(problems) + 1))

    args = argparser.parse_args()

    db = Database()
    with open(args.fname) as fin:
        db.read_from_csv(fin, True)

    for prob in args.problems:
        if prob not in argparser.get_default('problems'):
            raise IndexError('Problems must be in the range ' +
                             str(argparser.get_default('problems')) + '\n' +
                             argparser.format_help())
        problems[prob - 1](db)

    return locals()
Пример #18
0
def initArgs():
    parser = ArgumentParser()
    parser.add_argument("fileList",
                        nargs='*',
                        help="filename list to check, default is stdin / -.",
                        default="-")
    parser.add_argument("-r",
                        "--recursive",
                        help="read files in text mode (default on Unix/Linux)",
                        action='store_true',
                        default=False)
    parser.add_argument("-d",
                        "--dir",
                        help="extract files into exdir.",
                        default=".")

    global args
    try:
        args = parser.parse_args()
    except:
        print >> stderr, "\n" + parser.format_help()
        exit(-1)
Пример #19
0
class Command(object):

    NAME = u""
    DESCRIPTION = u""""""

    DATE_FORMAT = "%Y-%m-%d %H:%M:%S%z"

    def __init__(self):
        self.arg_parser = ArgumentParser(self.NAME,
                                         description=self.DESCRIPTION,
                                         formatter_class=ArgumentDefaultsHelpFormatter,
                                         add_help=False)
        self.arg_parser.add_argument("-h", "--help", dest="help", action="store_true")
        self.add_arguments()

    def add_arguments(self):
        pass

    def run(self, session, args):
        pass

    def __str__(self):
        return u"Command '{0}'".format(self.NAME)

    def execute(self, session, arguments):
        args = self.arg_parser.parse_args(arguments)
        if args.help:
            return self.arg_parser.format_help().splitlines()
        return self.run(session, args)

    def format_message(self, message):
        return u"[{0}] {1}: {2}".format(
            message.created.strftime(self.DATE_FORMAT),
            message.user,
            message.message
        )
Пример #20
0
def _main(arguments):
    global al_result_to_text

    signal(SIGINT, SIG_DFL)
    if sys.platform.startswith("linux"):
        from signal import SIGPIPE
        signal(SIGPIPE, SIG_DFL)

    user = None
    pw = None
    cert = None
    apikey = None
    transport = "https"
    host = "localhost"
    port = 443
    kw = {}
    verify = True

    config = ConfigParser()
    config.read([expanduser("~/.al/submit.cfg")])
    for section in config.sections():
        if section == "auth":
            if 'user' in config.options('auth'):
                user = config.get('auth', 'user')
            if 'password' in config.options('auth'):
                pw = config.get('auth', 'password')
            if 'cert' in config.options('auth'):
                cert = config.get('auth', 'cert')
            if 'apikey' in config.options('auth'):
                apikey = config.get('auth', 'apikey')
            if 'insecure' in config.options('auth'):
                verify = config.get('auth',
                                    'insecure').lower() not in ['true', 'yes']
        elif section == "server":
            if 'transport' in config.options('server'):
                transport = config.get('server', 'transport')
            if 'host' in config.options('server'):
                host = config.get('server', 'host')
            if 'port' in config.options('server'):
                port = config.get('server', 'port')
            if 'cert' in config.options('server'):
                verify = config.get('server', 'cert')

    server = "%s://%s:%s" % (transport, host, port)

    # parse the command line args
    from argparse import ArgumentParser
    parser = ArgumentParser(description=description_string)
    parser.add_argument('files', metavar='file/dir', nargs='+')
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version=__version__)
    parser.add_argument('-q',
                        '--quiet',
                        action='store_true',
                        help='Runs in quiet mode')
    parser.add_argument('-a',
                        '--async',
                        dest='async_command',
                        action='store_true',
                        help='Run in asynchronized mode (uses ingest API).')
    parser.add_argument(
        '-n',
        '--no-output',
        action='store_true',
        help=
        'Only works in conjunction with -a. Ingests the file and does not wait for the output.'
    )
    parser.add_argument(
        '-i',
        '--insecure',
        action='store_true',
        default=not verify,
        help=
        'Skip server cert validation. DEFAULT: insecure in auth section of ~/.al/submit.cfg'
    )
    parser.add_argument('-t',
                        '--text',
                        action='store_true',
                        help='Dumps results as text instead of json.')
    parser.add_argument(
        '-d',
        '--run-dynamic',
        action='store_true',
        help='Adds Dynamic Analysis to the list of service to run.')
    parser.add_argument(
        '-u',
        '--user',
        default=user,
        metavar='"user"',
        help=
        'username to be used to connect to AL. DEFAULT: user in auth section of ~/.al/submit.cfg'
    )
    parser.add_argument(
        '-p',
        '--password',
        default=pw,
        metavar='"MYPASSWORD"',
        help=
        'password of the user. DEFAULT: password in auth section of ~/.al/submit.cfg'
    )
    parser.add_argument('-o',
                        '--output-file',
                        metavar='"/home/user/output.txt"',
                        help='File to write the results to. DEFAULT: stdout')
    parser.add_argument(
        '-s',
        '--server',
        default=server,
        metavar='"https://localhost:443"',
        help='Server to connect to. DEFAULT: transport://host:port in '
        'server section of ~/.al/submit.cfg')
    parser.add_argument(
        '-c',
        '--cert',
        default=cert,
        metavar='"/path/to/pki.pem"',
        help=
        'Client cert used to connect to server. DEFAULT: cert in auth section of ~/.al/submit.cfg'
    )
    parser.add_argument(
        '-k',
        '--apikey',
        default=apikey,
        metavar='"MY_RANDOM_API_KEY"',
        help=
        'apikey to use for the user to login. DEFAULT: apikey in auth section of ~/.al/submit.cfg'
    )
    parser.add_argument('-j',
                        '--json-params',
                        metavar='"{ ... }"',
                        help='A JSON dictionary of submission parameters.')
    parser.add_argument('-m',
                        '--metadata',
                        metavar='"{ ... }"',
                        help='A JSON dictionary of submission metadata.')
    parser.add_argument(
        '--srv-spec',
        metavar='"{ ... }"',
        help='A JSON dictionary of service specific parameters.')
    parser.add_argument(
        '--server-crt',
        metavar='"/path/to/server.crt"',
        help='DEFAULT: cert in server section of ~/.al/submit.cfg')

    params = parser.parse_args(arguments)

    args = params.files
    verbose = not params.quiet
    async_command = params.async_command
    no_output = params.no_output
    json_output = not params.text
    dynamic = params.run_dynamic
    user = params.user
    cert = params.cert
    pw = params.password
    apikey = params.apikey

    if params.insecure:
        verify = False
    else:
        if params.server_crt:
            verify = params.server_crt

    if not cert and not user:
        sys.stderr.write("This server requires authentication...\n")
        sys.exit(1)

    if user and not pw and not apikey:
        if verbose:
            sys.stderr.write(
                "You specified a username without a password.  What is your password?\n"
            )
        pw = getpass()

    output = params.output_file

    if output:
        f = None
        try:
            f = open(output, "ab")
        except Exception:  # pylint: disable=W0702
            sys.stderr.write("!!ERROR!! Output file cannot be created (%s)\n" %
                             output)
        finally:
            try:
                f.close()
            except Exception:  # pylint: disable=W0702
                pass

    server = params.server

    if not server:
        sys.stderr.write(
            "!!ERROR!! No server specified, -s option is mandatory.\n\n%s" %
            parser.format_help())
        return -1

    if params.metadata:
        kw['metadata'] = json.loads(params.metadata)

    if params.json_params:
        kw["params"] = json.loads(params.json_params)

    if params.srv_spec:
        kw.setdefault("params", {})
        kw["params"]["service_spec"] = json.loads(params.srv_spec)

    auth = None
    api_auth = None
    if user and apikey:
        api_auth = (user, apikey)
    elif user and pw:
        auth = (user, pw)

    options = {
        'verbose': verbose,
        'json_output': json_output,
    }

    read_from_pipe = False
    if sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
        try:
            if select.select([
                    sys.stdin,
            ], [], [], 0.0)[0]:
                read_from_pipe = True
        except io.UnsupportedOperation:
            # stdin has probably been replaced with a non-file python object
            # this is fine.
            pass

    if len(args) == 0 and not read_from_pipe:
        sys.stdout.write("%s\n" % parser.format_help())
        return 0

    try:
        client = get_client(server,
                            apikey=api_auth,
                            auth=auth,
                            cert=cert,
                            verify=verify)
        if isinstance(client, Client4):
            from assemblyline_client.v4_client.common.submit_utils import al_result_to_text
        else:
            from assemblyline_client.v3_client.utils import al_result_to_text
    except ClientError as e:
        if e.status_code == 401:
            sys.stderr.write(
                "!!ERROR!! Authentication to the server failed.\n")
        elif e.status_code == 495:
            sys.stderr.write(
                "!!ERROR!! Invalid SSL connection to the server:\n\t%s\n" % e)
        else:
            raise
        return 1

    if dynamic:
        p = client.user.submission_params("__CURRENT__")
        if "Dynamic Analysis" not in p['services']['selected']:
            p['services']['selected'].append("Dynamic Analysis")

        if 'params' in kw:
            p.update(kw['params'])

        kw['params'] = p

    if async_command and not no_output:
        kw['nq'] = "al_submit_%s" % get_random_id()

    # sanity check path
    if len(args) == 0 and read_from_pipe:
        while True:
            line = sys.stdin.readline()
            if not line:
                break

            line = line.strip()
            if line == '-':
                line = '/dev/stdin'

            if async_command:
                kw.setdefault('metadata', {})
                kw['metadata']['al_submit_id'] = get_id_from_path(line)
                send_async(client, line, verbose=verbose, **kw)
            else:
                send(client, line, output, options, **kw)
    else:
        ret_val = 0
        file_list = []

        for arg in args:
            if arg == '-':
                file_list.append('/dev/stdin')
            elif not exists(arg):
                sys.stderr.write("!!ERROR!! %s => File does not exist.\n" %
                                 arg)
                ret_val = 1
            elif isdir(arg):
                for root, _, fname_list in walk(arg):
                    for fname in fname_list:
                        file_list.append(join(root, fname))
            else:
                file_list.append(arg)

        queued_files = [get_id_from_path(f) for f in file_list]
        output_thread = None
        if async_command and not no_output:
            output_thread = start_result_thread(client, queued_files, output,
                                                options, **kw)

        for input_file in file_list:
            if async_command:
                kw.setdefault('metadata', {})
                kw['metadata']['al_submit_id'] = get_id_from_path(input_file)
                if not send_async(client, input_file, verbose=verbose, **kw):
                    with ASYNC_LOCK:
                        queued_files.remove(get_id_from_path(input_file))
                    if verbose:
                        sys.stderr.write(
                            "\tWARNING: Could not send file %s.\n" %
                            input_file)
                    ret_val = 1
            else:
                if not send(client, input_file, output, options, **kw):
                    ret_val = 1

        if output_thread:
            output_thread.join()

        if ret_val != 0 and len(file_list) > 1:
            if verbose:
                sys.stderr.write(
                    "\n** WARNING: al_submit encountered some "
                    "errors while processing multiple files. **\n")

        return ret_val
Пример #21
0
def parse_args():
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    argparser = ArgumentParser(
        description='A tool for testing PyQt GUI applications by recording '
                    'and replaying scenarios.',
        formatter_class=ArgumentDefaultsHelpFormatter,
    )
    argparser.add_argument(
        '--verbose', '-v', action='count',
        help='Print verbose information (use twice for debug).')
    argparser.add_argument(
        '--log', metavar='FILE',
        help='Log program output.')

    subparsers = argparser.add_subparsers(
        title='Sub-commands', dest='_subcommand',
        help='Use --help for additional sub-command arguments.')
    parser_record = subparsers.add_parser(
        'record',
        formatter_class=ArgumentDefaultsHelpFormatter,
        help='Record the events the user sends to the entry-point application '
             'into the scenario file.')
    parser_replay = subparsers.add_parser(
        'replay',
        formatter_class=ArgumentDefaultsHelpFormatter,
        help='Replay the recorded scenario.')
    parser_explain = subparsers.add_parser(
        'explain',
        formatter_class=ArgumentDefaultsHelpFormatter,
        help='Explain in semi-human-readable form the events scenario contains.')

    # TODO: default try to figure out Qt version by grepping entry-point
    args, kwargs = (
        ('--qt',),
        dict(metavar='QT_VERSION', default='5', choices='45',
             help='The version of PyQt to run the entry-point app with (4 or 5).'))
    parser_record.add_argument(*args, **kwargs)
    parser_replay.add_argument(*args, **kwargs)

    args, kwargs = (
        ('scenario',),
        dict(metavar='SCENARIO',
             help='The scenario file.'))
    parser_record.add_argument(*args, **kwargs)
    parser_replay.add_argument(*args, **kwargs)
    parser_explain.add_argument(*args, **kwargs)

    args, kwargs = (
        ('main',),
        dict(metavar='MODULE_PATH',
             help='The application entry point (module.path.to:main function).'))
    parser_record.add_argument(*args, **kwargs)
    parser_replay.add_argument(*args, **kwargs)

    args, kwargs = (
        ('args',),
        dict(metavar='ARGS',
             nargs='*',
             help='Additional arguments to pass to the app as sys.argv.'))
    parser_record.add_argument(*args, **kwargs)
    parser_replay.add_argument(*args, **kwargs)

    parser_record.add_argument(
        '--events-include', metavar='REGEX',
        default=r'MouseEvent,KeyEvent,CloseEvent,FocusEvent,DragLeaveEvent,DragEnterEvent,HoverEvent,HideEvent,'
                'ShowEvent,StatusTipEvent,ResizeEvent,SocketNotifier,StatusTipEvent', # TODO: add Drag, Focus, Hover ?
                #ChildEvent do not work'
        help='When recording, record only events that match the filter.')
    parser_record.add_argument(
        '--events-exclude', metavar='REGEX',
        help="When recording, skip events that match the filter.")
    parser_record.add_argument( # TODO
        '--objects-include', metavar='REGEX',
        help='When recording, record only events on objects that match the filter.')
    parser_record.add_argument( # TODO
        '--objects-exclude', metavar='REGEX',
        help="When recording, skip events on objects that match the filter.")

    parser_replay.add_argument(
        '--x11', action='store_true',
        help=('When replaying scenarios, do it in a new, headless X11 server. '
              "This makes your app's stdout piped to stderr."))
    parser_replay.add_argument(
        '--x11-video', metavar='FILE', nargs='?', const=True,
        help='Record the video of scenario playback into FILE (default: SCENARIO.mp4).')
    parser_replay.add_argument( # TODO
        '--coverage', action='store_true',
        help='Run the coverage analysis simultaneously.')

    args = argparser.parse_args()

    def init_logging(verbose=0, log_file=None):
        formatter = logging.Formatter('%(relativeCreated)d %(levelname)s: %(message)s')
        for handler in (logging.StreamHandler(),
                        log_file and logging.FileHandler(log_file, 'w', encoding='utf-8')):
            if handler:
                handler.setFormatter(formatter)
                log.addHandler(handler)
        log.setLevel(logging.WARNING - 10 * verbose)

    init_logging(args.verbose or 0, args.log)
    log.info('Program arguments: %s', args)

    def _error(*args, **kwargs):
        log.error(*args, **kwargs)
        REAL_EXIT(1)

    def _is_command_available(command):
        try:
            return subprocess.call(['which', command],
                                   stdout=subprocess.DEVNULL) == 0
        except OSError:
            return False

    def _check_main(args):

        def _main(entry_point=args.main):
            # Make the application believe it was run unpatched
            sys.argv = [entry_point] + args.args
            try:
                module, main_func = entry_point.split(':')
                log.debug('Importing module %s ...', module)
                module = import_module(module)
                real_main = deepgetattr(module, main_func)
                if not callable(real_main):
                    raise ValueError
            except ValueError:
                _error('MODULE_PATH must be like module.path.to:main function')
            else:
                log.info('Running %s', entry_point)
                real_main()

        args.main = _main

    def _global_qt(args):
        global QtGui, QtCore, QWidget, Qt, qApp, QT_KEYS, EVENT_TYPE
        PyQt = 'PyQt' + str(args.qt)
        QtGui = import_module(PyQt + '.QtGui')
        QtCore = import_module(PyQt + '.QtCore')
        Qt = QtCore.Qt
        try:
            QWidget = QtGui.QWidget
            qApp = QtGui.qApp
        except AttributeError:  # PyQt5
            QtWidgets = import_module(PyQt + '.QtWidgets')
            QWidget = QtWidgets.QWidget
            qApp = QtWidgets.qApp
        QT_KEYS = {value: 'Qt.' + key
                   for key, value in Qt.__dict__.items()
                   if key.startswith('Key_')}
        EVENT_TYPE = {v: k
                      for k, v in QtCore.QEvent.__dict__.items()
                      if isinstance(v, int)}
        # This is just a simple unit test. Put here because real Qt has only
        # been made available above.
        assert Resolver._qflags_key(Qt, Qt.LeftButton | Qt.RightButton) == \
               'Qt.LeftButton|Qt.RightButton'

    def check_explain(args):
        try:
            args.scenario = open(args.scenario, 'rb')
        except (IOError, OSError) as e:
            _error('explain %s: %s', args.scenario, e)

    def check_record(args):
        _check_main(args)
        _global_qt(args)
        try:
            args.scenario = open(args.scenario, 'wb')
        except (IOError, OSError) as e:
            _error('record %s: %s', args.scenario, e)

    def check_replay(args):
        _check_main(args)
        _global_qt(args)
        try:
            args.scenario = open(args.scenario, 'rb')
        except (IOError, OSError) as e:
            _error('replay %s: %s', args.scenario, e)
        # TODO: https://coverage.readthedocs.org/en/coverage-4.0.2/api.html#api
        #       https://nose.readthedocs.org/en/latest/plugins/cover.html#source
        if args.x11_video:
            if not _is_command_available('ffmpeg'):
                _error('Recording video of X11 session (--x11-video) requires '
                       'ffmpeg. Install package ffmpeg.')
            if not args.x11:
                log.warning('--x11-video implies --x11')
                args.x11 = True
            if args.x11_video is True:
                args.x11_video = args.scenario.name + '.mp4'
        if args.x11:
            for xvfb in ('Xvfb', '/usr/X11/bin/Xvfb'):
                if _is_command_available(xvfb):
                    break
            else:
                _error('Headless X11 (--x11) requires working Xvfb. '
                       'Install package xvfb (or XQuartz on a Macintosh).')
            for xauth in ('xauth', '/usr/X11/bin/xauth'):
                if _is_command_available(xauth):
                    break
            else:
                _error('Headless X11 (--x11) requires working xauth. '
                       'Install package xauth (or XQuartz on a Macintosh).')

            log.info('Re-running head-less in Xvfb.')
            # Prevent recursion
            for arg in ('--x11', '--x11-video'):
                if arg in sys.argv:
                    sys.argv.remove(arg)

            from random import randint
            from hashlib import md5
            env = os.environ.copy()
            env.update(
                VIDEO_FILE=args.x11_video,
                RESOLUTION='1280x1024',
                SCENARIO=args.scenario.name,
                AUTH_FILE=os.path.join(os.path.expanduser('~'), '.Xauthority'),
                XVFB=xvfb,
                XAUTH=xauth,
                MCOOKIE=md5(os.urandom(30)).hexdigest(),
                DISPLAY=next(i for i in (randint(111, 10000) for _ in repeat(0))
                             if not os.path.exists('/tmp/.X{}-lock'.format(i))),
                ARGV=' '.join(sys.argv))
            REAL_EXIT(subprocess.call(X11_SHELL_SCRIPT,
                                      shell=True,
                                      stdout=sys.stderr,
                                      env=env))

    try:
        dict(record=check_record,
             replay=check_replay,
             explain=check_explain)[args._subcommand](args)
    except KeyError:
        return REAL_EXIT(argparser.format_help())
    return args
Пример #22
0
class Command(object):
    """
    Represents an actual command, something the user can enter into the shell.

    ...

    Attributes
    ----------
    aliases : List[str]
        A list of all the keywords (aliases) that the command has; that will trigger it.
    case_sensitive : bool
        Whether or not the command should respond to both the upper- and lowercase 
        versions of its aliases.
    callback : Callable[..., None]
        The function that will be called, when the command is executed.
    help : str
        A short summary of what the command does.
    cbargs : Union[Tuple[()], Tuple[Any]]
        Positional arguments that will be passed to the callback function.
    cbkwargs : Dict[str, Any]
        Keyword arguments that will be passed to the callback function.
    parent : Command
        The command's supercommand (needed for tracing).

    Methods
    -------
    matches(args)
        Checks whether or not the command should be triggered by the given cl arguments.
    trace(cu=None)
        Computes the command trace. The chain of supercommands that is needed to trigger this command.
    usage()
        Returns a short summary of this commands arguments and trace.
    __call__()
        Will call the specified callback function.
    __str__()
        Returns a nicely formatted, short help string for the command.
    """
    def __init__(self,
                 cmd: str,
                 *aliases: str,
                 case_sensitive: bool = True,
                 callback: Callable[..., None] = _def_callback,
                 hint: str = '',
                 cbargs: Union[Tuple[()], Tuple[Any]] = (),
                 cbkwargs: Optional[Dict[str, Any]] = None,
                 parent: Optional["Command"] = None):
        """
        Parameters
        ----------
        cmd : str
            The main keyword that will trigger this commands.
        *aliases : str
            Aliases for the main keyword.
        case_sensitive : bool
            Whether or not the lower-, upper- and mixedcase versions of the aliases should be acceptable.
            Default is True.
        callback : Callable[..., None]
            The function that will be called when this command is triggerd.
            Default is _def_callback.
        hint : str
            The short description (will be help)
            Default is ''.
        cbargs : Union[Tuple[()], Tuple[Any]]
            A tuple of positional arguments that will be passed to the callback.
            Default is ().
        cbkwargs : Optional[Dict[str, Any]]
            A dictionary of keyword arguments that will be passed to the callback.
            Default is None.
        parent : Optional[Command]
            The command's supercommand.
            Default is None.
        """
        self.aliases: List[str] = [cmd, *aliases]
        self.case_sensitive: bool = case_sensitive
        self.callback: Callable[..., None] = callback
        self.help: str = hint
        self.cbargs: Union[Tuple[()], Tuple[Any]] = cbargs
        self.cbkwargs: Dict[str, Any] = cbkwargs or dict()
        self.parent: "Command" = parent
        self.parser = ArgumentParser()

    def add_arg(self, *args, **kwargs) -> None:
        self.parser.add_argument(*args, **kwargs)

    def matches(self, args: Union[str, List[str]]) -> bool:
        """
        Checks whether or not the command should be triggered by the given cl arguments.
        
        ...

        Parameters
        ----------
        args : Union[str, List[str]]
            Either the commandline or the already split arguments.

        Returns
        -------
        bool
            Whether or not the command should be executed.
        """
        cmd: str = (args[0]
                    if isinstance(args, list) else args.split()[0]).strip()
        return cmd in self.aliases or (
            cmd.lower() in [a.lower()
                            for a in self.aliases] and not self.case_sensitive)

    def trace(self, cu: Optional["Command"] = None) -> str:
        """
        Will compute and return the command's trace.

        Recursively goes through the supercommands until it reaches the shell;
        returns the resulting trace.

        Parameters
        ----------
        cu : Optional[Command]
            Since it's recursive - the current command.
            Default is None

        Returns
        -------
        str
            The trace (chain of keywords) needed to trigger the command.
        """
        c = cu
        if not c:
            c = self
        return (self.trace(c.parent)
                if c.parent else '') + c.aliases[0] + (' ' if cu else '')

    def usage(self) -> str:
        """Will return how to use the command."""
        return self.trace() + ' ' + self.parser.format_help(
        )[self.parser.format_help().index('['):]

    def __call__(self, args: Union[str, List[str]]) -> None:
        """Will call the callback function."""
        try:
            ar = self.parser.parse_args(
                args if isinstance(args, list) else args.split())
        except SystemExit:
            print(self.usage())
            return
        self.callback(self, args if isinstance(args, list) else args.split(),
                      *self.cbargs, **self.cbkwargs, **vars(ar))

    def __str__(self) -> str:
        """Returns a nicely formatted help-string."""
        return self.aliases[0] + '\t' + self.help
Пример #23
0
class DoppelHandler(BotHandler):
    
    DOPPEL_PATTERNS = [
        u'doppel',
    ]
    def __init__(self, name, original_name):
        BotHandler.__init__(self, name)
        description = u'本人に成りすまして物まねをします。'.encode('utf-8')
        self.after_fake = False
        self.original_name = original_name
        self.original_user = None
        self.original_icon = None
        self.original_mension = None
        respond_patterns = [
            (DoppelHandler.DOPPEL_PATTERNS, self.fake),
        ]
        self.set_respond_patterns(respond_patterns)
        self.parser = ArgumentParser(description=description, prog=u'@slackbot doppel'.encode('utf-8'), add_help=False)

        self.parser.add_argument('-h', '--help', action='store_true', dest='usage', help=u'ヘルプを表示します。'.encode('utf-8'))

        self.parser.add_argument('user', help=u'なり替わるユーザ(@user)'.encode('utf-8'))

    def on_hello(self, bitslack_obj, event):
        BotHandler.on_hello(self, bitslack_obj, event)
        self.init_original_info(bitslack_obj)
        self._update_immitate_patterns()

    def on_user_change(self, bitslack_obj, event):
        self.original_name = event['user']['name']
        self.original_user = event['user']
        self.original_icon = self._get_icon()
        self.original_mension = u'<@%s>' % (self.original_user['id'])
        self._update_immitate_patterns()
        bitslack_obj.dump(event)

    def fake(self, bitslack_obj, event, args):
        text_list = event['text'].split()
        args, unknown_args = self.parser.parse_known_args(text_list)
        if args.usage:
            usage = self.parser.format_help()
            return usage.decode('utf-8')
        original_name = self._get_username(bitslack_obj, event['text'])
        self.init_original_info(bitslack_obj, original_name)
        text = u'%sさんになりすましました。' % (self.original_mension)
        self._update_immitate_patterns()
        self.talk_as_original(bitslack_obj, text, event['channel'])
        self.after_fake = True
        return None

    def _update_immitate_patterns(self):
        hear_patterns = [
            (
                self._make_immitate_patterns(), 
                self.immitate
            ),
        ]
        self.set_hear_patterns(hear_patterns)

    def _exists(self, key, dict_data):
        return key in dict_data and dict_data[key] != ''

    def _make_immitate_patterns(self):
        result = []
        if self.original_mension:
            result.append(self.original_mension)
        if self._exists('profile', self.original_user):
            profile = self.original_user['profile']
            if self._exists('real_name', profile):
                result.append(self.original_user['profile']['real_name'])
            if self._exists('real_name_normalized', profile):
                result.append(self.original_user['profile']['real_name_normalized'])
            if self._exists('last_name', profile):
                result.append(self.original_user['profile']['last_name'])
            if self._exists('first_name', profile):
                result.append(self.original_user['profile']['first_name'])
            if self._exists('skype', profile):
                result.append(self.original_user['profile']['skype'])
        if self._exists('name', self.original_user):
            result.append(self.original_user['name'])
        return result

    def immitate(self, bitslack_obj, event, args):
        if self.is_bot(event):
            return None
        if not self.after_fake:
            userid = '<@%s>' % (event['user'])
            text = re.sub(args[0], '', event['text'])
            self.talk_as_original(bitslack_obj, text, event['channel'])
        self.after_fake = False
        return None

    def _get_username(self, bitslack_obj, text):
        result = None
        userid = self._parse_user(text)
        users = bitslack_obj.get_users()
        for username, user in users.items():
            if user['id'] == userid:
                result = username
                break
        return result

    def _parse_user(self, text):
        result = None
        text = re.sub(r'<@USLACKBOT>:?', '', text, count=1)
        match_result = re.search(r'<@(.*?)>', text)
        if match_result is not None:
            result = match_result.group(1)
        return result

    def init_original_info(self, bitslack_obj, original_name=None):
        if original_name is not None:
            self.original_name = original_name
        self.original_user = bitslack_obj.get_user(self.original_name)
        self.original_icon = self._get_icon()
        self.original_mension = u'<@%s>' % (self.original_user['id'])

    def _get_icon(self):
        result = None
        if self.original_user is not None:
            image_key = None
            if 'profile' in self.original_user:
                for k in self.original_user['profile'].keys():
                    if 'image_' in k and k != 'image_original':
                        if image_key is None:
                            image_key = k
                        elif int(k[6:]) > int(image_key[6:]):
                            image_key = k
                if image_key is not None:
                    result = self.original_user['profile'][image_key]
        return result
   
    def talk_as_original(self, bitslack_obj, texts, channel):
        bitslack_obj.talk(texts, channel, botname=self.original_name, boticon=self.original_icon)
Пример #24
0
def parse_known_args_and_warn(
    parser: argparse.ArgumentParser,
    other_args: List[str],
    export_allowed: int = NO_EXPORT,
    raw: bool = False,
    limit: int = 0,
):
    """Parses list of arguments into the supplied parser

    Parameters
    ----------
    parser: argparse.ArgumentParser
        Parser with predefined arguments
    other_args: List[str]
        List of arguments to parse
    export_allowed: int
        Choose from NO_EXPORT, EXPORT_ONLY_RAW_DATA_ALLOWED,
        EXPORT_ONLY_FIGURES_ALLOWED and EXPORT_BOTH_RAW_DATA_AND_FIGURES
    raw: bool
        Add the --raw flag
    limit: int
        Add a --limit flag with this number default
    Returns
    -------
    ns_parser:
        Namespace with parsed arguments
    """
    parser.add_argument(
        "-h", "--help", action="store_true", help="show this help message"
    )
    if export_allowed > NO_EXPORT:
        choices_export = []
        help_export = "Does not export!"

        if export_allowed == EXPORT_ONLY_RAW_DATA_ALLOWED:
            choices_export = ["csv", "json", "xlsx"]
            help_export = "Export raw data into csv, json, xlsx"
        elif export_allowed == EXPORT_ONLY_FIGURES_ALLOWED:
            choices_export = ["png", "jpg", "pdf", "svg"]
            help_export = "Export figure into png, jpg, pdf, svg "
        else:
            choices_export = ["csv", "json", "xlsx", "png", "jpg", "pdf", "svg"]
            help_export = "Export raw data into csv, json, xlsx and figure into png, jpg, pdf, svg "

        parser.add_argument(
            "--export",
            default="",
            type=check_file_type_saved(choices_export),
            dest="export",
            help=help_export,
        )

    if raw:
        parser.add_argument(
            "--raw",
            dest="raw",
            action="store_true",
            default=False,
            help="Flag to display raw data",
        )
    if limit > 0:
        parser.add_argument(
            "-l",
            "--limit",
            dest="limit",
            default=limit,
            help="Number of entries to show in data.",
            type=int,
        )

    if gtff.USE_CLEAR_AFTER_CMD:
        system_clear()

    try:
        (ns_parser, l_unknown_args) = parser.parse_known_args(other_args)
    except SystemExit:
        # In case the command has required argument that isn't specified
        console.print("")
        return None

    if ns_parser.help:
        txt_help = parser.format_help()
        console.print(f"[help]{txt_help}[/help]")
        return None

    if l_unknown_args:
        console.print(f"The following args couldn't be interpreted: {l_unknown_args}")

    return ns_parser
Пример #25
0
class WhereIsHandler(BotHandler):
    file_path = 'whereis.txt'

    def __init__(self, name=None):
        BotHandler.__init__(self, name)
        description = u'居場所を設定します。'.encode('utf-8')
        respond_patterns = [
            ([u'where_is',], self._on_where_is),
            ([u'im_at',], self._on_im_at),
            ([u'bot_at',], self._on_bot_at),
            ([u'where_are_we',], self._on_where_are_we),
        ]
        self.set_respond_patterns(respond_patterns)
        self.parser = ArgumentParser(description=description, prog=u'@slackbot where_is|im_at|bot_at|where_are_we'.encode('utf-8'), add_help=False)

        self.parser.add_argument('-h', '--help', action='store_true', dest='usage', help=u'ヘルプを表示します。'.encode('utf-8'))

        self.parser.add_argument('user', help=u'ユーザID(@slackbotなど)'.encode('utf-8'))
        self.parser.add_argument('place', help=u'場所(im_at, bot_atのみ)'.encode('utf-8'))

    def _help(self, bitslack_obj, event, args):
        result = None
        text_list = event['text'].split()
        args, unknown_args = self.parser.parse_known_args(text_list)
        if args.usage:
            usage = self.parser.format_help()
            result = usage.decode('utf-8')
        return result

    def _on_where_is(self, bitslack_obj, event, args):
        help = self._help(bitslack_obj, event, args)
        if help is not None:
            return help
        user_list = self._parse_user_list(event['text'])
        not_user_list = self._parse_not_user_list(event['text'])
        whereis_data = load(self.file_path)
        texts = []
        userid = u'<@%s>' % (event['user'])
        texts.append(u'%s:' % userid)
        for user in user_list:
            if user in whereis_data:
                texts.append(u'%sさんは %s にいます。' % (user, whereis_data[user]))
            else:
                texts.append(u'%sさんの居場所はわかりません。' % (user))
        for not_user in not_user_list:
            texts.append(u'%sさんは多分このチームに所属していません。' % (not_user))
        return texts

    def _on_where_are_we(self, bitslack_obj, event, args):
        help = self._help(bitslack_obj, event, args)
        if help is not None:
            return help
        users = bitslack_obj.get_users()
        whereis_data = load(self.file_path)
        texts = []
        userid = u'<@%s>' % (event['user'])
        for username, user_info in users.items():
            user = u'<@%s>' % (user_info['id'])
            if user in whereis_data:
                texts.append(u'%sさんは %s にいます。' % (user, whereis_data[user]))
            else:
                texts.append(u'%sさんの居場所はわかりません。' % (user))
        return texts

    def _parse_user_list(self, text):
        result = []
        text = re.sub(r'<@USLACKBOT>:?', '', text, count=1)
        result = re.findall(r'(<@.*?>)', text)
        return result

    def _parse_not_user_list(self, text):
        result = []
        text = re.sub(r'<@USLACKBOT>:?', '', text, count=1)
        result = re.findall(r'[^<](@[a-zA-Z0-9._-]*)', text)
        return result

    def _on_im_at(self, bitslack_obj, event, args):
        help = self._help(bitslack_obj, event, args)
        if help is not None:
            return help
        where = self._parse_where(event['text'], u'im_at')
        texts = []
        userid = u'<@%s>' % (event['user'])
        whereis_data = load(self.file_path)
        old_where = None
        if userid in whereis_data:
            old_where = whereis_data[userid]
        whereis_data[userid] = where
        save(whereis_data, self.file_path)
        if old_where is not None:
            texts.append(u'%s: 居場所を変更しました。(%s -> %s)' % (userid, old_where, where))
        else:
            texts.append(u'%s: 居場所を設定しました。(%s)' % (userid, where))
        return texts

    def _on_bot_at(self, bitslack_obj, event, args):
        help = self._help(bitslack_obj, event, args)
        if help is not None:
            return help
        where = self._parse_where(event['text'], u'bot_at')
        texts = []
        userid = u'<@%s>' % (event['user'])
        botid = u'<@USLACKBOT>'
        whereis_data = load(self.file_path)
        old_where = None
        if botid in whereis_data:
            old_where = whereis_data[botid]
        whereis_data[botid] = where
        save(whereis_data, self.file_path)
        if old_where is not None:
            texts.append(u'%s: %sの居場所を変更しました。(%s -> %s)' % (userid, botid, old_where, where))
        else:
            texts.append(u'%s: %sの居場所を設定しました。(%s)' % (userid, botid, where))
        return texts

    def _parse_where(self, text, prefix):
        result = ''
        where_list = re.findall(u'%s (.*)' % (prefix), text)
        if len(where_list) > 0:
            result = where_list[0]
        return result
Пример #26
0
from argparse import ArgumentParser
__version__ = '0.1'
parser = ArgumentParser(version=__version__)
parser.add_argument("args", nargs="*")
parser.add_argument("-f", "--file", dest="filename",
          help="write report to FILE", metavar="FILE")

__doc__ = '\n'.join([parser.format_usage(), parser.format_help()])

if __name__ == '__main__':
    args = parser.parse_args()
    print args, args.filename, args.args
Пример #27
0
class Program(object):
    def __init__(self, argv=None):
        self._children = []
        if argv is None:
            argv = sys.argv
        else:
            sys.argv.extend(argv)
        self._program_name = os.path.basename(sys.argv[0])
        self._program_version = "v%s" % __version__
        self._program_build_date = str(__updated__)
        self._program_shortdesc = __import__("__main__").__doc__.split("\n")[1]
        self._program_date = str(__date__)
        self._program_author = __author__
        self._configuration = {}
        self.configuration = {}
        self._configuration["jinjamator_user_directory"] = os.path.sep.join(
            [os.path.expanduser("~"), ".jinjamator"])
        self._configuration["jinjamator_base_directory"] = os.path.dirname(
            os.path.realpath(__file__))
        self._output_plugin = None

    def addArguments(self):
        self._parser.add_argument(
            "-h",
            "--help",
            dest="help",
            action="count",
            help="show this help message and exit",
        )
        self._parser.add_argument(
            "-o",
            "--output-plugin",
            dest="output_plugin",
            help=
            "selects the plugin which is used for futher data processing after tasklrt template has been rendered [default: %(default)s]",
            default="console",
        )
        self._parser.add_argument(
            "-m",
            "--mapping",
            dest="mapping",
            help=
            "map data (strings,integer or json) to a variable, e.g. -m 'var_name:asdf' or -m 'var_name:{\"key\":\"value\"}' ",
            action="append",
        )
        self._parser.add_argument(
            "-t",
            "--task-dir",
            dest="_taskdir",
            help=
            "path to task directory or tasklet file which should be run (CLI only)",
        )
        self._parser.add_argument(
            "-v",
            "--verbose",
            dest="_verbose",
            action="count",
            help="set verbosity level [default: ERROR]",
        )
        self._parser.add_argument("-V",
                                  "--version",
                                  action="version",
                                  version=self._program_version_message)
        self._parser.add_argument(
            "-g",
            "--global-defaults",
            dest="_global_defaults",
            default=None,
            help="path to a global defaults.yaml [default: %(default)s]",
        )
        self._parser.add_argument(
            "--best-effort",
            dest="best_effort",
            default=False,
            action="store_true",
            help="allow task items to fail",
        )
        self._parser.add_argument(
            "-d",
            "--daemonize",
            dest="_daemonize",
            default=False,
            action="store_true",
            help="run in daemon mode",
        )
        self._parser.add_argument(
            "--listen-address",
            dest="_daemon_listen_address",
            default="127.0.0.1",
            help="on which ip should the daemon listen [default: %(default)s]",
        )
        self._parser.add_argument(
            "--listen-port",
            dest="_daemon_listen_port",
            default="5000",
            help=
            "on which TCP port should the daemon listen [default: %(default)s]",
        )
        self._parser.add_argument(
            "--no-worker",
            dest="_no_worker",
            default=False,
            action="store_true",
            help="do not spawn local celery worker",
        )
        self._parser.add_argument(
            "--just-worker",
            dest="_just_worker",
            default=False,
            action="store_true",
            help="spawn worker only",
        )
        self._parser.add_argument(
            "--celery-broker-url",
            dest="_celery_broker",
            # default="amqp://*****:*****@localhost:5672/jinjamator",
            default=f"filesystem://",
            help=
            "celery broker URL (required for daemon mode)  [default: %(default)s]",
        )
        self._parser.add_argument(
            "--celery-result-backend",
            dest="_celery_result_backend",
            default=
            f'sqlite:///{self._configuration.get("jinjamator_user_directory",tempfile.gettempdir())}/jinjamator-results.db',
            help=
            "celery result backend URL (required for daemon mode)  [default: %(default)s]",
        )
        self._parser.add_argument(
            "--celery-heartbeat-database",
            dest="_celery_beat_database",
            default=
            f'{self._configuration.get("jinjamator_user_directory", tempfile.gettempdir())}/jinjamator-beat.db',
            help=
            "celery result beat Database (required for daemon mode)  [default: %(default)s]",
        )
        self._parser.add_argument(
            "--task-base-dir",
            dest="_global_tasks_base_dirs",
            default=[
                os.path.sep.join([
                    self._configuration["jinjamator_user_directory"], "tasks"
                ]),
                os.path.sep.join([
                    self._configuration["jinjamator_base_directory"], "tasks"
                ]),
            ],
            action="append",
            help=
            "where should jinjamator look for tasks in daemon mode [default: %(default)s]",
        )

        self._parser.add_argument(
            "--output-plugin-base-dir",
            dest="_global_output_plugins_base_dirs",
            default=[
                os.path.sep.join([
                    self._configuration["jinjamator_base_directory"],
                    "plugins",
                    "output",
                ])
            ],
            action="append",
            help=
            "where should jinjamator look for output plugins  [default: %(default)s]",
        )

        self._parser.add_argument(
            "--content-plugin-base-dir",
            dest="_global_content_plugins_base_dirs",
            default=[
                os.path.sep.join([
                    self._configuration["jinjamator_base_directory"],
                    "plugins",
                    "content",
                ])
            ],
            action="append",
            help=
            "where should jinjamator look for output plugins  [default: %(default)s]",
        )

        self._parser.add_argument(
            "--environment-base-dir",
            dest="_global_environments_base_dirs",
            default=[
                os.path.sep.join([
                    self._configuration["jinjamator_user_directory"],
                    "environments"
                ])
            ],
            action="append",
            help=
            "where should jinjamator look for environments [default: %(default)s]",
        )

    def setupLogging(self):
        global logging
        logging.addLevelName(90, "TASKLET_RESULT")

        def tasklet_result(self, message, *args, **kws):
            # Yes, logger takes its '*args' as 'args'.
            self._log(90, message, args, **kws)

        logging.Logger.tasklet_result = tasklet_result

        msg_format = "%(asctime)s - %(process)d - %(threadName)s - %(funcName)s - %(levelname)s - %(message)s"
        stdout = logging.StreamHandler(sys.stdout)
        formatter = logging.Formatter(msg_format)
        stdout.setFormatter(formatter)
        self._log = logging.getLogger()
        self._log.addHandler(stdout)

    def setup(self):
        self.setupLogging()

        self._program_version_message = "%%(prog)s %s (%s)" % (
            self._program_version,
            self._program_build_date,
        )
        self._program_license = f"""{self._program_shortdesc}

  Copyright 2019 Wilhelm Putz
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

USAGE
"""
        try:
            # Setup argument parser
            self._parser = ArgumentParser(
                description=self._program_license,
                formatter_class=RawDescriptionHelpFormatter,
                add_help=False,
            )
            self.addArguments()

            # Process default arguments
            self._args, unknown = self._parser.parse_known_args()

            for arg in vars(self._args):
                if arg.startswith("_"):
                    self._configuration[arg[1:]] = getattr(self._args, arg)
                else:
                    self.configuration[arg] = getattr(self._args, arg)

            load_output_plugin(
                self,
                self.configuration["output_plugin"],
                self._configuration["global_output_plugins_base_dirs"],
            )
            if self._output_plugin:
                self._output_plugin.addArguments()
            self._args, unknown = self._parser.parse_known_args()

            if unknown:
                self._log.warn("unkown arguments found: {0}".format(unknown))

            if self._args.help:
                print(self._parser.format_help())
                sys.exit(0)

            if not self._configuration.get(
                    "taskdir") and not self._configuration.get("daemonize"):
                print(self._parser.format_help())
                sys.exit(1)

            verbose = self._configuration.get("verbose") or 0

            if verbose > 0:
                self._log.setLevel(logging.ERROR)
            if verbose > 1:
                self._log.setLevel(logging.WARN)
            if verbose > 2:
                self._log.setLevel(logging.INFO)
            if verbose > 3:
                self._log.setLevel(logging.DEBUG)
            try:
                self._args.config
                self.readConfig(self._args.config)
            except AttributeError:
                pass

            for arg in vars(self._args):
                if arg.startswith("_"):
                    self._configuration[arg[1:]] = getattr(self._args, arg)
                else:
                    self.configuration[arg] = getattr(self._args, arg)

            if self._configuration["taskdir"]:
                self._configuration["taskdir"] = os.path.abspath(
                    self._configuration["taskdir"])

            for index, global_tasks_base_dir in enumerate(
                    self._configuration["global_tasks_base_dirs"]):
                self._configuration["global_tasks_base_dirs"][
                    index] = os.path.abspath(global_tasks_base_dir)

        except Exception as e:
            raise (e)

    def run(self):
        self._log.debug("called run")
        try:
            self.main()
        except KeyboardInterrupt:
            sys.exit(0)

    def main(self):
        for d in ["environments", "logs", "tasks", "uploads"]:
            os.makedirs(
                os.path.sep.join(
                    [self._configuration["jinjamator_user_directory"], d]),
                exist_ok=True,
            )

        if self._configuration["daemonize"]:
            from jinjamator.daemon import run as app_run

            app_run(self._configuration)

        else:
            # legacy cli task
            from jinjamator.task import JinjamatorTask

            task = JinjamatorTask("interactive")
            if self._configuration["global_defaults"]:
                task.configuration.merge_yaml(
                    self._configuration["global_defaults"])

            task.configuration.merge_dict(self.configuration)
            task._configuration.merge_dict(self._configuration)

            task.load_output_plugin(
                self.configuration["output_plugin"],
                self._configuration["global_output_plugins_base_dirs"],
            )

            try:
                task.load(self._configuration["taskdir"])
            except ValueError:
                if os.path.isdir(self._configuration["taskdir"]):
                    self._log.error(
                        f'No Tasklets found in {self._configuration["taskdir"]} -> exiting'
                    )
                else:
                    self._log.error(
                        f'Task directory {self._configuration["taskdir"]} not found -> exiting'
                    )
            task.run()
Пример #28
0
 def __print_full_help(self, args, parser: argparse.ArgumentParser):
     return parser.format_help()
Пример #29
0
#!/usr/bin/env python
# -*- coding: utf8 -*-

import firewall.client as client
import re
from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument('-d', '--delport', action='store', help='delete forwarded port')
parser.add_argument('-D', '--deladdr', action='store', help='delete forwarded port')
parser.add_argument('-a', '--addport', action='store', help='add port to forward', nargs=2)
parser.add_argument('-v', '--view', action='store_true', help='View forwarded ports')
parser.add_argument('-i', '--interactive', action='store_true', help='Interactive mode')

args = parser.parse_args()
parser.format_help()

clnt = client.FirewallClient()
frwrd = clnt.getForwardPorts('external')


def viewforwards():
    """
    Displays currently forwarded ports and addresses that they are forwarded to
    """
    for i in range(len(frwrd)):
        port, proto, toport, toaddr = tuple(frwrd[i])
        print('| ' + str(
            i) + ' | FromPort: ' + port + ' | ToPort: ' + toport + ' | IpAddress: ' + '%-15s' % toaddr + ' | Protocol:' + proto)

 def show_help_from(self, argparser: argparse.ArgumentParser):
     """Set exit code and output help from an argparser."""
     self.exit_code = self.exit_codes.success
     self.poutput(argparser.format_help())
Пример #31
0
                    help="Where to put the results")

parser.add_argument("--overwrite", action="store_true", default=False,
                    help="Overwrite existing output?  Default is to append.")

parser.add_argument("--help", action="store_true",
                    help="Show help")

parser.add_argument("--num-matvecs", action="store", default=40,
                    type=int, help="Number of MatVecs to perform")

args, _ = parser.parse_known_args()


if args.help:
    help = parser.format_help()
    PETSc.Sys.Print("%s\n" % help)


if args.problem is None:
    PETSc.Sys.Print("Must provide problem type\n")
    sys.exit(1)


module = importlib.import_module("problem.%s" % args.problem)
prob_args, _ = module.Problem.argparser().parse_known_args()

if args.problem == "rayleigh_benard":
    if prob_args.dimension == 2:
        sizes = (150, 200, 250, 200)
        degrees = range(1, 5)
Пример #32
0
def main(scripts_dir):
    names = {}
    names['skool_daze.py'] = 'Skool Daze'
    names['back_to_skool.py'] = 'Back to Skool'
    names['skool_daze_take_too.py'] = 'Skool Daze Take Too'
    names['ezad_looks.py'] = 'Ezad Looks'
    names['back_to_skool_daze.py'] = 'Back to Skool Daze'
    name = names.get(prog, 'Unknown')
    default_ini_dir = os.path.join('ini', prog[:-3])
    parser = ArgumentParser(
        usage='%(prog)s [options]',
        description="Start Pyskool in {} mode.".format(name),
        add_help=False
    )
    group = parser.add_argument_group('Options')
    group.add_argument("--version", action="version", version=version,
        help="show Pyskool's version number and exit")
    group.add_argument("-h", "--help", action="help",
        help="show this help message and exit")
    group.add_argument("-c", "--cheat", dest="cheat", action="store_true",
        help="enable cheat keys")
    group.add_argument("--config", dest="config", metavar="P,V", action="append",
        help="set the value of the configuration parameter P to V; this option may be used multiple times")
    group.add_argument("--create-images", dest="create_images", action="store_true",
        help="create the images required by the game and exit")
    group.add_argument("--create-ini", dest="create_ini", action="store_true",
        help="create the ini files required by the game and exit")
    group.add_argument("--create-sounds", dest="create_sounds", action="store_true",
        help="create the sound files required by the game and exit")
    group.add_argument("--force", dest="force", action="store_true",
        help="overwrite existing images, ini files and sound files")
    group.add_argument("-i", "--inidir", dest="inidir",
        help="read ini files from this directory instead of %s" % default_ini_dir)
    group.add_argument("-l", "--load", dest="savefile",
        help="load a saved game from the specified file")
    group.add_argument("--package-dir", dest="package_dir", action="store_true",
        help="show path to pyskool package directory and exit")
    group.add_argument("-q", "--quick-start", dest="quick_start", action="store_true",
        help="start the game quickly")
    group.add_argument("-r", "--load-last", dest="savedir",
        help="load the most recently saved game in the specified directory")
    group.add_argument("--sample-rate", dest="sample_rate", metavar="RATE", type=int, default=44100,
        help="set the sample rate of the sound files created by --create-sounds (default: 44100)")
    group.add_argument("-s", "--scale", dest="scale", type=int,
        help="scale graphics by this factor (1=original Speccy size)")
    group.add_argument("--search-dirs", dest="search_dirs", action="store_true",
        help="show the locations that Pyskool searches for data files and exit")
    group.add_argument("--setup", dest="setup", action="store_true",
        help="create the images, ini files and sound files required by the game and exit")
    options, unknown_args = parser.parse_known_args()
    if unknown_args:
        parser.exit(2, parser.format_help())

    # Set the search path for 'pyskool.ini', the 'images' directory, the
    # 'sounds' directory, and the 'ini' directory
    cwd = os.getcwd()
    search_dirs = [
        cwd,
        os.path.join(os.sep, 'usr', 'share', 'pyskool'),
        os.path.join(package_dir, 'data')
    ]
    if scripts_dir not in search_dirs:
        search_dirs.insert(1, scripts_dir)

    # Attempt to create ~/.pyskool if it doesn't exist
    pyskool_dir = user_dir
    if not os.path.isdir(user_dir):
        try:
            os.makedirs(user_dir)
            info("Created directory '%s'" % user_dir)
        except OSError as e:
            info("Could not create directory '%s': %s" % (user_dir, e.strerror))
            pyskool_dir = cwd
    if pyskool_dir not in search_dirs:
        search_dirs.insert(1, pyskool_dir)

    # Show package directory if requested
    if options.package_dir:
        info(package_dir)
        sys.exit(0)

    # Show search directories if requested
    if options.search_dirs:
        info(SEARCH_DIRS_MSG.format(images=images_subdir, sounds=sounds_subdir, ini=default_ini_dir))
        for search_dir in search_dirs:
            info('- {0}'.format(search_dir))
        sys.exit(0)

    # Get images if requested
    if options.setup or options.create_images:
        images_ini = find('images.ini', search_dirs)
        info("Using ini file at %s" % images_ini)
        if prog == 'skool_daze.py':
            get_images(images_ini, SKOOL_DAZE, 0, user_dir, True, options.force)
        elif prog == 'skool_daze_take_too.py':
            get_images(images_ini, SKOOL_DAZE, 1, user_dir, True, options.force)
        elif prog == 'ezad_looks.py':
            get_images(images_ini, SKOOL_DAZE, 2, user_dir, True, options.force)
        elif prog == 'back_to_skool.py':
            get_images(images_ini, BACK_TO_SKOOL, 0, user_dir, True, options.force)
        elif prog == 'back_to_skool_daze.py':
            get_images(images_ini, BACK_TO_SKOOL, 1, user_dir, True, options.force)

    # Create ini files if requested
    if options.setup or options.create_ini:
        ini_maker = None
        if prog == 'skool_daze.py':
            ini_maker = SDIniMaker(0)
        elif prog == 'skool_daze_take_too.py':
            ini_maker = SDIniMaker(1)
        elif prog == 'ezad_looks.py':
            ini_maker = SDIniMaker(2)
        elif prog == 'back_to_skool.py':
            ini_maker = BTSIniMaker(0)
        elif prog == 'back_to_skool_daze.py':
            ini_maker = BTSIniMaker(1)
        if ini_maker:
            odir = os.path.join(user_dir, 'ini', prog[:-3])
            if not os.path.isdir(odir):
                os.makedirs(odir)
            ini_maker.write_ini_files(odir, True, options.force)

    # Create sound files if requested
    if options.setup or options.create_sounds:
        if prog in SOUNDS:
            odir = os.path.join(user_dir, 'sounds')
            skoolsound.create_sounds(SOUNDS[prog], odir, True, options.force, options.sample_rate)

    if options.setup or options.create_images or options.create_ini or options.create_sounds:
        sys.exit(0)

    # Look for 'pyskool.ini'
    pyskool_ini = find('pyskool.ini', search_dirs)
    info("Using ini file at %s" % pyskool_ini)

    # Look for the directory that contains the game ini files
    if options.inidir:
        ini_dir = os.path.abspath(options.inidir)
        if not os.path.isdir(ini_dir):
            error('%s: directory does not exist' % ini_dir)
    else:
        ini_dir = find_dir(default_ini_dir, search_dirs)
        info("Using game ini files in %s" % ini_dir)

    # Look for the 'images' subdirectory
    images_dir = find_dir(images_subdir, search_dirs)
    info("Using images in %s" % images_dir)

    # Look for the 'sounds' subdirectory
    sounds_dir = find_dir(sounds_subdir, search_dirs)
    info("Using sounds in %s" % sounds_dir)

    # Look for a saved game (if specified on the command line)
    sav_file = None
    if options.savefile:
        if not os.path.isfile(options.savefile):
            error('%s: file not found' % options.savefile)
        sav_file = os.path.abspath(options.savefile)
    elif options.savedir:
        if not os.path.isdir(options.savedir):
            error('%s: directory does not exist' % options.savedir)
        sav_files = [f for f in os.listdir(options.savedir) if f.endswith('.sav') and os.path.isfile(os.path.join(options.savedir, f))]
        if sav_files:
            sav_files.sort()
            sav_file = os.path.abspath(os.path.join(options.savedir, sav_files[-1]))
        else:
            info("No saved games found in %s" % options.savedir)

    # Change to the directory where games will be saved/loaded and screenshots
    # will be dumped
    os.chdir(user_dir)
    info("Using %s to save/load games" % os.getcwd())

    game = Game(pyskool_ini, images_dir, sounds_dir, ini_dir, options, version, sav_file)
    game.play()
Пример #33
0
    def test_register_config_arguments(self):
        class ModelConfig(Config):
            activation = ConfigField(str,
                                     nullable=True,
                                     choices=['relu', 'leaky_relu'])
            normalizer = None

        class TrainConfig(Config):
            max_step = ConfigField(int,
                                   default=1000,
                                   description='Maximum step to run.')
            max_epoch = 1

        # test errors
        with pytest.raises(TypeError,
                           match='`config` is not an instance of '
                           '`Config`'):
            register_config_arguments(ModelConfig, ArgumentParser())
        with pytest.raises(TypeError,
                           match='`config` is not an instance of '
                           '`Config`'):
            register_config_arguments(object(), ArgumentParser())
        with pytest.raises(ValueError,
                           match='`title` is required when '
                           '`description` is specified'):
            register_config_arguments(ModelConfig(),
                                      ArgumentParser(),
                                      description='abc')

        # test no title
        parser = ArgumentParser()
        model_config = ModelConfig()
        train_config = TrainConfig()

        register_config_arguments(model_config, parser, prefix='model')
        register_config_arguments(train_config, parser, sort_keys=True)

        help = parser.format_help()
        self.assertTrue(
            re.match(
                r".*"
                r"--model\.activation\s+MODEL\.ACTIVATION\s+"
                r"\(default None; choices \['leaky_relu', 'relu'\]\)\s+"
                r"--model\.normalizer\s+MODEL\.NORMALIZER\s+"
                r"\(default None\)\s+"
                r"--max_epoch\s+MAX_EPOCH\s+"
                r"\(default 1\)\s+"
                r"--max_step\s+MAX_STEP\s+"
                r"Maximum step to run. \(default 1000\)"
                r".*", help, re.S))
        args = parser.parse_args(
            ['--max_epoch=123', '--max_step=456', '--model.activation=relu'])
        self.assertEqual(model_config.activation, 'relu')
        self.assertIsNone(model_config.normalizer)
        self.assertEqual(train_config.max_epoch, 123)
        self.assertEqual(train_config.max_step, 456)
        self.assertEqual(getattr(args, 'model.activation'), 'relu')
        self.assertIsNone(getattr(args, 'model.normalizer'))
        self.assertEqual(args.max_epoch, 123)
        self.assertEqual(args.max_step, 456)

        # test has title
        parser = ArgumentParser()
        model_config = ModelConfig()
        train_config = TrainConfig()

        register_config_arguments(model_config, parser, title='Model options')
        register_config_arguments(train_config,
                                  parser,
                                  prefix='train',
                                  sort_keys=True,
                                  title='Train options')

        help = parser.format_help()
        self.assertTrue(
            re.match(
                r".*"
                r"Model options:\s+"
                r"--activation\s+ACTIVATION\s+"
                r"\(default None; choices \['leaky_relu', 'relu'\]\)\s+"
                r"--normalizer\s+NORMALIZER\s+"
                r"\(default None\)\s+"
                r"Train options:\s+"
                r"--train\.max_epoch\s+TRAIN\.MAX_EPOCH\s+"
                r"\(default 1\)\s+"
                r"--train\.max_step\s+TRAIN\.MAX_STEP\s+"
                r"Maximum step to run. \(default 1000\)"
                r".*", help, re.S))
        args = parser.parse_args(['--train.max_epoch=123', '--normalizer=789'])
        self.assertIsNone(model_config.activation)
        # note the value is parsed as yaml, thus should be 789 rather than '789'
        self.assertEqual(model_config.normalizer, 789)
        self.assertEqual(train_config.max_epoch, 123)
        self.assertEqual(train_config.max_step, 1000)
        self.assertIsNone(args.activation)
        self.assertEqual(args.normalizer, 789)
        self.assertEqual(getattr(args, 'train.max_epoch'), 123)
        self.assertEqual(getattr(args, 'train.max_step'), 1000)

        # test parse error
        parser = ArgumentParser()
        model_config = ModelConfig()
        register_config_arguments(model_config, parser, prefix='model')

        with pytest.raises(Exception,
                           match=r"Invalid value for argument "
                           r"`--model.activation`; 'sigmoid' "
                           r"is not one of \['relu', "
                           r"'leaky_relu'\]\."):
            _ = parser.parse_args(['--model.activation=sigmoid'])
Пример #34
0
def get_dict(args, lang):
    """Получить словарь с опциями.

    :param args: list
    :param lang: dict, локализация
    :return: tuple, (dict, str), словарь с опциями и справка
    """
    parser = ArgumentParser(lang['HELP']['prog'],
                            lang['HELP']['usage'],
                            lang['HELP']['desc'],
                            lang['HELP']['epilog'],
                            add_help=False)
    parser.add_argument('-first',
                        default='vfc',
                        type=str,
                        help=lang['HELP']['first'])
    parser.add_argument('-depth',
                        default=-1,
                        type=int,
                        help=lang['HELP']['depth'])
    parser.add_argument('-depth_vars',
                        default=-1,
                        type=int,
                        help=lang['HELP']['depth_vars'])
    parser.add_argument('-depth_func',
                        default=-1,
                        type=int,
                        help=lang['HELP']['depth_func'])
    parser.add_argument('-iface',
                        default='console',
                        type=str,
                        help=lang['HELP']['iface'],
                        choices=['console'])
    parser.add_argument('-gen',
                        default='rst',
                        type=str,
                        help=lang['HELP']['gen'],
                        choices=['rst'])
    parser.add_argument('-v',
                        '--version',
                        default=False,
                        action='store_true',
                        dest='version',
                        help=lang['HELP']['ver'])
    parser.add_argument('-h',
                        '--help',
                        default=False,
                        action='store_true',
                        dest='help',
                        help=lang['HELP']['help'])
    parser.add_argument('-path', type=str, help=lang['HELP']['path'])
    parser.add_argument('-out', type=str, help=lang['HELP']['out'])
    parser.add_argument('-step',
                        type=int,
                        default=-1,
                        help=lang['HELP']['step'])
    parser.add_argument('-proc',
                        type=int,
                        default=1,
                        help=lang['HELP']['proc'])
    parser.add_argument('-numbered',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['numbered'])
    parser.add_argument('-hidden',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['hidden'])
    parser.add_argument('-lang', type=str, help=lang['HELP']['lang'])
    parser.add_argument('-nohie',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['nohie'])
    parser.add_argument('-notype',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['notype'])
    parser.add_argument('-hide',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['hide'])
    parser.add_argument('-private',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['private'])
    parser.add_argument('-magic',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['magic'])
    parser.add_argument('-strip',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['strip'])
    parser.add_argument('-cleardir',
                        default=False,
                        action='store_true',
                        help=lang['HELP']['cleardir'])
    return vars(parser.parse_known_args(args)[0]), parser.format_help()
Пример #35
0
            for filename in filenames:
                self.zfile.write(os.path.join(dirpath, filename), os.path.join(dirpath.split(path)[-1], filename))

    def close(self):
        self.zfile.close()

    def extract_to(self, path):
        for p in self.zfile.namelist():
            self.extract(p, path)

    def extract(self, filename, path):
        if not filename.endswith('/'):
            f = os.path.join(path, filename)
            dir = os.path.dirname(f)
            if not os.path.exists(dir):
                os.makedirs(dir)
            file(f, 'wb').write(self.zfile.read(filename))


def create_mzp(zfile, path):

    z = ZFile(zfile, 'w')
    z.add_path(path)
    z.close()

if __name__ == "__main__":
    args = p.parse_args()

    print p.format_help()
    if args.outpath and args.filepath:
        create_mzp(args.outpath, args.filepath)
Пример #36
0
def print_help(args):
    """Re-create argparse's help menu for the api command"""
    parser = ArgumentParser()
    subparsers = parser.add_subparsers(title='', help='')
    parser = subparser(subparsers)
    print(parser.format_help())
Пример #37
0
class main:
    pKwargs = dict(add_help=False, formatter_class=RawTextHelpFormatter)
    hArgs = ['-h', '--help']
    hKwargs = dict(help="show this help message", action='store_true')
    wArgs = ['-w', '--window']
    wKwargs = dict(help="redirect help and errors to a new window",
                   action='store_true')
    console: bool
    dirname: Path
    subs: dict[C, ArgParser]
    parser: ArgParser

    def __init__(self):
        # init vars
        self.dirname = Path(__file__).parent
        self.console = False
        self.subs = dict()
        err = False
        # setup logging
        logfile = self.dirname.joinpath('lib', 'logging.log')
        logging.basicConfig(
            filename=logfile,
            filemode='w',
            level=logging.DEBUG,
            format=
            '[%(asctime)s] %(levelname)s: %(module)s.%(funcName)s\n%(message)s\n',
            datefmt='%m/%d/%Y %I:%M:%S%p')
        try:
            self.buildParser()
            self.runScript()
        except Exception:
            logging.exception('')
            err = True
            raise
        finally:
            if logfile.read_text():
                if err:
                    PlaySound()
                if self.console:
                    openfile(logfile, 'min')
                else:
                    print(logfile.read_text())
                    run(['powershell', 'pause'])

    def buildParser(self) -> None:
        # create parser
        self.parser = ArgParser(prog=self.dirname.name,
                                description=__doc__,
                                **self.pKwargs)
        self.parser.add_argument(*self.hArgs, **self.hKwargs)
        self.parser.add_argument(*self.wArgs, **self.wKwargs)
        self.subpars = self.parser.add_subparsers(
            help=f"METHOD DESCRIPTION:\n{'='*20}")
        # create help
        functions = {f: getattr(src, f) for f in src.__all__}
        for name, script in functions.items():
            self.createHelp(name, script)

    def createHelp(self, name: str, script):
        def wrap(txt: str) -> str:
            outstr = str()
            for line in dedent(txt).split('\n'):
                outstr += fill(text=line, width=90)
                outstr += '\n'
            return outstr

        subpar = self.subpars.add_parser(name=name,
                                         description=wrap(script.__doc__),
                                         help=wrap(script.__doc__),
                                         **self.pKwargs)
        subpar.add_argument(*self.hArgs, **self.hKwargs)
        subpar.add_argument(*self.wArgs, **self.wKwargs)
        subpar.add_argument('-a',
                            '--args',
                            help=wrap(
                                sub(pattern=r'Parameters.*\n\s*',
                                    repl='',
                                    string=script.__init__.__doc__)),
                            nargs='+')
        subpar.set_defaults(func=script)
        self.subs[script] = subpar

    def runScript(self):
        all_args = self.parser.parse_args()
        self.console = all_args.window
        if all_args.help:
            try:
                logging.info(self.subs[all_args.func].format_help())
            except Exception:
                logging.info(self.parser.format_help())
        else:
            args = all_args.args or list()
            all_args.func(*args)