Add version to config file

This commit is contained in:
Gobinath 2016-11-15 09:56:48 +05:30
parent 98344aaa93
commit 03f612d5e9
2 changed files with 46 additions and 7 deletions

View File

@ -1,4 +1,7 @@
{
"meta": {
"config_version": 1
},
"break_interval": 15,
"long_break_duration": 60,
"no_of_short_breaks_per_long_break": 5,

View File

@ -49,6 +49,7 @@ system_style_sheet_path = os.path.join(bin_directory, "config/style/safeeyes_sty
system_language_directory = os.path.join(bin_directory, "config/lang")
is_active = True
CONFIGURATION_VERSION = 1
def show_settings():
logging.info("Show Settings dialog")
@ -121,7 +122,8 @@ def disable_safeeyes():
is_active = False
core.toggle_active_state()
def prepare_local_config_dir():
def initialize_config():
global config
config_dir_path = os.path.join(os.path.expanduser('~'), '.config/safeeyes/style')
startup_dir_path = os.path.join(os.path.expanduser('~'), '.config/autostart')
try:
@ -152,23 +154,57 @@ def prepare_local_config_dir():
if not os.path.isfile(style_sheet_path):
shutil.copy2(system_style_sheet_path, style_sheet_path)
# Read the configuration
with open(config_file_path) as config_file:
config = json.load(config_file)
"""
Configuration file has a version config_version.
It is used to overwrite the exsiting config file if there is an update.
Earlier versions did not have this attribute so the following method
checks the version and if it mismatches, it will overwrite the exsiting
config files. If the version property is not available, the file is
considered as an older one and replaced by the new configuration file.
"""
def validate_config():
version_mismatch = False
try:
# Check the config version
config_version = config['meta']['config_version']
version_mismatch = config_version is not CONFIGURATION_VERSION
except:
version_mismatch = True
if version_mismatch:
# Remove ~/.config/safeeyes directory
try:
shutil.rmtree(os.path.join(os.path.expanduser('~'), '.config/safeeyes'), ignore_errors=False)
except:
pass
# Remove startup script
try:
os.remove("/usr/share/applications/safeeyes.desktop")
except:
pass
# Create config files again
initialize_config()
def main():
prepare_local_config_dir()
initialize_config()
# Configure logging. Reset with every restart
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', filename=log_file_path, filemode='w', level=logging.INFO)
logging.info("Starting Safe Eyes")
validate_config()
global break_screen
global core
global notification
global config
global tray_icon
global language
# Read the configuration
with open(config_file_path) as config_file:
config = json.load(config_file)
language_file_path = os.path.join(system_language_directory, str(config['language']) + '.json')
with open(language_file_path) as language_file: