Switch to unified view

a/src/mediaserver/cdplugins/uprcl/uprcl-app.py b/src/mediaserver/cdplugins/uprcl/uprcl-app.py
...
...
24
import conftree
24
import conftree
25
import cmdtalkplugin
25
import cmdtalkplugin
26
import urllib
26
import urllib
27
27
28
import uprclfolders
28
import uprclfolders
29
import uprcltags
29
import uprclsearch
30
import uprclsearch
30
from uprclutils import *
31
from uprclutils import *
31
32
32
from recoll import recoll
33
from recoll import recoll
33
from recoll import rclconfig
34
from recoll import rclconfig
35
36
# This must be consistent with what contentdirectory.cxx does
34
g_myprefix = '0$uprcl$folders'
37
g_myprefix = '0$uprcl$'
38
39
# The recoll documents
40
g_rcldocs = []
35
41
36
# Func name to method mapper
42
# Func name to method mapper
37
dispatcher = cmdtalkplugin.Dispatch()
43
dispatcher = cmdtalkplugin.Dispatch()
38
# Pipe message handler
44
# Pipe message handler
39
msgproc = cmdtalkplugin.Processor(dispatcher)
45
msgproc = cmdtalkplugin.Processor(dispatcher)
40
46
41
def uprcl_init():
47
def uprcl_init():
42
    global httphp, pathprefix, uprclhost, pathmap, rclconfdir
48
    global httphp, pathprefix, uprclhost, pathmap, rclconfdir, g_rcldocs
43
    
49
    
44
    if "UPMPD_HTTPHOSTPORT" not in os.environ:
50
    if "UPMPD_HTTPHOSTPORT" not in os.environ:
45
        raise Exception("No UPMPD_HTTPHOSTPORT in environment")
51
        raise Exception("No UPMPD_HTTPHOSTPORT in environment")
46
    httphp = os.environ["UPMPD_HTTPHOSTPORT"]
52
    httphp = os.environ["UPMPD_HTTPHOSTPORT"]
47
    if "UPMPD_PATHPREFIX" not in os.environ:
53
    if "UPMPD_PATHPREFIX" not in os.environ:
...
...
67
    global rclconfdir
73
    global rclconfdir
68
    rclconfdir = upconfig.get("uprclconfdir")
74
    rclconfdir = upconfig.get("uprclconfdir")
69
    if rclconfdir is None:
75
    if rclconfdir is None:
70
        raise Exception("uprclconfdir not in config")
76
        raise Exception("uprclconfdir not in config")
71
77
72
    uprclfolders.inittree(rclconfdir)
78
    g_rcldocs = uprclfolders.inittree(rclconfdir)
73
79
    uprcltags.recolltosql(g_rcldocs)
74
80
75
@dispatcher.record('trackuri')
81
@dispatcher.record('trackuri')
76
def trackuri(a):
82
def trackuri(a):
77
    msgproc.log("trackuri: [%s]" % a)
83
    msgproc.log("trackuri: [%s]" % a)
78
    if 'path' not in a:
84
    if 'path' not in a:
...
...
80
    path = urllib.quote(a['path'])
86
    path = urllib.quote(a['path'])
81
    media_url = rclpathtoreal(path, pathprefix, uprclhost, pathmap)
87
    media_url = rclpathtoreal(path, pathprefix, uprclhost, pathmap)
82
    msgproc.log("trackuri: returning: %s" % media_url)
88
    msgproc.log("trackuri: returning: %s" % media_url)
83
    return {'media_url' : media_url}
89
    return {'media_url' : media_url}
84
90
91
# objid prefix to module map
92
rootmap = {}
93
94
def _rootentries():
95
    # Build up root directory. This is our top internal structure. We
96
    # let the different modules return their stuff, and we take note
97
    # of the objid prefixes for later dispatching
98
    entries = []
99
    nents = uprclfolders.rootentries(g_myprefix)
100
    for e in nents:
101
        rootmap[e['id']] = 'folders'
102
    entries += nents
103
104
    nents = uprcltags.rootentries(g_myprefix)
105
    for e in nents:
106
        rootmap[e['id']] = 'tags'
107
    entries += nents
108
    uplog("Browse root: rootmap now %s" % rootmap)
109
    return entries
110
111
def _browsedispatch(objid, bflg, httphp, pathprefix):
112
    for id,mod in rootmap.iteritems():
113
        uplog("Testing %s against %s" % (objid, id))
114
        if objid.startswith(id):
115
            if mod == 'folders':
116
                return uprclfolders.browse(objid, bflg, httphp, pathprefix)
117
            elif mod == 'tags':
118
                return uprcltags.browse(objid, bflg, httphp, pathprefix)
119
            else:
120
                raise Exception("Browse: dispatch: bad mod " + mod)
121
    raise Exception("Browse: dispatch: bad objid not in rootmap" + objid)
85
122
86
@dispatcher.record('browse')
123
@dispatcher.record('browse')
87
def browse(a):
124
def browse(a):
88
    msgproc.log("browse: [%s]" % a)
125
    msgproc.log("browse: %s" % a)
89
    if 'objid' not in a:
126
    if 'objid' not in a:
90
        raise Exception("No objid in args")
127
        raise Exception("No objid in args")
91
    objid = a['objid']
128
    objid = a['objid']
92
    bflg = a['flag'] if 'flag' in a else 'children'
129
    bflg = a['flag'] if 'flag' in a else 'children'
93
    
130
    
94
    if re.match('0\$uprcl\$', objid) is None:
131
    if not objid.startswith(g_myprefix):
95
        raise Exception("bad objid [%s]" % objid)
132
        raise Exception("bad objid <%s>" % objid)
96
133
97
    idpath = objid.replace('0$uprcl$', '', 1)
134
    idpath = objid.replace(g_myprefix, '', 1)
98
    msgproc.log("browse: idpath: %s" % idpath)
135
    msgproc.log("browse: idpath: <%s>" % idpath)
99
136
100
    entries = []
137
    entries = []
101
138
102
    if bflg == 'meta':
139
    if bflg == 'meta':
103
        m = re.match('.*\$(.+)$', idpath)
140
        raise Exception("uprcl-app: browse: can't browse meta for now")
104
        if m:
105
            trackid = m.group(1)
106
            # get doc from trackid or whatever
107
            doc = {}
108
            entries += trackentries(httphp, pathprefix, objid, [])
109
    else:
141
    else:
110
        if not idpath:
142
        if not idpath:
111
            # Build up root directory. No external data needed, this is our
143
            entries = _rootentries()
112
            # top internal structure
113
            entries.append(rcldirentry('0$uprcl$' + 'folders', '0$uprcl$',
114
                                        '[folders]'))
115
        elif idpath.startswith("folders"):
116
            entries = uprclfolders.browse(objid, bflg, httphp, pathprefix)
117
        else:
144
        else:
118
            pass
145
            entries = _browsedispatch(objid, bflg, httphp, pathprefix)
119
120
146
121
    #msgproc.log("%s" % entries)
147
    #msgproc.log("%s" % entries)
122
    encoded = json.dumps(entries)
148
    encoded = json.dumps(entries)
123
    return {"entries" : encoded}
149
    return {"entries" : encoded}
124
150