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

102 lines, 55 significant
1"""Methods on IO type"""
2from __future__ import print_function
3
4from _devbuild.gen.value_asdl import value, value_t
5
6from core import error
7from core import num
8from core import vm
9from mycpp.mylib import log
10from osh import prompt
11
12from typing import Dict, cast, TYPE_CHECKING
13if TYPE_CHECKING:
14 from frontend import typed_args
15
16_ = log
17
18
19class Eval(vm._Callable):
20
21 def __init__(self):
22 # type: () -> None
23 pass
24
25 def Call(self, rd):
26 # type: (typed_args.Reader) -> value_t
27 return value.Null
28
29
30class CaptureStdout(vm._Callable):
31
32 def __init__(self, shell_ex):
33 # type: (vm._Executor) -> None
34 self.shell_ex = shell_ex
35
36 def Call(self, rd):
37 # type: (typed_args.Reader) -> value_t
38
39 io = rd.PosIO()
40 cmd = rd.PosCommand()
41 rd.Done() # no more args
42
43 status, stdout_str = self.shell_ex.CaptureStdout(cmd)
44 if status != 0:
45 properties = {
46 'status': num.ToBig(status)
47 } # type: Dict[str, value_t]
48 raise error.Structured(
49 4, 'Captured command failed with status %d' % status,
50 rd.LeftParenToken(), properties)
51
52 return value.Str(stdout_str)
53
54
55class PromptVal(vm._Callable):
56 """
57 _io->promptVal('$') is like \$
58 It expands to $ or # when root
59 """
60
61 def __init__(self):
62 # type: () -> None
63 pass
64
65 def Call(self, rd):
66 # type: (typed_args.Reader) -> value_t
67
68 # "self" param is guaranteed to succeed
69 io = rd.PosIO()
70 what = rd.PosStr()
71 rd.Done() # no more args
72
73 # Bug fix: protect against crash later in PromptVal()
74 if len(what) != 1:
75 raise error.Expr(
76 'promptVal() expected a single char, got %r' % what,
77 rd.LeftParenToken())
78
79 prompt_ev = cast(prompt.Evaluator, io.prompt_ev)
80 return value.Str(prompt_ev.PromptVal(what))
81
82
83class Time(vm._Callable):
84
85 def __init__(self):
86 # type: () -> None
87 pass
88
89 def Call(self, rd):
90 # type: (typed_args.Reader) -> value_t
91 return value.Null
92
93
94class Strftime(vm._Callable):
95
96 def __init__(self):
97 # type: () -> None
98 pass
99
100 def Call(self, rd):
101 # type: (typed_args.Reader) -> value_t
102 return value.Null