~mkanat/loggerhead/raw-controller

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

  • Committer: Max Kanat-Alexander
  • Date: 2010-12-03 16:02:45 UTC
  • Revision ID: mkanat@bugzilla.org-20101203160245-64zkirwiydhnftmq
Refactor in preparation for adding the Raw controller. This simplifies getting
file information and sending output with no template.

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 os
20
21
import time
21
22
 
22
23
from paste.request import path_info_pop, parse_querystring
66
67
        return self.__history
67
68
 
68
69
    def __call__(self, environ, start_response):
69
 
        z = time.time()
 
70
        self._call_start = time.time()
70
71
        kwargs = dict(parse_querystring(environ))
71
72
        util.set_context(kwargs)
72
73
        args = []
75
76
            if arg is None:
76
77
                break
77
78
            args.append(arg)
78
 
 
 
79
        self.args = args
 
80
        
79
81
        path = None
80
82
        if len(args) > 1:
81
83
            path = unicode('/'.join(args[1:]), 'utf-8')
82
 
        self.args = args
83
 
 
 
84
        
 
85
        return self.get_output(path, kwargs, start_response)
 
86
    
 
87
    def get_output(self, path, kwargs, start_response):
84
88
        vals = {
85
89
            'static_url': self._branch.static_url,
86
90
            'branch': self._branch,
93
97
        vals.update(self.get_values(path, kwargs, headers))
94
98
 
95
99
        self.log.info('Getting information for %s: %.3f secs' % (
96
 
            self.__class__.__name__, time.time() - z))
 
100
            self.__class__.__name__, time.time() - self._call_start))
97
101
        if 'Content-Type' not in headers:
98
102
            headers['Content-Type'] = 'text/html'
99
103
        writer = start_response("200 OK", headers.items())
105
109
        self.log.info(
106
110
            'Rendering %s: %.3f secs, %s bytes' % (
107
111
                self.__class__.__name__, time.time() - z, w.bytes))
108
 
        return []
 
112
        return []        
 
113
    
 
114
    def file_info(self, path, kwargs):
 
115
        history = self._history
 
116
        revid = self.get_revid()
 
117
        revid = history.fix_revid(revid)
 
118
        file_id = kwargs.get('file_id', None)
 
119
        if (file_id is None) and (path is None):
 
120
            raise HTTPBadRequest('No file_id or filename provided')
109
121
 
 
122
        if file_id is None:
 
123
            file_id = history.get_file_id(revid, path)
 
124
        
 
125
        if path is None:
 
126
            path = history.get_path(revid, file_id)
 
127
        
 
128
        filename = os.path.basename(path)
 
129
        
 
130
        return {
 
131
            'file_id': file_id,
 
132
            'revid': revid,
 
133
            'filename': filename,
 
134
            'path': path,
 
135
        }
 
136
    
110
137
    def get_revid(self):
111
138
        h = self._history
112
139
        if h is None: