4
from paste import request
5
from paste import httpexceptions
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
8
from loggerhead.apps import static_app
9
from loggerhead.changecache import FileChangeCache
10
from loggerhead.controllers.changelog_ui import ChangeLogUI
11
from loggerhead.controllers.inventory_ui import InventoryUI
12
from loggerhead.controllers.annotate_ui import AnnotateUI
13
from loggerhead.controllers.revision_ui import RevisionUI
14
from loggerhead.controllers.atom_ui import AtomUI
15
from loggerhead.controllers.download_ui import DownloadUI
16
from loggerhead.history import History
17
from loggerhead import util
20
logging.getLogger().setLevel(logging.DEBUG)
22
class BranchWSGIApp(object):
24
def __init__(self, branch_url, friendly_name=None, config={}):
25
self.branch_url = branch_url
28
self.friendly_name = friendly_name
29
self.log = logging.getLogger(friendly_name)
33
if (self._history is None) or self._history.out_of_date():
34
self.log.debug('Reload branch history...')
35
_history = self._history = History.from_folder(self.branch_url)
36
cache_path = self._config.get('cachepath', None)
37
if cache_path is not None:
38
_history.use_file_cache(FileChangeCache(_history, cache_path))
41
def url(self, *args, **kw):
42
if isinstance(args[0], list):
45
for k, v in kw.iteritems():
47
qs.append('%s=%s'%(k, urllib.quote(v)))
49
return request.construct_url(
50
self._environ, script_name=self._url_base,
51
path_info='/'.join(args),
54
def context_url(self, *args, **kw):
55
kw = util.get_context(**kw)
56
return self.url(*args, **kw)
58
def static_url(self, path):
59
return self._static_url_base + path
62
'annotate': AnnotateUI,
63
'changes': ChangeLogUI,
65
'revision': RevisionUI,
66
'download': DownloadUI,
70
def last_updated(self):
72
change = h.get_changes([ h.last_revid ])[0]
76
return self.history.get_config().get_user_option('public_branch')
78
def app(self, environ, start_response):
79
req = WSGIRequest(environ)
80
response = WSGIResponse()
81
response.headers['Content-Type'] = 'text/plain'
82
self._url_base = environ['SCRIPT_NAME']
83
self._static_url_base = environ.get('loggerhead.static.url')
84
if self._static_url_base is None:
85
self._static_url_base = self._url_base
86
self._environ = environ
87
path = request.path_info_pop(environ)
89
raise httpexceptions.HTTPMovedPermanently(
90
self._url_base + '/changes')
92
return static_app(environ, start_response)
93
cls = self.controllers_dict.get(path)
95
raise httpexceptions.HTTPNotFound()
97
c.default(req, response)
98
return response(environ, start_response)