~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Robey Pointer
  • Date: 2007-01-14 01:24:15 UTC
  • Revision ID: robey@lag.net-20070114012415-3hh0t8vxapxmwt1x
use uniq() to trim the revision page down a bit, and build an element cache
in the javascript code to make response time much better on the disclosure
triangles.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import logging
2
 
import urllib
3
 
 
4
 
from paste import request
5
 
from paste import httpexceptions
6
 
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
7
 
 
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
18
 
 
19
 
logging.basicConfig()
20
 
logging.getLogger().setLevel(logging.DEBUG)
21
 
 
22
 
class BranchWSGIApp(object):
23
 
 
24
 
    def __init__(self, branch_url, friendly_name=None, config={}):
25
 
        self.branch_url = branch_url
26
 
        self._history = None
27
 
        self._config = config
28
 
        self.friendly_name = friendly_name
29
 
        self.log = logging.getLogger(friendly_name)
30
 
 
31
 
    @property
32
 
    def history(self):
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))
39
 
        return self._history
40
 
 
41
 
    def url(self, *args, **kw):
42
 
        if isinstance(args[0], list):
43
 
            args = args[0]
44
 
        qs = []
45
 
        for k, v in kw.iteritems():
46
 
            if v is not None:
47
 
                qs.append('%s=%s'%(k, urllib.quote(v)))
48
 
        qs = '&'.join(qs)
49
 
        return request.construct_url(
50
 
            self._environ, script_name=self._url_base,
51
 
            path_info='/'.join(args),
52
 
            querystring=qs)
53
 
 
54
 
    def context_url(self, *args, **kw):
55
 
        kw = util.get_context(**kw)
56
 
        return self.url(*args, **kw)
57
 
 
58
 
    def static_url(self, path):
59
 
        return self._static_url_base + path
60
 
 
61
 
    controllers_dict = {
62
 
        'annotate': AnnotateUI,
63
 
        'changes': ChangeLogUI,
64
 
        'files': InventoryUI,
65
 
        'revision': RevisionUI,
66
 
        'download': DownloadUI,
67
 
        'atom': AtomUI,
68
 
        }
69
 
 
70
 
    def last_updated(self):
71
 
        h = self.history
72
 
        change = h.get_changes([ h.last_revid ])[0]
73
 
        return change.date
74
 
 
75
 
    def branch_url(self):
76
 
        return self.history.get_config().get_user_option('public_branch')
77
 
 
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)
88
 
        if not path:
89
 
            raise httpexceptions.HTTPMovedPermanently(
90
 
                self._url_base + '/changes')
91
 
        if path == 'static':
92
 
            return static_app(environ, start_response)
93
 
        cls = self.controllers_dict.get(path)
94
 
        if cls is None:
95
 
            raise httpexceptions.HTTPNotFound()
96
 
        c = cls(self)
97
 
        c.default(req, response)
98
 
        return response(environ, start_response)