Changeset 4113:90434cb52f2b in livinglogic.python.xist for src/ll/xist/scripts/xml2xsc.py

Show
Ignore:
Timestamp:
09/23/10 15:34:22 (9 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Use argparse instead of optparse.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/ll/xist/scripts/xml2xsc.py

    r4042 r4113  
    1010 
    1111 
    12 import sys, optparse 
     12import sys, argparse 
    1313 
    1414from ll.xist import xsc, xnd, sims 
     
    3838 
    3939 
    40 def etree2xnd(sims, node): 
     40def etree2xnd(model, node): 
    4141    ns = xnd.Module() 
    4242    elements = {} # maps (name, xmlns) to (xnd.Element, content set, attrname->xnd.Attr map) 
    4343    procinsts = {} # maps name to xnd.ProcInst 
    4444 
     45    # Iterate through the tree a collect: which elements are encountered and how they are nested 
    4546    for path in iterpath(node): 
    4647        node = path[-1] 
     
    8384 
    8485    # Put sims info into the element definitions 
    85     if sims == "none": 
     86    if model == "none": 
    8687        pass 
    87     elif sims == "simple": 
     88    elif model == "simple": 
    8889        for entry in elements.itervalues(): 
    8990            entry[0].modeltype = bool(entry[1]) 
    90     elif sims == "full": 
     91    elif model == "full": 
    9192        for entry in elements.itervalues(): 
    9293            if not entry[1]: 
     
    106107                    entry[0].modelargs = elements 
    107108    else: 
    108         raise ValueError("unknown sims mode {0!r}".format(sims)) 
     109        raise ValueError("unknown sims mode {0!r}".format(model)) 
    109110    return ns 
    110111 
    111112 
    112 def stream2xnd(stream, sims="simple", parser="etree"): 
     113def stream2xnd(stream, model="simple", parser="etree"): 
    113114    if parser == "etree": 
    114115        from xml.etree import cElementTree 
     
    120121        raise ValueError("unknown parser {0!r}".format(parser)) 
    121122 
    122     return etree2xnd(sims, node) 
     123    return etree2xnd(model, node) 
    123124 
    124125 
    125126def main(args=None): 
    126     p = optparse.OptionParser(usage="usage: %prog [options] <input.xml >output.py") 
    127     p.add_option("-p", "--parser", dest="parser", help="parser module to use for XML parsing (etree or lxml)", choices=("etree", "lxml"), default="etree") 
    128     choices = ["none", "simple", "full"] 
    129     p.add_option("-s", "--sims", dest="sims", help="Create sims info? ({0})".format(", ".join(choices)), metavar="MODE", default="simple") 
     127    p = argparse.ArgumentParser(description="Convert XML (on stdin) to XIST namespace (on stdout)") 
     128    p.add_argument("-p", "--parser", dest="parser", help="parser module to use for XML parsing (etree or lxml)", choices=("etree", "lxml"), default="etree") 
     129    p.add_argument("-m", "--model", dest="model", help="Create sims info?", choices=("none", "simple", "full"), default="simple") 
    130130 
    131     (options, args) = p.parse_args(args) 
    132     if len(args) != 0: 
    133         p.error("incorrect number of arguments") 
    134         return 1 
    135     print stream2xnd(sys.stdin, sims=options.sims, parser=options.parser).aspy() 
     131    args = p.parse_args(args) 
     132    print stream2xnd(sys.stdin, model=args.model, parser=args.parser).aspy() 
    136133 
    137134