add a Config class
This commit is contained in:
parent
d7e05bb76b
commit
c5d883e82d
|
@ -0,0 +1,71 @@
|
|||
import os
|
||||
import configparser
|
||||
import json
|
||||
|
||||
from helper import BorgException
|
||||
|
||||
|
||||
class Config():
|
||||
def __init__(self):
|
||||
self.list_values = ['excludes', 'includes']
|
||||
|
||||
def _get_path(self):
|
||||
home = os.environ['HOME']
|
||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
if os.path.exists(os.path.join(home, '.config/borg_qt/borg_qt.conf')):
|
||||
return os.path.join(home, '.config/borg_qt/borg_qt.conf')
|
||||
elif os.path.exists(os.path.join(dir_path, 'borg_qt.conf')):
|
||||
return os.path.join(dir_path, 'borg_qt.conf')
|
||||
else:
|
||||
raise BorgException("Configuration file not found!")
|
||||
|
||||
def _set_environment_variables(self):
|
||||
os.environ['BORG_REPO'] = str(self.repository_path)
|
||||
os.environ['BORG_PASSPHRASE'] = str(self.password)
|
||||
|
||||
def _create_server_path(self):
|
||||
if not self.config['borgqt']['user']:
|
||||
raise BorgException("User is missing in config.")
|
||||
if not self.config['borgqt']['port']:
|
||||
raise BorgException("Port is missing in config.")
|
||||
server_path = (self.config['borgqt']['user']
|
||||
+ "@"
|
||||
+ self.config['borgqt']['server']
|
||||
+ ":"
|
||||
+ self.config['borgqt']['port']
|
||||
+ self.config['borgqt']['repository_path'])
|
||||
return server_path
|
||||
|
||||
def read(self):
|
||||
"""Reads the config file
|
||||
"""
|
||||
self.path = self._get_path()
|
||||
self.config = configparser.ConfigParser()
|
||||
self.config.read(self.path)
|
||||
|
||||
def apply(self):
|
||||
for option, value in self.config.items('borgqt'):
|
||||
setattr(self, option, value)
|
||||
|
||||
for item in self.list_values:
|
||||
setattr(self, item, json.loads(
|
||||
self.config['borgqt'].get(item, '[]')))
|
||||
|
||||
if self.config['borgqt']['server']:
|
||||
self.repository_path = self._create_server_path()
|
||||
self._set_environment_variables()
|
||||
|
||||
def write(self):
|
||||
if self.server:
|
||||
self.config['borgqt']['port'] = self.port
|
||||
self.config['borgqt']['user'] = self.user
|
||||
self.config['borgqt']['server'] = self.server
|
||||
|
||||
for item in self.list_values:
|
||||
self.config['borgqt'][item] = json.dumps(
|
||||
getattr(self, item), indent=4, sort_keys=True)
|
||||
|
||||
self.config['borgqt']['password'] = self.password
|
||||
with open(self.path, 'w+') as configfile:
|
||||
self.config.write(configfile)
|
|
@ -0,0 +1,78 @@
|
|||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import os
|
||||
import configparser
|
||||
|
||||
import context
|
||||
from config import Config
|
||||
from helper import BorgException
|
||||
|
||||
|
||||
class TestConfiguration(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
self.config_path = os.path.join(self.dir_path,
|
||||
'../docs/borg_qt.conf.example')
|
||||
|
||||
def test_read_configuration(self):
|
||||
config = Config()
|
||||
config._get_path = MagicMock(return_value=self.config_path)
|
||||
config.read()
|
||||
parser = configparser.ConfigParser()
|
||||
parser.read(self.config_path)
|
||||
self.assertEqual(parser, config.config)
|
||||
|
||||
@patch('config.os.path')
|
||||
def test_absent_config_file(self, mock_path):
|
||||
mock_path.exists.return_value = False
|
||||
with self.assertRaises(BorgException):
|
||||
config = Config()
|
||||
config._get_path()
|
||||
|
||||
def test_absent_port(self):
|
||||
config = Config()
|
||||
config._get_path = MagicMock(return_value=self.config_path)
|
||||
config.read()
|
||||
config.config['borgqt']['port'] = ""
|
||||
with self.assertRaises(BorgException):
|
||||
config._create_server_path()
|
||||
|
||||
def test_absent_port(self):
|
||||
config = Config()
|
||||
config._get_path = MagicMock(return_value=self.config_path)
|
||||
config.read()
|
||||
config.config['borgqt']['user'] = ""
|
||||
with self.assertRaises(BorgException):
|
||||
config._create_server_path()
|
||||
|
||||
def test_apply_settings(self):
|
||||
config = Config()
|
||||
config._get_path = MagicMock(return_value=self.config_path)
|
||||
config.read()
|
||||
config.apply()
|
||||
self.assertIs(type(config.excludes), list)
|
||||
self.assertEqual(str(config.repository_path), os.environ['BORG_REPO'])
|
||||
self.assertEqual(str(config.password), os.environ['BORG_PASSPHRASE'])
|
||||
|
||||
|
||||
class TestWriteConfiguration(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
self.config_path = os.path.join(self.dir_path,
|
||||
'../docs/borg_qt.conf.example')
|
||||
self.config = Config()
|
||||
self.config._get_path = MagicMock(return_value=self.config_path)
|
||||
self.config.read()
|
||||
self.config.apply()
|
||||
self.config.path = '/tmp/test.conf'
|
||||
|
||||
def tearDown(self):
|
||||
os.remove(self.config.path)
|
||||
|
||||
def test_write_config(self):
|
||||
self.config.write()
|
||||
config = configparser.ConfigParser()
|
||||
config.read(self.config.path)
|
||||
self.assertEqual(self.config.config['borgqt']['password'],
|
||||
config['borgqt']['password'])
|
Loading…
Reference in New Issue