Changeset - d6a5e1c072ce
[Not reviewed]
default
0 2 0
Dennis Fink - 10 years ago 2015-07-08 22:21:30

call reload_json from __init__.py when creating the app instead of when
importing active.py
2 files changed with 4 insertions and 2 deletions:
0 comments (0 inline, 0 general)
spaceapi/__init__.py
Show inline comments
 
@@ -6,49 +6,52 @@ import base64
 
from flask import Flask
 

	
 
config_file = os.path.abspath('config.json')
 

	
 

	
 
def create_app():
 
    app = Flask(__name__)
 

	
 
    _default_secret_key = base64.b64encode(os.urandom(32)).decode('utf-8')
 
    app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY',
 
                                              _default_secret_key)
 

	
 
    if not hasattr(app.config, 'from_json'):
 
        def from_json(file, silent=True):
 
            try:
 
                with open(file, encoding='utf-8') as json_file:
 
                    obj = json.load(json_file)
 
            except IOError:
 
                if silent:
 
                    return False
 
                raise
 

	
 
            for key in obj:
 
                if key.isupper():
 
                    app.config[key] = obj[key]
 

	
 
            return True
 

	
 
        app.config.from_json = from_json
 

	
 
    app.config.from_json(config_file, silent=True)
 

	
 
    @app.after_request
 
    def add_headers(response):
 
        response.headers.setdefault('Access-Control-Allow-Origin', '*')
 
        response.headers.setdefault('Cache-Control', 'no-cache')
 

	
 
        return response
 

	
 
    from .views import root_views
 
    app.register_blueprint(root_views)
 

	
 
    from .state import state_views
 
    app.register_blueprint(state_views, url_prefix='/state')
 

	
 
    from .sensors import sensors_views
 
    app.register_blueprint(sensors_views, url_prefix='/sensors')
 

	
 
	return app
 
    from .active import reload_json
 
    reload_json()
 

	
 
    return app
spaceapi/active.py
Show inline comments
 
import copy
 
import json
 
import os.path
 

	
 

	
 
default_json_file = os.path.abspath('default.json')
 
last_state_file = os.path.abspath('laststate.json')
 

	
 
default_json = {}
 
active_json = {}
 

	
 
def reload_json():
 
    global default_json
 
    global active_json
 

	
 
    default_json = json.load(open(default_json_file, encoding='utf-8'))
 

	
 
    if os.path.exists(last_state_file):
 
        with open(last_state_file, encoding='utf-8') as f:
 
            active_json = json.load(f)
 

	
 
        if os.path.getmtime(last_state_file) \
 
           < os.path.getmtime(default_json_file):
 
            backup = copy.deepcopy(active_json)
 
            active_json.update(default_json)
 
            active_json['state']['open'] = backup['state']['open']
 
            active_json['state']['lastchange'] = backup['state']['lastchange']
 
    else:
 
        active_json = copy.deepcopy(default_json)
 

	
 
reload_json()
 

	
 
def save_last_state():
 

	
 
    with open(last_state_file, mode='w', encoding='utf-8') as f:
 
        json.dump(active_json, f, sort_keys=True)
0 comments (0 inline, 0 general)