Switch to unified view

a/src/mediaserver/cdplugins/uprcl/uprclfolders.py b/src/mediaserver/cdplugins/uprcl/uprclfolders.py
...
...
23
#
23
#
24
# Entry 0 in dirvec is special: it holds the 'topdirs' from the recoll
24
# Entry 0 in dirvec is special: it holds the 'topdirs' from the recoll
25
# configuration. The entries are paths instead of simple names, and
25
# configuration. The entries are paths instead of simple names, and
26
# the doc index (j) is 0. The dir index points normally to a dirvec
26
# the doc index (j) is 0. The dir index points normally to a dirvec
27
# entry.
27
# entry.
28
29
# Create new directory entry: insert in father and append dirvec slot (with ".." entry)
30
def _createdir(dirvec, fathidx, docidx, nm):
31
    dirvec.append({})
32
    dirvec[fathidx][nm] = (len(dirvec) - 1, docidx)
33
    dirvec[-1][".."] = (fathidx, -1)
34
    return len(dirvec) - 1
35
28
def _rcl2folders(docs, confdir):
36
def _rcl2folders(docs, confdir):
29
    global dirvec
37
    global dirvec
30
    dirvec = []
38
    dirvec = []
31
39
32
    rclconf = rclconfig.RclConfig(confdir)
40
    rclconf = rclconfig.RclConfig(confdir)
33
    topdirs = [os.path.expanduser(d) for d in
41
    topdirs = [os.path.expanduser(d) for d in
34
               shlex.split(rclconf.getConfParam('topdirs'))]
42
               shlex.split(rclconf.getConfParam('topdirs'))]
35
43
36
    topidx = 0
37
    dirvec.append({})
44
    dirvec.append({})
45
    dirvec[0][".."] = (0, -1)
38
    for d in topdirs:
46
    for d in topdirs:
39
        topidx += 1
40
        dirvec[0][d] = (topidx, -1)
41
        dirvec.append({})
47
        dirvec.append({})
48
        dirvec[0][d] = (len(dirvec)-1, -1)
49
        dirvec[-1][".."] = (0, -1)
42
50
43
    # Walk the doc list and update the directory tree according to the
51
    # Walk the doc list and update the directory tree according to the
44
    # url (create intermediary directories if needed, create leaf
52
    # url (create intermediary directories if needed, create leaf
45
    # entry
53
    # entry
46
    for docidx in range(len(docs)):
54
    for docidx in range(len(docs)):
...
...
87
                # Element has no entry in father directory (hence no
95
                # Element has no entry in father directory (hence no
88
                # dirvec entry either).
96
                # dirvec entry either).
89
                if idx != len(path) -1:
97
                if idx != len(path) -1:
90
                    # This is an intermediate element. Create a
98
                    # This is an intermediate element. Create a
91
                    # Doc-less directory
99
                    # Doc-less directory
92
                    topidx += 1
100
                    fathidx = _createdir(dirvec, fathidx, -1, elt)
93
                    dirvec.append({})
94
                    dirvec[fathidx][elt] = (topidx, -1)
95
                    fathidx = topidx
96
                else:
101
                else:
97
                    # Last element. If directory, needs a dirvec entry
102
                    # Last element. If directory, needs a dirvec entry
98
                    if doc.mtype == 'inode/directory':
103
                    if doc.mtype == 'inode/directory':
99
                        topidx += 1
104
                        fathidx = _createdir(dirvec, fathidx, docidx, elt)
100
                        dirvec.append({})
101
                        dirvec[fathidx][elt] = (topidx, docidx)
102
                        fathidx = topidx
103
                    else:
105
                    else:
104
                        dirvec[fathidx][elt] = (-1, docidx)
106
                        dirvec[fathidx][elt] = (-1, docidx)
105
107
106
    if False:
108
    if False:
107
        for ent in dirvec:
109
        for ent in dirvec:
...
...
174
    a1 = e1[k] if k in e1 else "0"
176
    a1 = e1[k] if k in e1 else "0"
175
    a2 = e2[k] if k in e2 else "0"
177
    a2 = e2[k] if k in e2 else "0"
