From e169ed03967fc8552c9300467bd99013ad7391df Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Tue, 6 Aug 2013 11:55:44 -0300 Subject: Check if schema exists before load a config. Related to #3310. --- src/leap/common/config/baseconfig.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/leap/common/config/baseconfig.py') diff --git a/src/leap/common/config/baseconfig.py b/src/leap/common/config/baseconfig.py index 699d734..7bf08dd 100644 --- a/src/leap/common/config/baseconfig.py +++ b/src/leap/common/config/baseconfig.py @@ -26,7 +26,7 @@ import os from abc import ABCMeta, abstractmethod -from leap.common.check import leap_assert +from leap.common.check import leap_assert, leap_check from leap.common.files import mkdir_p from leap.common.config.pluggableconfig import PluggableConfig from leap.common.config.prefixers import get_platform_prefixer @@ -34,6 +34,12 @@ from leap.common.config.prefixers import get_platform_prefixer logger = logging.getLogger(__name__) +class NonExistingSchema(Exception): + """ + Raised if the schema needed to verify the config is None. + """ + + class BaseConfig: """ Abstract base class for any JSON based configuration. @@ -112,6 +118,7 @@ class BaseConfig: def load(self, path="", data=None, mtime=None, relative=True): """ Loads the configuration from disk. + It may raise NonExistingSchema exception. :param path: if relative=True, this is a relative path to configuration. The absolute path @@ -131,8 +138,12 @@ class BaseConfig: else: config_path = path + schema = self._get_spec() + leap_check(schema is not None, + "There is no schema to use.", NonExistingSchema) + self._config_checker = PluggableConfig(format="json") - self._config_checker.options = copy.deepcopy(self._get_spec()) + self._config_checker.options = copy.deepcopy(schema) try: if data is None: -- cgit v1.2.3 From 011e8802b248ffa39fa40d4931d2577256d01bd7 Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Tue, 6 Aug 2013 18:20:59 -0300 Subject: Handle schemas and api versions in base class. --- src/leap/common/config/baseconfig.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/leap/common/config/baseconfig.py') diff --git a/src/leap/common/config/baseconfig.py b/src/leap/common/config/baseconfig.py index 7bf08dd..ba87df9 100644 --- a/src/leap/common/config/baseconfig.py +++ b/src/leap/common/config/baseconfig.py @@ -61,13 +61,25 @@ class BaseConfig: def __init__(self): self._data = {} self._config_checker = None + self._api_version = None @abstractmethod + def _get_schema(self): + """ + Returns the schema corresponding to the version given. + + :rtype: dict or None if the version is not supported. + """ + pass + def _get_spec(self): """ Returns the spec object for the specific configuration. """ - return None + leap_assert(self._api_version is not None, + "You should set the API version.") + + return self._get_schema() def _safe_get_value(self, key): """ @@ -79,6 +91,17 @@ class BaseConfig: leap_assert(self._config_checker, "Load the config first") return self._config_checker.config.get(key, None) + def set_api_version(self, version): + """ + Sets the supported api version. + + :param api_version: the version of the api supported by the provider. + :type api_version: str + """ + self._api_version = version + leap_assert(self._get_schema(self._api_version) is not None, + "Version %s is not supported." % (version, )) + def get_path_prefix(self): """ Returns the platform dependant path prefixer -- cgit v1.2.3 From 335d73ee94976300aae156084e8e275e98ee5ea5 Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Wed, 7 Aug 2013 15:23:42 -0300 Subject: Fix old parameter error. --- src/leap/common/config/baseconfig.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/leap/common/config/baseconfig.py') diff --git a/src/leap/common/config/baseconfig.py b/src/leap/common/config/baseconfig.py index ba87df9..e310bc0 100644 --- a/src/leap/common/config/baseconfig.py +++ b/src/leap/common/config/baseconfig.py @@ -75,6 +75,8 @@ class BaseConfig: def _get_spec(self): """ Returns the spec object for the specific configuration. + + :rtype: dict or None if the version is not supported. """ leap_assert(self._api_version is not None, "You should set the API version.") @@ -99,7 +101,7 @@ class BaseConfig: :type api_version: str """ self._api_version = version - leap_assert(self._get_schema(self._api_version) is not None, + leap_assert(self._get_schema() is not None, "Version %s is not supported." % (version, )) def get_path_prefix(self): -- cgit v1.2.3