Changeset - 3420f682cc1d
[Not reviewed]
default
0 1 0
Dennis Fink - 10 years ago 2015-07-08 22:22:34

use with statemant for loading the default_json_file
1 file changed with 2 insertions and 1 deletions:
0 comments (0 inline, 0 general)
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')
 

	
 
if not os.path.exists(default_json_file):
 
    raise RuntimeError('default.json does not exists!')
 
elif not os.path.isfile(default_json_file):
 
    raise RuntimeError('defaul.json is not a file!')
 

	
 

	
 
default_json = {}
 
active_json = {}
 

	
 

	
 
def reload_json():
 
    global default_json
 
    global active_json
 

	
 
    default_json = json.load(open(default_json_file, encoding='utf-8'))
 
    with open(default_json_file, encoding='utf-8') as f:
 
        default_json = json.load(f)
 

	
 
    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)
 

	
 

	
 
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)