From edb4e1437269ad8bded6fabe4133e2b486b0c852 Mon Sep 17 00:00:00 2001 From: Krateng Date: Mon, 11 Mar 2019 14:49:29 +0100 Subject: [PATCH] Discrimination between human-edited files (with comments) and generated logs --- database.py | 4 ++-- lastfmconverter.py | 2 +- rules/rules.info | 1 + utilities.py | 20 +++++++++++--------- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/database.py b/database.py index 5c72e66..20499ea 100644 --- a/database.py +++ b/database.py @@ -687,7 +687,7 @@ def build_db(): - db = parseAllTSV("scrobbles","int","string","string") + db = parseAllTSV("scrobbles","int","string","string",escape=False) for sc in db: artists = sc[1].split("␟") title = sc[2] @@ -742,7 +742,7 @@ def sync(): SCROBBLES[idx] = (SCROBBLES[idx][0],SCROBBLES[idx][1],True) for e in entries: - addEntries("scrobbles/" + e + ".tsv",entries[e]) + addEntries("scrobbles/" + e + ".tsv",entries[e],escape=False) combineChecksums("scrobbles/" + e + ".tsv",cla.checksums) diff --git a/lastfmconverter.py b/lastfmconverter.py index b6da66f..755632c 100644 --- a/lastfmconverter.py +++ b/lastfmconverter.py @@ -51,7 +51,7 @@ for l in log: entry = "\t".join([str(timestamp),artistsstr,title,album]) - + entry = entry.replace("#",r"\num") outputlog.write(entry) outputlog.write("\n") diff --git a/rules/rules.info b/rules/rules.info index 9b70c60..18f16f0 100644 --- a/rules/rules.info +++ b/rules/rules.info @@ -18,6 +18,7 @@ The first column defines the type of the rule: Third column the replacement artist / grouping label Rules in non-tsv files are ignored. '#' is used for comments. Additional columns are ignored. To have a '#' in a name, use '\num' +Comments are not supported in scrobble lists, but you probably never edit these manually anyway. An example file could look like this: diff --git a/utilities.py b/utilities.py index 9387a99..8bc59b3 100644 --- a/utilities.py +++ b/utilities.py @@ -9,14 +9,16 @@ import datetime ### TSV files -def parseTSV(filename,*args): +def parseTSV(filename,*args,escape=True): f = open(filename) result = [] for l in [l for l in f if (not l.startswith("#")) and (not l.strip()=="")]: - l = l.replace("\n","").split("#")[0] - l = l.replace(r"\num","#") + l = l.replace("\n","") + if escape: + l = l.split("#")[0] + l = l.replace(r"\num","#") # translate escape sequences even if we don't support comments in the file and they are not actually necessary (they might still be used for some reason) data = list(filter(None,l.split("\t"))) # Multiple tabs are okay, we don't accept empty fields unless trailing entry = [] * len(args) for i in range(len(args)): @@ -107,7 +109,7 @@ def consistentRulestate(folder,checksums): return True -def parseAllTSV(path,*args): +def parseAllTSV(path,*args,escape=True): result = [] @@ -115,7 +117,7 @@ def parseAllTSV(path,*args): if (f.endswith(".tsv")): - result += parseTSV(path + "/" + f,*args) + result += parseTSV(path + "/" + f,*args,escape=escape) return result @@ -124,21 +126,21 @@ def createTSV(filename): if not os.path.exists(filename): open(filename,"w").close() -def addEntry(filename,a): +def addEntry(filename,a,escape=True): createTSV(filename) line = "\t".join(a) - line = line.replace("#",r"\num") + if escape: line = line.replace("#",r"\num") with open(filename,"a") as f: f.write(line + "\n") -def addEntries(filename,al): +def addEntries(filename,al,escape=True): with open(filename,"a") as f: for a in al: line = "\t".join(a) - line = line.replace("#",r"\num") + if escape: line = line.replace("#",r"\num") f.write(line + "\n")