summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2020-02-21 07:54:32 -0300
committerSoniEx2 <endermoneymod@gmail.com>2020-02-21 07:54:32 -0300
commitba628500304ecf9460d7d39d7fb794e15401eb00 (patch)
treedffe331aecbe849e2e5c710205b233b97723cfad
parent3a8cc83cbe465b798647b92357eff2d52454f3dc (diff)
Make new config system load configs
-rw-r--r--ganarchy/cli/debug.py39
-rw-r--r--ganarchy/config.py40
2 files changed, 43 insertions, 36 deletions
diff --git a/ganarchy/cli/debug.py b/ganarchy/cli/debug.py
index 10e91e5..16f9e6f 100644
--- a/ganarchy/cli/debug.py
+++ b/ganarchy/cli/debug.py
@@ -36,35 +36,20 @@ def paths():
 def configs():
     def print_conf(conf):
         click.echo("\tRepos:")
-        for i, repo in enumerate(ganarchy.config.CONFIG_REPOS.match({'projects': conf.projects})):
+        for i, pctp in enumerate(conf.get_project_commit_tree_paths()):
             click.echo("\t\t{}.".format(i))
-            click.echo("\t\t\tProject: {}".format(repo['commit'][0]))
-            click.echo("\t\t\tURI: {}".format(repo['url'][0]))
-            click.echo("\t\t\tBranch: {}".format(repo['branch'][0]))
-            click.echo("\t\t\tActive: {}".format(repo['branch'][1] == {'active': True}))
+            click.echo("\t\t\tProject: {}".format(pctp.project_commit))
+            click.echo("\t\t\tURI: {}".format(pctp.uri))
+            click.echo("\t\t\tBranch: {}".format(pctp.branch))
+            click.echo("\t\t\tActive: {}".format(pctp.options == {'active': True}))
 
+    confs = ganarchy.config.ConfigManager.new_default()
+    click.echo("Configs: {}".format(confs.sources))
     click.echo("Breaking down the configs.")
-    conf = None
-    # reverse order is intentional
-    for d in reversed(ganarchy.config_dirs):
-        click.echo("Config: {}/config.toml".format(d))
-        try:
-            f = open(d + "/config.toml", 'r', encoding='utf-8', newline='')
-            conf = ganarchy.Config(f, conf)
-            click.echo("Updated entries:")
+    for conf in reversed(confs.sources):
+        click.echo("Config: {}".format(conf.filename))
+        e = conf.update()
+        if e is None:
             print_conf(conf)
-            f.close()
-        except (OSError, UnicodeDecodeError, qtoml.decoder.TOMLDecodeError) as e:
+        else:
             click.echo("\tError: {}".format(e))
-    try:
-        click.echo("Config: {}/config.toml".format(ganarchy.config_home))
-        f = open(ganarchy.config_home + "/config.toml", 'r', encoding='utf-8', newline='')
-        conf = ganarchy.Config(f, conf)
-        click.echo("Updated entries:")
-        print_conf(conf)
-        click.echo("-----")
-        click.echo("\tTitle: {}".format(conf.base_url))
-        click.echo("\tBase URI: {}".format(conf.base_url))
-        f.close()
-    except (OSError, UnicodeDecodeError, qtoml.decoder.TOMLDecodeError) as e:
-        click.echo("\tError: {}".format(e))
diff --git a/ganarchy/config.py b/ganarchy/config.py
index ae1615d..59766ed 100644
--- a/ganarchy/config.py
+++ b/ganarchy/config.py
@@ -27,7 +27,7 @@ from enum import Enum
 CONFIG_REPOS_SANITIZE = abdl.compile("""->'projects'?:?$dict
                                           ->commit/[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/?:?$dict
                                             ->url:?$dict
-                                              ->branch:?$dict(->'active'?:?$bool)""", {'bool': bool, 'dict': dict})
+                                              ->branch:?$dict(->'active'?:?$bool)""", {'bool': bool, 'dict': dict, 'uri': object})#URIValidator})
 CONFIG_REPOS = abdl.compile("->'projects'->commit->url->branch", {'dict': dict})
 
 CONFIG_TITLE_SANITIZE = abdl.compile("""->title'title'?:?$str""", {'str': str})
@@ -42,6 +42,13 @@ class ConfigProperty(Enum):
     TITLE = 1
     BASE_URL = 2
 
+class PCTP:
+    def __init__(self, project_commit, uri, branch, options):
+        self.project_commit = project_commit
+        self.uri = uri
+        self.branch = branch
+        self.options = options
+
 class ConfigSource(abc.ABC):
     @abc.abstractmethod
     def update(self):
@@ -121,8 +128,8 @@ class FileConfigSource(ConfigSource):
                 with open(self.filename) as f:
                     self.tomlobj = qtoml.load(f)
             self.file_exists = True
-        except OSError:
-            return
+        except (OSError, UnicodeDecodeError, qtoml.decoder.TOMLDecodeError) as e:
+            return e
 
     def exists(self):
         return self.file_exists
@@ -132,8 +139,8 @@ class FileConfigSource(ConfigSource):
             yield r['src'][1]
 
     def get_project_commit_tree_paths(self):
-        for r in CONFIG_PATTERN_SANITIZE.match(self.tomlobj):
-            yield (r['commit'][0], r['url'][0], r['branch'][0], r['branch'][1])
+        for r in CONFIG_REPOS_SANITIZE.match(self.tomlobj):
+            yield PCTP(r['commit'][0], r['url'][0], r['branch'][0], r['branch'][1])
 
     @classmethod
     def get_supported_properties(cls):
@@ -152,13 +159,28 @@ class RemoteConfigSource(ConfigSource):
         return self.remote_exists
 
     def get_project_commit_tree_paths(self):
-        for r in CONFIG_PATTERN_SANITIZE.match(self.tomlobj):
+        for r in CONFIG_REPOS_SANITIZE.match(self.tomlobj):
             yield (r['commit'][0], r['url'][0], r['branch'][0], r['branch'][1])
 
 class ConfigManager:
-    def __init__(self):
-        # FIXME ???
-        self.sources = []
+    """A ConfigManager takes care of managing config sources and
+    collecting their details."""
+    def __init__(self, sources):
+        self.sources = sources
+
+    def update(self):
+        for source in self.sources:
+            try:
+                source.update()
+            except:
+                raise # TODO
+
+    @classmethod
+    def new_default(cls):
+        from ganarchy import config_home, config_dirs
+        base_src = [FileConfigSource(config_home + "/config.toml")]
+        extra_srcs = [FileConfigSource(d + "/config.toml") for d in config_dirs]
+        return cls(base_src + extra_srcs)
 
 # class Config:
 #     def __init__(self, toml_file, base=None, remove=True):