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
awgur@0 1 # coding: utf-8
awgur@0 2 from configparser import ConfigParser, SectionProxy
awgur@0 3 from sys import argv
awgur@0 4 from os.path import isdir
awgur@0 5 from typing import Optional, Iterable
awgur@0 6
awgur@0 7
awgur@0 8 from .error import Error
awgur@0 9
awgur@0 10
awgur@0 11 class ConfigError(Error):
awgur@0 12 pass
awgur@0 13
awgur@0 14
awgur@0 15 COMMON_SECTION = 'common'
awgur@0 16 MAIN = 'main'
awgur@0 17 DEFAULT_PG_PORT = 5432
awgur@0 18
awgur@0 19
awgur@0 20 class DBTask(object):
awgur@0 21 def __init__(self, task_name: str, host_name: str, db_name: str, user_name: str, passwd: Optional[str],
awgur@0 22 dst_dir: str, port: int = DEFAULT_PG_PORT):
awgur@0 23 self.host_name = host_name
awgur@0 24 self.db_name = db_name
awgur@0 25 self.user_name = user_name
awgur@0 26 self.passwd = passwd
awgur@0 27 self.dst_dir = dst_dir
awgur@0 28 self.name = task_name
awgur@0 29 self.port = port
awgur@0 30
awgur@0 31 if not (self.db_name or self.user_name or self.dst_dir or self.host_name):
awgur@0 32 raise ConfigError(f'Some important config parameters not set: '
awgur@0 33 f'db_name="{db_name}" '
awgur@0 34 f'user_name="{user_name}" '
awgur@0 35 f'dst_dir="{dst_dir}" '
awgur@0 36 f'host_name="{host_name}"')
awgur@0 37
awgur@0 38
awgur@0 39 class CommonTaskDescription(object):
awgur@0 40 def __init__(self, host_name: Optional[str], user_name: Optional[str],
awgur@0 41 passwd: Optional[str], dst_dir: Optional[str], port: int = DEFAULT_PG_PORT):
awgur@0 42 self.user_name = user_name
awgur@0 43 self.passwd = passwd
awgur@0 44 self.dst_dir = dst_dir
awgur@0 45 self.host_name = host_name
awgur@0 46 self.port = port
awgur@0 47
awgur@0 48 def parse_section(self, config_section: SectionProxy) -> DBTask:
awgur@0 49 db_name = config_section.get('db_name')
awgur@0 50 user_name = config_section.get('user_name', self.user_name)
awgur@0 51 passwd = config_section.get('passwd', self.passwd)
awgur@0 52 dst_dir = config_section.get('dst_dir', self.dst_dir)
awgur@0 53 host_name = config_section.get('host_name', self.host_name)
awgur@0 54 port = config_section.getint('port', self.port)
awgur@0 55
awgur@0 56 if not isdir(dst_dir):
awgur@0 57 raise ConfigError(f'Destionation directory not exists for "{config_section.name}": {dst_dir}')
awgur@0 58
awgur@0 59 return DBTask(task_name=config_section.name, host_name=host_name, db_name=db_name,
awgur@0 60 user_name=user_name, passwd=passwd, port=port, dst_dir=dst_dir)
awgur@0 61
awgur@0 62 @classmethod
awgur@0 63 def parse_config(cls, config: ConfigParser) -> Iterable[DBTask]:
awgur@0 64 if not config.has_section(COMMON_SECTION):
awgur@0 65 tmpl = cls(host_name=None, user_name=None, passwd=None, dst_dir=None)
awgur@0 66
awgur@0 67 else:
awgur@0 68 _section = config['common']
awgur@0 69 host_name = _section.get('host_name', '127.0.0.1')
awgur@0 70 user_name = _section.get('user_name')
awgur@0 71 passwd = _section.get('passwd')
awgur@0 72 dst_dir = _section.get('dst_dir')
awgur@0 73 port = _section.getint('port', DEFAULT_PG_PORT)
awgur@0 74
awgur@0 75 tmpl = cls(host_name=host_name, user_name=user_name, passwd=passwd, dst_dir=dst_dir, port=port)
awgur@0 76
awgur@0 77 for _section in filter(lambda x: x not in (COMMON_SECTION, MAIN), config.sections()):
awgur@0 78 yield tmpl.parse_section(config[_section])
awgur@0 79
awgur@0 80
awgur@0 81 class Config(object):
awgur@0 82 def __init__(self):
awgur@0 83 try:
awgur@0 84 config_file = argv[1]
awgur@0 85 except IndexError:
awgur@0 86 raise ConfigError(f'No config file specified')
awgur@0 87
awgur@0 88 _config = ConfigParser()
awgur@0 89 _config.read(config_file)
awgur@0 90
awgur@0 91 _main_section = _config[MAIN]
awgur@0 92 self.pg_bin_path = _main_section.get('pg_bin_path')
awgur@0 93 self.pg_dump_flags = _main_section.get('pg_dump_flags', '')
awgur@0 94 self.log_dir = _main_section.get('log_dir')
awgur@0 95 self.teir1_days = _main_section.getint('teir1_days', 7)
awgur@0 96 self.teir2_copies_interval = _main_section.getint('teir2_copies_interval', 7)
awgur@0 97 self.tier2_store_days = _main_section.getint('tier2_store_days', 30)
awgur@0 98 self.keep_logs_days = _main_section.getint('keep_logs_days', 30)
awgur@0 99
awgur@0 100 if not isdir(self.pg_bin_path):
awgur@0 101 raise ConfigError(f'No valid directory with binnary files of PostgreSQL is set: {self.pg_bin_path}')
awgur@0 102
awgur@0 103 self.tasks = list(CommonTaskDescription.parse_config(_config))