1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Fixed most references to user files

This commit is contained in:
Krateng 2020-12-25 04:52:05 +01:00
parent fe2b0dd7c8
commit 0818dccac4
10 changed files with 41 additions and 43 deletions

View File

@ -246,10 +246,10 @@ def import_rulemodule(**keys):
if remove: if remove:
log("Deactivating predefined rulefile " + filename) log("Deactivating predefined rulefile " + filename)
os.remove(datadir("rules/" + filename + ".tsv")) os.remove(data_dir['rules'](filename + ".tsv"))
else: else:
log("Importing predefined rulefile " + filename) log("Importing predefined rulefile " + filename)
os.symlink(datadir("rules/predefined/" + filename + ".tsv"),datadir("rules/" + filename + ".tsv")) os.symlink(data_dir['rules']("predefined/" + filename + ".tsv"),data_dir['rules'](filename + ".tsv"))
@ -320,5 +320,5 @@ def add_picture(b64,artist:Multi=[],title=None):
@api.post("newrule") @api.post("newrule")
@authenticated_api @authenticated_api
def newrule(**keys): def newrule(**keys):
tsv.add_entry(datadir("rules/webmade.tsv"),[k for k in keys]) tsv.add_entry(data_dir['rules']("webmade.tsv"),[k for k in keys])
#addEntry("rules/webmade.tsv",[k for k in keys]) #addEntry("rules/webmade.tsv",[k for k in keys])

View File

@ -1,7 +1,7 @@
import re import re
#from . import utilities #from . import utilities
from doreah import tsv, settings from doreah import tsv, settings
from .globalconf import datadir from .globalconf import data_dir
import pkg_resources import pkg_resources
# need to do this as a class so it can retain loaded settings from file # need to do this as a class so it can retain loaded settings from file
@ -13,7 +13,7 @@ class CleanerAgent:
self.updateRules() self.updateRules()
def updateRules(self): def updateRules(self):
raw = tsv.parse_all(datadir("rules"),"string","string","string","string") raw = tsv.parse_all(data_dir["rules"](),"string","string","string","string")
self.rules_belongtogether = [b for [a,b,c,d] in raw if a=="belongtogether"] self.rules_belongtogether = [b for [a,b,c,d] in raw if a=="belongtogether"]
self.rules_notanartist = [b for [a,b,c,d] in raw if a=="notanartist"] self.rules_notanartist = [b for [a,b,c,d] in raw if a=="notanartist"]
self.rules_replacetitle = {b.lower():c for [a,b,c,d] in raw if a=="replacetitle"} self.rules_replacetitle = {b.lower():c for [a,b,c,d] in raw if a=="replacetitle"}
@ -175,7 +175,7 @@ class CollectorAgent:
# rules_include dict: credited artist -> all real artists # rules_include dict: credited artist -> all real artists
def updateRules(self): def updateRules(self):
raw = tsv.parse_all(datadir("rules"),"string","string","string") raw = tsv.parse_all(data_dir["rules"](),"string","string","string")
self.rules_countas = {b:c for [a,b,c] in raw if a=="countas"} self.rules_countas = {b:c for [a,b,c] in raw if a=="countas"}
self.rules_countas_id = {} self.rules_countas_id = {}
self.rules_include = {} #Twice the memory, double the performance! self.rules_include = {} #Twice the memory, double the performance!

View File

