pybackive/backive/config/config.py

62 lines
1.6 KiB
Python
Raw Normal View History

2019-01-06 17:34:02 +01:00
import os
import pwd
from ruamel.yaml import YAML
import logging
2019-01-06 21:29:21 +01:00
from backive.core.backup import Backup
from backive.core.device import Device
2019-01-06 17:34:02 +01:00
class Config:
def __init__(self):
2019-01-06 21:29:21 +01:00
self._config = dict()
2019-01-06 17:34:02 +01:00
def find_config(self):
# who are we?
uid = os.getuid()
# name?
user = pwd.getpwuid(uid).pw_name
try:
if uid == 0:
2019-01-06 21:29:21 +01:00
config_file = "/etc/backive.yml"
2019-01-06 17:34:02 +01:00
else:
2019-01-06 21:29:21 +01:00
config_file = os.path.join(os.path.expanduser("~"), ".backive", "backive.yml")
2019-01-06 17:34:02 +01:00
pass
with open(config_file, "r") as cfg:
self._config = YAML().load(cfg)
except Exception as e:
logging.error(e)
2019-01-06 21:29:21 +01:00
def get_devices(self):
devices = []
if self._config.get("devices", None):
data = self._config.get("devices")
for device in data:
devices.append(
Device.instance(
device,
data.get(device)
)
)
return devices
def get_backups(self):
backups = []
if self._config.get("backups", None):
data = self._config.get("backups")
for name in data:
backups.append(
Backup.instance(
name,
data.get(name)
)
)
return backups
def get_globals(self):
if self._config.get("defaults", None):
return self._config.get("defaults")
return {}