7
7
import os
8
8
import sys
9
9
import time
10
+ import datetime
10
11
import argparse
11
12
import threading
12
- from mudpi import utils , boot , importer
13
+ from mudpi .config import Config
14
+ from mudpi import utils , importer
15
+ from mudpi .logger .Logger import Logger , LOG_LEVEL
16
+ from mudpi .managers .core_manager import CoreManager
13
17
from mudpi .constants import __version__ , PATH_CONFIG , DEFAULT_CONFIG_FILE , \
14
18
FONT_RESET_CURSOR , FONT_RESET , YELLOW_BACK , GREEN_BACK , FONT_GREEN , FONT_RED , FONT_YELLOW , FONT_PADDING
15
19
from mudpi .exceptions import ConfigNotFoundError , ConfigFormatError
16
- from mudpi .core import MudPi
17
- from mudpi .config import Config
18
- from mudpi .logger .Logger import Logger , LOG_LEVEL
19
20
20
21
21
22
def main (args = None ):
@@ -36,7 +37,7 @@ def main(args=None):
36
37
Config ().save_to_file (config_path )
37
38
return
38
39
39
-
40
+ #######################
40
41
### Configurations ###
41
42
config_path = os .path .abspath (os .path .join (os .getcwd (), arguments .config ))
42
43
@@ -45,18 +46,18 @@ def main(args=None):
45
46
print (chr (27 ) + "[2J" )
46
47
display_greeting ()
47
48
48
- mudpi = MudPi ()
49
- boot . mudpi_from_config ( mudpi , config_path )
49
+ manager = CoreManager ()
50
+ manager . load_mudpi_from_config ( config_path )
50
51
51
52
print (f'{ "Loading MudPi Configs " :.<{FONT_PADDING + 1 }} { FONT_GREEN } Complete{ FONT_RESET } ' )
52
53
53
-
54
+ #####################
54
55
### Bootstrapping ###
55
56
56
57
if arguments .debug :
57
58
print (f'{ YELLOW_BACK } DEBUG MODE ENABLED{ FONT_RESET } ' )
58
- mudpi .config .config ["mudpi" ]["debug" ] = True
59
- print (arguments ) #DEBUG
59
+ manager . mudpi .config .config ["mudpi" ]["debug" ] = True
60
+ # print(arguments) #DEBUG
60
61
print (f'Config path: { config_path } ' ) #DEBUG
61
62
print (f'Current Directory: { os .getcwd ()} ' )
62
63
# print(f'Config keys {mudpi.config.keys()}')
@@ -65,95 +66,77 @@ def main(args=None):
65
66
# Logging Module
66
67
try :
67
68
print ('Initializing Logger \r ' , end = '' , flush = True )
68
- boot .initialize_logging (mudpi )
69
+ manager .initialize_logging ()
69
70
except Exception as e :
70
71
print (f'{ "Initializing Logger " :.<{FONT_PADDING }} { FONT_RED } Disabled{ FONT_RESET } ' )
71
72
72
- boot .load_mudpi_core (mudpi )
73
+ # MudPi Core Systems
74
+ manager .load_mudpi_core ()
73
75
74
- Logger .log_formatted (
75
- LOG_LEVEL ["warning" ], "Initializing Core " , "Complete" , 'success'
76
- )
76
+ Logger .log_formatted (LOG_LEVEL ["warning" ], "Initializing Core " , "Complete" , 'success' )
77
77
78
- loaded_extensions = boot .load_all_extensions (mudpi )
78
+ Logger .log (LOG_LEVEL ["debug" ], f'{ " Detecting Configurations " :_^{FONT_PADDING + 8 }} ' )
79
+ # Load the Extension System
80
+ loaded_extensions = manager .load_all_extensions ()
79
81
80
82
Logger .log_formatted (
81
- LOG_LEVEL ["warning" ], f"Loaded { len (loaded_extensions )} Prepared Extensions " , "Complete" , 'success'
83
+ LOG_LEVEL ["warning" ], f"Loaded { len (loaded_extensions )} Extensions " , "Complete" , 'success'
82
84
)
83
85
84
-
85
- # #########################
86
- # ### Register Workers ###
87
- # for worker in mudpi.config.workers:
88
- # worker_type = f'linux.{worker["type"]}_worker'
89
- # wrker = importer.get_worker(worker_type)
90
- # # Worker import magic
91
- # if wrker:
92
- # clss = [item for item in utils.get_module_classes(wrker.__name__) if 'Worker' in item[0] and item[0] is not "Worker"]
93
- # worker_instance = clss[0][1](mudpi, worker)
94
- # registered_worker_count += 1
95
- # else:
96
- # # Unable to import module
97
- # Logger.log(
98
- # LOG_LEVEL["error"],
99
- # f'{f"Failed Loading {worker_type} ":.<{FONT_PADDING}} {FONT_RED}Failed{FONT_RESET}'
100
- # )
101
- #
102
-
103
- Logger .log_formatted (
104
- LOG_LEVEL ["warning" ],
105
- "Booting MudPi " , 'Complete' , 'success'
106
- )
86
+ Logger .log_formatted (LOG_LEVEL ["warning" ], "MudPi Fully Loaded" , 'Complete' , 'success' )
107
87
108
88
#########################
109
89
### Start All Systems ###
110
- Logger .log_formatted (
111
- LOG_LEVEL ["info" ],
112
- "Signaling All Workers to Start " , 'Pending' , 'notice'
113
- )
114
- # for worker in mudpi.worker_registry:
115
- # t = mudpi.worker_registry[worker].run()
116
- # mudpi.threads.append(t)
117
-
118
- mudpi .start ()
119
- Logger .log_formatted (
120
- LOG_LEVEL ["info" ],
121
- "Signaling All Workers to Start " , 'Complete' , 'success'
122
- )
123
- print (f'{ "" :_<{FONT_PADDING + 8 }} ' )
124
- print ('' )
90
+ Logger .log (LOG_LEVEL ["debug" ], f'{ " Start Systems " :_^{FONT_PADDING + 8 }} ' )
91
+ Logger .log_formatted (LOG_LEVEL ["debug" ], "Starting All Workers " , 'Pending' , 'notice' )
92
+ manager .mudpi .start_workers ()
93
+ Logger .log_formatted (LOG_LEVEL ["info" ], "Started All Workers " , 'Complete' , 'success' )
125
94
95
+ # Everything should be loaded and running
96
+ Logger .log_formatted (LOG_LEVEL ["info" ], "MudPi Systems " , 'Online' , 'success' )
97
+ print (f'{ "" :_<{FONT_PADDING + 8 }} \n ' )
126
98
127
99
""" Debug Mode Dump After System Online """
128
- if arguments .debug :
129
- debug_dump (mudpi )
100
+ if arguments .debug and arguments .dump :
101
+ manager .debug_dump (cache_dump = arguments .cache_dump )
102
+ time .sleep (1 )
103
+
130
104
105
+ ###############################
106
+ """ MAIN PROGRAM HEARTBEAT """
107
+ manager .mudpi .start ()
108
+ PROGRAM_RUNNING = True
109
+ while PROGRAM_RUNNING :
110
+ try :
111
+ # Keep messages being processed
112
+ manager .mudpi .events .get_message ()
113
+ current_clock = datetime .datetime .now ().replace (microsecond = 0 )
114
+ manager .mudpi .events .publish ('clock' , {"clock" :current_clock .strftime ("%m-%d-%Y %H-%M-%S" ),
115
+ "date" :str (current_clock .date ()), "time" : str (current_clock .time ())})
116
+ for i in range (10 ):
117
+ time .sleep (0.1 )
118
+ manager .mudpi .events .get_message ()
119
+ except KeyboardInterrupt as error :
120
+ PROGRAM_RUNNING = False
121
+ except Exception as error :
122
+ Logger .log (
123
+ LOG_LEVEL ["error" ],
124
+ f"Runtime Error: { error } "
125
+ )
126
+ PROGRAM_RUNNING = False
131
127
132
128
""" PROGRAM SHUTDOWN """
133
- time .sleep (5 )
134
- mudpi .shutdown ()
135
-
136
- for thread in mudpi .threads :
137
- thread .join ()
138
-
139
- # Check the config
140
- #
141
- # Check other options before running
142
- #
143
- # Boot Up MudPi
144
- # - MudPi Set to LOADING
145
- # - Load the Config from Conf Path
146
- # - Load the MudPi Core Components
147
- # - Load the Event Bus (redis)
148
- # - Load the State Manager (redis)
149
- # - Load all Workers and Register Them
150
- # - Workers initalize components
151
- #
152
- # - MudPi Set to STARTING
153
- # - Set Threading Event for Core
154
- # - Start Up all Registered Workers
155
- #
156
- # - MudPi Set to RUNNING
129
+ print (f'{ "" :_<{FONT_PADDING + 8 }} ' )
130
+ Logger .log_formatted (
131
+ LOG_LEVEL ["info" ],
132
+ "Stopping All Workers for Shutdown " , 'Pending' , 'notice'
133
+ )
134
+ manager .mudpi .shutdown ()
135
+
136
+ Logger .log_formatted (
137
+ LOG_LEVEL ["info" ],
138
+ "All MudPi Systems " , 'Offline' , 'error'
139
+ )
157
140
158
141
159
142
def get_arguments ():
@@ -174,6 +157,12 @@ def get_arguments():
174
157
parser .add_argument (
175
158
"--debug" , action = "store_true" , help = "Start MudPi in forced debug mode"
176
159
)
160
+ parser .add_argument (
161
+ "--dump" , action = "store_true" , help = "Display important system information"
162
+ )
163
+ parser .add_argument (
164
+ "--cache_dump" , action = "store_true" , help = "Display cache when --dump is set"
165
+ )
177
166
parser .add_argument (
178
167
"--verbose" , action = "store_true" , help = "Enable verbose output."
179
168
)
@@ -211,61 +200,5 @@ def display_greeting():
211
200
print (FONT_RESET )
212
201
213
202
214
- def debug_dump (mudpi ):
215
- """ Dump important data from MudPi instance for debugging mode """
216
- Logger .log (
217
- LOG_LEVEL ["debug" ],
218
- f'{ YELLOW_BACK } MUDPI CACHE DUMP{ FONT_RESET } '
219
- )
220
- for key in mudpi .cache .keys ():
221
- Logger .log (
222
- LOG_LEVEL ["debug" ],
223
- f"{ FONT_YELLOW } { key } :{ FONT_RESET } { mudpi .cache [key ]} "
224
- )
225
-
226
- Logger .log (
227
- LOG_LEVEL ["debug" ],
228
- f'{ YELLOW_BACK } MUDPI LOADED EXTENSIONS{ FONT_RESET } '
229
- )
230
- for ext in mudpi .extensions .all ():
231
- ext = mudpi .cache .get ("extension_importers" , {}).get (ext )
232
- Logger .log (
233
- LOG_LEVEL ["debug" ],
234
- f"Namespace: { FONT_YELLOW } { ext .namespace } { FONT_RESET } \n { ext .description } \n { ext .documentation } "
235
- )
236
-
237
- if mudpi .components .all ():
238
- Logger .log (
239
- LOG_LEVEL ["debug" ],
240
- f'{ YELLOW_BACK } MUDPI REGISTERED COMPONENTS{ FONT_RESET } '
241
- )
242
- Logger .log (
243
- LOG_LEVEL ["debug" ],
244
- f"{ 'COMPONENT' :<10} { 'ID' :<20} NAME\n { '' :-<60} "
245
- )
246
- for comp in mudpi .components .all ():
247
- comp = mudpi .components .get (comp )
248
- Logger .log (
249
- LOG_LEVEL ["debug" ],
250
- f"{ comp .__class__ .__name__ :<10} | { comp .id :<20} | { comp .name } "
251
- )
252
-
253
- if mudpi .actions .all ():
254
- Logger .log (
255
- LOG_LEVEL ["debug" ],
256
- f'{ YELLOW_BACK } MUDPI REGISTERED ACTIONS{ FONT_RESET } '
257
- )
258
- Logger .log (
259
- LOG_LEVEL ["debug" ],
260
- f"{ 'ACTION CALL' :<24} { 'ACTION' :<20} NAMESPACE\n { '' :-<60} "
261
- )
262
- for namespace , actions in mudpi .actions .items ():
263
- for key , action in actions .items ():
264
- action_command = f"{ namespace } .{ key } " if namespace else key
265
- Logger .log (
266
- LOG_LEVEL ["debug" ],
267
- f"{ action_command :<24} | { key :<20} | { namespace } "
268
- )
269
-
270
203
if __name__ == "__main__" :
271
204
sys .exit (main ())
0 commit comments