mirror of
https://github.com/Xevion/trivia.git
synced 2026-01-31 00:26:10 -06:00
more configuration implementation, beginnings of demo app mode, switch to flask-apscheduler for proper app context (jank)
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='flask-demo',
|
||||
entry_points= {
|
||||
'con'
|
||||
}
|
||||
)
|
||||
+2
-2
@@ -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()
|
||||
app.run()
|
||||
|
||||
+11
-1
@@ -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
|
||||
|
||||
+18
-7
@@ -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
|
||||
|
||||
+38
-24
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user