From 92881093ae19e4d76193447c187028aee5cbe4c7 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 16 Nov 2020 17:03:07 +0100 Subject: getting the viewer-only version of the site working. flask command to run test server --- animism-align/cli/app/server/viewer.py | 69 +++++++++++++++++++++++++++++++ animism-align/cli/app/settings/app_cfg.py | 3 ++ animism-align/cli/cli.py | 9 +++- animism-align/cli/commands/site/export.py | 6 ++- 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 animism-align/cli/app/server/viewer.py (limited to 'animism-align/cli') diff --git a/animism-align/cli/app/server/viewer.py b/animism-align/cli/app/server/viewer.py new file mode 100644 index 0000000..3eecc4e --- /dev/null +++ b/animism-align/cli/app/server/viewer.py @@ -0,0 +1,69 @@ +import os +import logging +import logging.handlers + +logger = logging.getLogger("") +logger.setLevel(logging.DEBUG) +handler = logging.handlers.RotatingFileHandler("flask.log", + maxBytes=3000000, backupCount=2) +formatter = logging.Formatter( + '[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s') +handler.setFormatter(formatter) +logger.addHandler(handler) +logging.getLogger().addHandler(logging.StreamHandler()) + +from flask import Flask, Blueprint, send_from_directory, request + +from app.settings import app_cfg + +def create_app(script_info=None): + """ + functional pattern for creating the flask app + """ + logging.debug("Starting site viewer...") + # print(app_cfg.SERVER_NAME) + app = Flask(__name__, static_folder=app_cfg.DIR_STATIC_SITE_VIEWER, static_url_path='/') + app.config['SERVER_NAME'] = app_cfg.SERVER_NAME + app.url_map.strict_slashes = False + + # index_html = 'site.html' + + # @app.route('/episode1', methods=['GET']) + # def serve_dir_directory_index(): + # return send_from_directory(app_cfg.DIR_STATIC_SITE_VIEWER, 'episode1/index.html') + + # @app.errorhandler(404) + # def page_not_found(e): + # filename = request.url.replace(app_cfg.SERVER_NAME, '') + # print(filename) + # # return app.send_static_file(index_html), 200 + + # @app.route('/', methods=['GET']) + # def serve_file_in_dir(filename): + # print(filename) + # if os.path.isfile(os.path.join(app_cfg.DIR_STATIC_SITE_VIEWER, filename)): + # return send_from_directory(app_cfg.DIR_STATIC_SITE_VIEWER, filename) + # if os.path.isfile(os.path.join(app_cfg.DIR_STATIC_SITE_VIEWER, filename, 'index.html')): + # filename = os.path.join(filename, 'index.html') + # return send_from_directory(app_cfg.DIR_STATIC_SITE_VIEWER, filename) + # return 404 + + # return app.send_static_file(index_html), 200 + # path = os.path.join(os.path.dirname(__file__), 'site.html') + # with open(path, "r") as f: + # return f.read(), 200 + + # @app.route('/', methods=['GET']) + # def index(): + # return app.send_static_file('site.html') + + @app.route('/favicon.ico') + def favicon(): + return send_from_directory(os.path.join(app.root_path, 'static/img/'), + 'favicon.ico',mimetype='image/vnd.microsoft.icon') + + @app.shell_context_processor + def shell_context(): + return { 'app': app, 'db': db } + + return app diff --git a/animism-align/cli/app/settings/app_cfg.py b/animism-align/cli/app/settings/app_cfg.py index 963e265..de5cb6e 100644 --- a/animism-align/cli/app/settings/app_cfg.py +++ b/animism-align/cli/app/settings/app_cfg.py @@ -35,6 +35,7 @@ CLICK_GROUPS = { 'site': 'commands/site', 'db': '', 'flask': '', + 'viewer': '', } @@ -63,6 +64,8 @@ if 'cli' in os.getcwd(): else: DIR_STATIC = os.path.abspath('static') +DIR_STATIC_SITE_VIEWER = join(DIR_DATA_STORE, 'exports/animism') + HASH_TREE_DEPTH = 3 # for sha256 subdirs HASH_BRANCH_SIZE = 3 # for sha256 subdirs diff --git a/animism-align/cli/cli.py b/animism-align/cli/cli.py index 2158398..0913cd1 100755 --- a/animism-align/cli/cli.py +++ b/animism-align/cli/cli.py @@ -15,7 +15,7 @@ if __name__ == '__main__': # argparse: intercept group argv_tmp = sys.argv sys.argv = sys.argv[:2] - ap = argparse.ArgumentParser('\033[1m\033[94mSwimmer\033[0m') + ap = argparse.ArgumentParser('\033[1m\033[94mAnimism\033[0m') ap.add_argument('group', choices=app_cfg.CLICK_GROUPS.keys()) args = ap.parse_args() sys.argv = argv_tmp @@ -29,6 +29,13 @@ if __name__ == '__main__': cli = FlaskGroup(create_app=create_app) + elif args.group == 'viewer': + + from flask.cli import FlaskGroup + from app.server.viewer import create_app + + cli = FlaskGroup(create_app=create_app) + elif args.group == 'db': import re diff --git a/animism-align/cli/commands/site/export.py b/animism-align/cli/commands/site/export.py index f4cc23e..f9560c6 100644 --- a/animism-align/cli/commands/site/export.py +++ b/animism-align/cli/commands/site/export.py @@ -10,7 +10,7 @@ import os @click.command('info') # @click.option('-g', '--graph', 'opt_graph_path', required=True, # help='Graph name') -@click.option('-o', '--output', 'opt_output_dir', required=False, +@click.option('-o', '--output', 'opt_output_dir', required=False, default="animism", help='Output directory') @click.pass_context def cli(ctx, opt_output_dir): @@ -20,6 +20,7 @@ def cli(ctx, opt_output_dir): # imports import datetime + import json from distutils.dir_util import copy_tree # ------------------------------------------------ @@ -45,7 +46,7 @@ def cli(ctx, opt_output_dir): prune_db(db) media_to_copy = rewrite_db_media(db, site_fp_media, media_url) - db['urls'] = { 'audio': 'media/animism_episode_01.mp3' } + db['urls'] = { 'audio': '/' + page_name + '/media/animism_episode_01.mp3' } media_to_copy['audio'] = { 'src': join(app_cfg.DIR_DATA_STORE, 'peaks/animism_episode_01_2810.mp3'), 'dst': join(site_fp_media, 'animism_episode_01.mp3'), @@ -56,6 +57,7 @@ def cli(ctx, opt_output_dir): index_html = load_text(join(app_cfg.DIR_STATIC, 'site.html'), split=False) index_html = index_html.replace('SITE_PATH', page_url) + index_html = index_html.replace('SITE_JSON', json.dumps(db, separators=(',', ':'))) index_html = index_html.replace('PAGE_TITLE', page_title) index_html = index_html.replace('PAGE_DESCRIPTION', page_desc) index_html = index_html.replace('PLAIN_CONTENT', plain_content(db, site_title)) -- cgit v1.2.3-70-g09d2