Ejemplo n.º 1
0
 def go_2(p, current_prefix, current_ns):
     if isinstance(p, BaseParser):
         new_p = ArgParser(default_config_files=config_files)
         for a in p.argparser._actions:
             new_a = copy.copy(a)
             ss = copy.deepcopy(new_a.option_strings)
             for ix, s in enumerate(new_a.option_strings):
                 if s.startswith("--"):
                     ss[ix] = "-" + current_prefix + "-" + s[2:]
                 else:
                     raise NotImplementedError
                 new_a.option_strings = ss
             new_p._add_action(new_a)
         _used_args, _rest = new_p.parse_known_args(args, namespace=current_ns)
         # add a "_flags" field to each object so we know what flags caused a certain option to be set:
         # (however, note that post-parsing we may munge around ...)
         flags_dict = defaultdict(set)
         for action in new_p._actions:
             for opt in action.option_strings:
                 flags_dict[action.dest].add(opt)
         current_ns.flags_ = Namespace(**flags_dict)
         # TODO: could continue parsing from `_rest` instead of original `args`
     elif isinstance(p, CompoundParser):
         current_ns.flags_ = set()  # could also check for the CompoundParser case and not set flags there,
                                    # since there will never be any
         for q in p.parsers:
             ns = Namespace()
             if q.namespace in current_ns.__dict__:
                 raise ValueError("Namespace field '%s' already in use" % q.namespace)
                 # TODO could also allow, say, a None
             else:
                 # gross but how to write n-ary identity fn that behaves sensibly on single arg??
                 current_ns.__dict__[q.namespace] = ns
                 # FIXME this casting doesn't work for configurations with positional arguments,
                 # which aren't unpacked correctly -- better to use a namedtuple
                 # (making all arguments keyword-only also works, but then you have to supply
                 # often meaningless defaults in the __init__)
             go_2(q.parser, current_prefix=current_prefix + (('-' + q.prefix) if q.prefix is not None else ''),
                  current_ns=ns)
             # If a cast function is provided, apply it to the namespace, possibly doing dynamic type checking
             # and also allowing the checker to provide hinting for the types of the fields
             flags = ns.flags_
             del ns.flags_
             fixed = (q.cast(current_ns.__dict__[q.namespace]) #(q.cast(**vars(current_ns.__dict__[q.namespace]))
                                                 if q.cast else current_ns.__dict__[q.namespace])
             if isinstance(fixed, tuple):
                 fixed = fixed.replace(flags_=flags)
             elif isinstance(fixed, Namespace):
                 setattr(fixed, "flags_", flags)
             else:
                 raise ValueError("currently only Namespace and NamedTuple objects are supported return types from "
                                  "parsing; got %s (a %s)" % (fixed, type(fixed)))
             current_ns.__dict__[q.namespace] = fixed
             # TODO current_ns or current_namespace or ns or namespace?
     else:
         raise TypeError("parser %s wasn't a %s (%s or %s) but a %s" %
                         (p, Parser, BaseParser, CompoundParser, p.__class__))
Ejemplo n.º 2
0
 def go_2(p, current_prefix, current_ns):
     if isinstance(p, BaseParser):
         new_p = ArgParser(default_config_files=config_files)
         for a in p.argparser._actions:
             new_a = copy.copy(a)
             ss = copy.deepcopy(new_a.option_strings)
             for ix, s in enumerate(new_a.option_strings):
                 if s.startswith("--"):
                     ss[ix] = "-" + current_prefix + "-" + s[2:]
                 else:
                     raise NotImplementedError
                 new_a.option_strings = ss
             new_p._add_action(new_a)
         _used_args, _rest = new_p.parse_known_args(args, namespace=current_ns)
         # TODO: could continue parsing from `_rest` instead of original `args`
     elif isinstance(p, CompoundParser):
         for q in p.parsers:
             ns = Namespace()
             if q.namespace in current_ns.__dict__:
                 raise ValueError("Namespace field '%s' already in use" % q.namespace)
                 # TODO could also allow, say, a None
             else:
                 # gross but how to write n-ary identity fn that behaves sensibly on single arg??
                 current_ns.__dict__[q.namespace] = ns
                 # FIXME this casting doesn't work for configurations with positional arguments,
                 # which aren't unpacked correctly -- better to use a namedtuple
                 # (making all arguments keyword-only also works, but then you have to supply
                 # often meaningless defaults in the __init__)
             go_2(q.parser, current_prefix=current_prefix + (('-' + q.prefix) if q.prefix is not None else ''),
                  current_ns=ns)
             # If a cast function is provided, apply it to the namespace, possibly doing dynamic type checking
             # and also allowing the checker to provide hinting for the types of the fields
             current_ns.__dict__[q.namespace] = (q.cast(current_ns.__dict__[q.namespace]) #(q.cast(**vars(current_ns.__dict__[q.namespace]))
                                                 if q.cast else current_ns.__dict__[q.namespace])
             # TODO current_ns or current_namespace or ns or namespace?
     else:
         raise TypeError("parser %s wasn't a %s (%s or %s) but a %s" %
                         (p, Parser, BaseParser, CompoundParser, p.__class__))
