summary refs log tree commit diff stats
path: root/ganarchy
diff options
context:
space:
mode:
Diffstat (limited to 'ganarchy')
-rw-r--r--ganarchy/__init__.py15
-rw-r--r--ganarchy/__main__.py7
-rw-r--r--ganarchy/cli/__init__.py (renamed from ganarchy/debug.py)19
-rw-r--r--ganarchy/cli/debug.py70
-rw-r--r--ganarchy/config.py52
5 files changed, 135 insertions, 28 deletions
diff --git a/ganarchy/__init__.py b/ganarchy/__init__.py
index 0f13b44..6d24994 100644
--- a/ganarchy/__init__.py
+++ b/ganarchy/__init__.py
@@ -32,7 +32,7 @@ import requests
 from collections import defaultdict
 from urllib.parse import urlparse
 
-import ganarchy.config
+import ganarchy.config as m_ganarchy_config
 
 MIGRATIONS = {
         "toml-config": (
@@ -210,10 +210,8 @@ def get_env():
     env.filters['tomle'] = env.filters['tomlescape']
     return env
 
-
-@click.group()
-def ganarchy():
-    pass
+# FIXME
+from ganarchy.cli import main as ganarchy
 
 @ganarchy.command()
 def initdb():
@@ -457,12 +455,11 @@ class Config:
         # TODO re.compile("(^" + "|^".join(map(re.escape, domains)) + "|" + "|".join(map(re.escape, suffixes) + ")$")
         if base:
             # FIXME is remove=remove the right thing to do?
-            self._update_projects(base.projects, remove=remove, sanitize=False) # already sanitized
-        projects = config_data.get('projects', {})
-        self._update_projects(projects, remove=remove)
+            self._update_projects({'projects': base.projects}, remove=remove, sanitize=False) # already sanitized
+        self._update_projects(config_data, remove=remove)
 
     def _update_projects(self, projects, remove, sanitize=True):
-        m = (ganarchy.config.CONFIG_PATTERN_SANITIZE if sanitize else ganarchy.config.CONFIG_PATTERN).match(projects)
+        m = (m_ganarchy_config.CONFIG_REPOS_SANITIZE if sanitize else m_ganarchy_config.CONFIG_REPOS).match(projects)
         for v in m:
             commit, repo_url, branchname, options = v['commit'][0], v['url'][0], v['branch'][0], v['branch'][1]
             try:
diff --git a/ganarchy/__main__.py b/ganarchy/__main__.py
index b270251..ca17cb7 100644
--- a/ganarchy/__main__.py
+++ b/ganarchy/__main__.py
@@ -15,9 +15,12 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 # The base CLI
+import ganarchy.cli
+
+# FIXME this shouldn't be here
 import ganarchy
 
 # Additional CLI commands
-import ganarchy.debug
+import ganarchy.cli.debug
 
-ganarchy.ganarchy(prog_name='ganarchy')
+ganarchy.cli.main(prog_name='ganarchy')
diff --git a/ganarchy/debug.py b/ganarchy/cli/__init__.py
index 1310549..9effabb 100644
--- a/ganarchy/debug.py
+++ b/ganarchy/cli/__init__.py
@@ -16,21 +16,6 @@
 
 import click
 
-import ganarchy
-import ganarchy.config
-
-@ganarchy.ganarchy.group()
-def debug():
+@click.group()
+def main():
     pass
-
-@debug.command()
-def paths():
-    click.echo('Config home: {}'.format(ganarchy.config_home))
-    click.echo('Additional config search path: {}'.format(ganarchy.config_dirs))
-    click.echo('Cache home: {}'.format(ganarchy.cache_home))
-    click.echo('Data home: {}'.format(ganarchy.data_home))
-
-@debug.command()
-def configs():
-    pass
-
diff --git a/ganarchy/cli/debug.py b/ganarchy/cli/debug.py
new file mode 100644
index 0000000..10e91e5
--- /dev/null
+++ b/ganarchy/cli/debug.py
@@ -0,0 +1,70 @@
+# This file is part of GAnarchy - decentralized project hub
+# Copyright (C) 2019  Soni L.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+import click
+import qtoml
+
+import ganarchy
+import ganarchy.cli
+import ganarchy.config
+
+@ganarchy.cli.main.group()
+def debug():
+    pass
+
+@debug.command()
+def paths():
+    click.echo('Config home: {}'.format(ganarchy.config_home))
+    click.echo('Additional config search path: {}'.format(ganarchy.config_dirs))
+    click.echo('Cache home: {}'.format(ganarchy.cache_home))
+    click.echo('Data home: {}'.format(ganarchy.data_home))
+
+@debug.command()
+def configs():
+    def print_conf(conf):
+        click.echo("\tRepos:")
+        for i, repo in enumerate(ganarchy.config.CONFIG_REPOS.match({'projects': conf.projects})):
+            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("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:")
+            print_conf(conf)
+            f.close()
+        except (OSError, UnicodeDecodeError, qtoml.decoder.TOMLDecodeError) as e:
+            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 154447b..ae1615d 100644
--- a/ganarchy/config.py
+++ b/ganarchy/config.py
@@ -155,3 +155,55 @@ class RemoteConfigSource(ConfigSource):
         for r in CONFIG_PATTERN_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 = []
+
+# class Config:
+#     def __init__(self, toml_file, base=None, remove=True):
+#         self.projects = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(dict))))
+#         config_data = qtoml.load(toml_file)
+#         self.remote_configs = config_data.get('config_srcs', [])
+#         self.title = config_data.get('title', '')
+#         self.base_url = config_data.get('base_url', '')
+#         # TODO blocked domains (but only read them from config_data if remove is True)
+#         self.blocked_domains = []
+#         self.blocked_domain_suffixes = []
+#         self.blocked_domains.sort()
+#         self.blocked_domain_suffixes.sort(key=lambda x: x[::-1])
+#         # FIXME remove duplicates and process invalid entries
+#         self.blocked_domains = tuple(self.blocked_domains)
+#         self.blocked_domain_suffixes = tuple(self.blocked_domain_suffixes) # MUST be tuple
+#         # TODO re.compile("(^" + "|^".join(map(re.escape, domains)) + "|" + "|".join(map(re.escape, suffixes) + ")$")
+#         if base:
+#             # FIXME is remove=remove the right thing to do?
+#             self._update_projects(base.projects, remove=remove, sanitize=False) # already sanitized
+#         projects = config_data.get('projects', {})
+#         self._update_projects(projects, remove=remove)
+# 
+#     def _update_projects(self, projects, remove, sanitize=True):
+#         m = (m_ganarchy_config.CONFIG_PATTERN_SANITIZE if sanitize else m_ganarchy_config.CONFIG_PATTERN).match(projects)
+#         for v in m:
+#             commit, repo_url, branchname, options = v['commit'][0], v['url'][0], v['branch'][0], v['branch'][1]
+#             try:
+#                 u = urlparse(repo_url)
+#                 if not u:
+#                     raise ValueError
+#                 # also raises for invalid ports, see https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse
+#                 # "Reading the port attribute will raise a ValueError if an invalid port is specified in the URL. [...]"
+#                 if u.port == 0:
+#                     raise ValueError
+#                 if u.scheme not in ('http', 'https'):
+#                     raise ValueError
+#                 if (u.hostname in self.blocked_domains) or (u.hostname.endswith(self.blocked_domain_suffixes)):
+#                     raise ValueError
+#             except ValueError:
+#                 continue
+#             if branchname == "HEAD":
+#                 branchname = None
+#             active = options.get('active', None)
+#             if active not in (True, False):
+#                 continue
+#             branch = self.projects[commit][repo_url][branchname]
+#             branch['active'] = active or (branch.get('active', False) and not remove)