This repository has been archived on 2020-03-22. You can view files and clone it, but cannot push or open issues/pull-requests.
ThatMinecraftLauncher/lib/launcher/python/cfghandler.py

126 lines
3.8 KiB
Python

import json, os, errno
# self-explanatory
default_config = {}
# This will contain the current configuration after loading the file and
# combining it with the default configuration. All changes will be done here
config = None
# The file to work with
config_file_path = None
################################################################################
# file interaction
################################################################################
# set the config file to use
def set_file_path(file_path):
global config_file_path
# error handling
if os.path.exists(file_path):
if os.path.isdir(file_path):
raise IOError(errno.EISDIR, "Is a directory", file_path)
if os.access(file_path, os.R_OK) and os.access(file_path, os.W_OK):
pass
else:
raise IOError(errno.EACCES, "Read AND write permissions required", file_path)
else:
config_file = open(file_path, "w")
config_file.write("{}")
config_file.close()
# actually setting the file path
config_file_path = file_path
# loads the json file at file_path and updates the default configuration with it
# overwrites any existing configuration adjustments
# TODO: handle None as file name (raise an exception)
def load():
global config_file_path
global config
global default_config
# read the config file
config_file = open(config_file_path,"r")
config_string = config_file.read()
config_file.close()
# combine with default config
config = default_config.copy()
config.update(json.loads(config_string))
# stores the current configuration to the file at file_path
# TODO: handle None (raise an exception)
def store():
global config
global config_file_path
if not config is None:
config_file = open(config_file_path, "w")
config_file.write(json.dumps(config))
config_file.close()
################################################################################
# default config / config setup
# These functions should be used BEFORE loading the config file
################################################################################
#defines a new option group in the default config
# TODO: handle existing groups (raise an exception)
def define_group(group):
global default_config
default_config.update({group : {}})
# defines a new option in the default config
# TODO: handle existing options (raise an exception)
def define_option(group, name, default_value):
global default_config
default_config[group].update({name : default_value})
# self-explanatory
def clear_definitions():
global default_config
default_config = {}
################################################################################
# adjust an existing configuration for new needs
################################################################################
# remove the given option from the configuration and return the value to allow
# for conversion if desired
# TODO: implement
def remove_option(group, name):
pass
# move the value of an option to another place
# TODO: implement
def rename_option(old_group, old_name, new_group, new_name):
pass
# remove an entire group from the configuration, never to be seen again
# TODO: implement
def remove_group(group):
pass
# self-explanatory
# TODO: implement
def rename_group(old_group, new_group):
pass
################################################################################
# config interaction
################################################################################
# get the value of the given option
def fetch(group, name):
global config
if config is None:
load()
return config[group][name]
# set the value of a config option
def set(group, name, value):
global config
if config is None:
load()
config[group][name] = value