Coverage for src/probable_fiesta/app/builder/app_abstract_machine.py: 39%
84 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
1from abc import ABC
2from enum import Enum, auto
3from .my_app_builder import MyAppBuilder
4from ...command.builder.command_builder import CommandBuilder
5from .context_factory import ContextFactory
6from ...config.config_factory import ConfigFactory
7from ...cli.builder.args_parser_factory import ArgsParserFactory
9class App(ABC):
10 def run(self):
11 pass
13class MyApp(App):
14 def run(self):
15 print("This is the my app")
17class FlaskApp(App):
18 def run(self):
19 print("This is the flask app")
21class AppFactory(ABC):
22 def create_app(self, name, context, args_parse, args, config):
23 pass
25 def prepare_default(self):
26 pass
28class MyappFactory(AppFactory):
29 def create_app(self, name, context, args_parse, args, config):
30 print("Creating my app")
31 my_app_builder = MyAppBuilder()
32 my_app = my_app_builder\
33 .name\
34 .set_name(name)\
35 .context\
36 .set_context(context)\
37 .arguments\
38 .validate_with_args_parse(args_parse, args)\
39 .config\
40 .set_config(config)\
41 .build()
42 return my_app
44 def prepare_default(self):
45 print("Preparing default my app sample")
46 args = ["--test"]
47 function = lambda x: (x)
49 # get commands
50 command_builder = CommandBuilder()
51 commands = command_builder.queue\
52 .add_new_command("test", function, args)\
53 .build()
55 # get context
56 context = ContextFactory().new_context("default", commands)
57 context.command_queue.print_queue()
59 args_parser = ArgsParserFactory().new("--test", action='store_true', help=f"Current version")
61 # get default config
62 config = ConfigFactory.new_default_config_builder('default', 'myapp_factory')
64 # create app
65 my_app_builder = MyAppBuilder()
66 my_app = my_app_builder\
67 .name\
68 .set_name("sample app")\
69 .context\
70 .set_context(context)\
71 .args_parse\
72 .set_args_parse(args_parser)\
73 .arguments\
74 .set_arguments(args)\
75 .validate_args()\
76 .config\
77 .set_config(config)\
78 .build()
79 return my_app
81class FlaskFactory(AppFactory):
82 def create_app(self, name, context, args_parse, args, config):
83 print("Creating flask app")
84 my_app_builder = MyAppBuilder()
85 config = ConfigFactory.new_config_builder('flask' , 'flask_factory').build()
86 #logger = get_config('flask', 'flask_factory')
87 my_app = my_app_builder\
88 .name\
89 .set_name(name)\
90 .context\
91 .set_context(context)\
92 .args_parse\
93 .set_args_parse(args_parse)\
94 .arguments\
95 .set_arguments(args)\
96 .config\
97 .set_config(config)\
98 .build()
99 return my_app
102class AppMachine:
103 class AvailableApps(Enum):
104 MYAPP = auto()
105 FLASK = auto()
107 factories = []
108 initialized = False
110 def __init__(self):
111 if not self.initialized:
112 self.initialized = True
113 for d in self.AvailableApps:
114 name = d.name[0] + d.name[1:].lower()
115 factory_name = name + "Factory"
116 factory_instance = eval(factory_name)()
117 self.factories.append((name, factory_instance))
119 def __str__(self):
120 return f"AppMachine: Available apps: {self.factories}"
122 def prepare_app(self):
123 print("Available apps:")
124 for f in self.factories:
125 print(f[0])
126 s = input(f'Please pick app (0-{len(self.factories)-1}):')
127 idx = int(s)
128 # specify app name
129 return self.factories[idx][1].prepare()
131 def prepare_default_app(self):
132 return self.factories[0][1].prepare_default()
134def create_app(type):
135 if type == 'my_app':
136 return MyappFactory().create_app()
137 elif type == 'metrics_app':
138 return FlaskFactory().create_app()
139 else:
140 print("Invalid app type")
141 return None
143def prepare_default_app(type):
144 if type == 'my_app':
145 return MyappFactory().prepare_default()
146 else:
147 print("Only my_app is supported")
148 return None