~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/templatefunctions.py

  • Committer: j.c.sackett
  • Date: 2011-11-23 20:08:17 UTC
  • mfrom: (459.1.2 add-lock-icon)
  • Revision ID: jonathan.sackett@canonical.com-20111123200817-b1wad8vgo9aa7siw
Updated css to include the privacy icon to match the LP privacy notification style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This program is free software; you can redistribute it and/or modify
 
3
# it under the terms of the GNU General Public License as published by
 
4
# the Free Software Foundation; either version 2 of the License, or
 
5
# (at your option) any later version.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
#
 
16
 
 
17
import os
 
18
import urllib
 
19
 
 
20
import pkg_resources
 
21
 
 
22
import bzrlib
 
23
 
 
24
import loggerhead
 
25
from loggerhead.zptsupport import zpt
 
26
from loggerhead.util import html_format
 
27
 
 
28
 
 
29
templatefunctions = {}
 
30
 
 
31
 
 
32
def templatefunc(func):
 
33
    templatefunctions[func.__name__] = func
 
34
    return func
 
35
 
 
36
 
 
37
_base = os.path.dirname(__file__)
 
38
 
 
39
 
 
40
def _pt(name):
 
41
    return zpt(os.path.join(_base, 'templates', name + '.pt'))
 
42
 
 
43
 
 
44
templatefunctions['macros'] = _pt('macros').macros
 
45
templatefunctions['breadcrumbs'] = _pt('breadcrumbs').macros
 
46
 
 
47
 
 
48
@templatefunc
 
49
def file_change_summary(url, entry, file_changes, style='normal', currently_showing=None):
 
50
    if style == 'fragment':
 
51
        def file_link(filename):
 
52
            if currently_showing and filename == currently_showing:
 
53
                return html_format(
 
54
                    '<b><a href="#%s">%s</a></b>',
 
55
                    urllib.quote(filename.encode('utf-8')), filename)
 
56
            else:
 
57
                return revision_link(
 
58
                    url, entry.revno, filename,
 
59
                    '#' + urllib.quote(filename.encode('utf-8')))
 
60
    else:
 
61
        def file_link(filename):
 
62
            return html_format(
 
63
                '<a href="%s%s" title="View changes to %s in revision %s">'
 
64
                '%s</a>',
 
65
                url(['/revision', entry.revno]),
 
66
                '#' + urllib.quote(filename.encode('utf-8')),
 
67
                filename, entry.revno, filename)
 
68
    return _pt('revisionfilechanges').expand(
 
69
        entry=entry, file_changes=file_changes, file_link=file_link, **templatefunctions)
 
70
 
 
71
 
 
72
@templatefunc
 
73
def revisioninfo(url, branch, entry, file_changes=None, currently_showing=None, merged_in=None):
 
74
    from loggerhead import util
 
75
    return _pt('revisioninfo').expand(
 
76
        url=url, change=entry, branch=branch, util=util,
 
77
        file_changes=file_changes, currently_showing=currently_showing,
 
78
        merged_in=merged_in, **templatefunctions)
 
79
 
 
80
 
 
81
@templatefunc
 
82
def branchinfo(branch):
 
83
    if branch.served_url is not None:
 
84
        return _pt('branchinfo').expand(branch=branch, **templatefunctions)
 
85
    else:
 
86
        return ''
 
87
 
 
88
 
 
89
@templatefunc
 
90
def collapse_button(group, name, branch, normal='block'):
 
91
    return _pt('collapse-button').expand(
 
92
        group=group, name=name, normal=normal, branch=branch,
 
93
        **templatefunctions)
 
94
 
 
95
 
 
96
@templatefunc
 
97
def collapse_all_button(group, branch, normal='block'):
 
98
    return _pt('collapse-all-button').expand(
 
99
        group=group, normal=normal, branch=branch,
 
100
        **templatefunctions)
 
101
 
 
102
 
 
103
@templatefunc
 
104
def revno_with_nick(entry):
 
105
    if entry.branch_nick:
 
