Natalie/config/settings.py
2024-11-24 10:28:21 +03:30

60 lines
1.8 KiB
Python

import os
from xdg.BaseDirectory import xdg_config_home
from configparser import ConfigParser
class Config:
def __init__(self):
self.config = ConfigParser()
self.config_dir = os.path.join(xdg_config_home, "natalie")
def check_dir_exists(self):
if not os.path.exists(self.config_dir):
os.makedirs(self.config_dir)
def check_file_exists(self, filename):
config_file = os.path.join(self.config_dir, filename)
return os.path.exists(config_file)
class PortainerConfig(Config):
def __init__(self):
super().__init__()
self.config_file = os.path.join(self.config_dir, "portainer.ini")
self.config.read(self.config_file)
def get_portainer_instances(self):
return self.config.sections()
def set_new_portainer(self, name, base_url, token):
self.config[name] = {"url": base_url, "token": token}
with open(self.config_file, 'w') as configfile:
self.config.write(configfile)
def get_portainer_info(self, name):
base_url = self.config[name]['url']
token = self.config[name]['token']
if base_url and token:
return base_url, token
else:
return None
class DockerConfig(Config):
def __init__(self):
super().__init__()
self.config_file = os.path.join(self.config_dir, 'docker.ini')
self.config.read(self.config_file)
def get_docker_hosts_list(self):
docker_hosts_list = self.config['hosts'].keys()
return list(docker_hosts_list)
def get_docker_host(self, hostname):
return self.config['hosts'][hostname]
def set_new_docker_host(self, hostname, url, port):
self.config['hosts'][hostname] = f"{url}:{port}"
with open(self.config_file, 'w') as configfile:
self.config.write(configfile)