summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/core/configurable.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/bitmask/core/configurable.py')
-rw-r--r--src/leap/bitmask/core/configurable.py74
1 files changed, 11 insertions, 63 deletions
diff --git a/src/leap/bitmask/core/configurable.py b/src/leap/bitmask/core/configurable.py
index f305cc3c..51a100d6 100644
--- a/src/leap/bitmask/core/configurable.py
+++ b/src/leap/bitmask/core/configurable.py
@@ -17,22 +17,13 @@
"""
Configurable Backend for Bitmask Service.
"""
-import ConfigParser
-import os
-
from twisted.application import service
-from leap.common import files
-from leap.common.config import get_path_prefix
-
-
-DEFAULT_BASEDIR = os.path.join(get_path_prefix(), 'leap')
-
-
-class MissingConfigEntry(Exception):
- """
- A required config entry was not found.
- """
+from leap.bitmask.config import (
+ Configuration,
+ DEFAULT_BASEDIR,
+ MissingConfigEntry
+)
class ConfigurableService(service.MultiService):
@@ -42,59 +33,14 @@ class ConfigurableService(service.MultiService):
def __init__(self, basedir=DEFAULT_BASEDIR):
service.MultiService.__init__(self)
-
- path = os.path.abspath(os.path.expanduser(basedir))
- if not os.path.isdir(path):
- files.mkdir_p(path)
- self.basedir = path
-
- # creates self.config
- self.read_config()
+ self.cfg = Configuration(self.config_file, basedir, DEFAULT_CONFIG)
+ self.basedir = basedir
def get_config(self, section, option, default=None, boolean=False):
- try:
- if boolean:
- return self.config.getboolean(section, option)
-
- item = self.config.get(section, option)
- return item
-
- except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
- if default is None:
- fn = self._get_config_path()
- raise MissingConfigEntry("%s is missing the [%s]%s entry"
- % fn, section, option)
- return default
+ return self.cfg.get(section, option, default=default, boolean=boolean)
def set_config(self, section, option, value):
- if not self.config.has_section(section):
- self.config.add_section(section)
- self.config.set(section, option, value)
- self.save_config()
- self.read_config()
- assert self.config.get(section, option) == value
-
- def read_config(self):
- self.config = ConfigParser.SafeConfigParser()
- bitmaskd_cfg = self._get_config_path()
-
- if not os.path.isfile(bitmaskd_cfg):
- self._create_default_config(bitmaskd_cfg)
-
- with open(bitmaskd_cfg, "rb") as f:
- self.config.readfp(f)
-
- def save_config(self):
- bitmaskd_cfg = self._get_config_path()
- with open(bitmaskd_cfg, 'wb') as f:
- self.config.write(f)
-
- def _create_default_config(self, path):
- with open(path, 'w') as outf:
- outf.write(DEFAULT_CONFIG)
-
- def _get_config_path(self):
- return os.path.join(self.basedir, self.config_file)
+ return self.cfg.set(section, option, value)
DEFAULT_CONFIG = """
@@ -106,3 +52,5 @@ web = True
onion = False
websockets = False
"""
+
+__all__ = ["ConfigurableService", DEFAULT_BASEDIR, MissingConfigEntry]