summary refs log tree commit diff stats
path: root/ganarchy/config.py
blob: 154447b4deaaec6701829b8706460092e7d2a240 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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 abc
import os

import abdl
import qtoml

from enum import Enum

# sanitize = skip invalid entries
# validate = error on invalid entries
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})
CONFIG_REPOS = abdl.compile("->'projects'->commit->url->branch", {'dict': dict})

CONFIG_TITLE_SANITIZE = abdl.compile("""->title'title'?:?$str""", {'str': str})
CONFIG_BASE_URL_SANITIZE = abdl.compile("""->base_url'base_url'?:?$str""", {'str': str})
CONFIG_SRCS_SANITIZE = abdl.compile("""->'config_srcs'?:?$list->src:?$str""", {'list': list, 'str': str})

CONFIG_TITLE_VALIDATE = abdl.compile("""->title'title':$str""", {'str': str})
CONFIG_BASE_URL_VALIDATE = abdl.compile("""->base_url'base_url':$str""", {'str': str})
CONFIG_SRCS_VALIDATE = abdl.compile("""->'config_srcs':$list->src:$str""", {'list': list, 'str': str})

class ConfigProperty(Enum):
    TITLE = 1
    BASE_URL = 2

class ConfigSource(abc.ABC):
    @abc.abstractmethod
    def update(self):
        """Refreshes the config if necessary."""
        pass

    @abc.abstractmethod
    def exists(self):
        """Returns whether the config exists."""
        pass

    def is_domain_blocked(self, domain):
        """Returns whether the given domain is blocked."""
        return False

    def get_remote_config_sources(self):
        """Yields URI strings for additional configs.

        Yields:
            str: A remote config URI.

        """
        yield from ()

    @abc.abstractmethod
    def get_project_commit_tree_paths(self):
        """Yields (project, URI, branch, options) tuples.

        Yields:
            tuple of (str, str, str, dict): A project commit-tree path.

            Composed of a project commit hash, a repo URI, a branch name
            and a dict of options respectively.

        """
        pass

    def get_supported_properties(self):
        """Returns an iterable of properties supported by this config source.

        Returns:
            Iterable of ConfigProperty: Supported properties.

        """
        return ()

    def get_property_value(self, prop):
        """Returns the value associated with the given property.

        Args:
            prop (ConfigProperty): The property.

        Returns:
            The value associated with the given property.

        Raises:
            ValueError: If the property is not supported by this config
            source.

        """
        raise ValueError

class FileConfigSource(ConfigSource):
    SUPPORTED_PROPERTIES = {}

    def __init__(self, filename):
        self.file_exists = False
        self.last_updated = None
        self.filename = filename
        self.tomlobj = None

    def update(self):
        try:
            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:
                    self.tomlobj = qtoml.load(f)
            self.file_exists = True
        except OSError:
            return

    def exists(self):
        return self.file_exists

    def get_remote_config_sources(self):
        for r in CONFIG_SRCS_SANITIZE.match(self.tomlobj):
            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])

    @classmethod
    def get_supported_properties(cls):
        return cls.SUPPORTED_PROPERTIES

class RemoteConfigSource(ConfigSource):
    def __init__(self, uri):
        self.uri = uri
        self.tomlobj = None
        self.remote_exists = False

    def update(self):
        raise NotImplementedError

    def exists(self):
        return self.remote_exists

    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])