From 96023662d3affef05e0d6f697a1dedf9be414572 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 7 Oct 2021 13:26:43 +0300 Subject: [PATCH] move config and manager --- app/__init__.py | 0 app/config.py | 12 ++++ app/manager.py | 136 +++++++++++++++++++++++++++++++++++++++++++++ pub1c-rest.py | 144 +----------------------------------------------- 4 files changed, 150 insertions(+), 142 deletions(-) create mode 100644 app/__init__.py create mode 100644 app/config.py create mode 100644 app/manager.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..0d7e7b3 --- /dev/null +++ b/app/config.py @@ -0,0 +1,12 @@ +import os + +import yaml + + +def load_config(filename: str): + with open(filename, 'r', encoding='utf-8') as cfg_file: + return yaml.safe_load(cfg_file) + + +def config_location() -> str: + return os.getenv('WEBPUB1C_CONFIG', 'config.yml') \ No newline at end of file diff --git a/app/manager.py b/app/manager.py new file mode 100644 index 0000000..5d312d0 --- /dev/null +++ b/app/manager.py @@ -0,0 +1,136 @@ +import logging +import os +from typing import Optional, List, Dict + +from pathvalidate import is_valid_filepath + +from webpub1c.webpub.apache_config import ApacheConfig +from webpub1c.webpub.common import VRDConfig, urlpath_join +from webpub1c.webpub.webpublication import WebPublication + + +def infobase_data_blank(name: str): + return { + 'name': name, + 'publicated': False, + 'url': '', + 'directory': '', + 'vrd_filename': '', + 'infobase_filepath': '', + 'is_file_infobase': False, + } + + +def publication_data(publication: WebPublication): + return { + 'name': publication.name, + 'publicated': True, + 'url': publication.url_path, + 'directory': publication.directory, + 'vrd_filename': publication.vrd_filename, + 'infobase_filepath': publication.infobase_filepath, + 'is_file_infobase': publication.is_file_infobase(), + } + + +class PublicationManager: + def __init__(self, config, verbose: bool = False): + level = logging.INFO if verbose else logging.WARNING + logging.basicConfig(level=level) + self._log = logging.getLogger("manager") + self._log.setLevel(level) + + self._config = config + + vrd_params: Optional[VRDConfig] = self._config.get('vrd_params', None) + apache_config: str = self._config.get('apache_config', '') + self._vrd_path: str = self._config.get('vrd_path', '') + self._dir_path: str = self._config.get('dir_path', '') + self._url_base: str = self._config.get('url_base', '') + + self._apache_cfg = ApacheConfig(apache_config, self._vrd_path, + self._dir_path, self._url_base, + vrd_params) + + def _is_vrd_path_valid(self) -> bool: + return os.path.isdir(self._vrd_path) + + def _is_dir_path_valid(self) -> bool: + return os.path.isdir(self._dir_path) + + def _is_url_base_valid(self) -> bool: + return is_valid_filepath(self._url_base, platform='posix') and self._url_base.startswith('/') + + def _is_module_valid(self) -> bool: + if 'platform_path' not in self._config: + return False + if 'ws_module' not in self._config: + return False + return os.path.isfile(os.path.join(self._config['platform_path'], self._config['ws_module'])) + + def check(self): + return { + 'is_apache_cfg_valid': self._apache_cfg.is_valid(), + 'is_vrd_path_valid': self._is_vrd_path_valid(), + 'is_dir_path_valid': self._is_dir_path_valid(), + 'is_url_base_valid': self._is_url_base_valid(), + 'is_module_valid': self._is_module_valid() + } + + def has_module(self): + return self._apache_cfg.has_1cws_module() + + def add_module(self): + """ Add 1cws module to apache config """ + + if self._apache_cfg.has_1cws_module(): + self._log.info('config unchanged') + else: + module: str = os.path.join(self._config['platform_path'], self._config['ws_module']) + self._apache_cfg.add_1cws_module(module) + self._log.info('module added') + + def list(self) -> List[str]: + """ List publication names """ + return list(self._apache_cfg.publications) + + def publications(self) -> List[Dict[str, str]]: + """ List of publications """ + + pubs = map(lambda p: publication_data(p), self._apache_cfg.iter()) + return list(pubs) + + def get(self, ibname: str): + """ Get publication info """ + + publication = self._apache_cfg.get_publication(ibname) + if publication is None: + return publication + + return publication_data(publication) + + def add(self, ibname: str, url: Optional[str] = None, file: str = '', force: bool = False) -> str: + """ Add new publication """ + + publication = self._apache_cfg.create_publication(ibname, url, file, force) + self._apache_cfg.add_publication(publication, force) + self._log.info(f'publication added: {ibname}') + return publication.url_path + + def set_url(self, ibname: str, url: str) -> None: + """ Set publication url """ + + publication = self._apache_cfg.get_publication(ibname) + if publication is None: + raise KeyError(f'infobase "{ibname}" not publicated') + + publication.url_path = urlpath_join(self._url_base, url) + self._apache_cfg.remove_publication(publication.name, destroy=False) + self._apache_cfg.add_publication(publication) + self._log.info(f'publication changed: {ibname}') + + def remove(self, ibname: str, force: bool = False): + """ Remove publication """ + + self._apache_cfg.remove_publication(ibname, force=force) + self._log.info(f'publication removed: {ibname}') \ No newline at end of file diff --git a/pub1c-rest.py b/pub1c-rest.py index f0b11e8..79daa8b 100644 --- a/pub1c-rest.py +++ b/pub1c-rest.py @@ -1,149 +1,18 @@ -import logging import os from typing import List, Optional, Dict import traceback -import yaml from flask import Flask, g, render_template from flask_restful import Resource, Api, reqparse, abort from flask_cors import CORS from pathvalidate import is_valid_filepath +from app.config import load_config, config_location +from app.manager import infobase_data_blank, PublicationManager from brackets import get_infobases as br_get_infobases -from webpub1c.webpub.common import VRDConfig, urlpath_join -from webpub1c.webpub.apache_config import ApacheConfig -from webpub1c.webpub.webpublication import WebPublication apache_restart_flagfile = 'restart_apache' - -def infobase_data_blank(name: str): - return { - 'name': name, - 'publicated': False, - 'url': '', - 'directory': '', - 'vrd_filename': '', - 'infobase_filepath': '', - 'is_file_infobase': False, - } - - -def publication_data(publication: WebPublication): - return { - 'name': publication.name, - 'publicated': True, - 'url': publication.url_path, - 'directory': publication.directory, - 'vrd_filename': publication.vrd_filename, - 'infobase_filepath': publication.infobase_filepath, - 'is_file_infobase': publication.is_file_infobase(), - } - - -class PublicationManager: - def __init__(self, config, verbose: bool = False): - level = logging.INFO if verbose else logging.WARNING - logging.basicConfig(level=level) - self._log = logging.getLogger("manager") - self._log.setLevel(level) - - self._config = config - - vrd_params: Optional[VRDConfig] = self._config.get('vrd_params', None) - apache_config: str = self._config.get('apache_config', '') - self._vrd_path: str = self._config.get('vrd_path', '') - self._dir_path: str = self._config.get('dir_path', '') - self._url_base: str = self._config.get('url_base', '') - - self._apache_cfg = ApacheConfig(apache_config, self._vrd_path, - self._dir_path, self._url_base, - vrd_params) - - def _is_vrd_path_valid(self) -> bool: - return os.path.isdir(self._vrd_path) - - def _is_dir_path_valid(self) -> bool: - return os.path.isdir(self._dir_path) - - def _is_url_base_valid(self) -> bool: - return is_valid_filepath(self._url_base, platform='posix') and self._url_base.startswith('/') - - def _is_module_valid(self) -> bool: - if 'platform_path' not in self._config: - return False - if 'ws_module' not in self._config: - return False - return os.path.isfile(os.path.join(self._config['platform_path'], self._config['ws_module'])) - - def check(self): - return { - 'is_apache_cfg_valid': self._apache_cfg.is_valid(), - 'is_vrd_path_valid': self._is_vrd_path_valid(), - 'is_dir_path_valid': self._is_dir_path_valid(), - 'is_url_base_valid': self._is_url_base_valid(), - 'is_module_valid': self._is_module_valid() - } - - def has_module(self): - return self._apache_cfg.has_1cws_module() - - def add_module(self): - """ Add 1cws module to apache config """ - - if self._apache_cfg.has_1cws_module(): - self._log.info('config unchanged') - else: - module: str = os.path.join(self._config['platform_path'], self._config['ws_module']) - self._apache_cfg.add_1cws_module(module) - self._log.info('module added') - - def list(self) -> List[str]: - """ List publication names """ - return list(self._apache_cfg.publications) - - def publications(self) -> List[Dict[str, str]]: - """ List of publications """ - - pubs = map(lambda p: publication_data(p), self._apache_cfg.iter()) - return list(pubs) - - def get(self, ibname: str): - """ Get publication info """ - - publication = self._apache_cfg.get_publication(ibname) - if publication is None: - return publication - - return publication_data(publication) - - def add(self, ibname: str, url: Optional[str] = None, file: str = '', force: bool = False) -> str: - """ Add new publication """ - - publication = self._apache_cfg.create_publication(ibname, url, file, force) - self._apache_cfg.add_publication(publication, force) - self._log.info(f'publication added: {ibname}') - return publication.url_path - - def set_url(self, ibname: str, url: str) -> None: - """ Set publication url """ - - publication = self._apache_cfg.get_publication(ibname) - if publication is None: - raise KeyError(f'infobase "{ibname}" not publicated') - - publication.url_path = urlpath_join(self._url_base, url) - self._apache_cfg.remove_publication(publication.name, destroy=False) - self._apache_cfg.add_publication(publication) - self._log.info(f'publication changed: {ibname}') - - def remove(self, ibname: str, force: bool = False): - """ Remove publication """ - - self._apache_cfg.remove_publication(ibname, force=force) - self._log.info(f'publication removed: {ibname}') - - pub_parser = reqparse.RequestParser() pub_parser.add_argument('url', type=str) pub_parser.add_argument('file', type=str, default='') @@ -159,15 +28,6 @@ remove_parser = reqparse.RequestParser() remove_parser.add_argument('force', type=bool, default=False) -def load_config(filename: str): - with open(filename, 'r', encoding='utf-8') as cfg_file: - return yaml.safe_load(cfg_file) - - -def config_location() -> str: - return os.getenv('WEBPUB1C_CONFIG', 'config.yml') - - def get_config(): if 'config' not in g: g.config = load_config(config_location())