tools.win_pg_dump_controller
5:7de4fb4ef2f5
Go to Latest
tools.win_pg_dump_controller/win_pg_dump_controller/config.py
. Не тот порт по умолчанию для postgres
2 from configparser import ConfigParser, SectionProxy
4 from os.path import isdir
5 from typing import Optional, Iterable
8 from .error import Error
11 class ConfigError(Error):
15 COMMON_SECTION = 'common'
18 DEFAULT_PG_PORT = 5432
22 def __init__(self, task_name: str, host_name: str, db_name: str, user_name: str, passwd: Optional[str],
23 dst_dir: str, port: int = DEFAULT_PG_PORT):
24 self.host_name = host_name
25 self.db_name = db_name
26 self.user_name = user_name
28 self.dst_dir = dst_dir
32 if not (self.db_name or self.user_name or self.dst_dir or self.host_name):
33 raise ConfigError(f'Some important config parameters not set: '
34 f'db_name="{db_name}" '
35 f'user_name="{user_name}" '
36 f'dst_dir="{dst_dir}" '
37 f'host_name="{host_name}"')
40 class CommonTaskDescription(object):
41 def __init__(self, host_name: Optional[str], user_name: Optional[str],
42 passwd: Optional[str], dst_dir: Optional[str], port: int = DEFAULT_PG_PORT):
43 self.user_name = user_name
45 self.dst_dir = dst_dir
46 self.host_name = host_name
49 def parse_section(self, config_section: SectionProxy) -> DBTask:
50 db_name = config_section.get('db_name')
51 user_name = config_section.get('user_name', self.user_name)
52 passwd = config_section.get('passwd', self.passwd)
53 dst_dir = config_section.get('dst_dir', self.dst_dir)
54 host_name = config_section.get('host_name', self.host_name)
55 port = config_section.getint('port', self.port)
57 if not isdir(dst_dir):
58 raise ConfigError(f'Destionation directory not exists for "{config_section.name}": {dst_dir}')
60 return DBTask(task_name=config_section.name, host_name=host_name, db_name=db_name,
61 user_name=user_name, passwd=passwd, port=port, dst_dir=dst_dir)
64 def parse_config(cls, config: ConfigParser) -> Iterable[DBTask]:
65 if not config.has_section(COMMON_SECTION):
66 tmpl = cls(host_name=None, user_name=None, passwd=None, dst_dir=None)
69 _section = config['common']
70 host_name = _section.get('host_name', '127.0.0.1')
71 user_name = _section.get('user_name')
72 passwd = _section.get('passwd')
73 dst_dir = _section.get('dst_dir')
74 port = _section.getint('port', DEFAULT_PG_PORT)
76 tmpl = cls(host_name=host_name, user_name=user_name, passwd=passwd, dst_dir=dst_dir, port=port)
78 for _section in filter(lambda x: x not in (COMMON_SECTION, MAIN, SMTP), config.sections()):
79 yield tmpl.parse_section(config[_section])
87 raise ConfigError(f'No config file specified')
89 _config = ConfigParser()
90 _config.read(config_file)
92 _main_section = _config[MAIN]
93 self.pg_bin_path = _main_section.get('pg_bin_path')
94 self.pg_dump_flags = _main_section.get('pg_dump_flags', '')
95 self.log_dir = _main_section.get('log_dir')
96 self.teir1_days = _main_section.getint('teir1_days', 7)
97 self.teir2_copies_interval = _main_section.getint('teir2_copies_interval', 7)
98 self.tier2_store_days = _main_section.getint('tier2_store_days', 30)
99 self.keep_logs_days = _main_section.getint('keep_logs_days', 30)
101 self.smtp_server = None
102 self.mail_from = None
104 self.smtp_port = None
106 if _config.has_section(SMTP):
107 _smtp_section = _config[SMTP]
108 self.smtp_server = _smtp_section.get('smtp server')
109 self.mail_from = _smtp_section.get('mail from')
110 self.mail_to = _smtp_section.get('mail to', fallback='root')
111 self.smtp_port = _smtp_section.getint('smtp port', fallback=25)
113 if not isdir(self.pg_bin_path):
114 raise ConfigError(f'No valid directory with binnary files of PostgreSQL is set: {self.pg_bin_path}')
116 self.tasks = list(CommonTaskDescription.parse_config(_config))