~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

Merge my old pep8 branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
 
import bzrlib.errors
21
 
import simplejson
22
20
import time
23
21
 
24
 
from paste.httpexceptions import HTTPNotFound
25
22
from paste.request import path_info_pop, parse_querystring
26
23
 
27
24
from loggerhead import util
54
51
class TemplatedBranchView(object):
55
52
 
56
53
    template_path = None
57
 
    supports_json = False
58
54
 
59
55
    def __init__(self, branch, history_callable):
60
56
        self._branch = branch
69
65
        self.__history = self._history_callable()
70
66
        return self.__history
71
67
 
72
 
    def parse_args(self, environ):
 
68
    def __call__(self, environ, start_response):
 
69
        z = time.time()
73
70
        kwargs = dict(parse_querystring(environ))
74
71
        util.set_context(kwargs)
75
72
        args = []
76
 
        while True:
 
73
        while 1:
77
74
            arg = path_info_pop(environ)
78
75
            if arg is None:
79
76
                break
83
80
        if len(args) > 1:
84
81
            path = unicode('/'.join(args[1:]), 'utf-8')
85
82
        self.args = args
86
 
        self.kwargs = kwargs
87
 
        return path
88
83
 
89
 
    def add_template_values(self, values):
90
 
        values.update({
 
84
        vals = {
91
85
            'static_url': self._branch.static_url,
92
86
            'branch': self._branch,
93
87
            'util': util,
94
88
            'url': self._branch.context_url,
95
 
        })
96
 
        values.update(templatefunctions)
97
 
 
98
 
    def __call__(self, environ, start_response):
99
 
        z = time.time()
100
 
        if environ.get('loggerhead.as_json') and not self.supports_json:
101
 
            raise HTTPNotFound
102
 
        path = self.parse_args(environ)
 
89
        }
 
90
        vals.update(templatefunctions)
103
91
        headers = {}
104
 
        values = self.get_values(path, self.kwargs, headers)
105
 
 
106
 
        self.log.info('Getting information for %s: %.3f secs' % (
 
92
 
 
93
        vals.update(self.get_values(path, kwargs, headers))
 
94
 
 
95
        self.log.info('Getting information for %s: %r secs' % (
107
96
            self.__class__.__name__, time.time() - z))
108
 
        if environ.get('loggerhead.as_json'):
109
 
            headers['Content-Type'] = 'application/json'
110
 
        elif 'Content-Type' not in headers:
 
97
        if 'Content-Type' not in headers:
111
98
            headers['Content-Type'] = 'text/html'
112
99
        writer = start_response("200 OK", headers.items())
113
 
        if environ.get('REQUEST_METHOD') == 'HEAD':
114
 
            # No content for a HEAD request
115
 
            return []
 
100
        template = load_template(self.template_path)
116
101
        z = time.time()
117
102
        w = BufferingWriter(writer, 8192)
118
 
        if environ.get('loggerhead.as_json'):
119
 
            w.write(simplejson.dumps(values,
120
 
                default=util.convert_to_json_ready))
121
 
        else:
122
 
            self.add_template_values(values)
123
 
            template = load_template(self.template_path)
124
 
            template.expand_into(w, **values)
 
103
        template.expand_into(w, **vals)
125
104
        w.flush()
126
105
        self.log.info(
127
 
            'Rendering %s: %.3f secs, %s bytes' % (
 
106
            'Rendering %s: %r secs, %s bytes' % (
128
107
                self.__class__.__name__, time.time() - z, w.bytes))
129
108
        return []
130
109
 
133
112
        if h is None:
134
113
            return None
135
114
        if len(self.args) > 0 and self.args != ['']:
136
 
            try:
137
 
                revid = h.fix_revid(self.args[0])
138
 
            except bzrlib.errors.NoSuchRevision:
139
 
                raise HTTPNotFound;
 
115
            return h.fix_revid(self.args[0])
140
116
        else:
141
 
            revid = h.last_revid
142
 
        return revid
 
117
            return h.last_revid