Coverage for src/probable_fiesta/app/builder/app_builder.py: 82%
137 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-01-30 18:57 -0500
« prev ^ index » next coverage.py v7.1.0, created at 2023-01-30 18:57 -0500
1"""App builder module."""
2from ...cli.builder.args_parse import Parser
3from .context_holder import ContextHolder
5class App:
6 def __init__(self):
7 # properties
8 self.name = None
9 self.args_parser = None # validation
10 self.arguments = None # validation
11 self.config = None
12 self.context = None # context_holder # validation
13 # internal properties
14 self.valid = False
15 self.error = None
16 self.run_history = []
17 self.cleaned_args = []
18 self.cleaned_argv = []
20 def __str__(self):
21 return f"App: {self.__dict__}"
23 def validate(self):
24 local_valid = True
26 # arguments
27 if self.arguments is None:
28 print("validate fail: App arguments are empty")
29 local_valid = False
31 # validate args_parser
32 if self.args_parser is not None:
33 if not self.args_parser.validate(self.arguments):
34 print("App args_parser is not valid")
35 self.valid = False
37 # validate context
38 if self.context is not None:
39 if not self.context.validate(self.args_parser.get_parsed_args()):
40 print("App context is not valid")
41 local_valid = False
43 self.valid = local_valid
44 return self
46 def run(self, name=None):
47 if not self.valid:
48 print("App is not valid.\nTrying to run anyway.")
49 #print(self.context)
50 command_queue = self.context.command_queue.run_all()
51 #print("Command Queue: ", command_queue)
52 self.run_history.append(command_queue.get_history())
53 return
54 #print("App is valid")
55 if name is None:
56 #print("Running all commands")
57 for name in self.context.context_holder.keys():
58 #print("Running command for context: ", name)
59 context = self.context.context_holder[name]
60 #print("context: ", context)
61 context_run = context.command_queue.run_all()
62 #print("context run: ", context_run)
63 context_run_history = context_run.get_history()
64 self.run_history.append(context_run_history)
65 return
66 #print("Running one command for context: ", name)
67 command_queue = self.context.context_holder[name].command_queue.run_all()
68 #print("Command Queue: ", command_queue)
69 self.run_history.append(command_queue.get_history())
70 return self
72 def get_run_history(self):
73 if len(self.run_history) == 0:
74 print("No run history found")
75 return
76 if len(self.run_history) == 1:
77 return self.run_history[0]
78 return self.run_history
80 def clear_run_history(self):
81 self.run_history = []
82 return self
84class AppBuilder:
86 def __init__(self, app=None):
87 if app is None:
88 self.app = App()
89 else:
90 self.app = app
92 @property
93 def context(self):
94 return AppContextBuilder(self.app)
96 @property
97 def name(self):
98 return AppNameBuilder(self.app)
100 @property
101 def args_parser(self):
102 return AppArgsParserBuilder(self.app)
104 @property
105 def arguments(self):
106 return AppArgumentsBuilder(self.app)
108 @property
109 def config(self):
110 return MyAppConfigBuilder(self.app)
112 def build(self):
113 return self.app
115 def validate(self):
116 self.app.validate()
117 return self
119class AppContextBuilder(AppBuilder):
120 def __init__(self, app):
121 super().__init__(app)
123 def add_context(self, context):
124 if self.app.context is None:
125 self.app.context = ContextHolder()
126 self.app.context.add_context(context)
127 return self
129 def set_context(self, context):
130 self.app.context = context
131 return self
133class AppNameBuilder(AppBuilder):
134 def __init__(self, app):
135 super().__init__(app)
137 def set_name(self, name):
138 self.app.name = name
139 return self
141class AppArgsParserBuilder(AppBuilder):
142 def __init__(self, app):
143 super().__init__(app)
145 def add_argument(self, *args, **kwargs):
146 if self.app.args_parser is None:
147 self.app.args_parser = Parser().Factory.new(add_help=True, description="My app")
148 self.app.args_parser.add_argument(*args, **kwargs)
149 return self
151 def set_args_parser(self, args_parser):
152 self.app.args_parser = args_parser
153 return self
155class AppArgumentsBuilder(AppBuilder):
156 def __init__(self, app):
157 super().__init__(app)
159 def set_arguments(self, arguments):
160 self.clean_arguments(arguments)
161 self.app.arguments = arguments
162 return self
164 def clean_arguments(self, arguments):
165 #self.app.cleaned_args = []
166 #self.app.cleaned_argv = []
167 self.app.cleaned_args, self.app.cleaned_argv = self.clean_arg_function(arguments)
168 return self
170 @staticmethod
171 def clean_arg_function(arguments: list):
172 args = []
173 argv = []
174 for x in arguments: # TODO validate type
175 if x.startswith('--'):
176 #print("Cleaning x: ", x)
177 args.append(x.replace('--', '',2).replace('-', '_'))
178 elif x.startswith('-'):
179 argv.append(x.replace('-', '',1).replace('-', '_'))
180 #print("Cleaning x: ", x)
181 else:
182 argv.append(x)
183 #print("Skipping x: ", x)
184 return args, argv
187class MyAppConfigBuilder(AppBuilder):
188 def __init__(self, app):
189 super().__init__(app)
191 def set_config(self, config):
192 self.app.config = config
193 return self