Coverage for obj2cli/main.py : 15%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import argparse
2from logging import getLogger
3from .parser import Parser
4from .argparse import Argparse
6log = getLogger(__name__)
9def main_cls(clsmap, args, deftype=str, positional=False):
10 ps = Parser(deftype)
11 ap = Argparse(positional)
12 parser = argparse.ArgumentParser()
13 sub = parser.add_subparsers()
14 for ifcls, cls in clsmap.items():
15 data = ps.parse_cls(ifcls)
16 log.debug("if=%s, cls=%s, data: %s", ifcls, cls, data)
17 s = sub.add_parser(cls.__name__)
18 s.set_defaults(__IFCLASS__=ifcls, __CLASS__=cls)
19 ap.convert_cls(data, s)
20 parsed = parser.parse_args(args)
21 log.debug("parsed: %s dict=%s", parsed, parsed.__dict__)
22 carg, farg = ap.split_arg(data, parsed)
23 log.debug("carg: %s", carg)
24 log.debug("farg: %s", farg)
25 obj = parsed.__CLASS__(*carg[0], **carg[1])
26 if parsed.__FUNCNAME__ not in ("new"):
27 return getattr(obj, parsed.__FUNCNAME__)(*farg[0], **farg[1])
28 parser.print_usage()
31def main_single(ifcls, cls, args, deftype=str, positional=False):
32 parser = Parser(deftype)
33 ap = Argparse(positional)
34 data = parser.parse_cls(ifcls)
35 log.debug("if=%s, cls=%s, data: %s", ifcls, cls, data)
36 parser = ap.convert_cls(data)
37 parsed = parser.parse_args(args)
38 log.debug("parsed: %s dict=%s", parsed, parsed.__dict__)
39 carg, farg = ap.split_arg(data, parsed)
40 obj = cls(*carg[0], **carg[1])
41 if parsed.__FUNCNAME__ not in ("new"):
42 return getattr(obj, parsed.__FUNCNAME__)(*farg[0], **farg[1])
43 parser.print_usage()
46def main_func(fn, args, noskip=False, deftype=str, positional=False):
47 parser = Parser(deftype)
48 ap = Argparse(positional)
49 log.debug("fn: %s", fn)
50 data = parser.parse_fn(fn)
51 log.debug("data: %s", data)
52 if not noskip:
53 data["flags"].append("staticmethod")
54 p2 = ap.convert_fn(fn.__name__, data)
55 parsed2 = p2.parse_args(args)
56 log.debug("parsed2: %s dict=%s", parsed2, parsed2.__dict__)
57 kwarg, posarg = ap.fn_arg(data, parsed2)
58 log.debug("kwarg=%s, posarg=%s", kwarg, posarg)
59 return fn(*posarg, **kwarg)