mirror of
https://github.com/LorenEteval/Furious.git
synced 2026-09-27 01:10:22 +03:00
98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
# Copyright (C) 2024–present Loren Eteval & contributors <loren.eteval@proton.me>
|
||
#
|
||
# This file is part of Furious.
|
||
#
|
||
# This program is free software: you can redistribute it and/or modify
|
||
# it under the terms of the GNU General Public License as published by
|
||
# the Free Software Foundation, either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
#
|
||
# This program is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
#
|
||
# You should have received a copy of the GNU General Public License
|
||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
from __future__ import annotations
|
||
|
||
from Furious.Utility.Constants import PLATFORM
|
||
|
||
from abc import ABC
|
||
from typing import Callable, Union
|
||
|
||
import ujson
|
||
|
||
__all__ = ['CoreFactory']
|
||
|
||
|
||
class CoreFactory(ABC):
|
||
class ExitCode:
|
||
ConfigurationError = 23
|
||
# Windows: 4294967295. Darwin, Linux: 255 (-1)
|
||
ServerStartFailure = 4294967295 if PLATFORM == 'Windows' else 255
|
||
# Windows shutting down
|
||
SystemShuttingDown = 0x40010004
|
||
|
||
def __init__(
|
||
self, exitCallback: Union[Callable[[CoreFactory, int], None], None] = None
|
||
):
|
||
super().__init__()
|
||
|
||
self._exitCallback = exitCallback
|
||
self._jsonConfig = ''
|
||
|
||
def callExitCallback(self, exitcode: int):
|
||
if callable(self._exitCallback):
|
||
self._exitCallback(self, exitcode)
|
||
|
||
def registerCurrentJSONConfig(self, config: Union[str, dict]) -> CoreFactory:
|
||
self._jsonConfig = config
|
||
|
||
return self
|
||
|
||
def configJSONString(self) -> str:
|
||
if isinstance(self._jsonConfig, str):
|
||
return self._jsonConfig
|
||
if isinstance(self._jsonConfig, dict):
|
||
try:
|
||
return ujson.dumps(
|
||
self._jsonConfig,
|
||
ensure_ascii=False,
|
||
escape_forward_slashes=False,
|
||
)
|
||
except Exception:
|
||
# Any non-exit exceptions
|
||
|
||
return ''
|
||
|
||
return ''
|
||
|
||
def configJSONObject(self) -> dict:
|
||
if isinstance(self._jsonConfig, str):
|
||
try:
|
||
return ujson.loads(self._jsonConfig)
|
||
except Exception:
|
||
# Any non-exit exceptions
|
||
|
||
return {}
|
||
if isinstance(self._jsonConfig, dict):
|
||
return self._jsonConfig
|
||
|
||
return {}
|
||
|
||
@staticmethod
|
||
def name() -> str:
|
||
raise NotImplementedError
|
||
|
||
@staticmethod
|
||
def version() -> str:
|
||
raise NotImplementedError
|
||
|
||
def start(self, *args, **kwargs) -> bool:
|
||
raise NotImplementedError
|
||
|
||
def stop(self):
|
||
raise NotImplementedError
|