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

257 lines, 154 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 asdl import format as fmt
11from core import error
12from core.error import e_usage
13from core import state
14from core import ui
15from core import vm
16from data_lang import j8
17from data_lang import pretty
18from frontend import flag_util
19from frontend import match
20from frontend import typed_args
21from mycpp import mylib
22from mycpp.mylib import log
23
24from typing import TYPE_CHECKING, cast
25if TYPE_CHECKING:
26 from core.alloc import Arena
27 from core.ui import ErrorFormatter
28 from osh import cmd_eval
29
30_ = log
31
32
33class _Builtin(vm._Builtin):
34
35 def __init__(self, mem, errfmt):
36 # type: (state.Mem, 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__(self, mem, errfmt, procs, arena):
48 # type: (state.Mem, ErrorFormatter, state.Procs, Arena) -> None
49 _Builtin.__init__(self, mem, errfmt)
50 self.procs = procs
51 self.arena = arena
52 self.stdout_ = mylib.Stdout()
53
54 def Run(self, cmd_val):
55 # type: (cmd_value.Argv) -> int
56 arg, arg_r = flag_util.ParseCmdVal('pp',
57 cmd_val,
58 accept_typed_args=True)
59
60 action, action_loc = arg_r.Peek2()
61
62 # pp (x) prints in the same way that '= x' does
63 # TODO: We also need pp [x], which shows the expression
64 if action is None:
65 rd = typed_args.ReaderForProc(cmd_val)
66 val = rd.PosValue()
67 rd.Done()
68
69 # IOError caught by caller
70 ui.PrettyPrintValue(val, mylib.Stdout())
71 return 0
72
73 arg_r.Next()
74
75 # Actions that print unstable formats start with '.'
76 if action == 'cell':
77 argv, locs = arg_r.Rest2()
78
79 status = 0
80 for i, name in enumerate(argv):
81 if name.startswith(':'):
82 name = name[1:]
83
84 if not match.IsValidVarName(name):
85 raise error.Usage('got invalid variable name %r' % name,
86 locs[i])
87
88 cell = self.mem.GetCell(name)
89 if cell is None:
90 self.errfmt.Print_("Couldn't find a variable named %r" %
91 name,
92 blame_loc=locs[i])
93 status = 1
94 else:
95 self.stdout_.write('%s = ' % name)
96 pretty_f = fmt.DetectConsoleOutput(self.stdout_)
97 fmt.PrintTree(cell.PrettyTree(), pretty_f)
98 self.stdout_.write('\n')
99
100 elif action == 'asdl':
101 # TODO: could be pp asdl (x, y, z)
102 rd = typed_args.ReaderForProc(cmd_val)
103 val = rd.PosValue()
104 rd.Done()
105
106 tree = val.PrettyTree()
107 #tree = val.AbbreviatedTree() # I used this to test cycle detection
108
109 # TODO: ASDL should print the IDs. And then they will be
110 # line-wrapped.
111 # The IDs should also be used to detect cycles, and omit values
112 # already printed.
113 #id_str = vm.ValueIdString(val)
114 #f.write(' <%s%s>\n' % (ysh_type, id_str))
115
116 pretty_f = fmt.DetectConsoleOutput(self.stdout_)
117 fmt.PrintTree(tree, pretty_f)
118 self.stdout_.write('\n')
119
120 status = 0
121
122 elif action == 'line':
123 # Print format for unit tests
124
125 # TODO: could be pp line (x, y, z)
126 rd = typed_args.ReaderForProc(cmd_val)
127 val = rd.PosValue()
128 rd.Done()
129
130 if pretty.TypeNotPrinted(val):
131 ysh_type = ui.ValType(val)
132 self.stdout_.write('(%s) ' % ysh_type)
133
134 j8.PrintLine(val, self.stdout_)
135
136 status = 0
137
138 elif action == 'gc-stats':
139 print('TODO')
140 status = 0
141
142 elif action == 'proc':
143 names, locs = arg_r.Rest2()
144 if len(names):
145 for i, name in enumerate(names):
146 node = self.procs.Get(name)
147 if node is None:
148 self.errfmt.Print_('Invalid proc %r' % name,
149 blame_loc=locs[i])
150 return 1
151 else:
152 names = self.procs.GetNames()
153
154 # TSV8 header
155 print('proc_name\tdoc_comment')
156 for name in names:
157 proc = self.procs.Get(name) # must exist
158 #log('Proc %s', proc)
159 body = proc.body
160
161 # TODO: not just command.ShFunction, but command.Proc!
162 doc = ''
163 if body.tag() == command_e.BraceGroup:
164 bgroup = cast(BraceGroup, body)
165 if bgroup.doc_token:
166 token = bgroup.doc_token
167 # 1 to remove leading space
168 doc = token.line.content[token.col + 1:token.col +
169 token.length]
170
171 # Note: these should be attributes on value.Proc
172 buf = mylib.BufWriter()
173 j8.EncodeString(name, buf, unquoted_ok=True)
174 buf.write('\t')
175 j8.EncodeString(doc, buf, unquoted_ok=True)
176 print(buf.getvalue())
177
178 status = 0
179
180 else:
181 e_usage('got invalid action %r' % action, action_loc)
182
183 return status
184
185
186class Write(_Builtin):
187 """
188 write -- @strs
189 write --sep ' ' --end '' -- @strs
190 write -n -- @
191 write --j8 -- @strs # argv serialization
192 write --j8 --sep $'\t' -- @strs # this is like TSV8
193 """
194
195 def __init__(self, mem, errfmt):
196 # type: (state.Mem, ErrorFormatter) -> None
197 _Builtin.__init__(self, mem, errfmt)
198 self.stdout_ = mylib.Stdout()
199
200 def Run(self, cmd_val):
201 # type: (cmd_value.Argv) -> int
202 attrs, arg_r = flag_util.ParseCmdVal('write', cmd_val)
203 arg = arg_types.write(attrs.attrs)
204 #print(arg)
205
206 i = 0
207 while not arg_r.AtEnd():
208 if i != 0:
209 self.stdout_.write(arg.sep)
210 s = arg_r.Peek()
211
212 if arg.json:
213 s = j8.MaybeEncodeJsonString(s)
214
215 elif arg.j8:
216 s = j8.MaybeEncodeString(s)
217
218 self.stdout_.write(s)
219
220 arg_r.Next()
221 i += 1
222
223 if arg.n:
224 pass
225 elif len(arg.end):
226 self.stdout_.write(arg.end)
227
228 return 0
229
230
231class Fopen(vm._Builtin):
232 """fopen does nothing but run a block.
233
234 It's used solely for its redirects.
235 fopen >out.txt { echo hi }
236
237 It's a subset of eval
238 eval >out.txt { echo hi }
239 """
240
241 def __init__(self, mem, cmd_ev):
242 # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
243 self.mem = mem
244 self.cmd_ev = cmd_ev # To run blocks
245
246 def Run(self, cmd_val):
247 # type: (cmd_value.Argv) -> int
248 _, arg_r = flag_util.ParseCmdVal('fopen',
249 cmd_val,
250 accept_typed_args=True)
251
252 cmd = typed_args.OptionalBlock(cmd_val)
253 if not cmd:
254 raise error.Usage('expected a block', loc.Missing)
255
256 unused = self.cmd_ev.EvalCommand(cmd)
257 return 0