@@ -39,30 +39,42 @@ def init_environment(self, db_path: Path, *, migrate: bool = False, config_path:
3939 if root_path .is_file () and root_path .name == db_constants .DB_FILE_NAME :
4040 root_path = root_path .parent
4141
42+ config = self .load_config (root_path , config_path )
43+ set_config_instance (config )
44+
45+ if not (dbf := root_path / db_constants .DB_FILE_NAME ).is_file ():
46+ self .logger .error ('Database file {!r} does not exist' .format (dbf .as_posix ()))
47+ ErrorReturnCodes .invalid_argument .sys_exit ()
48+ self .init_db (root_path , create = False , migrate = migrate )
49+
50+ @classmethod
51+ def load_config (cls , root_path : Path , config_path : Optional [Path ]) -> Config :
4252 if config_path is None :
4353 auto_config_path = root_path .parent / 'config' / 'prime_backup' / 'config.json'
4454 if auto_config_path .is_file ():
45- config = self .__load_config (auto_config_path )
46- self . logger .info ('Config file auto-detected and loaded from {!r}' .format (auto_config_path .as_posix ()))
55+ config = cls .__load_config (auto_config_path )
56+ logger . get () .info ('Config file auto-detected and loaded from {!r}' .format (auto_config_path .as_posix ()))
4757 else :
4858 config = Config .get_default ()
49- self . logger .info ('Config file not provided; auto-detected path {!r} does not exist, using default config' .format (auto_config_path .as_posix ()))
59+ logger . get () .info ('Config file not provided; auto-detected path {!r} does not exist, using default config' .format (auto_config_path .as_posix ()))
5060 else :
51- config = self .__load_config (config_path )
52- self .logger .info ('Config file loaded from {!r}' .format (config_path .as_posix ()))
53-
54- set_config_instance (config )
61+ config = cls .__load_config (config_path )
62+ logger .get ().info ('Config file loaded from {!r}' .format (config_path .as_posix ()))
63+ return config
5564
56- if not (dbf := root_path / db_constants .DB_FILE_NAME ).is_file ():
57- self .logger .error ('Database file {!r} does not exist' .format (dbf .as_posix ()))
65+ def init_db (self , root_path : Path , * , create : bool , migrate : bool ):
66+ if root_path .is_file ():
67+ self .logger .error ('Storage root {!r} is a file' .format (root_path .as_posix ()))
5868 ErrorReturnCodes .invalid_argument .sys_exit ()
69+
70+ config = Config .get ()
5971 config .storage_root = str (root_path .as_posix ())
6072
6173 self .logger .info ('Storage root set to {!r}' .format (config .storage_root ))
6274 try :
6375 if DbAccess .is_initialized ():
6476 DbAccess .shutdown ()
65- DbAccess .init (create = False , migrate = migrate )
77+ DbAccess .init (create = create , migrate = migrate )
6678 except BadDbVersion as e :
6779 self .logger .info ('Load database failed, you need to ensure the database is accessible with MCDR plugin: {}' .format (e ))
6880 ErrorReturnCodes .action_failed .sys_exit ()
0 commit comments