From 4186171b8fff984bc189b8cfaa2816d8a26c50fb Mon Sep 17 00:00:00 2001 From: krateng Date: Sun, 19 Dec 2021 21:10:55 +0100 Subject: [PATCH] Replaced settings calls --- maloja/apis/listenbrainz.py | 4 ++-- maloja/apis/native_v1.py | 4 ++-- maloja/cleanup.py | 14 +++++++------- maloja/database.py | 16 ++++++++-------- maloja/malojatime.py | 7 ++++--- maloja/proccontrol/control.py | 2 +- maloja/proccontrol/setup.py | 24 ++++++++++++------------ maloja/proccontrol/supervisor.py | 4 ++-- maloja/server.py | 10 +++++----- maloja/thirdparty/__init__.py | 12 ++++++------ maloja/utilities/images.py | 10 +++++----- maloja/utilities/maintenance.py | 6 +++--- 12 files changed, 57 insertions(+), 56 deletions(-) diff --git a/maloja/apis/listenbrainz.py b/maloja/apis/listenbrainz.py index 9948148..80f8ba1 100644 --- a/maloja/apis/listenbrainz.py +++ b/maloja/apis/listenbrainz.py @@ -3,7 +3,7 @@ from ._exceptions import * from .. import database import datetime -from doreah.settings import get_settings +from .globalconf import malojaconfig class Listenbrainz(APIHandler): @@ -72,7 +72,7 @@ class Listenbrainz(APIHandler): if token not in database.allAPIkeys(): raise InvalidAuthException() else: - return 200,{"code":200,"message":"Token valid.","valid":True,"user_name":get_settings("NAME") or 'Maloja User'} + return 200,{"code":200,"message":"Token valid.","valid":True,"user_name":malojaconfig["NAME"]} def get_token_from_request_keys(self,keys): if 'token' in keys: diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index 8844d7e..a9899a8 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -1,5 +1,5 @@ from ..database import * -from doreah import settings +from ..globalconf import malojaconfig from ..__pkginfo__ import version from ..malojauri import uri_to_internal from .. import utilities @@ -41,7 +41,7 @@ def server_info(): response.set_header("Content-Type","application/json") return { - "name":settings.get_settings("NAME"), + "name":malojaconfig["NAME"], "version":version, "versionstring":".".join(str(n) for n in version), "db_status":dbstatus diff --git a/maloja/cleanup.py b/maloja/cleanup.py index 6b2e238..589a591 100644 --- a/maloja/cleanup.py +++ b/maloja/cleanup.py @@ -1,7 +1,7 @@ import re #from . import utilities -from doreah import tsv, settings -from .globalconf import data_dir +from doreah import tsv +from .globalconf import data_dir, malojaconfig import pkg_resources # need to do this as a class so it can retain loaded settings from file @@ -62,13 +62,13 @@ class CleanerAgent: #Delimiters used for extra artists, even when in the title field #delimiters_feat = ["ft.","ft","feat.","feat","featuring","Ft.","Ft","Feat.","Feat","Featuring"] - delimiters_feat = settings.get_settings("DELIMITERS_FEAT") + delimiters_feat = malojaconfig["DELIMITERS_FEAT"] #Delimiters in informal artist strings, spaces expected around them #delimiters = ["vs.","vs","&"] - delimiters = settings.get_settings("DELIMITERS_INFORMAL") + delimiters = malojaconfig["DELIMITERS_FEAT"] #Delimiters used specifically to tag multiple artists when only one tag field is available, no spaces used #delimiters_formal = ["; ",";","/"] - delimiters_formal = settings.get_settings("DELIMITERS_FORMAL") + delimiters_formal = malojaconfig["DELIMITERS_FEAT"] def parseArtists(self,a): @@ -76,7 +76,7 @@ class CleanerAgent: res = [self.parseArtists(art) for art in a] return [a for group in res for a in group] - if a.strip() in settings.get_settings("INVALID_ARTISTS"): + if a.strip() in malojaconfig["DELIMITERS_FEAT"]: return [] if a.strip().lower() in self.rules_ignoreartist: @@ -135,7 +135,7 @@ class CleanerAgent: t = re.sub(r" \(originally by .*?\)","",t) t = re.sub(r" \(.*?Remaster.*?\)","",t) - for s in settings.get_settings("REMOVE_FROM_TITLE"): + for s in malojaconfig["DELIMITERS_FEAT"]: if s in t: t = t.replace(s,"") diff --git a/maloja/database.py b/maloja/database.py index a1b83fc..bbe1a7f 100644 --- a/maloja/database.py +++ b/maloja/database.py @@ -283,7 +283,7 @@ def info(): artists = {} return { - "name":settings.get_settings("NAME"), + "name":malojaconfig["NAME"], "artists":{ chartentry["artist"]:round(chartentry["scrobbles"] * 100 / totalscrobbles,3) for chartentry in get_charts_artists() if chartentry["scrobbles"]/totalscrobbles >= 0 @@ -476,7 +476,7 @@ def trackInfo(track): scrobbles = c["scrobbles"] position = c["rank"] cert = None - threshold_gold, threshold_platinum, threshold_diamond = settings.get_settings("SCROBBLES_GOLD","SCROBBLES_PLATINUM","SCROBBLES_DIAMOND") + threshold_gold, threshold_platinum, threshold_diamond = malojaconfig["SCROBBLES_GOLD","SCROBBLES_PLATINUM","SCROBBLES_DIAMOND"] if scrobbles >= threshold_diamond: cert = "diamond" elif scrobbles >= threshold_platinum: cert = "platinum" elif scrobbles >= threshold_gold: cert = "gold" @@ -742,7 +742,7 @@ def build_db(): scrobblenum = len(db) log(f"Found {scrobblenum} scrobbles...") - usebar = not settings.get_settings("CLEAN_OUTPUT") + usebar = not malojaconfig["CLEAN_OUTPUT"] if usebar: pbar = ProgressBar(max=scrobblenum,prefix="Loading scrobbles") else: n = 0 @@ -860,7 +860,7 @@ def sync(): import copy -if settings.get_settings("USE_DB_CACHE"): +if malojaconfig["USE_DB_CACHE"]: def db_query(**kwargs): return db_query_cached(**kwargs) def db_aggregate(**kwargs): @@ -872,8 +872,8 @@ else: return db_aggregate_full(**kwargs) -csz = settings.get_settings("DB_CACHE_ENTRIES") -cmp = settings.get_settings("DB_MAX_MEMORY") +csz = malojaconfig["DB_CACHE_ENTRIES"] +cmp = malojaconfig["DB_MAX_MEMORY"] try: import psutil use_psutil = True @@ -885,8 +885,8 @@ cache_query_perm = lru.LRU(csz) cache_aggregate = lru.LRU(csz) cache_aggregate_perm = lru.LRU(csz) -perm_caching = settings.get_settings("CACHE_DATABASE_PERM") -temp_caching = settings.get_settings("CACHE_DATABASE_SHORT") +perm_caching = malojaconfig["CACHE_DATABASE_PERM"] +temp_caching = malojaconfig["CACHE_DATABASE_SHORT"] cachestats = { "cache_query":{ diff --git a/maloja/malojatime.py b/maloja/malojatime.py index 3355399..76c13a3 100644 --- a/maloja/malojatime.py +++ b/maloja/malojatime.py @@ -2,10 +2,11 @@ from datetime import timezone, timedelta, date, time, datetime from calendar import monthrange from os.path import commonprefix import math -from doreah.settings import get_settings + +from .globalconf import malojaconfig -OFFSET = get_settings("TIMEZONE") +OFFSET = malojaconfig["TIMEZONE"] TIMEZONE = timezone(timedelta(hours=OFFSET)) UTC = timezone.utc @@ -486,7 +487,7 @@ def timestamp_desc(t,short=False): timeobj = datetime.fromtimestamp(t,tz=TIMEZONE) - if not short: return timeobj.strftime(get_settings("TIME_FORMAT")) + if not short: return timeobj.strftime(malojaconfig["TIMEZONE"]) difference = int(datetime.now().timestamp() - t) diff --git a/maloja/proccontrol/control.py b/maloja/proccontrol/control.py index 0feed3c..8eb8d91 100644 --- a/maloja/proccontrol/control.py +++ b/maloja/proccontrol/control.py @@ -45,7 +45,7 @@ def start(): sp = subprocess.Popen(["python3","-m","maloja.proccontrol.supervisor"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) print(col["green"]("Maloja started!")) - port = settings.get_settings("WEB_PORT") + port = malojaconfig["WEB_PORT"] print("Visit your server address (Port " + str(port) + ") to see your web interface. Visit /admin_setup to get started.") print("If you're installing this on your local machine, these links should get you there:") diff --git a/maloja/proccontrol/setup.py b/maloja/proccontrol/setup.py index 587e2f1..0f61e00 100644 --- a/maloja/proccontrol/setup.py +++ b/maloja/proccontrol/setup.py @@ -1,11 +1,10 @@ import pkg_resources from distutils import dir_util -from doreah import settings from doreah.io import col, ask, prompt from doreah import auth import os -from ..globalconf import data_dir, dir_settings +from ..globalconf import data_dir, dir_settings, malojaconfig # EXTERNAL API KEYS @@ -32,17 +31,17 @@ def randomstring(length=32): def setup(): copy_initial_local_files() - SKIP = settings.get_settings("SKIP_SETUP") + SKIP = malojaconfig["SKIP_SETUP"] print("Various external services can be used to display images. If not enough of them are set up, only local images will be used.") for k in apikeys: - key = settings.get_settings(k) + key = malojaconfig[k] if key is False: print("\t" + "Currently not using a " + apikeys[k] + " for image display.") elif key is None or key == "ASK": print("\t" + "Please enter your " + col['gold'](apikeys[k]) + ". If you do not want to use one at this moment, simply leave this empty and press Enter.") key = prompt("",types=(str,),default=False,skip=SKIP) - settings.update_settings(data_dir['settings']("settings.ini"),{k:key},create_new=True) + malojaconfig[k] = key else: print("\t" + apikeys[k] + " found.") @@ -57,8 +56,8 @@ def setup(): keyfile.write(key + "\t" + "Default Generated Key") # PASSWORD - defaultpassword = settings.get_settings("DEFAULT_PASSWORD") - forcepassword = settings.get_settings("FORCE_PASSWORD") + defaultpassword = malojaconfig["DEFAULT_PASSWORD"] + forcepassword = malojaconfig["FORCE_PASSWORD"] # this is mainly meant for docker, supply password via environment variable if forcepassword is not None: @@ -79,13 +78,14 @@ def setup(): # we still 'ask' the user to set one, but for docker this will be skipped newpw = prompt("Please set a password for web backend access. Leave this empty to use the default password.",skip=SKIP,default=defaultpassword,secret=True) auth.defaultuser.setpw(newpw) - if settings.get_settings("NAME") is None: + if malojaconfig["NAME"] is None: name = prompt("Please enter your name. This will be displayed e.g. when comparing your charts to another user. Leave this empty if you would not like to specify a name right now.",default="Generic Maloja User",skip=SKIP) - settings.update_settings(data_dir['settings']("settings.ini"),{"NAME":name},create_new=True) + malojaconfig["NAME"] = name - if settings.get_settings("SEND_STATS") is None: + if malojaconfig["SEND_STATS"] is None: answer = ask("I would like to know how many people use Maloja. Would it be okay to send a daily ping to my server (this contains no data that isn't accessible via your web interface already)?",default=True,skip=SKIP) if answer: - settings.update_settings(data_dir['settings']("settings.ini"),{"SEND_STATS":True,"PUBLIC_URL":None},create_new=True) + malojaconfig["SEND_STATS"] = True + malojaconfig["PUBLIC_URL"] = None else: - settings.update_settings(data_dir['settings']("settings.ini"),{"SEND_STATS":False},create_new=True) + malojaconfig["SEND_STATS"] = False diff --git a/maloja/proccontrol/supervisor.py b/maloja/proccontrol/supervisor.py index 097ca3a..102ac7c 100644 --- a/maloja/proccontrol/supervisor.py +++ b/maloja/proccontrol/supervisor.py @@ -5,8 +5,8 @@ import subprocess import setproctitle import signal from doreah.logging import log -from doreah.settings import get_settings +from .globalconf import malojaconfig from .control import getInstance @@ -33,7 +33,7 @@ def start(): while True: log("Maloja is not running, starting...",module="supervisor") - if get_settings("UPDATE_AFTER_CRASH"): + if malojaconfig["UPDATE_AFTER_CRASH"]: update() process = start() diff --git a/maloja/server.py b/maloja/server.py index 85214d7..2203ba2 100644 --- a/maloja/server.py +++ b/maloja/server.py @@ -47,8 +47,8 @@ import urllib #settings.config(files=["settings/default.ini","settings/settings.ini"]) #settings.update("settings/default.ini","settings/settings.ini") -MAIN_PORT = settings.get_settings("WEB_PORT") -HOST = settings.get_settings("HOST") +MAIN_PORT = malojaconfig["WEB_PORT"] +HOST = malojaconfig["HOST"] THREADS = 24 BaseRequest.MEMFILE_MAX = 15 * 1024 * 1024 @@ -90,7 +90,7 @@ css = generate_css() def clean_html(inp): return inp - #if settings.get_settings("DEV_MODE"): return inp + #if malojaconfig["DEV_MODE"]: return inp #else: return html_minify(inp) @@ -218,7 +218,7 @@ def static_image(pth): def get_css(): response.content_type = 'text/css' global css - if settings.get_settings("DEV_MODE"): css = generate_css() + if malojaconfig["DEV_MODE"]: css = generate_css() return css @@ -263,7 +263,7 @@ def static_html(name): except (ValueError, IndexError) as e: abort(404,"This Artist or Track does not exist") - if settings.get_settings("DEV_MODE"): jinja_environment.cache.clear() + if malojaconfig["DEV_MODE"]: jinja_environment.cache.clear() log("Generated page {name} in {time:.5f}s".format(name=name,time=clock.stop()),module="debug_performance") return clean_html(res) diff --git a/maloja/thirdparty/__init__.py b/maloja/thirdparty/__init__.py index 9acc691..c3ec838 100644 --- a/maloja/thirdparty/__init__.py +++ b/maloja/thirdparty/__init__.py @@ -10,9 +10,9 @@ import xml.etree.ElementTree as ElementTree import json import urllib.parse, urllib.request import base64 -from doreah.settings import get_settings from doreah.logging import log +from ..globalconf import malojaconfig services = { @@ -69,7 +69,7 @@ class GenericInterface: # populate from settings file once on creation # avoid constant disk access, restart on adding services is acceptable for key in self.settings: - self.settings[key] = get_settings(self.settings[key]) + self.settings[key] = malojaconfig[self.settings[key]] self.authorize() # this makes sure that of every class we define, we immediately create an @@ -105,7 +105,7 @@ class ProxyScrobbleInterface(GenericInterface,abstract=True): def active_proxyscrobble(self): return ( all(self.settings[key] not in [None,"ASK",False] for key in self.proxyscrobble["required_settings"]) and - get_settings(self.proxyscrobble["activated_setting"]) + malojaconfig[self.proxyscrobble["activated_setting"]] ) def scrobble(self,artists,title,timestamp): @@ -130,7 +130,7 @@ class ImportInterface(GenericInterface,abstract=True): def active_import(self): return ( all(self.settings[key] not in [None,"ASK",False] for key in self.scrobbleimport["required_settings"]) and - get_settings(self.scrobbleimport["activated_setting"]) + malojaconfig[self.scrobbleimport["activated_setting"]] ) @@ -147,7 +147,7 @@ class MetadataInterface(GenericInterface,abstract=True): def active_metadata(self): return ( all(self.settings[key] not in [None,"ASK",False] for key in self.metadata["required_settings"]) and - self.identifier in get_settings("METADATA_PROVIDERS") + self.identifier in malojaconfig["METADATA_PROVIDERS"] ) def get_image_track(self,track): @@ -228,5 +228,5 @@ from . import * services["metadata"].sort( - key=lambda provider : get_settings("METADATA_PROVIDERS").index(provider.identifier) + key=lambda provider : malojaconfig["METADATA_PROVIDERS"].index(provider.identifier) ) diff --git a/maloja/utilities/images.py b/maloja/utilities/images.py index 0bc1792..69e4476 100644 --- a/maloja/utilities/images.py +++ b/maloja/utilities/images.py @@ -34,8 +34,8 @@ else: ### Caches -cacheage = settings.get_settings("CACHE_EXPIRE_POSITIVE") * 24 * 3600 -cacheage_neg = settings.get_settings("CACHE_EXPIRE_NEGATIVE") * 24 * 3600 +cacheage = malojaconfig["CACHE_EXPIRE_POSITIVE"] * 24 * 3600 +cacheage_neg = malojaconfig["CACHE_EXPIRE_NEGATIVE"] * 24 * 3600 artist_cache = caching.Cache(name="imgcache_artists",maxage=cacheage,maxage_negative=cacheage_neg,persistent=True) track_cache = caching.Cache(name="imgcache_tracks",maxage=cacheage,maxage_negative=cacheage_neg,persistent=True) @@ -133,7 +133,7 @@ def local_files(artist=None,artists=None,title=None): # these caches are there so we don't check all files every time, but return the same one -local_cache_age = settings.get_settings("LOCAL_IMAGE_ROTATE") +local_cache_age = malojaconfig["LOCAL_IMAGE_ROTATE"] local_artist_cache = caching.Cache(maxage=local_cache_age) local_track_cache = caching.Cache(maxage=local_cache_age) @@ -142,7 +142,7 @@ def getTrackImage(artists,title,fast=False): hashable_track = (frozenset(artists),title) # Prio 1: Local image - if settings.get_settings("USE_LOCAL_IMAGES"): + if malojaconfig["USE_LOCAL_IMAGES"]: try: return thumborize(local_track_cache.get(hashable_track)) except: @@ -189,7 +189,7 @@ def getTrackImage(artists,title,fast=False): def getArtistImage(artist,fast=False): # Prio 1: Local image - if settings.get_settings("USE_LOCAL_IMAGES"): + if malojaconfig["USE_LOCAL_IMAGES"]: try: return thumborize(local_artist_cache.get(artist)) except: diff --git a/maloja/utilities/maintenance.py b/maloja/utilities/maintenance.py index 9d26549..780ef5a 100644 --- a/maloja/utilities/maintenance.py +++ b/maloja/utilities/maintenance.py @@ -87,7 +87,7 @@ def update_weekly(): @daily def send_stats(): - if settings.get_settings("SEND_STATS"): + if malojaconfig["SEND_STATS"]: log("Sending daily stats report...") @@ -98,8 +98,8 @@ def send_stats(): "method":"POST", "headers":{"Content-Type": "application/json"}, "data":json.dumps({ - "name":settings.get_settings("NAME"), - "url":settings.get_settings("PUBLIC_URL"), + "name":malojaconfig["NAME"], + "url":malojaconfig["PUBLIC_URL"], "version":".".join(str(d) for d in version), "artists":len(ARTISTS), "tracks":len(TRACKS),