~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Robey Pointer
  • Date: 2006-12-11 06:44:19 UTC
  • Revision ID: robey@lag.net-20061211064419-8ssa7mlsiflpmy0c
initial checkin

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.controllers.bundle_ui import BundleUI
17
 
from loggerhead.history import History
18
 
from loggerhead import util
19
 
 
20
 
logging.basicConfig()
21
 
logging.getLogger().setLevel(logging.DEBUG)
22
 
 
23
 
class BranchWSGIApp(object):
24
 
 
25
 
    def __init__(self, branch_url, friendly_name=None, config={}):
26
 
        self.branch_url = branch_url
27
 
        self._history = None
28
 
        self._config = config
29
 
        self.friendly_name = friendly_name
30
 
        self.log = logging.getLogger(friendly_name)
31
 
 
32
 
    @property
33
 
    def history(self):
34
 
        if (self._history is None) or self._history.out_of_date():
35
 
            self.log.debug('Reload branch history...')
36
 
            _history = self._history = History.from_folder(self.branch_url)
37
 
            cache_path = self._config.get('cachepath', None)
38
 
            if cache_path is not None:
39
 
                _history.use_file_cache(FileChangeCache(_history, cache_path))
40
 
        return self._history
41
 
 
42
 
    def url(self, *args, **kw):
43
 
        if isinstance(args[0], list):
44
 
            args = args[0]
45
 
        qs = []
46
 
        for k, v in kw.iteritems():
47
 
            if v is not None:
48
 
                qs.append('%s=%s'%(k, urllib.quote(v)))
49
 
        qs = '&'.join(qs)
50
 
        return request.construct_url(
51
 
            self._environ, script_name=self._url_base,
52
 
            path_info='/'.join(args),
53
 
            querystring=qs)
54
 
 
55
 
    def context_url(self, *args, **kw):
56
 
        kw = util.get_context(**kw)
57
 
        return self.url(*args, **kw)
58
 
 
59
 
    def static_url(self, path):
60
 
        return self._static_url_base + path
61
 
 
62
 
    controllers_dict = {
63
 
        'annotate': AnnotateUI,
64
 
        'changes': ChangeLogUI,
65
 
        'files': InventoryUI,
66
 
        'revision': RevisionUI,
67
 
        'download': DownloadUI,
68
 
        'atom': AtomUI,
69
 
        'bundle': BundleUI,
70
 
        }
71
 
 
72
 
    def last_updated(self):
73
 
        h = self.history
74
 
        change = h.get_changes([ h.last_revid ])[0]
75
 
        return change.date
76
 
 
77
 
    def branch_url(self):
78
 
        return self.history.get_config().get_user_option('public_branch')
79
 
 
80
 
    def app(self, environ, start_response):
81
 
        req = WSGIRequest(environ)
82
 
        response = WSGIResponse()
83
 
        response.headers['Content-Type'] = 'text/plain'
84
 
        self._url_base = environ['SCRIPT_NAME']
85
 
        self._static_url_base = environ.get('loggerhead.static.url')
86
 
        if self._static_url_base is None:
87
 
            self._static_url_base = self._url_base
88
 
        self._environ = environ
89
 
        path = request.path_info_pop(environ)
90
 
        if not path:
91
 
            raise httpexceptions.HTTPMovedPermanently(
92
 
                self._url_base + '/changes')
93
 
        if path == 'static':
94
 
            return static_app(environ, start_response)
95
 
        cls = self.controllers_dict.get(path)
96
 
        if cls is None:
97
 
            raise httpexceptions.HTTPNotFound()
98
 
        c = cls(self)
99
 
        c.default(req, response)
100
 
        return response(environ, start_response)