annotate pylons_app/lib/helpers.py @ 153:a5a3bcc5ee89

Added colored formatter to project, and configs
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 16 May 2010 15:06:20 +0200
parents 4cea52709743
children ea893ffb7f00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 """Helper functions
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
3 Consists of functions to typically be used within templates, but also
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
4 available to Controllers. This module is available to both as 'h'.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
5 """
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents: 0
diff changeset
6 from pylons import url
97
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
7 from pylons.i18n.translation import _, ungettext
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
8 from webhelpers.html import (literal, HTML, escape)
98
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
9 from webhelpers.html.builder import make_tag
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
10 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
11 , mail_to, strip_links, strip_tags, tag_re)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
12 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes,
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
13 end_form, file, form, hidden, image,
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
14 javascript_link, link_to, link_to_if,
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
15 link_to_unless, ol, required_legend,
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
16 select, stylesheet_link,
94
0bb9391bc287 webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
17 submit, text, password, textarea, title,
0bb9391bc287 webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
18 ul, xml_declaration)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
19 from webhelpers.text import (chop_at, collapse, convert_accented_entities,
94
0bb9391bc287 webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
20 convert_misc_entities, lchop, plural, rchop,
0bb9391bc287 webhelpers update
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
21 remove_formatting, replace_whitespace, urlify)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
22
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
23 from webhelpers.pylonslib import Flash as _Flash
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
24 from webhelpers.pylonslib.secure_form import secure_form
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
25
98
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
26 from pygments import highlight
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
27 from pygments.formatters import HtmlFormatter
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
28 from pygments.lexers import guess_lexer
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
29 from pygments.lexers import get_lexer_by_name
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
30
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
31 #Custom helper here :)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
32 class _Link(object):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
33 '''
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
34 Make a url based on label and url with help of url_for
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
35 @param label:name of link if not defined url is used
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
36 @param url: the url for link
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
37 '''
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
38
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents: 0
diff changeset
39 def __call__(self, label='', *url_, **urlargs):
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
40 if label is None or '':
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
41 label = url
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents: 0
diff changeset
42 link_fn = link_to(label, url(*url_, **urlargs))
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
43 return link_fn
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
44
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
45
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
46 class _GetError(object):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
47
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
48 def __call__(self, field_name, form_errors):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
49 tmpl = """<span class="error_msg">%s</span>"""
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
50 if form_errors and form_errors.has_key(field_name):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
51 return literal(tmpl % form_errors.get(field_name))
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
52
98
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
53 class _FileSizeFormat(object):
97
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
54 """
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
55 Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
56 102 bytes, etc).
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
57 """
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
58 def __call__(self, bytes):
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
59 try:
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
60 bytes = float(bytes)
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
61 except TypeError:
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
62 return u"0 bytes"
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
63
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
64 if bytes < 1024:
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
65 return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
66 if bytes < 1024 * 1024:
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
67 return _("%.1f KB") % (bytes / 1024)
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
68 if bytes < 1024 * 1024 * 1024:
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
69 return _("%.1f MB") % (bytes / (1024 * 1024))
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
70 return _("%.1f GB") % (bytes / (1024 * 1024 * 1024))
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
71
102
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
72 class _FilesBreadCrumbs(object):
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
73
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
74 def __call__(self, repo_name, rev, paths):
104
4cea52709743 fixed file browser breadcrumbs with revision
Marcin Kuzminski <marcin@python-works.com>
parents: 102
diff changeset
75 url_l = [link_to(repo_name, url('files_home', repo_name=repo_name, revision=rev, f_path=''))]
102
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
76 paths_l = paths.split('/')
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
77
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
78 for cnt, p in enumerate(paths_l, 1):
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
79 if p != '':
104
4cea52709743 fixed file browser breadcrumbs with revision
Marcin Kuzminski <marcin@python-works.com>
parents: 102
diff changeset
80 url_l.append(link_to(p, url('files_home', repo_name=repo_name, revision=rev, f_path='/'.join(paths_l[:cnt]))))
97
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
81
102
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
82 return literal(' / '.join(url_l))
98
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
83
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
84 def pygmentize(code, **kwargs):
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
85 '''
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
86 Filter for chunks of html to replace code tags with pygmented code
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
87 '''
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
88 return literal(highlight(code, guess_lexer(code), HtmlFormatter(**kwargs)))
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
89
01d0f363f36d added pygments webhelper
Marcin Kuzminski <marcin@python-works.com>
parents: 97
diff changeset
90
102
2dc0c8e4f384 Updated tempaltes, added file browser breadcrumbs, and feed icons
Marcin Kuzminski <marcin@python-works.com>
parents: 98
diff changeset
91 files_breadcrumbs = _FilesBreadCrumbs()
97
be0096a02772 added helper for filesize
Marcin Kuzminski <marcin@python-works.com>
parents: 94
diff changeset
92 filesizeformat = _FileSizeFormat()
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
93 link = _Link()
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
94 flash = _Flash()
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
95 get_error = _GetError()