1 """
2 A small user interface for testing STC.
3 """
4
5
6
7
8
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
25
26
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
46
47
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
80
81
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
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
109
110
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
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