~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Michael Hudson
  • Date: 2008-03-03 23:10:35 UTC
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: michael.hudson@canonical.com-20080303231035-3yrnrsajxpo9bgt6
poolie feedback

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""The WSGI application for serving a Bazaar branch."""
2
 
 
3
 
import logging
4
 
import urllib
5
 
 
6
 
import bzrlib.branch
7
 
import bzrlib.lru_cache
8
 
 
9
 
from paste import request
10
 
from paste import httpexceptions
11
 
 
12
 
from loggerhead.apps import static_app
13
 
from loggerhead.controllers.changelog_ui import ChangeLogUI
14
 
from loggerhead.controllers.inventory_ui import InventoryUI
15
 
from loggerhead.controllers.annotate_ui import AnnotateUI
16
 
from loggerhead.controllers.revision_ui import RevisionUI
17
 
from loggerhead.controllers.atom_ui import AtomUI
18
 
from loggerhead.controllers.download_ui import DownloadUI
19
 
from loggerhead.controllers.search_ui import SearchUI
20
 
from loggerhead.history import History
21
 
from loggerhead import util
22
 
 
23
 
 
24
 
class BranchWSGIApp(object):
25
 
 
26
 
    def __init__(self, branch, friendly_name=None, config={}, graph_cache=None):
27
 
        self.branch = branch
28
 
        self._config = config
29
 
        self.friendly_name = friendly_name
30
 
        self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
31
 
        if graph_cache is None:
32
 
            graph_cache = bzrlib.lru_cache.LRUCache()
33
 
        self.graph_cache = graph_cache
34
 
 
35
 
    def get_history(self):
36
 
        _history = History(self.branch, self.graph_cache)
37
 
        cache_path = self._config.get('cachepath', None)
38
 
        if cache_path is not None:
39
 
            # Only import the cache if we're going to use it.
40
 
            # This makes sqlite optional
41
 
            try:
42
 
                from loggerhead.changecache import FileChangeCache
43
 
            except ImportError:
44
 
                self.log.debug("Couldn't load python-sqlite,"
45
 
                               " continuing without using a cache")
46
 
            else:
47
 
                _history.use_file_cache(
48
 
                    FileChangeCache(_history, cache_path))
49
 
        return _history
50
 
 
51
 
    def url(self, *args, **kw):
52
 
        if isinstance(args[0], list):
53
 
            args = args[0]
54
 
        qs = []
55
 
        for k, v in kw.iteritems():
56
 
            if v is not None:
57
 
                qs.append('%s=%s'%(k, urllib.quote(v)))
58
 
        qs = '&'.join(qs)
59
 
        return request.construct_url(
60
 
            self._environ, script_name=self._url_base,
61
 
            path_info='/'.join(args),
62
 
            querystring=qs)
63
 
 
64
 
    def context_url(self, *args, **kw):
65
 
        kw = util.get_context(**kw)
66
 
        return self.url(*args, **kw)
67
 
 
68
 
    def static_url(self, path):
69
 
        return self._static_url_base + path
70
 
 
71
 
    controllers_dict = {
72
 
        'annotate': AnnotateUI,
73
 
        'changes': ChangeLogUI,
74
 
        'files': InventoryUI,
75
 
        'revision': RevisionUI,
76
 
        'download': DownloadUI,
77
 
        'atom': AtomUI,
78
 
        'search': SearchUI,
79
 
        }
80
 
 
81
 
    def last_updated(self):
82
 
        h = self.get_history()
83
 
        change = h.get_changes([ h.last_revid ])[0]
84
 
        return change.date
85
 
 
86
 
    def branch_url(self):
87
 
        return self.branch.get_config().get_user_option('public_branch')
88
 
 
89
 
    def app(self, environ, start_response):
90
 
        self._url_base = environ['SCRIPT_NAME']
91
 
        self._static_url_base = environ.get('loggerhead.static.url')
92
 
        if self._static_url_base is None:
93
 
            self._static_url_base = self._url_base
94
 
        self._environ = environ
95
 
        path = request.path_info_pop(environ)
96
 
        if not path:
97
 
            raise httpexceptions.HTTPMovedPermanently(
98
 
                self._url_base + '/changes')
99
 
        if path == 'static':
100
 
            return static_app(environ, start_response)
101
 
        cls = self.controllers_dict.get(path)
102
 
        if cls is None:
103
 
            raise httpexceptions.HTTPNotFound()
104
 
        self.branch.lock_read()
105
 
        try:
106
 
            c = cls(self, self.get_history())
107
 
            return c(environ, start_response)
108
 
        finally:
109
 
            self.branch.unlock()