""" A small user interface for testing STC. """ #c Copyright 2008-2025, the GAVO project #c #c This program is free software, covered by the GNU GPL. See the #c COPYING file in the source distribution. import sys import textwrap from gavo import base from gavo import stc def cmd_resprof(opts, srcSTCS): """ -- make a resource profile for srcSTCS. """ ast0 = stc.parseSTCS(srcSTCS) sys.stdout.buffer.write(stc.getSTCXProfile(ast0)) def cmd_parseX(opts, srcFile): """ -- read STC-X from srcFile and output it as STC-S, - for stdin """ if srcFile=="-": src = sys.stdin else: src = open(srcFile) asf = stc.parseSTCX(src.read()) src.close() print("\n\n====================\n\n".join(stc.getSTCS(ast) for _, ast in asf)) def cmd_conform(opts, srcSTCS, dstSTCS): """. -- prints srcSTCS in the system of dstSTCS. """ ast0, ast1 = stc.parseSTCS(srcSTCS), stc.parseSTCS(dstSTCS) res = stc.conformTo(ast0, ast1) print(stc.getSTCS(res)) def cmd_utypes(opts, srcSTCS): """ -- prints the utypes for the quoted STC string . """ utypes = stc.getUtypes(stc.parseQSTCS(srcSTCS)) for utype, val in sorted(utypes, key=lambda a:a[0]): if isinstance(val, stc.ColRef): print("%-60s -> %s"%(utype, val.dest)) else: print("%-60s = %s"%(utype, val)) def cmd_parseUtypes(opts): """--- reads the output of utypes and prints quoted STC for it. """ types = [] for ln in sys.stdin: try: utype, val = ln.split("=", 1) types.append((utype.strip(), val.strip())) except ValueError: pass else: continue try: utype, val = ln.split("->", 1) types.append((utype.strip(), stc.ColRef(val.strip()))) except ValueError: pass else: continue raise base.ReportableError("Not a proper input line for STC-S: %r"%ln) print(stc.getSTCS(stc.parseFromUtypes(types))) def makeParser(): from optparse import OptionParser parser = OptionParser(usage="%prog [options] {