From 8913363ca8f6b65e62f7a6a6c8b052b4952e74b5 Mon Sep 17 00:00:00 2001 From: caryoscelus Date: Tue, 26 Jul 2022 19:57:14 +0000 Subject: [PATCH] minor code improvements fixes #138 --- src/Config.py | 2 +- src/main.py | 49 +++++++++++++++++++++++++++++++------------------ zeronet.py | 2 +- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/Config.py b/src/Config.py index d7a25d7e..ba020c9a 100644 --- a/src/Config.py +++ b/src/Config.py @@ -16,7 +16,7 @@ class Config(object): self.version = "0.7.6+" self.user_agent = "conservancy" # DEPRECATED ; replace with git-generated commit - self.rev = 5032 + self.rev = 5033 self.argv = argv self.action = None self.test_parser = None diff --git a/src/main.py b/src/main.py index 8a677193..b86f09c3 100644 --- a/src/main.py +++ b/src/main.py @@ -25,23 +25,34 @@ gevent.monkey.patch_all(thread=False, subprocess=False) update_after_shutdown = False # If set True then update and restart zeronet after main loop ended restart_after_shutdown = False # If set True then restart zeronet after main loop ended -# Load config from Config import config -config.parse(silent=True) # Plugins need to access the configuration -if not config.arguments: # Config parse failed, show the help screen and exit + +def load_config(): + config.parse(silent=True) # Plugins need to access the configuration + if not config.arguments: # Config parse failed, show the help screen and exit + config.parse() + +load_config() + +def init_dirs(): config.parse() + if not os.path.isdir(config.data_dir): + os.mkdir(config.data_dir) + try: + os.chmod(config.data_dir, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) + except Exception as err: + startupError("Can't change permission of %s: %s" % (config.data_dir, err)) -if not os.path.isdir(config.data_dir): - os.mkdir(config.data_dir) - try: - os.chmod(config.data_dir, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) - except Exception as err: - startupError("Can't change permission of %s: %s" % (config.data_dir, err)) + sites_json = f"{config.data_dir}/sites.json" + if not os.path.isfile(sites_json): + with open(sites_json, "w") as f: + f.write("{}") + users_json = f"{config.data_dir}/users.json" + if not os.path.isfile(users_json): + with open(users_json, "w") as f: + f.write("{}") -if not os.path.isfile("%s/sites.json" % config.data_dir): - open("%s/sites.json" % config.data_dir, "w").write("{}") -if not os.path.isfile("%s/users.json" % config.data_dir): - open("%s/users.json" % config.data_dir, "w").write("{}") +init_dirs() if config.action == "main": from util import helper @@ -69,12 +80,14 @@ config.initLogging() # Debug dependent configuration from Debug import DebugHook - -# Load plugins from Plugin import PluginManager -PluginManager.plugin_manager.loadPlugins() -config.loadPlugins() -config.parse() # Parse again to add plugin configuration options + +def load_plugins(): + PluginManager.plugin_manager.loadPlugins() + config.loadPlugins() + config.parse() # Parse again to add plugin configuration options + +load_plugins() # Log current config logging.debug("Config: %s" % config) diff --git a/zeronet.py b/zeronet.py index 36b2ed74..97371e5f 100755 --- a/zeronet.py +++ b/zeronet.py @@ -44,7 +44,7 @@ def main(): print("Error: Python 3.x is required") sys.exit(0) - if "--silent" not in sys.argv: + if '--silent' not in sys.argv: fancy_greet() main = None