Commit 63b8ddf5 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

Media server, local files: reorganised in classes for more regularity

parent 4c66fbb1
......@@ -22,9 +22,6 @@ import json
import re
import cmdtalkplugin
import uprclfolders
import uprcltags
import uprcluntagged
import uprclsearch
import uprclindex
......@@ -72,39 +69,23 @@ def _rootentries():
# let the different modules return their stuff, and we take note
# of the objid prefixes for later dispatching
entries = []
nents = uprcltags.rootentries(g_myprefix)
for e in nents:
rootmap[e['id']] = 'tags'
entries += nents
nents = uprcluntagged.rootentries(g_myprefix)
for e in nents:
rootmap[e['id']] = 'untagged'
entries += nents
nents = uprclfolders.rootentries(g_myprefix)
for e in nents:
rootmap[e['id']] = 'folders'
entries += nents
for treename in uprclinit.g_trees.keys():
nents = uprclinit.g_trees[treename].rootentries(g_myprefix)
for e in nents:
rootmap[e['id']] = treename
entries += nents
uplog("Browse root: rootmap now %s" % rootmap)
return entries
def _browsedispatch(objid, bflg, httphp, pathprefix):
for id,mod in rootmap.iteritems():
def _browsedispatch(objid, bflg):
for id,treename in rootmap.items():
#uplog("Testing %s against %s" % (objid, id))
if objid.startswith(id):
if mod == 'folders':
return uprclfolders.browse(objid, bflg, httphp, pathprefix)
elif mod == 'tags':
return uprcltags.browse(objid, bflg, httphp, pathprefix)
elif mod == 'untagged':
return uprcluntagged.browse(objid, bflg, httphp, pathprefix)
else:
raise Exception("Browse: dispatch: bad mod " + mod)
return uprclinit.g_trees[treename].browse(objid, bflg)
raise Exception("Browse: dispatch: bad objid not in rootmap: " + objid)
@dispatcher.record('browse')
def browse(a):
msgproc.log("browse: %s. g_httphp [%s]" % (a, uprclinit.g_httphp))
......@@ -134,8 +115,7 @@ def browse(a):
elif not idpath:
entries = _rootentries()
else:
entries = _browsedispatch(objid, bflg, uprclinit.g_httphp,
uprclinit.g_pathprefix)
entries = _browsedispatch(objid, bflg)
finally:
uprclinit.g_dblock.release_read()
......@@ -160,9 +140,9 @@ def search(a):
uprclinit.g_httphp),]
nocache = "1"
else:
entries = uprclsearch.search(uprclinit.g_rclconfdir, objid, upnps,
g_myprefix, uprclinit.g_httphp,
uprclinit.g_pathprefix)
entries = uprclsearch.search(
uprclinit.g_trees['folders'], uprclinit.g_rclconfdir, objid,
upnps, g_myprefix, uprclinit.g_httphp, uprclinit.g_pathprefix)
finally:
uprclinit.g_dblock.release_read()
......
......@@ -24,9 +24,10 @@ import time
from timeit import default_timer as timer
from rwlock import ReadWriteLock
import uprclfolders
import uprcltags
import uprcluntagged
from uprclfolders import Folders
from uprcluntagged import Untagged
from uprcltags import Tagged
import uprclsearch
import uprclindex
from uprclhttp import runbottle
......@@ -34,18 +35,21 @@ from uprclhttp import runbottle
from upmplgutils import uplog
from uprclutils import findmyip, stringToStrings
# Once initialization (not on imports)
try:
_s = g_httphp
except:
# The recoll documents
g_rcldocs = []
g_pathprefix = ""
g_httphp = ""
g_dblock = ReadWriteLock()
g_rclconfdir = ""
g_friendlyname = "UpMpd-mediaserver"
# Create or update Recoll index, then read and process the data.
g_trees = {}
# Create or update Recoll index, then read and process the data. This
# runs in the separate uprcl_init_worker thread, and signals
# startup/completion by setting/unsetting the g_initrunning flag
def _update_index():
uplog("Creating/updating index in %s for %s" % (g_rclconfdir, g_rcltopdirs))
......@@ -54,7 +58,7 @@ def _update_index():
# lock. This allows future browse operations to signal the
# condition to the user instead of blocking (if we kept the write
# lock).
global g_initrunning
global g_initrunning, g_trees
g_dblock.acquire_write()
g_initrunning = True
g_dblock.release_write()
......@@ -69,10 +73,14 @@ def _update_index():
fin = timer()
uplog("Indexing took %.2f Seconds" % (fin - start))
global g_rcldocs
g_rcldocs = uprclfolders.inittree(g_rclconfdir, g_httphp, g_pathprefix)
uprcltags.recolltosql(g_rcldocs)
uprcluntagged.recoll2untagged(g_rcldocs)
folders = Folders(g_rclconfdir, g_httphp, g_pathprefix)
untagged = Untagged(folders.rcldocs(), g_httphp, g_pathprefix)
tagged = Tagged(folders.rcldocs(), g_httphp, g_pathprefix)
newtrees = {}
newtrees['folders'] = folders
newtrees['untagged'] = untagged
newtrees['tags'] = tagged
g_trees = newtrees
finally:
g_dblock.acquire_write()
g_initrunning = False
......
......@@ -17,7 +17,6 @@
from __future__ import print_function
import re
import uprclfolders
from recoll import recoll
from upmplgutils import uplog
......@@ -101,8 +100,8 @@ def _appendterms(out, v, field, oper):
out.append(oper)
out.append('"' + ph + '"')
def upnpsearchtorecoll(s):
uplog("upnpsearchtorecoll:in: <%s>" % s)
def _upnpsearchtorecoll(s):
uplog("_upnpsearchtorecoll:in: <%s>" % s)
s = re.sub('[\t\n\r\f ]+', ' ', s)
......@@ -115,7 +114,7 @@ def upnpsearchtorecoll(s):
i,c = _getchar(s, i)
if not c:
break
#uplog("upnpsearchtorecoll: nextchar: <%s>" % c)
#uplog("_upnpsearchtorecoll: nextchar: <%s>" % c)
if c.isspace():
continue
......@@ -185,14 +184,14 @@ def upnpsearchtorecoll(s):
ostr = ""
for tok in out:
ostr += tok + " "
uplog("upnpsearchtorecoll:out: <%s>" % ostr)
uplog("_upnpsearchtorecoll:out: <%s>" % ostr)
return ostr
def search(rclconfdir, objid, upnps, idprefix, httphp, pathprefix):
rcls = upnpsearchtorecoll(upnps)
def search(foldersobj, rclconfdir, objid, upnps, idprefix, httphp, pathprefix):
rcls = _upnpsearchtorecoll(upnps)
filterdir = uprclfolders.dirpath(objid)
filterdir = foldersobj.dirpath(objid)
if filterdir and filterdir != "/":
rcls += " dir:\"" + filterdir + "\""
......
......@@ -35,74 +35,76 @@ import sys
from upmplgutils import uplog
from uprclutils import rcldoctoentry, rcldirentry
untg_prefix = '0$uprcl$untagged'
# Create the untagged entries static vector by filtering the global
# doc vector, storing the indexes of all tracks without a title
# field. We keep a reference to the doc vector.
def recoll2untagged(docs):
global g_utidx, g_rcldocs
g_rcldocs = docs
# The -1 entry is because we use index 0 for our root.
g_utidx = [-1]
class Untagged(object):
def __init__(self, docs, httphp, pathprefix):
self._idprefix = '0$uprcl$untagged'
self._httphp = httphp
self._pprefix = pathprefix
self.recoll2untagged(docs)
# Create the untagged entries static vector by filtering the global
# doc vector, storing the indexes of all tracks without a title
# field. We keep a reference to the doc vector.
def recoll2untagged(self, docs):
self.rcldocs = docs
# The -1 entry is because we use index 0 for our root.
self.utidx = [-1]
for docidx in range(len(docs)):
doc = docs[docidx]
if doc.mtype == 'inode/directory':
continue
if not doc.title:
g_utidx.append(docidx)
# Compute index into our entries vector by 'parsing' the objid.
def _objidtoidx(pid):
if not pid.startswith(untg_prefix):
raise Exception("untagged.browse: bad pid %s" % pid)
if len(g_rcldocs) == 0:
raise Exception("untagged:browse: no docs")
idx = pid[len(untg_prefix):]
if not idx:
# Browsing the root.
idx = 0
else:
if idx[1] != 'u':
raise Exception("untagged:browse: called on bad objid %s" % pid)
idx = int(idx[2:])
for docidx in range(len(docs)):
doc = docs[docidx]
if doc.mtype == 'inode/directory':
continue
if not doc.title:
self.utidx.append(docidx)
# Compute index into our entries vector by 'parsing' the objid.
def _objidtoidx(self, pid):
if not pid.startswith(self._idprefix):
raise Exception("untagged:browse: bad pid %s" % pid)
if len(self.rcldocs) == 0:
raise Exception("untagged:browse: no docs")
idx = pid[len(self._idprefix):]
if not idx:
# Browsing the root.
idx = 0
else:
if idx[1] != 'u':
raise Exception("untagged:browse: called on bad objid %s" % pid)
idx = int(idx[2:])
if idx >= len(g_utidx):
raise Exception("untagged:browse: bad pid %s" % pid)
return idx
# Return entry to be created in the top-level directory ([untagged]).
def rootentries(pid):
return [rcldirentry(pid + 'untagged', pid, '[untagged]'),]
# Browse method
# objid is like untagged$u<index>
# flag is meta or children.
def browse(pid, flag, httphp, pathprefix):
idx = _objidtoidx(pid)
entries = []
if idx == 0:
# Browsing root
for i in range(len(g_utidx))[1:]:
doc = g_rcldocs[g_utidx[i]]
id = untg_prefix + '$u' + str(i)
e = rcldoctoentry(id, pid, httphp, pathprefix, doc)
if idx >= len(self.utidx):
raise Exception("untagged:browse: bad pid %s" % pid)
return idx
# Return entry to be created in the top-level directory ([untagged]).
def rootentries(self, pid):
return [rcldirentry(pid + 'untagged', pid, '[untagged]'),]
# Browse method
# objid is like untagged$u<index>
# flag is meta or children.
def browse(self, pid, flag):
idx = self._objidtoidx(pid)
entries = []
if idx == 0:
# Browsing root
for i in range(len(self.utidx))[1:]:
doc = self.rcldocs[self.utidx[i]]
id = self._idprefix + '$u' + str(i)
e = rcldoctoentry(id, pid, self._httphp, self._pprefix, doc)
if e:
entries.append(e)
else:
# Non root: only items in there. flag needs to be 'meta'
doc = self.rcldocs[thisdocidx]
id = self._idprefix + '$u' + str(idx)
e = rcldoctoentry(id, pid, self._httphp, self._pprefix, doc)
if e:
entries.append(e)
else:
# Non root: only items in there. flag needs to be 'meta'
doc = g_rcldocs[thisdocidx]
id = untg_prefix + '$u' + str(idx)
e = rcldoctoentry(id, pid, httphp, pathprefix, doc)
if e:
entries.append(e)
return sorted(entries, key=lambda entry: entry['tt'].lower())
return sorted(entries, key=lambda entry: entry['tt'].lower())
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