aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cgitize/config.py
blob: 84c7149c210112b0a90615f60fb520d328fc48a8 (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
# Copyright (c) 2021 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "cgitize" project.
# For details, see https://github.com/egor-tensin/cgitize.
# Distributed under the MIT License.

import configparser
import importlib
import os.path
import sys

from cgitize.utils import chdir


class Config:
    DEFAULT_PATH = '/etc/cgitize/cgitize.conf'
    DEFAULT_OUTPUT_DIR = '/var/tmp/cgitize/output'
    DEFAULT_MY_REPOS_PATH = '/etc/cgitize/my_repos.py'

    @staticmethod
    def read(path):
        return Config(path)

    def __init__(self, path):
        self.path = os.path.abspath(path)
        self.impl = configparser.ConfigParser()
        self.impl.read(path)

    def _resolve_relative(self, path):
        if os.path.isabs(path):
            return path
        with chdir(os.path.dirname(self.path)):
            path = os.path.abspath(path)
            return path

    @property
    def output(self):
        path = self.impl.get('DEFAULT', 'output', fallback=Config.DEFAULT_OUTPUT_DIR)
        return self._resolve_relative(path)

    @property
    def clone_url(self):
        return self.impl.get('DEFAULT', 'clone_url', fallback=None)

    @property
    def default_owner(self):
        return self.impl.get('DEFAULT', 'owner', fallback=None)

    @property
    def via_ssh(self):
        return self.impl.getboolean('DEFAULT', 'ssh', fallback=True)

    @property
    def github_username(self):
        return self.impl.get('GITHUB', 'username', fallback=None)

    @property
    def github_access_token(self):
        return self.impl.get('GITHUB', 'access_token', fallback=None)

    @property
    def bitbucket_username(self):
        return self.impl.get('BITBUCKET', 'username', fallback=None)

    @property
    def bitbucket_app_password(self):
        return self.impl.get('BITBUCKET', 'app_password', fallback=None)

    @property
    def my_repos(self):
        path = self.impl.get('DEFAULT', 'my_repos', fallback=Config.DEFAULT_MY_REPOS_PATH)
        return self._resolve_relative(path)

    def import_my_repos(self):
        sys.path.append(os.path.dirname(self.my_repos))
        module_name = os.path.splitext(os.path.basename(self.my_repos))[0]
        module = importlib.import_module(module_name)
        return module.MY_REPOS