176
    return int(a1) - int(a2)
178
    return int(a1) - int(a2)
177
179
178
180
181
def _objidtodiridx(pid):
182
    if not pid.startswith(g_myprefix):
183
        raise Exception("folders.browse: bad pid %s" % pid)
184
185
    if len(g_alldocs) == 0:
186
        raise Exception("folders:browse: no docs")
187
188
    diridx = pid[len(g_myprefix):]
189
    if not diridx:
190
        diridx = 0
191
    else:
192
        if diridx[1] != 'd':
193
            raise Exception("folders:browse: called on non dir objid %s" % pid)
194
        diridx = int(diridx[2:])
195
    
196
    if diridx >= len(g_dirvec):
197
        raise Exception("folders:browse: bad pid %s" % pid)
198
199
    return diridx
200
201
179
# Browse method
202
# Browse method
180
# objid is like folders$index
203
# objid is like folders$index
181
# flag is meta or children. 
204
# flag is meta or children. 
182
def browse(pid, flag, httphp, pathprefix):
205
def browse(pid, flag, httphp, pathprefix):
183
    global g_alldocs, g_dirvec
184
206
185
    if not pid.startswith(g_myprefix):
207
    diridx = _objidtodiridx(pid)
186
        uplog("folders.browse: bad pid %s" % pid)
187
        return []
188
189
    if len(g_alldocs) == 0:
190
        uplog("folders:browse: no docs")
191
        return []
192
193
    diridx = pid[len(g_myprefix):]
194
    if not diridx:
195
        diridx = 0
196
    else:
197
        diridx = int(diridx[2:])
198
    
199
    if diridx >= len(g_dirvec):
200
        uplog("folders:browse: bad pid %s" % pid)
201
        return []
202
208
203
    entries = []
209
    entries = []
204
210
205
    # The basename call is just for diridx==0 (topdirs). Remove it if
211
    # The basename call is just for diridx==0 (topdirs). Remove it if
206
    # this proves a performance issue
212
    # this proves a performance issue
207
    for nm,ids in g_dirvec[diridx].iteritems():
213
    for nm,ids in g_dirvec[diridx].iteritems():
214
        #uplog("folders:browse: got nm %s" % nm.decode('utf-8'))
215
        if nm == "..":
216
            continue
208
        thisdiridx = ids[0]
217
        thisdiridx = ids[0]
209
        thisdocidx = ids[1]
218
        thisdocidx = ids[1]
210
        if thisdiridx >= 0:
219
        if thisdiridx >= 0:
211
            id = g_myprefix + '$' + 'd' + str(thisdiridx)
220
            id = g_myprefix + '$' + 'd' + str(thisdiridx)
212
            entries.append(rcldirentry(id, pid, os.path.basename(nm)))
221
            entries.append(rcldirentry(id, pid, os.path.basename(nm)))
...
...
219
            id = g_myprefix + '$' + 'i' + str(thisdocidx)
228
            id = g_myprefix + '$' + 'i' + str(thisdocidx)
220
            e = rcldoctoentry(id, pid, httphp, pathprefix, doc)
229
            e = rcldoctoentry(id, pid, httphp, pathprefix, doc)
221
            if e:
230
            if e:
222
                entries.append(e)
231
                entries.append(e)
223
232
224
    #return entries
225
    return sorted(entries, cmp=_cmpentries)
233
    return sorted(entries, cmp=_cmpentries)
234
235
# return path for objid, which has to be a container. This is good old pwd
236
def dirpath(objid):
237
238
    diridx = _objidtodiridx(objid)
239
240
    lpath = []
241
    while True:
242
        fathidx = g_dirvec[diridx][".."][0]
243
        for nm, ids in g_dirvec[fathidx].iteritems():
244
            if ids[0] == diridx:
245
                lpath.append(nm)
246
                break
247
        diridx = fathidx
248
        if diridx == 0:
249
            break
250
251
    if not lpath:
252
        path = "/"
253
    else:
254
        path = ""
255
    for elt in reversed(lpath):
256
        path += elt + "/"
257
258
    return path