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

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