Package gavo :: Package stc :: Module cli
[frames] | no frames]

Source Code for Module gavo.stc.cli

  1  """ 
  2  A small user interface for testing STC. 
  3  """ 
  4   
  5  #c Copyright 2008-2019, the GAVO project 
  6  #c 
  7  #c This program is free software, covered by the GNU GPL.  See the 
  8  #c COPYING file in the source distribution. 
  9   
 10   
 11  from __future__ import print_function 
 12   
 13  import sys 
 14  import textwrap 
 15   
 16  from gavo import base 
 17  from gavo import stc 
 18   
 19   
20 -def cmd_resprof(opts, srcSTCS):
21 """<srcSTCS> -- make a resource profile for srcSTCS. 22 """ 23 ast0 = stc.parseSTCS(srcSTCS) 24 print(stc.getSTCXProfile(ast0))
25 26
27 -def cmd_parseX(opts, srcFile):
28 """<srcFile> -- read STC-X from srcFile and output it as STC-S, - for stdin 29 """ 30 if srcFile=="-": 31 src = sys.stdin 32 else: 33 src = open(srcFile) 34 asf = stc.parseSTCX(src.read()) 35 src.close() 36 print("\n\n====================\n\n".join(stc.getSTCS(ast) 37 for _, ast in asf))
38 39
40 -def cmd_conform(opts, srcSTCS, dstSTCS):
41 """<srcSTCS>. <dstSTCS> -- prints srcSTCS in the system of dstSTCS. 42 """ 43 ast0, ast1 = stc.parseSTCS(srcSTCS), stc.parseSTCS(dstSTCS) 44 res = stc.conformTo(ast0, ast1) 45 print(stc.getSTCS(res))
46 47
48 -def cmd_utypes(opts, srcSTCS):
49 """<QSTCS> -- prints the utypes for the quoted STC string <QSTCS>. 50 """ 51 utypes = stc.getUtypes(stc.parseQSTCS(srcSTCS)) 52 for utype, val in sorted(utypes, key=lambda a:a[0]): 53 if isinstance(val, stc.ColRef): 54 print("%-60s -> %s"%(utype, val.dest)) 55 else: 56 print("%-60s = %s"%(utype, val))
57 58
59 -def cmd_parseUtypes(opts):
60 """--- reads the output of utypes and prints quoted STC for it. 61 """ 62 types = [] 63 for ln in sys.stdin: 64 try: 65 utype, val = ln.split("=", 1) 66 types.append((utype.strip(), val.strip())) 67 except ValueError: 68 pass 69 else: 70 continue 71 try: 72 utype, val = ln.split("->", 1) 73 types.append((utype.strip(), stc.ColRef(val.strip()))) 74 except ValueError: 75 pass 76 else: 77 continue 78 raise base.ReportableError("Not a proper input line for STC-S: %r"%ln) 79 print(stc.getSTCS(stc.parseFromUtypes(types)))
80 81
82 -def makeParser():
83 from optparse import OptionParser 84 parser = OptionParser(usage="%prog [options] <command> {<command-args}\n" 85 " Use command 'help' to see commands available.") 86 parser.add_option("-e", "--dump-exception", help="Dump exceptions.", 87 dest="dumpExc", default=False, action="store_true") 88 return parser
89 90 _cmdArgParser = makeParser() 91 92
93 -def cmd_help(opts):
94 """ -- outputs help to stdout. 95 """ 96 _cmdArgParser.print_help(file=sys.stdout) 97 sys.stdout.write("\nCommands include:\n") 98 for name in sorted(n for n in globals() if n.startswith("cmd_")): 99 sys.stdout.write("%s %s\n"%(name[4:], 100 globals()[name].__doc__.strip()))
101 102
103 -def parseArgs():
104 opts, args = _cmdArgParser.parse_args() 105 if not args: 106 _cmdArgParser.print_help(file=sys.stderr) 107 sys.exit(1) 108 return opts, args[0], args[1:]
109 110
111 -def bailOnExc(opts, msg):
112 import traceback 113 if opts.dumpExc: 114 traceback.print_exc() 115 sys.stderr.write(textwrap.fill(msg, replace_whitespace=True, 116 initial_indent='', subsequent_indent=" ")+"\n") 117 sys.exit(1)
118 119
120 -def main():
121 opts, cmd, args = parseArgs() 122 try: 123 handler = globals()["cmd_"+cmd] 124 except KeyError: 125 bailOnExc(opts, "Unknown command: %s."%cmd) 126 try: 127 handler(opts, *args) 128 except TypeError: 129 bailOnExc(opts, "Invalid arguments for %s: %s."%(cmd, args)) 130 except stc.STCSParseError as ex: 131 bailOnExc(opts, "STCS expression '%s' bad somewhere after %s (%s)"%( 132 ex.expr, ex.pos, ex.message)) 133 except stc.STCNotImplementedError as ex: 134 bailOnExc(opts, "Feature not yet supported: %s."%ex) 135 except stc.STCValueError as ex: 136 bailOnExc(opts, "Bad value in STC input: %s."%ex)
137