@ -10,7 +10,7 @@ from .malojauri import uri_to_internal, internal_to_uri, compose_querystring
from .thirdparty import proxy_scrobble_all from .thirdparty import proxy_scrobble_all
from .__pkginfo__ import version from .__pkginfo__ import version
from .globalconf import datadir from .globalconf import data_dir
# doreah toolkit # doreah toolkit
from doreah.logging import log from doreah.logging import log
@ -83,7 +83,7 @@ lastsync = 0
try: try:
with open(datadir("known_servers.yml"),"r") as f: with open(data_dir['state']("known_servers.yml"),"r") as f:
KNOWN_SERVERS = set(yaml.safe_load(f)) KNOWN_SERVERS = set(yaml.safe_load(f))
except: except:
KNOWN_SERVERS = set() KNOWN_SERVERS = set()
@ -91,7 +91,7 @@ except:
def add_known_server(url): def add_known_server(url):
KNOWN_SERVERS.add(url) KNOWN_SERVERS.add(url)
with open(datadir("known_servers.yml"),"w") as f: with open(data_dir['state']("known_servers.yml"),"w") as f:
f.write(yaml.dump(list(KNOWN_SERVERS))) f.write(yaml.dump(list(KNOWN_SERVERS)))
@ -99,9 +99,9 @@ def add_known_server(url):
### symmetric keys are fine for now since we hopefully use HTTPS ### symmetric keys are fine for now since we hopefully use HTTPS
def loadAPIkeys(): def loadAPIkeys():
global clients global clients
tsv.create(datadir("clients/authenticated_machines.tsv")) tsv.create(data_dir['clients']("authenticated_machines.tsv"))
#createTSV("clients/authenticated_machines.tsv") #createTSV("clients/authenticated_machines.tsv")
clients = tsv.parse(datadir("clients/authenticated_machines.tsv"),"string","string") clients = tsv.parse(data_dir['clients']("authenticated_machines.tsv"),"string","string")
#clients = parseTSV("clients/authenticated_machines.tsv","string","string") #clients = parseTSV("clients/authenticated_machines.tsv","string","string")
log("Authenticated Machines: " + ", ".join([m[1] for m in clients])) log("Authenticated Machines: " + ", ".join([m[1] for m in clients]))
@ -690,7 +690,7 @@ def get_predefined_rulesets():
rulesets = [] rulesets = []
for f in os.listdir(datadir("rules/predefined")): for f in os.listdir(data_dir['rules']("predefined")):
if f.endswith(".tsv"): if f.endswith(".tsv"):
rawf = f.replace(".tsv","") rawf = f.replace(".tsv","")
@ -704,7 +704,7 @@ def get_predefined_rulesets():
if not "_" in rawf: continue if not "_" in rawf: continue
try: try:
with open(datadir("rules/predefined",f)) as tsvfile: with open(data_dir['rules']("predefined",f)) as tsvfile:
line1 = tsvfile.readline() line1 = tsvfile.readline()
line2 = tsvfile.readline() line2 = tsvfile.readline()
@ -721,7 +721,7 @@ def get_predefined_rulesets():
ruleset = {"file":rawf} ruleset = {"file":rawf}
rulesets.append(ruleset) rulesets.append(ruleset)
if os.path.exists(datadir("rules",f)): if os.path.exists(data_dir['rules'](f)):
ruleset["active"] = True ruleset["active"] = True
else: else:
ruleset["active"] = False ruleset["active"] = False
@ -774,7 +774,7 @@ def build_db():
# parse files # parse files
db = tsv.parse_all(datadir("scrobbles"),"int","string","string",comments=False) db = tsv.parse_all(data_dir['scrobbles'](),"int","string","string",comments=False)
scrobblenum = len(db) scrobblenum = len(db)
log(f"Found {scrobblenum} scrobbles...") log(f"Found {scrobblenum} scrobbles...")
@ -864,7 +864,7 @@ def sync():
#log("Sorted into months",module="debug") #log("Sorted into months",module="debug")
for e in entries: for e in entries:
tsv.add_entries(datadir("scrobbles/" + e + ".tsv"),entries[e],comments=False) tsv.add_entries(data_dir['scrobbles'](e + ".tsv"),entries[e],comments=False)
#addEntries("scrobbles/" + e + ".tsv",entries[e],escape=False) #addEntries("scrobbles/" + e + ".tsv",entries[e],escape=False)
#log("Written files",module="debug") #log("Written files",module="debug")

View File

