From 009475e18d4819d2ada9a53663a1b5537f699a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0abata?= Date: Tue, 28 Jun 2016 14:26:59 +0200 Subject: [PATCH] Add a simple configuration class and a config file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Petr Ĺ abata --- rida.conf | 6 +++++ rida/config.py | 61 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 rida.conf diff --git a/rida.conf b/rida.conf new file mode 100644 index 00000000..609c3057 --- /dev/null +++ b/rida.conf @@ -0,0 +1,6 @@ +[DEFAULT] +system = koji +koji = http://koji.stg.fedoraproject.org/kojihub +db = sqlite:///rida.db +pdc = http://pdc.stg.fedoraproject.org/ +scmurls = ["git://pkgs.stg.fedoraproject.org/modules/"] diff --git a/rida/config.py b/rida/config.py index ce9cf36f..936c8520 100644 --- a/rida/config.py +++ b/rida/config.py @@ -25,8 +25,9 @@ """Configuration handler functions.""" -# TODO: Pick the configuration format -# TODO: Add properties for all the required options. +import os.path +import configparser +import json def from_file(filename=None): """Create the configuration instance from a file. @@ -39,8 +40,17 @@ def from_file(filename=None): filename = "/etc/rida/rida.conf" if not isinstance(filename, str): raise TypeError("The configuration filename must be a string.") + if not os.path.isfile(filename): + raise IOError("The configuration file doesn't exist.") + cp = configparser.ConfigParser(allow_no_value=True) + cp.read(filename) + default = cp["DEFAULT"] conf = Config() - # TODO: Parse the file and set the properties + conf.db = default.get("db") + conf.system = default.get("system") + conf.pdc = default.get("pdc") + conf.koji = default.get("koji") + conf.scmurls = json.loads(default.get("scmurls")) return conf class Config(object): @@ -48,48 +58,57 @@ class Config(object): def __init__(self): """Initialize the Config object.""" - # Buildsystem to use; koji, copr, mock self._system = "" - # SQLAlchemy RDB URL self._db = "" - # PDC URL self._pdc = "" - # Koji URL self._koji = "" @property - def system(): - """Buildsystem to use by the orchestrator.""" + def system(self): + """The buildsystem to use.""" return self._system @system.setter - def system(s): - # XXX: Check if it's one of the supported values - self._system = str(s) + def system(self, s): + s = str(s) + if s not in ("koji"): + raise ValueError("Unsupported buildsystem.") + self._system = s @property - def db(): - """RDB URL for the orchestrator.""" + def db(self): + """RDB URL.""" return self._db @db.setter - def db(s): + def db(self, s): self._db = str(s) @property - def pdc(): - """PDC URL for the orchestrator.""" + def pdc(self): + """PDC URL.""" return self._pdc @pdc.setter - def pdc(s): + def pdc(self, s): self._pdc = str(s) @property - def koji(): - """Koji URL for the orchestrator.""" + def koji(self): + """Koji URL.""" return self._koji @koji.setter - def koji(s): + def koji(self, s): self._koji = str(s) + + @property + def scmurls(self): + """Allowed SCM URLs.""" + return self._scmurls + + @scmurls.setter + def scmurls(self, l): + if not isinstance(l, list): + raise TypeError("scmurls needs to be a list.") + self._scmurls = [str(x) for x in l]