Plugins are loading
This commit is contained in:
parent
72627995a7
commit
98c490dab9
4 changed files with 64 additions and 21 deletions
|
@ -27,4 +27,4 @@ Repository = "https://forge.monodon.me/Gnarwhal/sshare"
|
||||||
Issues = "https://forge.monodon.me/Gnarwhal/sshare/issues"
|
Issues = "https://forge.monodon.me/Gnarwhal/sshare/issues"
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
sshare = "sshare.cli:main"
|
sshare = "sshare.main:main"
|
||||||
|
|
|
@ -12,6 +12,6 @@
|
||||||
# You should have received a copy of the GNU General Public License along with
|
# You should have received a copy of the GNU General Public License along with
|
||||||
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from cli import main
|
from main import main
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -12,10 +12,12 @@
|
||||||
# You should have received a copy of the GNU General Public License along with
|
# You should have received a copy of the GNU General Public License along with
|
||||||
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
plugin_type = "logger"
|
||||||
|
|
||||||
def _print_with_color(color, message):
|
def _print_with_color(color, message):
|
||||||
print(f"\033[{color}m{message}\033[0m")
|
print(f"\033[{color}m{message}\033[0m")
|
||||||
|
|
||||||
def log(message):
|
def info(message):
|
||||||
_print_with_color(0, message)
|
_print_with_color(0, message)
|
||||||
|
|
||||||
def warn(message):
|
def warn(message):
|
|
@ -23,14 +23,12 @@ import time
|
||||||
import tomllib
|
import tomllib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from sshare.version import version
|
from pathlib import Path
|
||||||
|
from version import version
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self):
|
def __init__(self, config_directory):
|
||||||
config_directory = os.environ.get("XDG_CONFIG_DIR")
|
_config = self._load_from_file(config_directory / "config.toml")
|
||||||
if config_directory == None:
|
|
||||||
config_directory = f"{os.environ["HOME"]}/.config"
|
|
||||||
_config = self._load_from_file(f"{config_directory}/sshare/config.toml")
|
|
||||||
|
|
||||||
self.source_directory = _config.get("source_directory")
|
self.source_directory = _config.get("source_directory")
|
||||||
|
|
||||||
|
@ -90,21 +88,64 @@ def rebase(number):
|
||||||
return rebased
|
return rebased
|
||||||
|
|
||||||
|
|
||||||
|
class MetaLogger:
|
||||||
|
def __init__(self, loggers):
|
||||||
|
def info(message):
|
||||||
|
for logger in loggers:
|
||||||
|
logger.info(message)
|
||||||
|
|
||||||
|
def warn(message):
|
||||||
|
for logger in loggers:
|
||||||
|
logger.warn(message)
|
||||||
|
|
||||||
|
def error(message):
|
||||||
|
for logger in loggers:
|
||||||
|
logger.error(message)
|
||||||
|
|
||||||
|
self.info = info
|
||||||
|
self.warn = warn
|
||||||
|
self.error = error
|
||||||
|
|
||||||
|
class Plugin:
|
||||||
|
def __init__(self, name, module):
|
||||||
|
self.name = name
|
||||||
|
self.module = module
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
config_directory = Path(os.environ.get("XDG_CONFIG_DIR") or f"{os.environ["HOME"]}/.config") / "sshare"
|
||||||
|
logger = importlib.import_module("command_line_logger")
|
||||||
|
modules = {
|
||||||
|
"logger": [ Plugin("command_line_logger", logger) ],
|
||||||
|
"data": [],
|
||||||
|
}
|
||||||
|
error = False
|
||||||
|
sys.dont_write_bytecode = True
|
||||||
|
for plugin in (config_directory / "plugins").iterdir():
|
||||||
|
if plugin.is_file():
|
||||||
module_spec = importlib.util.spec_from_file_location(
|
module_spec = importlib.util.spec_from_file_location(
|
||||||
"logger_cli",
|
plugin.stem,
|
||||||
"/home/gnarwhal/meine/projects/active/sshare/src/sshare/plugins/logger_cli.py"
|
plugin.as_posix(),
|
||||||
)
|
)
|
||||||
module = importlib.util.module_from_spec(module_spec)
|
module = importlib.util.module_from_spec(module_spec)
|
||||||
module_spec.loader.exec_module(module)
|
module_spec.loader.exec_module(module)
|
||||||
|
|
||||||
module.log("test?")
|
modules_of_type = modules.get(module.plugin_type)
|
||||||
module.warn("test?")
|
if modules_of_type == None:
|
||||||
module.error("test?")
|
logger.error(f"Error: Plugin '{plugin.stem}' has an invalid plugin type '{module.plugin_type}'")
|
||||||
|
error = True
|
||||||
|
else:
|
||||||
|
modules_of_type.append(Plugin(plugin.stem, module))
|
||||||
|
if hasattr(module, "init"):
|
||||||
|
module.init()
|
||||||
|
if error:
|
||||||
|
sys.exit(1)
|
||||||
|
sys.dont_write_bytecode = False
|
||||||
|
logger = MetaLogger([ logger.module for logger in modules["logger"] ])
|
||||||
|
logger.info("Successfully loaded plugins")
|
||||||
|
|
||||||
arguments = parse_arguments()
|
arguments = parse_arguments()
|
||||||
|
|
||||||
config = Config()
|
config = Config(config_directory)
|
||||||
|
|
||||||
contents = b''
|
contents = b''
|
||||||
target_file_extension = ""
|
target_file_extension = ""
|
Loading…
Reference in a new issue