From 78aa9079158cbbf3c01facb4a3f1a76358b4258a Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Thu, 30 Apr 2020 19:50:24 -0300 Subject: Fix bugs in the new config system --- ganarchy/__init__.py | 1 + ganarchy/cli/debug.py | 14 ++++++++++++++ ganarchy/data.py | 35 ++++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/ganarchy/__init__.py b/ganarchy/__init__.py index 42ff1ea..c9ff942 100644 --- a/ganarchy/__init__.py +++ b/ganarchy/__init__.py @@ -33,6 +33,7 @@ from collections import defaultdict from urllib.parse import urlparse import ganarchy as m_ganarchy +from ganarchy import data as m_data MIGRATIONS = { "toml-config": ( diff --git a/ganarchy/cli/debug.py b/ganarchy/cli/debug.py index 5bff054..95ad045 100644 --- a/ganarchy/cli/debug.py +++ b/ganarchy/cli/debug.py @@ -33,6 +33,20 @@ def paths(): click.echo('Data home: {}'.format(ganarchy.data_home)) def print_data_source(data_source): + if ganarchy.data.DataProperty.INSTANCE_TITLE in data_source.get_supported_properties(): + try: + title = data_source.get_property_value(ganarchy.data.DataProperty.INSTANCE_TITLE) + except LookupError: + title = None + click.echo("\tTitle: {}".format(title)) + + if ganarchy.data.DataProperty.INSTANCE_BASE_URL in data_source.get_supported_properties(): + try: + base_url = data_source.get_property_value(ganarchy.data.DataProperty.INSTANCE_BASE_URL) + except LookupError: + base_url = None + click.echo("\tBase URL: {}".format(base_url)) + if ganarchy.data.DataProperty.REPO_LIST_SOURCES in data_source.get_supported_properties(): click.echo("\tRepo list sources:") try: diff --git a/ganarchy/data.py b/ganarchy/data.py index bc5a1b6..7de0764 100644 --- a/ganarchy/data.py +++ b/ganarchy/data.py @@ -70,7 +70,7 @@ class CommitPredicate(abdl.predicates.Predicate): # validate = error on invalid entries # LEGACY. DO NOT USE. CONFIG_REPOS_SANITIZE = abdl.compile("""->'projects'?:?$dict - ->commit[:?$commit]:?$dict + ->commit[:?$str:?$commit]:?$dict ->url[:?$str:?$uri]:?$dict ->branch:?$dict(->'active'?:?$bool)""", dict(bool=bool, dict=dict, str=str, uri=URIPredicate(), commit=CommitPredicate())) @@ -82,7 +82,7 @@ CONFIG_BASE_URL_SANITIZE = abdl.compile("""->base_url'base_url'?:?$str:?$uri""", # they still skip "bad" entries, just like the old matchers. _MATCHER_REPOS = abdl.compile("""->'projects':$dict - ->commit[:?$commit]:?$dict + ->commit[:?$str:?$commit]:?$dict ->url[:?$str:?$uri]:?$dict ->branch:?$dict(->'active'?:?$bool)""", dict(bool=bool, dict=dict, str=str, uri=URIPredicate(), commit=CommitPredicate())) @@ -95,6 +95,12 @@ _MATCHER_REPO_LIST_SRCS = abdl.compile("""->'repo_list_srcs':$dict # ->commit/[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/?:?$dict # """, {'dict': dict}) # FIXME check for aliases, might require changes to abdl +# TODO +#_MATCHER_URI_FILTERS = abdl.compile("""->'uri_filters':$dict +# ->filter[:?$str]:?$dict +# (->'active'?:?$bool)""", +# dict(dict=dict, str=str, bool=bool)) + _MATCHER_TITLE = abdl.compile("""->title'title':$str""", dict(str=str)) _MATCHER_BASE_URL = abdl.compile("""->base_url'base_url':$str:$uri""", dict(str=str, uri=URIPredicate())) @@ -242,6 +248,10 @@ class DataSource(abc.ABC): """ raise PropertyError +class DummyDataSource(DataSource): + """A DataSource that provides nothing. + """ + class ObjectDataSource(DataSource): """A DataSource backed by a Python object. @@ -292,7 +302,7 @@ class LocalDataSource(ObjectDataSource): updtime = self.last_updated self.last_updated = os.stat(self.filename).st_mtime if not self.file_exists or updtime != self.last_updated: - with open(self.filename) as f: + with open(self.filename, 'r', encoding='utf-8', newline='') as f: self._obj = qtoml.load(f) self.file_exists = True except (OSError, UnicodeDecodeError, qtoml.decoder.TOMLDecodeError) as e: @@ -383,7 +393,7 @@ class ConfigManager(DataSource): def new_default(cls): from ganarchy import config_home, config_dirs srcs = [LocalDataSource(d + "/config.toml") for d in [config_home] + config_dirs] - return cls(srcs) + return cls(srcs + [DefaultsDataSource()]) def exists(self): return True @@ -459,7 +469,7 @@ class RepoListManager(DataSource): pass else: self.sources.extend(RemoteDataSource(rls.uri) for rls in it if rls.active) - for source in self.sources: + for source in self.sources[1:]: excs.append(source.update()) return excs @@ -470,14 +480,17 @@ class RepoListManager(DataSource): if prop not in self.get_supported_properties(): raise PropertyError assert prop == DataProperty.VCS_REPOS - # must raise exceptions *now* - # not when the generator runs - return self._get_vcs_repos(self.config_manager.get_property_values(DataProperty.VCS_REPOS)) + return self._get_vcs_repos() - def _get_vcs_repos(self, it): + def _get_vcs_repos(self): assert self.config_manager == self.sources[0] - # config manager may override repo lists - yield from it + try: + # config manager may override repo lists + iterator = self.config_manager.get_property_values(DataProperty.VCS_REPOS) + except (PropertyError, LookupError): + pass + else: + yield from iterator for source in self.sources: if DataProperty.VCS_REPOS in source.get_supported_properties(): try: -- cgit 1.4.1