mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
Finished globalconf rework (I hope?)
This commit is contained in:
parent
e006a10f70
commit
e94607dc2c
@ -4,6 +4,19 @@ from doreah.configuration import Configuration
|
|||||||
from doreah.configuration import types as tp
|
from doreah.configuration import types as tp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# if DATA_DIRECTORY is specified, this is the directory to use for EVERYTHING, no matter what
|
||||||
|
# but with asynnetrical structure, cache and logs in subfolders
|
||||||
|
# otherwise, each directory is treated seperately
|
||||||
|
# in that case, individual settings for each are respected
|
||||||
|
# DIRECRORY_CONFIG, DIRECRORY_STATE, DIRECTORY_LOGS and DIRECTORY_CACHE
|
||||||
|
# config can only be determined by environment variable, the others can be loaded
|
||||||
|
# from the config files
|
||||||
|
# explicit settings will always be respected. if there are none:
|
||||||
|
# first check if there is any indication of one of the possibilities being populated already
|
||||||
|
# if not, use the first we have permissions for
|
||||||
|
# after we decide which to use, fix it in settings to avoid future heuristics
|
||||||
|
|
||||||
# USEFUL FUNCS
|
# USEFUL FUNCS
|
||||||
pthj = os.path.join
|
pthj = os.path.join
|
||||||
|
|
||||||
@ -21,31 +34,67 @@ def get_env_vars(key,pathsuffix=[]):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
# get user dirs from environment
|
directory_info = {
|
||||||
user_dirs = {}
|
"config":{
|
||||||
|
"sentinel":"settings",
|
||||||
|
"possible_folders":[
|
||||||
|
"/etc/maloja",
|
||||||
|
os.path.expanduser("~/.local/share/maloja")
|
||||||
|
],
|
||||||
|
"setting":"directory_config"
|
||||||
|
},
|
||||||
|
"cache":{
|
||||||
|
"sentinel":"dummy",
|
||||||
|
"possible_folders":[
|
||||||
|
"/var/cache/maloja",
|
||||||
|
os.path.expanduser("~/.local/share/maloja/cache")
|
||||||
|
],
|
||||||
|
"setting":"directory_cache"
|
||||||
|
},
|
||||||
|
"state":{
|
||||||
|
"sentinel":"scrobbles",
|
||||||
|
"possible_folders":[
|
||||||
|
"/var/lib/maloja",
|
||||||
|
os.path.expanduser("~/.local/share/maloja")
|
||||||
|
],
|
||||||
|
"setting":"directory_state"
|
||||||
|
},
|
||||||
|
"logs":{
|
||||||
|
"sentinel":"dbfix",
|
||||||
|
"possible_folders":[
|
||||||
|
"/var/log/maloja",
|
||||||
|
os.path.expanduser("~/.local/share/maloja/logs")
|
||||||
|
],
|
||||||
|
"setting":"directory_logs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# function that
|
||||||
|
# 1) checks if folder has been specified by user
|
||||||
|
# 2) if not, checks if one has been in use before and writes it to dict/config
|
||||||
|
# 3) if not, determines which to use and writes it to dict/config
|
||||||
|
# returns determined folder
|
||||||
|
def find_good_folder(datatype,configobject):
|
||||||
|
info = directory_info[datatype]
|
||||||
|
|
||||||
|
# check each possible folder if its used
|
||||||
|
for p in info['possible_folders']:
|
||||||
|
if os.path.exists(pthj(p,info['sentinel'])):
|
||||||
|
print(p,"has been determined as maloja's folder for",datatype)
|
||||||
|
configobject[info['setting']] = p
|
||||||
|
return p
|
||||||
|
|
||||||
|
print("Could not find previous",datatype,"folder")
|
||||||
|
# check which one we can use
|
||||||
|
for p in info['possible_folders']:
|
||||||
|
if is_dir_usable(p):
|
||||||
|
print(p,"has been selected as maloja's folder for",datatype)
|
||||||
|
configobject[info['setting']] = p
|
||||||
|
return p
|
||||||
|
print("No folder can be used for",datatype)
|
||||||
|
print("This should not happen!")
|
||||||
|
|
||||||
user_dirs["home"] = get_env_vars("HOME")
|
|
||||||
user_dirs["config"] = get_env_vars("XDG_CONFIG_HOME",['maloja'])
|
|
||||||
user_dirs["cache"] = get_env_vars("XDG_CACHE_HOME",['maloja'])
|
|
||||||
user_dirs["data"] = get_env_vars("XDG_DATA_HOME",['maloja'])
|
|
||||||
try:
|
|
||||||
user_data_dir = os.environ["XDG_DATA_HOME"].split(":")[0]
|
|
||||||
assert os.path.exists(user_data_dir)
|
|
||||||
except:
|
|
||||||
user_data_dir = os.path.join(os.environ["HOME"],".local/share/")
|
|
||||||
user_data_dir = pthj(user_data_dir,"maloja")
|
|
||||||
|
|
||||||
# if DATA_DIRECTORY is specified, this is the directory to use for EVERYTHING, no matter what
|
|
||||||
# but with asynnetrical structure, cache and logs in subfolders
|
|
||||||
# otherwise, each directory is treated seperately
|
|
||||||
# in that case, individual settings for each are respected
|
|
||||||
# DIRECRORY_CONFIG, DIRECRORY_STATE, DIRECTORY_LOGS and DIRECTORY_CACHE
|
|
||||||
# config can only be determined by environment variable, the others can be loaded
|
|
||||||
# from the config files
|
|
||||||
# explicit settings will always be respected. if there are none:
|
|
||||||
# first check if there is any indication of one of the possibilities being populated already
|
|
||||||
# if not, use the first we have permissions for
|
|
||||||
# after we decide which to use, fix it in settings to avoid future heuristics
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -53,25 +102,13 @@ user_data_dir = pthj(user_data_dir,"maloja")
|
|||||||
# environment variables
|
# environment variables
|
||||||
maloja_dir_config = os.environ.get("MALOJA_DATA_DIRECTORY") or os.environ.get("MALOJA_DIRECTORY_CONFIG")
|
maloja_dir_config = os.environ.get("MALOJA_DATA_DIRECTORY") or os.environ.get("MALOJA_DIRECTORY_CONFIG")
|
||||||
|
|
||||||
|
|
||||||
if maloja_dir_config is None:
|
if maloja_dir_config is None:
|
||||||
potentialpaths = [
|
maloja_dir_config = find_good_folder('config',{})
|
||||||
"/etc/maloja",
|
found_new_config_dir = True
|
||||||
user_data_dir
|
else:
|
||||||
]
|
found_new_config_dir = False
|
||||||
# check if it exists anywhere else
|
# remember whether we had to find our config dir or it was user-specified
|
||||||
for pth in potentialpaths:
|
|
||||||
if os.path.exists(pthj(pth,"settings")):
|
|
||||||
maloja_dir_config = pth
|
|
||||||
break
|
|
||||||
# new installation, pick where to put it
|
|
||||||
else:
|
|
||||||
# test if we can write to that location
|
|
||||||
for pth in potentialpaths:
|
|
||||||
if is_dir_usable(pth):
|
|
||||||
maloja_dir_config = pth
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
print("Could not find a proper path to put settings file. Please check your permissions!")
|
|
||||||
|
|
||||||
oldsettingsfile = pthj(maloja_dir_config,"settings","settings.ini")
|
oldsettingsfile = pthj(maloja_dir_config,"settings","settings.ini")
|
||||||
newsettingsfile = pthj(maloja_dir_config,"settings.ini")
|
newsettingsfile = pthj(maloja_dir_config,"settings.ini")
|
||||||
@ -156,72 +193,52 @@ malojaconfig = Configuration(
|
|||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
malojaconfig["DIRECTORY_CONFIG"] = maloja_dir_config
|
if found_new_config_dir:
|
||||||
|
malojaconfig["DIRECTORY_CONFIG"] = maloja_dir_config
|
||||||
|
# this really doesn't matter because when are we gonna load info about where
|
||||||
|
# the settings file is stored from the settings file
|
||||||
|
# but oh well
|
||||||
|
|
||||||
|
malojaconfig.render_help(pthj(maloja_dir_config,"settings.md"),
|
||||||
|
top_text='''If you wish to adjust settings in the settings.ini file, do so while the server
|
||||||
|
is not running in order to avoid data being overwritten.
|
||||||
|
|
||||||
|
Technically, each setting can be set via environment variable or the settings
|
||||||
|
file - simply add the prefix `MALOJA_` for environment variables. It is recommended
|
||||||
|
to use the settings file where possible and not configure each aspect of your
|
||||||
|
server via environment variables!''')
|
||||||
|
|
||||||
|
|
||||||
### STEP 3 - check all possible folders for files (old installation)
|
### STEP 3 - check all possible folders for files (old installation)
|
||||||
|
|
||||||
directory_info = {
|
|
||||||
"cache":{
|
|
||||||
"sentinel":"dummy",
|
|
||||||
"possible_folders":[
|
|
||||||
"/var/cache/maloja",
|
|
||||||
"$HOME/.local/share/maloja/cache"
|
|
||||||
],
|
|
||||||
"setting":"directory_cache"
|
|
||||||
},
|
|
||||||
"state":{
|
|
||||||
"sentinel":"scrobbles",
|
|
||||||
"possible_folders":[
|
|
||||||
"/var/lib/maloja",
|
|
||||||
"$HOME/.local/share/maloja"
|
|
||||||
],
|
|
||||||
"setting":"directory_state"
|
|
||||||
},
|
|
||||||
"logs":{
|
|
||||||
"sentinel":"dbfix",
|
|
||||||
"possible_folders":[
|
|
||||||
"/var/log/maloja",
|
|
||||||
"$HOME/.local/share/maloja/logs"
|
|
||||||
],
|
|
||||||
"setting":"directory_logs"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for datatype in directory_info:
|
|
||||||
info = directory_info[datatype]
|
|
||||||
|
|
||||||
# check if we already have a user-specified setting
|
for datatype in ("state","cache","logs"):
|
||||||
# default obv shouldn't count here, so use get_specified
|
# obviously default values shouldn't trigger this
|
||||||
if malojaconfig.get_specified(info['setting']) is None and malojaconfig.get_specified('DATA_DIRECTORY') is None:
|
# if user has nothing specified, we need to use this
|
||||||
# check each possible folder if its used
|
if malojaconfig.get_specified(directory_info[datatype]['setting']) is None and malojaconfig.get_specified('DATA_DIRECTORY') is None:
|
||||||
for p in info['possible_folders']:
|
find_good_folder(datatype,malojaconfig)
|
||||||
if os.path.exists(pthj(p,info['sentinel'])):
|
|
||||||
print(p,"has been determined as maloja's folder for",datatype)
|
|
||||||
malojaconfig[info['setting']] = p
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
print("Could not find previous",datatype,"folder")
|
|
||||||
# check which one we can use
|
### STEP 4 - this is where all the guessing about previous installation ends
|
||||||
for p in info['possible_folders']:
|
### we have our definite settings and are now just generating the real
|
||||||
if is_dir_usable(p):
|
### folder names for everything
|
||||||
print(p,"has been selected as maloja's folder for",datatype)
|
|
||||||
malojaconfig[info['setting']] = p
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
print("No folder can be used for",datatype)
|
|
||||||
print("This should not happen!")
|
|
||||||
|
|
||||||
if malojaconfig['DATA_DIRECTORY'] is None:
|
if malojaconfig['DATA_DIRECTORY'] is None:
|
||||||
top_dirs = {
|
dir_settings = {
|
||||||
"config":malojaconfig['DIRECTORY_CONFIG'],
|
"config":malojaconfig['DIRECTORY_CONFIG'],
|
||||||
"state":malojaconfig['DIRECTORY_STATE'],
|
"state":malojaconfig['DIRECTORY_STATE'],
|
||||||
"cache":malojaconfig['DIRECTORY_CACHE'],
|
"cache":malojaconfig['DIRECTORY_CACHE'],
|
||||||
"logs":malojaconfig['DIRECTORY_LOGS'],
|
"logs":malojaconfig['DIRECTORY_LOGS'],
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
top_dirs = {
|
dir_settings = {
|
||||||
"config":malojaconfig['DATA_DIRECTORY'],
|
"config":malojaconfig['DATA_DIRECTORY'],
|
||||||
"state":malojaconfig['DATA_DIRECTORY'],
|
"state":malojaconfig['DATA_DIRECTORY'],
|
||||||
"cache":pthj(malojaconfig['DATA_DIRECTORY'],"cache"),
|
"cache":pthj(malojaconfig['DATA_DIRECTORY'],"cache"),
|
||||||
@ -231,16 +248,16 @@ else:
|
|||||||
|
|
||||||
|
|
||||||
data_directories = {
|
data_directories = {
|
||||||
"auth":pthj(top_dirs['state'],"auth"),
|
"auth":pthj(dir_settings['state'],"auth"),
|
||||||
"backups":pthj(top_dirs['state'],"backups"),
|
"backups":pthj(dir_settings['state'],"backups"),
|
||||||
"images":pthj(top_dirs['state'],"images"),
|
"images":pthj(dir_settings['state'],"images"),
|
||||||
"scrobbles":pthj(top_dirs['state'],"scrobbles"),
|
"scrobbles":pthj(dir_settings['state'],"scrobbles"),
|
||||||
"rules":pthj(top_dirs['config'],"rules"),
|
"rules":pthj(dir_settings['config'],"rules"),
|
||||||
"clients":pthj(top_dirs['config'],"clients"),
|
"clients":pthj(dir_settings['config'],"clients"),
|
||||||
"settings":pthj(top_dirs['config']),
|
"settings":pthj(dir_settings['config']),
|
||||||
"css":pthj(top_dirs['config'],"custom_css"),
|
"css":pthj(dir_settings['config'],"custom_css"),
|
||||||
"logs":pthj(top_dirs['logs']),
|
"logs":pthj(dir_settings['logs']),
|
||||||
"cache":pthj(top_dirs['cache']),
|
"cache":pthj(dir_settings['cache']),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -276,3 +293,8 @@ config(
|
|||||||
)
|
)
|
||||||
|
|
||||||
settingsconfig._readpreconfig()
|
settingsconfig._readpreconfig()
|
||||||
|
|
||||||
|
# what the fuck did i just write
|
||||||
|
# this spaghetti file is proudly sponsored by the rice crackers i'm eating at the
|
||||||
|
# moment as well as some cute chinese girl whose asmr i'm listening to in the
|
||||||
|
# background. and now to bed!
|
||||||
|
Loading…
Reference in New Issue
Block a user