uprcluntagged.py 3.78 KB
Newer Older
Jean-Francois Dockes's avatar
Jean-Francois Dockes committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#
# Copyright (C) 2017 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Jean-Francois Dockes's avatar
Jean-Francois Dockes committed
16

17
18
19
20
21
22
23
24
25
26
27
28
29
# Manage the 'untagged' section of the tree.  Our selection is made of
# all tracks without a 'title' field. This is different from what
# Minimserver does (I think that more or less any field present makes
# a track not untagged for minim).
#
# Initialization filters the untagged tracks and creates a vector of
# indexes into the global doc vector.
#
# Object Id prefix: 0$uprcl$untagged
# 
# Obect id inside the section: $u<idx> where <idx> is the document index
#  inside the global document vector.

Jean-Francois Dockes's avatar
Jean-Francois Dockes committed
30
31
32
33
import os
import shlex
import sys

34
35
from upmplgutils import uplog, direntry
from uprclutils import rcldoctoentry
36
import uprclinit
Jean-Francois Dockes's avatar
Jean-Francois Dockes committed
37

38
class Untagged(object):
39
    def __init__(self, rcldocs, httphp, pathprefix):
40
41
42
        self._idprefix = '0$uprcl$untagged'
        self._httphp = httphp
        self._pprefix = pathprefix
43
        self.recoll2untagged(rcldocs)
44
45
46
47
        
    # 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.
48
    def recoll2untagged(self, rcldocs):
49
50
        # The -1 entry is because we use index 0 for our root.
        self.utidx = [-1]
Jean-Francois Dockes's avatar
Jean-Francois Dockes committed
51
    
52
53
        for docidx in range(len(rcldocs)):
            doc = rcldocs[docidx]
54
55
            if doc["mtype"] == 'inode/directory' or \
               doc["mtype"] == 'audio/x-mpegurl':
56
                continue
57
            if not doc["title"]:
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                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)

        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:])
Jean-Francois Dockes's avatar
Jean-Francois Dockes committed
73
    
74
75
76
77
78
79
80
        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):
81
        return [direntry(pid + 'untagged', pid, '[untagged]'),]
82
83
84
85
86


    # Browse method
    # objid is like untagged$u<index>
    # flag is meta or children.
87
    def browse(self, pid, flag, offset, count):
88
89
90
        idx = self._objidtoidx(pid)

        entries = []
91
        rcldocs = uprclinit.getTree('folders').rcldocs()
92
93
94
        if idx == 0:
            # Browsing root
            for i in range(len(self.utidx))[1:]:
95
                doc = rcldocs[self.utidx[i]]
96
97
98
99
100
101
                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'
102
            doc = rcldocs[idx]
103
104
            id = self._idprefix + '$u' + str(idx)
            e = rcldoctoentry(id, pid, self._httphp, self._pprefix, doc)
Jean-Francois Dockes's avatar
Jean-Francois Dockes committed
105
106
107
            if e:
                entries.append(e)

108
        return sorted(entries, key=lambda entry: entry['tt'].lower())