Ejemplo n.º 3
0
 def go_2(p, current_prefix, current_ns):
     if isinstance(p, BaseParser):
         new_p = ArgParser(default_config_files=config_files)
         for a in p.argparser._actions:
             new_a = copy.copy(a)
             ss = copy.deepcopy(new_a.option_strings)
             for ix, s in enumerate(new_a.option_strings):
                 if s.startswith("--"):
                     ss[ix] = "-" + current_prefix + "-" + s[2:]
                 else:
                     raise NotImplementedError
                 new_a.option_strings = ss
             new_p._add_action(new_a)
         _used_args, _rest = new_p.parse_known_args(args,
                                                    namespace=current_ns)
         # add a "_flags" field to each object so we know what flags caused a certain option to be set:
         # (however, note that post-parsing we may munge around ...)
         flags_dict = defaultdict(set)
         for action in new_p._actions:
             for opt in action.option_strings:
                 flags_dict[action.dest].add(opt)
         current_ns.flags_ = Namespace(**flags_dict)
         # TODO: could continue parsing from `_rest` instead of original `args`
     elif isinstance(p, CompoundParser):
         current_ns.flags_ = set(
         )  # could also check for the CompoundParser case and not set flags there,
         # since there will never be any
         for q in p.parsers:
             ns = Namespace()
             if q.namespace in current_ns.__dict__:
                 raise ValueError("Namespace field '%s' already in use" %
                                  q.namespace)
                 # TODO could also allow, say, a None
             else:
                 # gross but how to write n-ary identity fn that behaves sensibly on single arg??
                 current_ns.__dict__[q.namespace] = ns
                 # FIXME this casting doesn't work for configurations with positional arguments,
                 # which aren't unpacked correctly -- better to use a namedtuple
                 # (making all arguments keyword-only also works, but then you have to supply
                 # often meaningless defaults in the __init__)
             go_2(q.parser,
                  current_prefix=current_prefix +
                  (('-' + q.prefix) if q.prefix is not None else ''),
                  current_ns=ns)
             # If a cast function is provided, apply it to the namespace, possibly doing dynamic type checking
             # and also allowing the checker to provide hinting for the types of the fields
             flags = ns.flags_
             del ns.flags_
             fixed = (
                 q.cast(
                     current_ns.__dict__[q.namespace]
                 )  #(q.cast(**vars(current_ns.__dict__[q.namespace]))
                 if q.cast else current_ns.__dict__[q.namespace])
             if isinstance(fixed, tuple):
                 fixed = fixed.replace(flags_=flags)
             elif isinstance(fixed, Namespace):
                 setattr(fixed, "flags_", flags)
             else:
                 raise ValueError(
                     "currently only Namespace and NamedTuple objects are supported return types from "
                     "parsing; got %s (a %s)" % (fixed, type(fixed)))
             current_ns.__dict__[q.namespace] = fixed
             # TODO current_ns or current_namespace or ns or namespace?
     else:
         raise TypeError(
             "parser %s wasn't a %s (%s or %s) but a %s" %
             (p, Parser, BaseParser, CompoundParser, p.__class__))