106
        extra = ' ' + entry.branch_nick
 
107
    else:
 
108
        extra = ''
 
109
    return '(%s%s)' % (entry.revno, extra)
 
110
 
 
111
 
 
112
@templatefunc
 
113
def search_box(branch, navigation):
 
114
    return _pt('search-box').expand(branch=branch, navigation=navigation,
 
115
        **templatefunctions)
 
116
 
 
117
 
 
118
@templatefunc
 
119
def feed_link(branch, url):
 
120
    return _pt('feed-link').expand(branch=branch, url=url, **templatefunctions)
 
121
 
 
122
 
 
123
@templatefunc
 
124
def menu(branch, url, fileview_active=False):
 
125
    return _pt('menu').expand(branch=branch, url=url,
 
126
        fileview_active=fileview_active, **templatefunctions)
 
127
 
 
128
 
 
129
@templatefunc
 
130
def view_link(url, revno, path):
 
131
    return html_format(
 
132
        '<a href="%s" title="Annotate %s">%s</a>',
 
133
        url(['/view', revno, path]), path, path)
 
134
 
 
135
 
 
136
@templatefunc
 
137
def revision_link(url, revno, path, frag=''):
 
138
    return html_format(
 
139
        '<a href="%s%s" title="View changes to %s in revision %s">%s</a>',
 
140
        url(['/revision', revno, path]), frag, path, revno, path)
 
141
 
 
142
 
 
143
@templatefunc
 
144
def loggerhead_version():
 
145
    return loggerhead.__version__
 
146
 
 
147
_cached_generator_string = None
 
148
 
 
149
@templatefunc
 
150
def generator_string():
 
151
    global _cached_generator_string
 
152
    if _cached_generator_string is None:
 
153
        versions = []
 
154
 
 
155
        # TODO: Errors -- e.g. from a missing/invalid __version__ attribute, or
 
156
        # ValueError accessing Distribution.version -- should be non-fatal.
 
157
 
 
158
        versions.append(('Loggerhead', loggerhead.__version__))
 
159
 
 
160
        import sys
 
161
        python_version = bzrlib._format_version_tuple(sys.version_info)
 
162
        versions.append(('Python', python_version))
 
163
 
 
164
        versions.append(('Bazaar', bzrlib.__version__))
 
165
 
 
166
        Paste = pkg_resources.get_distribution('Paste')
 
167
        versions.append(('Paste', Paste.version))
 
168
 
 
169
        try:
 
170
            PasteDeploy = pkg_resources.get_distribution('PasteDeploy')
 
171
        except pkg_resources.DistributionNotFound:
 
172
            pass
 
173
        else:
 
174
            versions.append(('PasteDeploy', PasteDeploy.version))
 
175
 
 
176
        import simpletal
 
177
        versions.append(('SimpleTAL', simpletal.__version__))
 
178
 
 
179
        try:
 
180
            import pygments
 
181
        except ImportError:
 
182
            pass
 
183
        else:
 
184
            versions.append(('Pygments', pygments.__version__))
 
185
 
 
186
        try:
 
187
            from bzrlib.plugins import search
 
188
        except ImportError:
 
189
            pass
 
190
        else:
 
191
            bzr_search_version = bzrlib._format_version_tuple(
 
192
                search.version_info)
 
193
            versions.append(('bzr-search', bzr_search_version))
 
194
 
 
195
        # TODO: On old Python versions, elementtree may be used.
 
196
 
 
197
        import simplejson
 
198
        versions.append(('simplejson', simplejson.__version__))
 
199
 
 
200
        try:
 
201
            Dozer = pkg_resources.get_distribution('Dozer')
 
202
        except pkg_resources.DistributionNotFound:
 
203
            pass
 
204
        else:
 
205
            versions.append(('Dozer', Dozer.version))
 
206
 
 
207
        version_strings = ("%s/%s" % t for t in versions)
 
208
        _cached_generator_string = ' '.join(version_strings)
 
209
    return _cached_generator_string