OILS / core / optview.py View on Github | oils.pub

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