From ea35e121d9422a298c95ac4f71110d7d98f4e1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Thu, 9 Jun 2022 15:31:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=86=D0=B5=D1=81=D1=81=20?= =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B0=20+=20?= =?UTF-8?q?=D1=81=D0=B2=D0=B5=D0=B6=D0=B5=D0=BD=D1=8C=D0=BA=D0=BE=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution.py | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/solution.py b/solution.py index 4be34d5..e25c893 100644 --- a/solution.py +++ b/solution.py @@ -5,36 +5,53 @@ from typing import List import os import configparser import aiohttp +import aiofiles async def write_config_files( - servers: list, path_to_template_file: str, path_for_config: str + hosts: list, path_to_template_file: str, path_to_config_dir: str, template: str ) -> None: + """Записываем конфиги для списка hosts.""" + full_path_to_config_dir: str = os.path.abspath(path_to_config_dir) + if not os.path.isdir(full_path_to_config_dir): + os.mkdir(full_path_to_config_dir) + + for host in hosts: + config_filename: str = f"{host}.conf" + config_file = os.path.join(full_path_to_config_dir, config_filename) + + if not os.path.isfile(config_file): + config_body: str = template.replace( + "server_name _;", f"server_name {host}.server.com;" + ) + async with aiofiles.open(config_file, mode="w") as file: + await file.write(config_body) + + +def _get_template(templ_file: str) -> str: + """Возвращает шаблон конфига для хоста из файла `templ_file`.""" + template: str = "" - with open(path_to_template_file, "r") as file: + with open(templ_file, "r") as file: template = file.read() - config_full_path: str = os.path.abspath(path_for_config) - if not os.path.isdir(config_full_path): - os.mkdir(config_full_path) + return template - for server in servers: - config_body: str = template.replace( - "server_name _;", f"server_name {server}.server.com;" - ) - config_filename: str = f"{server}.conf" - - with open(os.path.join(config_full_path, config_filename), "w") as file: - file.write(config_body) +async def get_records_count() -> int: + async with aiohttp.ClientSession() as session: + pass +# изменить сигнатуру на (cfg: Conifg, json_body: dict) async def send_async_request(cfg: dict, columns: list, limit: int = 1) -> None: body = {"columns": columns, "limit": 1} server = cfg["central_host_url"] path_to_template_file = cfg["template"] path_for_config = cfg["path_for_config"] + template: str = _get_template(path_to_template_file) + async with aiohttp.ClientSession() as session: tasks = [session.get(server, json=body) for i in range(limit)] responses = await asyncio.gather(*tasks) @@ -42,8 +59,9 @@ async def send_async_request(cfg: dict, columns: list, limit: int = 1) -> None: for response in responses: r = await response.json() hosts = [i["hostname"] for i in r.get("result")] - - await write_config_files(hosts, path_to_template_file, path_for_config) + await write_config_files( + hosts, path_to_template_file, path_for_config, template + ) def read_config(path_to_conf_file: str, section: str = "Main") -> dict: @@ -84,7 +102,7 @@ async def main() -> None: wait_sec: int = int(cnf["frequency_sec"]) while True: - await send_async_request(cnf, columns=["hostname"], limit=9999) + await send_async_request(cnf, columns=["hostname"], limit=10) await asyncio.sleep(wait_sec)