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

295 lines, 179 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
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 == 'gc-stats_':
179 print('TODO')
180 return 0
181
182 if action == 'proc':
183 names, locs = arg_r.Rest2()
184 if len(names):
185 for i, name in enumerate(names):
186 node = self.procs.Get(name)
187 if node is None:
188 self.errfmt.Print_('Invalid proc %r' % name,
189 blame_loc=locs[i])
190 return 1
191 else:
192 names = self.procs.GetNames()
193
194 # TSV8 header
195 print('proc_name\tdoc_comment')
196 for name in names:
197 proc = self.procs.Get(name) # must exist
198 #log('Proc %s', proc)
199 body = proc.body
200
201 # TODO: not just command.ShFunction, but command.Proc!
202 doc = ''
203 if body.tag() == command_e.BraceGroup:
204 bgroup = cast(BraceGroup, body)
205 if bgroup.doc_token:
206 token = bgroup.doc_token
207 # 1 to remove leading space
208 doc = token.line.content[token.col + 1:token.col +
209 token.length]
210
211 # Note: these should be attributes on value.Proc
212 buf = mylib.BufWriter()
213 j8.EncodeString(name, buf, unquoted_ok=True)
214 buf.write('\t')
215 j8.EncodeString(doc, buf, unquoted_ok=True)
216 print(buf.getvalue())
217
218 return 0
219
220 e_usage('got invalid action %r' % action, action_loc)
221 #return status
222
223
224class Write(_Builtin):
225 """
226 write -- @strs
227 write --sep ' ' --end '' -- @strs
228 write -n -- @
229 write --j8 -- @strs # argv serialization
230 write --j8 --sep $'\t' -- @strs # this is like TSV8
231 """
232
233 def __init__(self, mem, errfmt):
234 # type: (state.Mem, ui.ErrorFormatter) -> None
235 _Builtin.__init__(self, mem, errfmt)
236 self.stdout_ = mylib.Stdout()
237
238 def Run(self, cmd_val):
239 # type: (cmd_value.Argv) -> int
240 attrs, arg_r = flag_util.ParseCmdVal('write', cmd_val)
241 arg = arg_types.write(attrs.attrs)
242 #print(arg)
243
244 i = 0
245 while not arg_r.AtEnd():
246 if i != 0:
247 self.stdout_.write(arg.sep)
248 s = arg_r.Peek()
249
250 if arg.json:
251 s = j8.MaybeEncodeJsonString(s)
252
253 elif arg.j8:
254 s = j8.MaybeEncodeString(s)
255
256 self.stdout_.write(s)
257
258 arg_r.Next()
259 i += 1
260
261 if arg.n:
262 pass
263 elif len(arg.end):
264 self.stdout_.write(arg.end)
265
266 return 0
267
268
269class Fopen(vm._Builtin):
270 """fopen does nothing but run a block.
271
272 It's used solely for its redirects.
273 fopen >out.txt { echo hi }
274
275 It's a subset of eval
276 eval >out.txt { echo hi }
277 """
278
279 def __init__(self, mem, cmd_ev):
280 # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
281 self.mem = mem
282 self.cmd_ev = cmd_ev # To run blocks
283
284 def Run(self, cmd_val):
285 # type: (cmd_value.Argv) -> int
286 _, arg_r = flag_util.ParseCmdVal('fopen',
287 cmd_val,
288 accept_typed_args=True)
289
290 cmd = typed_args.OptionalBlock(cmd_val)
291 if not cmd:
292 raise error.Usage('expected a block', loc.Missing)
293
294 unused = self.cmd_ev.EvalCommand(cmd)
295 return 0