tools.win_pg_dump_controller

Yohn Y. 2022-01-30 Child:7c93b0305522

0:be791d354d2a Go to Latest

tools.win_pg_dump_controller/win_pg_dump_controller/config.py

..init

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/win_pg_dump_controller/config.py	Sun Jan 30 22:17:39 2022 +0300
     1.3 @@ -0,0 +1,103 @@
     1.4 +# coding: utf-8
     1.5 +from configparser import ConfigParser, SectionProxy
     1.6 +from sys import argv
     1.7 +from os.path import isdir
     1.8 +from typing import Optional, Iterable
     1.9 +
    1.10 +
    1.11 +from .error import Error
    1.12 +
    1.13 +
    1.14 +class ConfigError(Error):
    1.15 +    pass
    1.16 +
    1.17 +
    1.18 +COMMON_SECTION = 'common'
    1.19 +MAIN = 'main'
    1.20 +DEFAULT_PG_PORT = 5432
    1.21 +
    1.22 +
    1.23 +class DBTask(object):
    1.24 +    def __init__(self, task_name: str, host_name: str, db_name: str, user_name: str, passwd: Optional[str],
    1.25 +                 dst_dir: str, port: int = DEFAULT_PG_PORT):
    1.26 +        self.host_name = host_name
    1.27 +        self.db_name = db_name
    1.28 +        self.user_name = user_name
    1.29 +        self.passwd = passwd
    1.30 +        self.dst_dir = dst_dir
    1.31 +        self.name = task_name
    1.32 +        self.port = port
    1.33 +
    1.34 +        if not (self.db_name or self.user_name or self.dst_dir or self.host_name):
    1.35 +            raise ConfigError(f'Some important config parameters not set: '
    1.36 +                              f'db_name="{db_name}" '
    1.37 +                              f'user_name="{user_name}" '
    1.38 +                              f'dst_dir="{dst_dir}" '
    1.39 +                              f'host_name="{host_name}"')
    1.40 +
    1.41 +
    1.42 +class CommonTaskDescription(object):
    1.43 +    def __init__(self, host_name: Optional[str], user_name: Optional[str],
    1.44 +                 passwd: Optional[str], dst_dir: Optional[str], port: int = DEFAULT_PG_PORT):
    1.45 +        self.user_name = user_name
    1.46 +        self.passwd = passwd
    1.47 +        self.dst_dir = dst_dir
    1.48 +        self.host_name = host_name
    1.49 +        self.port = port
    1.50 +
    1.51 +    def parse_section(self, config_section: SectionProxy) -> DBTask:
    1.52 +        db_name = config_section.get('db_name')
    1.53 +        user_name = config_section.get('user_name', self.user_name)
    1.54 +        passwd = config_section.get('passwd', self.passwd)
    1.55 +        dst_dir = config_section.get('dst_dir', self.dst_dir)
    1.56 +        host_name = config_section.get('host_name', self.host_name)
    1.57 +        port = config_section.getint('port', self.port)
    1.58 +
    1.59 +        if not isdir(dst_dir):
    1.60 +            raise ConfigError(f'Destionation directory not exists for "{config_section.name}": {dst_dir}')
    1.61 +
    1.62 +        return DBTask(task_name=config_section.name, host_name=host_name, db_name=db_name,
    1.63 +                      user_name=user_name, passwd=passwd, port=port, dst_dir=dst_dir)
    1.64 +
    1.65 +    @classmethod
    1.66 +    def parse_config(cls, config: ConfigParser) -> Iterable[DBTask]:
    1.67 +        if not config.has_section(COMMON_SECTION):
    1.68 +            tmpl = cls(host_name=None, user_name=None, passwd=None, dst_dir=None)
    1.69 +
    1.70 +        else:
    1.71 +            _section = config['common']
    1.72 +            host_name = _section.get('host_name', '127.0.0.1')
    1.73 +            user_name = _section.get('user_name')
    1.74 +            passwd = _section.get('passwd')
    1.75 +            dst_dir = _section.get('dst_dir')
    1.76 +            port = _section.getint('port', DEFAULT_PG_PORT)
    1.77 +
    1.78 +            tmpl = cls(host_name=host_name, user_name=user_name, passwd=passwd, dst_dir=dst_dir, port=port)
    1.79 +
    1.80 +        for _section in filter(lambda x: x not in (COMMON_SECTION, MAIN), config.sections()):
    1.81 +            yield tmpl.parse_section(config[_section])
    1.82 +
    1.83 +
    1.84 +class Config(object):
    1.85 +    def __init__(self):
    1.86 +        try:
    1.87 +            config_file = argv[1]
    1.88 +        except IndexError:
    1.89 +            raise ConfigError(f'No config file specified')
    1.90 +
    1.91 +        _config = ConfigParser()
    1.92 +        _config.read(config_file)
    1.93 +
    1.94 +        _main_section = _config[MAIN]
    1.95 +        self.pg_bin_path = _main_section.get('pg_bin_path')
    1.96 +        self.pg_dump_flags = _main_section.get('pg_dump_flags', '')
    1.97 +        self.log_dir = _main_section.get('log_dir')
    1.98 +        self.teir1_days = _main_section.getint('teir1_days', 7)
    1.99 +        self.teir2_copies_interval = _main_section.getint('teir2_copies_interval', 7)
   1.100 +        self.tier2_store_days = _main_section.getint('tier2_store_days', 30)
   1.101 +        self.keep_logs_days = _main_section.getint('keep_logs_days', 30)
   1.102 +
   1.103 +        if not isdir(self.pg_bin_path):
   1.104 +            raise ConfigError(f'No valid directory with binnary files of PostgreSQL is set: {self.pg_bin_path}')
   1.105 +
   1.106 +        self.tasks = list(CommonTaskDescription.parse_config(_config))