OILS / core / optview.py View on Github | oilshell.org

58 lines, 33 significant
1"""optview.py."""
2from __future__ import print_function
3
4from frontend import consts
5from frontend import option_def
6
7from typing import List
8
9
10class _Getter(object):
11
12 def __init__(self, opt0_array, opt_stacks, opt_name):
13 # type: (List[bool], List[List[bool]], str) -> None
14 self.opt0_array = opt0_array
15 self.opt_stacks = opt_stacks
16 self.num = consts.OptionNum(opt_name)
17 assert self.num != 0, opt_name
18
19 def __call__(self):
20 # type: () -> bool
21 overlay = self.opt_stacks[self.num]
22 if overlay is None or len(overlay) == 0:
23 return self.opt0_array[self.num]
24 else:
25 return overlay[-1] # The top value
26
27
28class _View(object):
29 """Allow read-only access to a subset of options."""
30
31 def __init__(self, opt0_array, opt_stacks, allowed):
32 # type: (List[bool], List[List[bool]], List[str]) -> None
33 self.opt0_array = opt0_array
34 self.opt_stacks = opt_stacks
35 self.allowed = allowed
36
37 def __getattr__(self, opt_name):
38 # type: (str) -> _Getter
39 """Make the API look like self.exec_opts.strict_control_flow()"""
40 if opt_name in self.allowed:
41 return _Getter(self.opt0_array, self.opt_stacks, opt_name)
42 else:
43 raise AttributeError(opt_name)
44
45
46class Parse(_View):
47
48 def __init__(self, opt0_array, opt_stacks):
49 # type: (List[bool], List[List[bool]]) -> None
50 _View.__init__(self, opt0_array, opt_stacks,
51 option_def.ParseOptNames())
52
53
54class Exec(_View):
55
56 def __init__(self, opt0_array, opt_stacks):
57 # type: (List[bool], List[List[bool]]) -> None
58 _View.__init__(self, opt0_array, opt_stacks, option_def.ExecOptNames())