@ -152,19 +152,19 @@ from doreah import config
config( config(
settings={ settings={
"files":[ "files":[
datadir("settings/default.ini"), data_dir['settings']("default.ini"),
datadir("settings/settings.ini") data_dir['settings']("settings.ini")
], ],
"environ_prefix":"MALOJA_" "environ_prefix":"MALOJA_"
}, },
caching={ caching={
"folder": datadir("cache") "folder": data_dir['cache']()
}, },
auth={ auth={
"multiuser":False, "multiuser":False,
"cookieprefix":"maloja", "cookieprefix":"maloja",
"stylesheets":["/style.css"], "stylesheets":["/style.css"],
"dbfile":datadir("auth/auth.ddb") "dbfile":data_dir['auth']("auth.ddb")
} }
) )
@ -173,7 +173,7 @@ settingsconfig._readpreconfig()
config( config(
logging={ logging={
"logfolder": datadir("logs") if get_settings("LOGGING") else None "logfolder": data_dir['logs']() if get_settings("LOGGING") else None
}, },
regular={ regular={
"autostart": False, "autostart": False,

View File

@ -5,7 +5,7 @@ from doreah.io import col, ask, prompt
from doreah import auth from doreah import auth
import os import os
from ..globalconf import datadir from ..globalconf import data_dir
# EXTERNAL API KEYS # EXTERNAL API KEYS
@ -50,14 +50,14 @@ def setup():
# OWN API KEY # OWN API KEY
if os.path.exists(datadir("clients/authenticated_machines.tsv")): if os.path.exists(data_dir['clients']("authenticated_machines.tsv")):
pass pass
else: else:
answer = ask("Do you want to set up a key to enable scrobbling? Your scrobble extension needs that key so that only you can scrobble tracks to your database.",default=True,skip=SKIP) answer = ask("Do you want to set up a key to enable scrobbling? Your scrobble extension needs that key so that only you can scrobble tracks to your database.",default=True,skip=SKIP)
if answer: if answer:
key = randomstring(64) key = randomstring(64)
print("Your API Key: " + col["yellow"](key)) print("Your API Key: " + col["yellow"](key))
with open(datadir("clients/authenticated_machines.tsv"),"w") as keyfile: with open(data_dir['clients']("authenticated_machines.tsv"),"w") as keyfile:
keyfile.write(key + "\t" + "Default Generated Key") keyfile.write(key + "\t" + "Default Generated Key")
else: else:
pass pass

View File

@ -1,7 +1,7 @@
import os import os
from doreah.io import ask from doreah.io import ask
from ...globalconf import datadir from ...globalconf import data_dir
@ -11,14 +11,13 @@ def loadlastfm(filename):
print("File could not be found.") print("File could not be found.")
return return
if os.path.exists(datadir("scrobbles/lastfmimport.tsv")): if os.path.exists(data_dir['scrobbles']("lastfmimport.tsv")):
overwrite = ask("Already imported Last.FM data. Overwrite?",default=False) overwrite = ask("Already imported Last.FM data. Overwrite?",default=False)
if not overwrite: return if not overwrite: return
print("Please wait...") print("Please wait...")
from .lastfmconverter import convert from .lastfmconverter import convert
convert(filename,datadir("scrobbles/lastfmimport.tsv")) convert(filename,data_dir['scrobbles']("lastfmimport.tsv"))
#os.system("python3 -m maloja.lastfmconverter " + filename + " " + datadir("scrobbles/lastfmimport.tsv"))
print("Successfully imported your Last.FM scrobbles!") print("Successfully imported your Last.FM scrobbles!")

View File

@ -2,7 +2,7 @@ import tarfile
from datetime import datetime from datetime import datetime
import glob import glob
import os import os
from ...globalconf import datadir from ...globalconf import data_dir
from pathlib import PurePath from pathlib import PurePath
from doreah.logging import log from doreah.logging import log

View File

@ -1,5 +1,5 @@
import os import os
from ...globalconf import datadir from ...globalconf import data_dir
import re import re
from ...cleanup import CleanerAgent from ...cleanup import CleanerAgent
from doreah.logging import log from doreah.logging import log
@ -23,26 +23,25 @@ exp = r"([0-9]*)(\t+)([^\t]+?)(\t+)([^\t]+)([^\n]*)\n"
def fix(): def fix():
backup(level="minimal",folder=datadir("backups")) backup(level="minimal",folder=data_dir['backups']())
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
nowstr = now.strftime("%Y_%m_%d_%H_%M_%S") nowstr = now.strftime("%Y_%m_%d_%H_%M_%S")
datestr = now.strftime("%Y/%m/%d") datestr = now.strftime("%Y/%m/%d")
timestr = now.strftime("%H:%M:%S") timestr = now.strftime("%H:%M:%S")
patchfolder = datadir("logs","dbfix",nowstr) patchfolder = data_dir['logs']("dbfix",nowstr)
os.makedirs(patchfolder) os.makedirs(patchfolder)
#with open(datadir("logs","dbfix",nowstr + ".log"),"a") as logfile:
log("Fixing database...") log("Fixing database...")
for filename in os.listdir(datadir("scrobbles")): for filename in os.listdir(data_dir['scrobbles']()):
if filename.endswith(".tsv"): if filename.endswith(".tsv"):
log("Fix file " + filename) log("Fix file " + filename)
filename_new = filename + "_new" filename_new = filename + "_new"
with open(datadir("scrobbles",filename_new),"w") as newfile: with open(data_dir['scrobbles'](filename_new),"w") as newfile:
with open(datadir("scrobbles",filename),"r") as oldfile: with open(data_dir['scrobbles'](filename),"r") as oldfile:
for l in oldfile: for l in oldfile:
@ -57,7 +56,7 @@ def fix():
#os.system("diff " + "scrobbles/" + fn + "_new" + " " + "scrobbles/" + fn) #os.system("diff " + "scrobbles/" + fn + "_new" + " " + "scrobbles/" + fn)
with open(datadir("scrobbles",filename_new),"r") as newfile, open(datadir("scrobbles",filename),"r") as oldfile: with open(data_dir['scrobbles'](filename_new),"r") as newfile, open(data_dir['scrobbles'](filename),"r") as oldfile:
diff = difflib.unified_diff(oldfile.read().split("\n"),newfile.read().split("\n"),lineterm="",n=0) diff = difflib.unified_diff(oldfile.read().split("\n"),newfile.read().split("\n"),lineterm="",n=0)
diff = list(diff) diff = list(diff)
@ -65,7 +64,7 @@ def fix():
with open(os.path.join(patchfolder,filename + ".diff"),"w") as patchfile: with open(os.path.join(patchfolder,filename + ".diff"),"w") as patchfile:
patchfile.write("\n".join(diff)) patchfile.write("\n".join(diff))
os.replace(datadir("scrobbles",filename_new),datadir("scrobbles",filename)) os.replace(data_dir['scrobbles'](filename_new),data_dir['scrobbles'](filename))
log("Database fixed!") log("Database fixed!")

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
from .globalconf import datadir, DATA_DIR from .globalconf import data_dir, DATA_DIR
# server stuff # server stuff
@ -154,18 +154,18 @@ def static_image(pth):
type = pth.split(".")[-1] type = pth.split(".")[-1]
small_pth = pth + "-small" small_pth = pth + "-small"
if os.path.exists(datadir("images",small_pth)): if os.path.exists(data_dir['images'](small_pth)):
response = static_file(pthjoin("images",small_pth),root=DATAFOLDER) response = static_file(pthjoin("images",small_pth),root=DATAFOLDER)
else: else:
try: try:
from wand.image import Image from wand.image import Image
img = Image(filename=datadir("images",pth)) img = Image(filename=data_dir['images'](pth))
x,y = img.size[0], img.size[1] x,y = img.size[0], img.size[1]
smaller = min(x,y) smaller = min(x,y)
if smaller > 300: if smaller > 300:
ratio = 300/smaller ratio = 300/smaller
img.resize(int(ratio*x),int(ratio*y)) img.resize(int(ratio*x),int(ratio*y))
img.save(filename=datadir("images",small_pth)) img.save(filename=data_dir['images'](small_pth))
response = static_file(pthjoin("images",small_pth),root=DATAFOLDER) response = static_file(pthjoin("images",small_pth),root=DATAFOLDER)
else: else:
response = static_file(pthjoin("images",pth),root=DATAFOLDER) response = static_file(pthjoin("images",pth),root=DATAFOLDER)

View File

@ -1,5 +1,5 @@
from .. import globalconf from .. import globalconf
from ..globalconf import datadir from ..globalconf import data_dir
from .. import thirdparty from .. import thirdparty
from doreah import settings, caching from doreah import settings, caching