""".. __init__.pyResponsible for initialization of RetroArcher."""# future importsfrom__future__importannotations# standard importsimportosimportsubprocessimportsysimportthreadingfromtypingimportUnion# local importsfrompyraimportconfigfrompyraimportdefinitionsfrompyraimporthelpersfrompyraimportlogger# get loggerlog=logger.get_logger(name=__name__)_INITIALIZED=FalseCONFIG=NoneCONFIG_FILE=NoneDEBUG=FalseDEV=FalseSIGNAL=None# Signal to watch forINIT_LOCK=threading.Lock()QUIET=False
[docs]definitialize(config_file:str)->bool:""" Initialize RetroArcher. Sets up config, loggers, and http port. Parameters ---------- config_file : str The path to the config file. Returns ------- bool True if initialize succeeds, otherwise False. Raises ------ SystemExit If unable to correct possible issues with config file. Examples -------- >>> initialize(config_file='config.ini') True """withINIT_LOCK:globalCONFIGglobalCONFIG_FILEglobalDEBUGglobal_INITIALIZEDtry:CONFIG=config.create_config(config_file=config_file)exceptException:raiseSystemExit("Unable to initialize due to a corrupted config file. Exiting...")CONFIG_FILE=config_fileassertCONFIGisnotNonelogger.blacklist_config(config=CONFIG)# setup log blacklistif_INITIALIZED:returnFalse# create logs folderdefinitions.Paths.LOG_DIR,log_writable=helpers.check_folder_writable(folder=definitions.Paths.LOG_DIR,fallback=os.path.join(definitions.Paths.DATA_DIR,'logs'),name='logs')ifnotlog_writableandnotQUIET:sys.stderr.write("Unable to create the log directory. Logging to screen only.\n")# setup loggers... cannot use logging until this is finishedlogger.setup_loggers()ifCONFIG['Network']['HTTP_PORT']<21orCONFIG['Network']['HTTP_PORT']>65535:log.warning(msg=f"HTTP_PORT out of bounds: 21 < {CONFIG['Network']['HTTP_PORT']} < 65535")CONFIG['Network']['HTTP_PORT']=9696DEBUG=DEBUGorbool(CONFIG['Logging']['DEBUG_LOGGING'])_INITIALIZED=TruereturnTrue
[docs]defstop(exit_code:Union[int,str]=0,restart:bool=False):""" Stop RetroArcher. This function ends the tray icon if it's running. Then restarts or shutdowns RetroArcher depending on the value of the `restart` parameter. Parameters ---------- exit_code : Union[int, str], default = 0 The exit code to send. Does not apply if `restart = True`. restart : bool, default = False Set to True to restart RetroArcher. Examples -------- >>> stop(exit_code=0, restart=False) """# stop the tray iconfrompyra.tray_iconimporttray_endtry:tray_end()exceptAttributeError:passifrestart:ifdefinitions.Modes.FROZEN:args=[definitions.Paths.BINARY_PATH]else:args=[sys.executable,definitions.Paths.BINARY_PATH]args+=sys.argv[1:]if'--nolaunch'notinargs:# don't launch the browser againargs+=['--nolaunch']# also os.execv requires at least one argument# os.execv(sys.executable, args)# `os.execv` is more desirable, but is not working correctly# flask app does not respond to requests after restarting# alternative to os.execv()subprocess.Popen(args=args,cwd=os.getcwd())sys.exit(exit_code)