コード例 #1
0
def get_arguments(msmlfile):
    """Parses the command line arguments for the given `msmlfile`.

    1. Generate an argument parser with :py:mod:`clictk`
    2. Parses the command line
    3. Check for `--xml`
        1. if `--xml` is set, abort execution and generate CLI XML
        2. else restore stdio channels

    ns = p.parse_args()

    .. note: only non-generated variables are exported as cli arguments

    :param msmlfile:
    :return: a dictionary of MSML variables
    """

    exe = generate_cli(msmlfile)
    parser = clictk.build_argument_parser(exe)
    ns = parser.parse_args()

    if ns.__xml__:  # if `--xml` is set
        xml = clictk.prettify(exe.as_xml())
        # write to real stdout
        consolecatcher._true_channels[0].write(xml)
        sys.exit(0)
    else:
        # we do not need stdout sanity further more
        consolecatcher._reset_stdio()


    args = {}

    for n, t in msmlfile.variables.items():
        if hasattr(ns, n):
            value = getattr(ns, n)

            # file arguments are relative to the current working dir
            if issubclass(t.sort.physical, InFile):
                value = os.path.abspath(value)

            try:
                args[n] = conversion(str, t.sort)(value)
            except TypeError as e:
                print("Parameter %s missing" % n)
                log.error("Parameter %s missing: Error", n)
                sys.exit(1)
                raise

    return args
コード例 #2
0
def xml_load(filename, schema=None):
    parser = etree.XMLParser(
        encoding="utf-8",
        attribute_defaults=True,
        ns_clean=True,
        remove_blank_text=True,
        remove_pis=True,
        remove_comments=True,
        schema=etree.XMLSchema(file=schema)
    )
    if os.path.exists(filename):
        try:
            return etree.parse(filename, parser)
        except etree.XMLSyntaxError:
            error("Syntax error in %s", filename)
            raise FileNotFoundError()

    else:
        raise FileNotFoundError()