aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--cgitize/config.py77
-rw-r--r--cgitize/main.py73
2 files changed, 79 insertions, 71 deletions
diff --git a/cgitize/config.py b/cgitize/config.py
new file mode 100644
index 0000000..1226382
--- /dev/null
+++ b/cgitize/config.py
@@ -0,0 +1,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
+
+import cgitize.utils as utils
+
+
+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 utils.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
diff --git a/cgitize/main.py b/cgitize/main.py
index 4aebda1..02e11fb 100644
--- a/cgitize/main.py
+++ b/cgitize/main.py
@@ -4,27 +4,20 @@
# Distributed under the MIT License.
from argparse import ArgumentParser
-import configparser
-import importlib
import logging
-import os.path
import sys
+from cgitize.config import Config
from cgitize.cgit import CGit, Output
import cgitize.utils as utils
-DEFAULT_OUTPUT_DIR = '/var/tmp/cgitize/output'
-DEFAULT_CONFIG_PATH = '/etc/cgitize/cgitize.conf'
-DEFAULT_MY_REPOS_PATH = '/etc/cgitize/my_repos.py'
-
-
def parse_args(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = ArgumentParser()
parser.add_argument('--config', metavar='PATH',
- default=DEFAULT_CONFIG_PATH,
+ default=Config.DEFAULT_PATH,
help='config file path')
parser.add_argument('--repo', metavar='REPO_ID',
nargs='*', dest='repos',
@@ -32,68 +25,6 @@ def parse_args(argv=None):
return parser.parse_args(argv)
-class Config:
- @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 utils.chdir(os.path.dirname(self.path)):
- path = os.path.abspath(path)
- return path
-
- @property
- def output(self):
- path = self.impl.get('DEFAULT', 'output', fallback=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=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
-
-
def main(args=None):
with utils.setup_logging():
args = parse_args(args)