~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

  • Committer: Michael Hudson
  • Date: 2008-12-15 21:26:51 UTC
  • mfrom: (255.1.1 trunk)
  • Revision ID: michael.hudson@canonical.com-20081215212651-buc09dcygemvb9lt
Tags: 1.10
fix ConfigObj import (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
 
2
# Copyright (C) 2008  Canonical Ltd.
2
3
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
 
4
# Copyright (C) 2006  Goffredo Baroncelli <kreijack@inwind.it>
3
5
#
4
6
# This program is free software; you can redistribute it and/or modify
5
7
# it under the terms of the GNU General Public License as published by
14
16
# You should have received a copy of the GNU General Public License
15
17
# along with this program; if not, write to the Free Software
16
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
#
18
 
 
19
 
import logging
20
 
 
21
 
import turbogears
22
 
from turbogears import controllers
23
 
from cherrypy import HTTPRedirect, NotFound
24
 
 
25
 
"""
26
 
from okulo.controllers.page_ui import PageUI
27
 
from okulo.controllers.login_ui import LoginUI, LogoutUI
28
 
from okulo.controllers.history_ui import HistoryUI
29
 
from okulo.controllers.edit_ui import EditUI
30
 
from okulo.controllers.contents_ui import ContentsUI
31
 
from okulo.controllers.source_ui import SourceUI
32
 
from okulo.controllers.search_ui import SearchUI
33
 
from okulo.controllers.add_ui import AddUI
34
 
from okulo.controllers.signup_ui import SignupUI
35
 
from okulo.controllers.atom_ui import AtomUI
36
 
from okulo.controllers.userhome_ui import UserHomeUI
37
 
from okulo.controllers.delete_ui import DeleteUI
38
 
"""
39
 
from loggerhead.controllers.changelog_ui import ChangeLogUI
40
 
from loggerhead.controllers.atom_ui import AtomUI
41
 
 
42
 
 
43
 
log = logging.getLogger("loggerhead.controllers")
44
 
 
45
 
class Root (controllers.RootController):
46
 
    changes = ChangeLogUI()
47
 
    atom = AtomUI()
48
 
    
49
 
    @turbogears.expose(template="loggerhead.templates.welcome")
50
 
    def index(self):
51
 
        import time
52
 
        log.debug("Happy TurboGears Controller Responding For Duty")
53
 
        return dict(now=time.ctime())
54
 
 
55
 
"""
56
 
            if args['cmd'][0] == 'changelog':
57
 
 
58
 
                otherrevid = get("otherrevid")
59
 
                pathrevid = get("pathrevid")
60
 
                path = get("path")
61
 
 
62
 
                self.write(self.changelog(revno, None, path, pathrevid, otherrevid))
63
 
 
64
 
    def changelog(self, revid, search=None, path=None,
65
 
        pathrevid=None, otherrevid=None ):
66
 
            import cmd_changelog
67
 
            return cmd_changelog.changelog(self, revid, search, path,
68
 
                pathrevid, otherrevid )
69
 
 
70
 
 
71
 
 
72
 
    revid, history, pathrevid, otherrevid =
73
 
     hgweb.compute_history(revid, path,        pathrevid, otherrevid )
74
 
                                                    
75
 
                                                    
76
 
class Root (controllers.Root):
77
 
    page = PageUI()
78
 
    login = LoginUI()
79
 
    logout = LogoutUI()
80
 
    history = HistoryUI()
81
 
    edit = EditUI()
82
 
    contents = ContentsUI()
83
 
    source = SourceUI()
84
 
    search = SearchUI()
85
 
    add = AddUI()
86
 
    signup = SignupUI()
87
 
    atom = AtomUI()
88
 
    delete = DeleteUI()
89
 
    
90
 
    @turbogears.expose()
91
 
    def index(self, *args):
92
 
        raise HTTPRedirect(turbogears.url('/page/start'))
93
 
    
94
 
    @turbogears.expose()
95
 
    def default(self, *args):
96
 
        if args[0].startswith('~'):
97
 
            home = UserHomeUI(args[0][1:])
98
 
            if len(args) == 1:
99
 
                return home.index()
100
 
            return home.default(*args[1:])
101
 
        raise NotFound()
102
 
"""
 
19
 
 
20
import re
 
21
import time
 
22
 
 
23
from paste.request import path_info_pop, parse_querystring
 
24
 
 
25
from loggerhead import util
 
26
from loggerhead.templatefunctions import templatefunctions
 
27
from loggerhead.zptsupport import load_template
 
28
 
 
29
 
 
30
class BufferingWriter(object):
 
31
 
 
32
    def __init__(self, writefunc, buf_limit):
 
33
        self.bytes = 0
 
34
        self.buf = []
 
35
        self.buflen = 0
 
36
        self.writefunc = writefunc
 
37
        self.bytes_saved = 0
 
38
        self.buf_limit = buf_limit
 
39
 
 
40
    def flush(self):
 
41
        chunk = ''.join(self.buf)
 
42
        chunk = re.sub(r'\s*\n\s*', '\n', chunk)
 
43
        chunk = re.sub(r'[ \t]+', ' ', chunk)
 
44
        self.bytes_saved += self.buflen - len(chunk)
 
45
        self.writefunc(chunk)
 
46
        self.buf = []
 
47
        self.buflen = 0
 
48
 
 
49
    def write(self, data):
 
50
        self.buf.append(data)
 
51
        self.buflen += len(data)
 
52
        self.bytes += len(data)
 
53
        if self.buflen > self.buf_limit:
 
54
            self.flush()
 
55
 
 
56
 
 
57
class TemplatedBranchView(object):
 
58
 
 
59
    template_path = None
 
60
 
 
61
    def __init__(self, branch, history_callable):
 
62
        self._branch = branch
 
63
        self._history_callable = history_callable
 
64
        self.__history = None
 
65
        self.log = branch.log
 
66
 
 
67
    @property
 
68
    def _history(self):
 
69
        if self.__history is not None:
 
70
            return self.__history
 
71
        self.__history = self._history_callable()
 
72
        return self.__history
 
73
 
 
74
    def __call__(self, environ, start_response):
 
75
        z = time.time()
 
76
        kwargs = dict(parse_querystring(environ))
 
77
        util.set_context(kwargs)
 
78
        args = []
 
79
        while 1:
 
80
            arg = path_info_pop(environ)
 
81
            if arg is None:
 
82
                break
 
83
            args.append(arg)
 
84
 
 
85
        path = None
 
86
        if len(args) > 1:
 
87
            path = '/'.join(args[1:])
 
88
        self.args = args
 
89
 
 
90
        vals = {
 
91
            'static_url': self._branch.static_url,
 
92
            'branch': self._branch,
 
93
            'util': util,
 
94
            'url': self._branch.context_url,
 
95
        }
 
96
        vals.update(templatefunctions)
 
97
        headers = {}
 
98
 
 
99
        vals.update(self.get_values(path, kwargs, headers))
 
100
 
 
101
        self.log.info('Getting information for %s: %r secs' % (
 
102
            self.__class__.__name__, time.time() - z))
 
103
        if 'Content-Type' not in headers:
 
104
            headers['Content-Type'] = 'text/html'
 
105
        writer = start_response("200 OK", headers.items())
 
106
        template = load_template(self.template_path)
 
107
        z = time.time()
 
108
        w = BufferingWriter(writer, 8192)
 
109
        template.expand_into(w, **vals)
 
110
        w.flush()
 
111
        self.log.info(
 
112
            'Rendering %s: %r secs, %s bytes, %s (%2.1f%%) bytes saved' % (
 
113
                self.__class__.__name__,
 
114
                time.time() - z,
 
115
                w.bytes,
 
116
                w.bytes_saved,
 
117
                100.0*w.bytes_saved/w.bytes))
 
118
        return []
 
119
 
 
120
    def get_revid(self):
 
121
        h = self._history
 
122
        if h is None:
 
123
            return None
 
124
        if len(self.args) > 0:
 
125
            return h.fix_revid(self.args[0])
 
126
        else:
 
127
            return h.last_revid