OILS / builtin / io_ysh.py View on Github | oilshell.org

314 lines, 196 significant
1#!/usr/bin/env python2
2"""
3builtin/io_ysh.py - YSH builtins that perform I/O
4"""
5from __future__ import print_function
6
7from _devbuild.gen import arg_types
8from _devbuild.gen.runtime_asdl import cmd_value
9from _devbuild.gen.syntax_asdl import command_e, BraceGroup, loc
10from _devbuild.gen.value_asdl import value, value_e, value_t
11from asdl import format as fmt
12from core import error
13from core.error import e_usage
14from core import state
15from display import ui
16from core import vm
17from data_lang import j8
18from frontend import flag_util
19from frontend import match
20from frontend import typed_args
21from mycpp import mylib
22from mycpp.mylib import tagswitch, log, iteritems
23
24from typing import TYPE_CHECKING, cast
25if TYPE_CHECKING:
26 from core.alloc import Arena
27 from osh import cmd_eval
28 from ysh import expr_eval
29
30_ = log
31
32
33class _Builtin(vm._Builtin):
34
35 def __init__(self, mem, errfmt):
36 # type: (state.Mem, ui.ErrorFormatter) -> None
37 self.mem = mem
38 self.errfmt = errfmt
39
40
41class Pp(_Builtin):
42 """Given a list of variable names, print their values.
43
44 'pp cell a' is a lot easier to type than 'argv.py "${a[@]}"'.
45 """
46
47 def __init__(
48 self,
49 expr_ev, # type: expr_eval.ExprEvaluator
50 mem, # type: state.Mem
51 errfmt, # type: ui.ErrorFormatter
52 procs, # type: state.Procs
53 arena, # type: Arena
54 ):
55 # type: (...) -> None
56 _Builtin.__init__(self, mem, errfmt)
57 self.expr_ev = expr_ev
58 self.procs = procs
59 self.arena = arena
60 self.stdout_ = mylib.Stdout()
61
62 def _PrettyPrint(self, cmd_val):
63 # type: (cmd_value.Argv) -> int
64 rd = typed_args.ReaderForProc(cmd_val)
65 val = rd.PosValue()
66 rd.Done()
67
68 blame_tok = rd.LeftParenToken()
69
70 # It might be nice to add a string too, like
71 # pp 'my annotation' (actual)
72 # But the var name should meaningful in most cases
73
74 UP_val = val
75 result = None # type: value_t
76 with tagswitch(val) as case:
77 if case(value_e.Expr): # Destructured assert [true === f()]
78 val = cast(value.Expr, UP_val)
79
80 # In this case, we could get the unevaluated code string and
81 # print it. Although quoting the line seems enough.
82 result = self.expr_ev.EvalExpr(val.e, blame_tok)
83 else:
84 result = val
85
86 # Show it with location
87 self.stdout_.write('\n')
88 excerpt, prefix = ui.CodeExcerptAndPrefix(blame_tok)
89 self.stdout_.write(excerpt)
90 ui.PrettyPrintValue(prefix, result, self.stdout_)
91
92 return 0
93
94 def Run(self, cmd_val):
95 # type: (cmd_value.Argv) -> int
96 arg, arg_r = flag_util.ParseCmdVal('pp',
97 cmd_val,
98 accept_typed_args=True)
99
100 action, action_loc = arg_r.Peek2()
101
102 # Special cases
103 # pp (x) quotes its code location
104 # pp [x] also evaluates
105 if action is None:
106 return self._PrettyPrint(cmd_val)
107
108 arg_r.Next()
109
110 if action == 'value':
111 # pp value (x) prints in the same way that '= x' does
112 rd = typed_args.ReaderForProc(cmd_val)
113 val = rd.PosValue()
114 rd.Done()
115
116 ui.PrettyPrintValue('', val, self.stdout_)
117 return 0
118
119 if action == 'asdl_':
120 # TODO: could be pp asdl_ (x, y, z)
121 rd = typed_args.ReaderForProc(cmd_val)
122 val = rd.PosValue()
123 rd.Done()
124
125 tree = val.PrettyTree()
126 #tree = val.AbbreviatedTree() # I used this to test cycle detection
127
128 # TODO: ASDL should print the IDs. And then they will be
129 # line-wrapped.
130 # The IDs should also be used to detect cycles, and omit values
131 # already printed.
132 #id_str = vm.ValueIdString(val)
133 #f.write(' <%s%s>\n' % (ysh_type, id_str))
134
135 pretty_f = fmt.DetectConsoleOutput(self.stdout_)
136 fmt.PrintTree(tree, pretty_f)
137 self.stdout_.write('\n')
138
139 return 0
140
141 if action == 'test_': # Print format for spec tests
142 # TODO: could be pp test_ (x, y, z)
143 rd = typed_args.ReaderForProc(cmd_val)
144 val = rd.PosValue()
145 rd.Done()
146
147 if ui.TypeNotPrinted(val):
148 ysh_type = ui.ValType(val)
149 self.stdout_.write('(%s) ' % ysh_type)
150
151 j8.PrintLine(val, self.stdout_)
152
153 return 0
154
155 if action == 'cell_': # Format may change
156 argv, locs = arg_r.Rest2()
157
158 status = 0
159 for i, name in enumerate(argv):
160
161 if not match.IsValidVarName(name):
162 raise error.Usage('got invalid variable name %r' % name,
163 locs[i])
164
165 cell = self.mem.GetCell(name)
166 if cell is None:
167 self.errfmt.Print_("Couldn't find a variable named %r" %
168 name,
169 blame_loc=locs[i])
170 status = 1
171 else:
172 self.stdout_.write('%s = ' % name)
173 pretty_f = fmt.DetectConsoleOutput(self.stdout_)
174 fmt.PrintTree(cell.PrettyTree(), pretty_f)
175 self.stdout_.write('\n')
176 return status
177
178 if action == 'stacks_': # Format may change
179 if mylib.PYTHON:
180 var_stack, argv_stack, unused = self.mem.Dump()
181 print(var_stack)
182 print('===')
183 print(argv_stack)
184 if 0:
185 var_stack = self.mem.var_stack
186 for i, frame in enumerate(var_stack):
187 print('=== Frame %d' % i)
188 for name, cell in iteritems(frame):
189 print('%s = %s' % (name, cell))
190
191 return 0
192
193 if action == 'frame_vars_': # Print names in current frame, for testing
194 top = self.mem.var_stack[-1]
195 print(' [frame_vars_] %s' % ' '.join(top.keys()))
196 return 0
197
198 if action == 'gc-stats_':
199 print('TODO')
200 return 0
201
202 if action == 'proc':
203 names, locs = arg_r.Rest2()
204 if len(names):
205 for i, name in enumerate(names):
206 node, _ = self.procs.GetInvokable(name)
207 if node is None:
208 self.errfmt.Print_('Invalid proc %r' % name,
209 blame_loc=locs[i])
210 return 1
211 else:
212 names = self.procs.InvokableNames()
213
214 # TSV8 header
215 print('proc_name\tdoc_comment')
216 for name in names:
217 proc, _ = self.procs.GetInvokable(name) # must exist
218 body = proc.body
219
220 # TODO: not just command.ShFunction, but command.Proc!
221 doc = ''
222 if body.tag() == command_e.BraceGroup:
223 bgroup = cast(BraceGroup, body)
224 if bgroup.doc_token:
225 token = bgroup.doc_token
226 # 1 to remove leading space
227 doc = token.line.content[token.col + 1:token.col +
228 token.length]
229
230 # Note: these should be attributes on value.Proc
231 buf = mylib.BufWriter()
232 j8.EncodeString(name, buf, unquoted_ok=True)
233 buf.write('\t')
234 j8.EncodeString(doc, buf, unquoted_ok=True)
235 print(buf.getvalue())
236
237 return 0
238
239 e_usage('got invalid action %r' % action, action_loc)
240 #return status
241
242
243class Write(_Builtin):
244 """
245 write -- @strs
246 write --sep ' ' --end '' -- @strs
247 write -n -- @
248 write --j8 -- @strs # argv serialization
249 write --j8 --sep $'\t' -- @strs # this is like TSV8
250 """
251
252 def __init__(self, mem, errfmt):
253 # type: (state.Mem, ui.ErrorFormatter) -> None
254 _Builtin.__init__(self, mem, errfmt)
255 self.stdout_ = mylib.Stdout()
256
257 def Run(self, cmd_val):
258 # type: (cmd_value.Argv) -> int
259 attrs, arg_r = flag_util.ParseCmdVal('write', cmd_val)
260 arg = arg_types.write(attrs.attrs)
261 #print(arg)
262
263 i = 0
264 while not arg_r.AtEnd():
265 if i != 0:
266 self.stdout_.write(arg.sep)
267 s = arg_r.Peek()
268
269 if arg.json:
270 s = j8.MaybeEncodeJsonString(s)
271
272 elif arg.j8:
273 s = j8.MaybeEncodeString(s)
274
275 self.stdout_.write(s)
276
277 arg_r.Next()
278 i += 1
279
280 if arg.n:
281 pass
282 elif len(arg.end):
283 self.stdout_.write(arg.end)
284
285 return 0
286
287
288class RunBlock(vm._Builtin):
289 """Used for 'redir' builtin
290
291 It's used solely for its redirects.
292 fopen >out.txt { echo hi }
293
294 It's a subset of eval
295 eval >out.txt { echo hi }
296 """
297
298 def __init__(self, mem, cmd_ev):
299 # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
300 self.mem = mem
301 self.cmd_ev = cmd_ev # To run blocks
302
303 def Run(self, cmd_val):
304 # type: (cmd_value.Argv) -> int
305 _, arg_r = flag_util.ParseCmdVal('redir',
306 cmd_val,
307 accept_typed_args=True)
308
309 cmd = typed_args.OptionalBlock(cmd_val)
310 if not cmd:
311 raise error.Usage('expected a block', loc.Missing)
312
313 unused = self.cmd_ev.EvalCommand(cmd)
314 return 0