diff --git a/setup.py b/setup.py deleted file mode 100644 index 20151cb..0000000 --- a/setup.py +++ /dev/null @@ -1,8 +0,0 @@ -from setuptools import setup - -setup( - name='flask-demo', - entry_points= { - 'con' - } -) \ No newline at end of file diff --git a/trivia/__main__.py b/trivia/__main__.py index 0a3ebdc..285487a 100644 --- a/trivia/__main__.py +++ b/trivia/__main__.py @@ -1,5 +1,5 @@ -from create_app import create_app +from trivia.create_app import create_app if __name__ == '__main__': app = create_app() - app.run() \ No newline at end of file + app.run() diff --git a/trivia/config.py b/trivia/config.py index 6430c2e..a51b0c6 100644 --- a/trivia/config.py +++ b/trivia/config.py @@ -12,11 +12,21 @@ configs = { class Config(object): + # Main Configuration SCORE_FILE = 'scores.json' - DEMO = False + POLLING_INTERVAL = 5 + # Demo Configuration + DEMO = False + DEMO_TEAM_COUNT = 0 + DEMO_ALTERATION_INTERVAL = 0 class DemoConfig(Config): + # Main Configuration SCORE_FILE = 'demo.json' + + # Demo Configuration DEMO = True + DEMO_TEAM_COUNT = 30 + DEMO_ALTERATION_INTERVAL = 15 diff --git a/trivia/create_app.py b/trivia/create_app.py index 66dce45..853fc23 100644 --- a/trivia/create_app.py +++ b/trivia/create_app.py @@ -1,9 +1,9 @@ -from apscheduler.schedulers.background import BackgroundScheduler +from flask_apscheduler import APScheduler from flask import Flask -from trivia import utils from trivia.config import configs +scheduler: APScheduler = None def create_app(env=None): app = Flask(__name__) @@ -12,12 +12,23 @@ def create_app(env=None): env = app.config['ENV'] app.config.from_object(configs[env]) - # Setup a scheduler for automatically refreshing data - scheduler = BackgroundScheduler() - scheduler.start() - scheduler.add_job(func=utils.refreshScores, trigger="interval", seconds=5) - with app.app_context(): + # noinspection PyUnresolvedReferences + from trivia import routes, api, utils + + # Setup a scheduler for automatically refreshing data + global scheduler + scheduler = APScheduler() + scheduler.init_app(app) + scheduler.start() + + # Add score file polling + scheduler.add_job(id='polling', func=utils.refreshScores, trigger="interval", seconds=app.config['POLLING_INTERVAL']) + + if app.config['DEMO']: + app.logger.info('Generating Demo Data...') + utils.generateDemo() + utils.refreshScores() return app diff --git a/trivia/utils.py b/trivia/utils.py index 0e2f545..6537e36 100644 --- a/trivia/utils.py +++ b/trivia/utils.py @@ -9,7 +9,7 @@ from collections import namedtuple from typing import List # Simple fake 'class' for passing to jinja templates -# from trivia import app +import faker as faker from flask import current_app Team = namedtuple('Team', ['id', 'name', 'scores']) @@ -17,7 +17,7 @@ Team = namedtuple('Team', ['id', 'name', 'scores']) # Generate paths BASE_DIR = os.path.dirname(os.path.abspath(__file__)) DATA_DIR = os.path.join(BASE_DIR, 'data') -SCORES_FILE = os.path.join(DATA_DIR, 'scores.json') +SCORES_FILE = os.path.join(DATA_DIR, current_app.config['SCORE_FILE']) # Initialize global data/tracking vars lastChange: int = -1 @@ -41,33 +41,47 @@ def refreshScores() -> None: global lastChange curChange = lastModified() - if lastChange < curChange: - try: - # Update tracking var - lastChange = curChange + from trivia.create_app import scheduler + app = scheduler.app - current_app.logger.debug('Attempting to load and parse scores file.') - with open(SCORES_FILE) as file: - temp = json.load(file) + with app.app_context(): + if lastChange < curChange: + try: + # Update tracking var + lastChange = curChange - # Place all values into Team object for jinja - temp = [ - Team( - id=team['teamno'], - name=team['teamname'], - scores=team['scores'] - ) for team in temp - ] - current_app.logger.debug(f'Successfully loaded ({len(temp)} teams).') + current_app.logger.debug('Attempting to load and parse scores file.') + with open(SCORES_FILE, 'r') as file: + temp = json.load(file) - global teams - teams = temp + # Place all values into Team object for jinja + temp = [ + Team( + id=team['teamno'], + name=team['teamname'], + scores=team['scores'] + ) for team in temp + ] + current_app.logger.debug(f'Successfully loaded ({len(temp)} teams).') - # If invalid or inaccessible, simply do nothing. - except json.JSONDecodeError: - current_app.logger.error('Scores file could not be opened or parsed.', print_exc=True) + global teams + teams = temp + + # If invalid or inaccessible, simply do nothing. + except json.JSONDecodeError: + current_app.logger.error('Scores file could not be opened or parsed.', print_exc=True) def generateDemo() -> None: - pass + fake = faker.Faker() + data = [ + { + 'teamno': i + 1, + 'teamname': fake.user_name(), + 'scores': [] + } for i in range(current_app.config['DEMO_TEAM_COUNT']) + ] + + with open(SCORES_FILE, 'w') as file: + json.dump(data, file)