diff --git a/app/glob.py b/app/glob.py new file mode 100644 index 0000000..dbdcbb3 --- /dev/null +++ b/app/glob.py @@ -0,0 +1,18 @@ +from flask import g + +from app.config import load_config, config_location +from app.manager import PublicationManager + + +def get_config(): + if 'config' not in g: + g.config = load_config(config_location()) + + return g.config + + +def get_manager(): + if 'manager' not in g: + g.manager = PublicationManager(get_config()) + + return g.manager \ No newline at end of file diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..3d7f52c --- /dev/null +++ b/app/utils.py @@ -0,0 +1,10 @@ +from typing import Optional + +from flask_restful import abort +from pathvalidate import is_valid_filepath + + +def validate_url(url: Optional[str]): + if url is not None: + if not is_valid_filepath(url, platform='posix'): + abort(400, message='invalid url') \ No newline at end of file diff --git a/pub1c-rest.py b/pub1c-rest.py index 79daa8b..389188b 100644 --- a/pub1c-rest.py +++ b/pub1c-rest.py @@ -1,14 +1,14 @@ import os -from typing import List, Optional, Dict +from typing import List, Dict import traceback -from flask import Flask, g, render_template +from flask import Flask, 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 app.glob import get_config, get_manager +from app.manager import infobase_data_blank +from app.utils import validate_url from brackets import get_infobases as br_get_infobases apache_restart_flagfile = 'restart_apache' @@ -28,20 +28,6 @@ remove_parser = reqparse.RequestParser() remove_parser.add_argument('force', type=bool, default=False) -def get_config(): - if 'config' not in g: - g.config = load_config(config_location()) - - return g.config - - -def get_manager(): - if 'manager' not in g: - g.manager = PublicationManager(get_config()) - - return g.manager - - def load_infobases(config) -> List[str]: result = [] if 'infobases' in config and 'server_file' in config['infobases']: @@ -49,12 +35,6 @@ def load_infobases(config) -> List[str]: return result -def validate_url(url: Optional[str]): - if url is not None: - if not is_valid_filepath(url, platform='posix'): - abort(400, message='invalid url') - - class InfobasesAvailable(Resource): def get(self) -> List[str]: cfg = get_config()