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