Commit 622cc828 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

uprcl: implement the minim aliastag function at index time (rclaudio) (and prepare for tagvalue)

parent 8d43013c
# Copyright (C) 2019 J.F.Dockes
# Copyright (C) 2019-2020 J.F.Dockes
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -53,6 +53,8 @@ class MinimConfig(object):
out = []
if not str:
return out
# For some reason, the active colons are backslash-escaped in
# the file
lst = str.replace('\\:', ':').split(',')
for e in lst:
l = e.split(':')
......@@ -69,6 +71,49 @@ class MinimConfig(object):
return out
# Comma-separated list. Each element maybe a simple tagname.option
# value or an \= assignement, with a comma-separated list of values in
# braces. Returns a list of quadruplet: (tag, neg, optname, values)
def minimsplitbraceslist(self, value):
# Split everything on commas
l1 = value.split(',')
# Re-join parts inside braces, creating list of options
inbraces=False
accum = ''
l2 = []
for e in l1:
if inbraces:
accum += ', ' + e
if e.find('}'):
inbraces = False
l2.append(accum)
accum=''
continue
if e.find('{') != -1 and e.find('}') == -1:
inbraces = True
accum = e
continue
l2.append(e)
# Parse each option [-]tag.option={tag1,tag2} or [-]tag.option
alloptions = []
for e in l2:
lhsrhs=e.split('''\=''')
if len(lhsrhs) == 2:
values = [v.strip().lower() for v in lhsrhs[1].lstrip('{').rstrip('}').split(',')]
else:
values = []
tagopt = lhsrhs[0].split('.')
tag = tagopt[0].strip()
if tag.startswith('-'):
neg = True
tag = tag[1:]
else:
neg = False
opt = '.'.join(tagopt[1:])
alloptions.append((tag, neg, opt, values))
return alloptions
def getexcludepatterns(self):
spats = self.conf.get("minimserver.excludePattern")
if spats:
......@@ -81,20 +126,28 @@ class MinimConfig(object):
return spats
def gettagaliases(self):
def gettagvalue(self):
stagv = self.conf.get("minimserver.tagValue")
tagvalue = None
if stagv:
tagvalue = self.minimsplitbraceslist(stagv)
return tagvalue
def getaliastags(self):
aliases = []
saliases = self.conf.get("minimserver.aliasTags")
uplog("Minim:gettagaliases:in: [%s]" % saliases)
#uplog("Minim:getaliastags:in: [%s]" % saliases)
lst = self.minimsplitsplit(saliases)
for orig,target in lst:
orig = orig.lower()
target = target.lower()
rep = True
if target[0] == '-'[0]:
rep = False
rep = False
if target.startswith('-'):
rep = True
target = target[1:]
aliases.append((orig, target, rep))
uplog("Minim:gettagaliases:out: %s" % aliases)
#uplog("Minim:getaliastags:out: %s" % aliases)
return aliases
......
......@@ -16,15 +16,23 @@
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# This is imported from rclaudio when the audiotagfixerscript variable
# is set in the recoll configuration (uprcl sets it). It then operates
# transformations on audio tag sets, as prescribed in the minimserver
# configuration, which is pointed to by the uprclminimconfig upmpdcli
# configuration variable.
import sys
import os
import conftree
import minimconfig
def uplog(s):
if not type(s) == type(b''):
s = ("%s: %s" % ('uprcl:minimtagfixer', s)).encode('utf-8')
sys.stderr.buffer.write(s + b'\n')
sys.stderr.buffer.write(s + b'\n')
sys.stderr.flush()
......@@ -41,25 +49,31 @@ class TagUpdateData(object):
def __init__(self):
self.tgupfile = None
upconfig = conftree.ConfSimple(os.environ["UPMPD_CONFIG"])
self.tgupfile = None
self.aliastags = None
self.minimcnffn = upconfig.get("uprclminimconfig")
if self.minimcnffn:
conf = conftree.ConfSimple(self.minimcnffn)
self.tgupfile = conf.get("minimserver.tagUpdate")
#uplog("Minim config read: tagUpdate: %s" % self.tgupfile)
self.tgupfile = self._makeabs(self.tgupfile)
if not os.path.exists(self.tgupfile):
uplog("gettagupdate: %s does not exist" % self.tgupfile)
return
wlogfn = conf.get("minimserver.writeTagChanges")
wlogfn = self._makeabs(wlogfn)
#uplog("gettagupdate: log file: %s" % wlogfn)
if wlogfn:
global _logfp
try:
_logfp = open(wlogfn, "a")
except Exception as ex:
uplog("can't open %s : %s" % (wlogfn, ex))
if not self.minimcnffn:
return
self.minimconfig = minimconfig.MinimConfig(self.minimcnffn)
self.tgupfile = self.minimconfig.getsimplevalue("minimserver.tagUpdate")
#uplog("Minim config read: tagUpdate: %s" % self.tgupfile)
self.tgupfile = self._makeabs(self.tgupfile)
if not os.path.exists(self.tgupfile):
uplog("gettagupdate: %s does not exist" % self.tgupfile)
return
wlogfn = self.minimconfig.getsimplevalue("minimserver.writeTagChanges")
wlogfn = self._makeabs(wlogfn)
#uplog("gettagupdate: log file: %s" % wlogfn)
if wlogfn:
global _logfp
try:
_logfp = open(wlogfn, "a")
except Exception as ex:
uplog("can't open %s : %s" % (wlogfn, ex))
self.aliastags = self.minimconfig.getaliastags()
self.tagvalue = self.minimconfig.gettagvalue()
# The defs and log files are to be found in the same dir as the
# logs (see minim doc). So compute minimserver/etc/xx.conf/../../data/fn
......@@ -73,6 +87,7 @@ class TagUpdateData(object):
# https://minimserver.com/ug-other.html#Tag%20update
# Tag value replacement specs, e.g. "Johann Sebastian Bach" -> "Bach J.S."
def gettagupdate(self):
if not self.tgupfile:
return ()
......@@ -137,10 +152,9 @@ class TagUpdateData(object):
tud = TagUpdateData()
groups = tud.gettagupdate()
# uplog("minimtagfixer: groups %s" % groups)
#uplog("minimtagfixer: groups %s" % groups)
def tagfix(tags):
#uplog("tagfix: tags: %s" % tags)
def tagupdate(tags):
for group in groups:
# Must match at least one element of group[0]
sel = False
......@@ -165,3 +179,28 @@ def tagfix(tags):
for tag,val in group[3]:
_logchange("Clearing [%s]" % tag)
del tags[tag]
def tagvalue(tags):
pass
def aliastags(tags):
if tud.aliastags:
for orig, target, rep in tud.aliastags:
try:
val = tags[orig]
#uplog("aliastags: Rep %s tags[%s]=[%s] tags[%s]=[%s]"%
# (rep, orig, val, target, tags[target]))
if val and (rep or not tags[target]):
#uplog("tags[%s] -> %s" % (target,val))
tags[target] = val
except:
pass
def tagfix(tags={}):
#uplog("tagfix(%s)"%tags)
tagvalue(tags)
aliastags(tags)
tagupdate(tags)
......@@ -361,10 +361,6 @@ class Folders(object):
#rclq.execute('album:a* OR album:b* OR album:c*', stemming=0)
uplog("Estimated alldocs query results: %d" % (rclq.rowcount))
tagaliases = None
if uprclinit.g_minimconfig:
tagaliases = uprclinit.g_minimconfig.gettagaliases()
if _has_resultstore:
fields = [r[1] for r in uprclutils.upnp2rclfields.items()]
fields += _otherneededfields
......@@ -373,6 +369,9 @@ class Folders(object):
self._rcldocs = qresultstore.QResultStore()
self._rcldocs.storeQuery(rclq, fieldspec=fields, isinc = True)
else:
tagaliases = None
if uprclinit.g_minimconfig:
tagaliases = uprclinit.g_minimconfig.getaliastags()
self._rcldocs = []
for doc in rclq:
if tagaliases:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment