Parent: [dd5296] (diff)

Child: [a1b68f] (diff)

Download this file

uprcl-app.py    162 lines (135 with data), 5.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
#
# 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/>.
import sys
import os
import json
import re
import cmdtalkplugin
import uprclfolders
import uprcltags
import uprcluntagged
import uprclsearch
import uprclhttp
import uprclindex
from uprclutils import uplog, g_myprefix, rcldirentry
import uprclinit
#####
# Initialize communication with our parent process: pipe and method
# call dispatch
# Func name to method mapper
dispatcher = cmdtalkplugin.Dispatch()
# Pipe message handler
msgproc = cmdtalkplugin.Processor(dispatcher)
@dispatcher.record('trackuri')
def trackuri(a):
# This is used for plugins which generate temporary local urls
# pointing to the microhttpd instance. The microhttpd
# answer_to_connection() routine in plgwithslave calls 'trackuri'
# to get a real URI to redirect to. We generate URIs which
# directly point to our python http server, so this method should
# never be called.
msgproc.log("trackuri: [%s]" % a)
raise Exception("trackuri: should not be called for uprcl!")
# objid prefix to module map
rootmap = {}
def _rootentries():
# Build up root directory. This is our top internal structure. We
# 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
uplog("Browse root: rootmap now %s" % rootmap)
return entries
def _browsedispatch(objid, bflg, httphp, pathprefix):
for id,mod in rootmap.iteritems():
#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)
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))
if 'objid' not in a:
raise Exception("No objid in args")
objid = a['objid']
bflg = a['flag'] if 'flag' in a else 'children'
if not objid.startswith(g_myprefix):
raise Exception("bad objid <%s>" % objid)
idpath = objid.replace(g_myprefix, '', 1)
msgproc.log("browse: idpath: <%s>" % idpath)
entries = []
nocache = "0"
if bflg == 'meta':
raise Exception("uprcl-app: browse: can't browse meta for now")
else:
try:
if not uprclinit.ready():
entries = [rcldirentry(objid + 'notready', objid,
'Initializing...'),]
nocache = "1"
elif not idpath:
entries = _rootentries()
else:
entries = _browsedispatch(objid, bflg, uprclinit.g_httphp,
uprclinit.g_pathprefix)
finally:
uprclinit.g_dblock.release_read()
#msgproc.log("%s" % entries)
encoded = json.dumps(entries)
return {"entries" : encoded, "nocache":nocache}
@dispatcher.record('search')
def search(a):
msgproc.log("search: [%s]" % a)
objid = a['objid']
if re.match('0\$uprcl\$', objid) is None:
raise Exception("bad objid [%s]" % objid)
upnps = a['origsearch']
nocache = "0"
try:
if not uprclinit.ready():
entries = [rcldirentry(objid + 'notready', objid,
'Initializing...'),]
nocache = "1"
else:
entries = uprclsearch.search(uprclinit.g_rclconfdir, objid, upnps,
g_myprefix, uprclinit.g_httphp,
uprclinit.g_pathprefix)
finally:
uprclinit.g_dblock.release_read()
encoded = json.dumps(entries)
return {"entries" : encoded, "nocache":nocache}
uprclinit.uprcl_init()
msgproc.log("Uprcl running")
msgproc.mainloop()