From c259a653ae490dcfa8ea4408f94ae68a3950ca92 Mon Sep 17 00:00:00 2001 From: enkore Date: Mon, 11 Mar 2013 00:21:27 +0100 Subject: [PATCH] Refactor the config part --- i3pystatus/__init__.py | 33 ++++-------------------------- i3pystatus/core/config.py | 43 +++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/i3pystatus/__init__.py b/i3pystatus/__init__.py index 3fd11f7..1d3630f 100644 --- a/i3pystatus/__init__.py +++ b/i3pystatus/__init__.py @@ -5,8 +5,7 @@ import sys from .core import Status from .core.modules import Module, IntervalModule from .core.settings import SettingsBase -from .core.config import ConfigFinder -from .core.render import render_json +from .core.config import Config __all__ = [ "SettingsBase", @@ -14,33 +13,9 @@ __all__ = [ "Status", ] -def run_config(): - ConfigFinder().run_config() - -def test_config(): - def test_setup(): - """This is a wrapped method so no one ever tries to use it outside of this""" - import i3pystatus - class TestStatus(Status): - def run(self): - self.call_start_hooks() - for module in self.modules: - sys.stdout.write("{module}: ".format(module=module.__name__)) - sys.stdout.flush() - module.run() - output = module.output or {"full_text": "(no output)"} - print(render_json(output)) - - i3pystatus.Status = TestStatus - test_setup() - cf = ConfigFinder() - print("Using configuration file {file}".format(file=cf.get_config_path()[1])) - print("Output, would be displayed right to left in i3bar") - cf.run_config() - sys.exit(0) - def main(): + config = Config() if len(sys.argv) == 2 and sys.argv[1] == "test": - test_config() + config.test() else: - run_config() + config.run() diff --git a/i3pystatus/core/config.py b/i3pystatus/core/config.py index ae1aad0..8cbbee2 100644 --- a/i3pystatus/core/config.py +++ b/i3pystatus/core/config.py @@ -1,6 +1,9 @@ import os.path import runpy +import sys + +from .render import render_json SEARCHPATH = ( "~/.i3pystatus.py", @@ -24,19 +27,41 @@ class ConfigFinder: def exists(path): return os.path.isfile(path) - def get_config_path(self): + def find_config_file(self): failed = [] for path in map(self.expand, self.searchpath): if self.exists(path): - return failed, path + return path else: failed.append(path) - return failed, None + raise RuntimeError("Didn't find a config file, tried\n * {mods}".format(mods="\n * ".join(failed))) - def run_config(self): - failed, path = self.get_config_path() - if path: - runpy.run_path(path, run_name="i3pystatus._config") - else: - raise RuntimeError("Didn't find a config file, tried\n * {mods}".format(mods="\n * ".join(failed))) +class Config: + def __init__(self): + self.finder = ConfigFinder() + self.config_file = self.finder.find_config_file() + + def run(self): + runpy.run_path(self.config_file, run_name="i3pystatus._config") + + def test(self): + def setup(): + """This is a wrapped method so no one ever tries to use it outside of this""" + import i3pystatus + class TestStatus(i3pystatus.Status): + def run(self): + self.call_start_hooks() + for module in self.modules: + sys.stdout.write("{module}: ".format(module=module.__name__)) + sys.stdout.flush() + module.run() + output = module.output or {"full_text": "(no output)"} + print(render_json(output)) + + i3pystatus.Status = TestStatus + setup() + print("Using configuration file {file}".format(file=self.config_file)) + print("Output, would be displayed right to left in i3bar") + self.run() + sys.exit(0) # Exit program, kill any state left behind by TestStatus