Skip to content
Myr

Altered filesizeformat to support both MB and MiB, documented `{% filter %}`.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent a0e3ac0 · Browse files · 4 files+37 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+37 added−16 removed

  • tests:+10 added−2 removed
  • other:+27 added−14 removed

4 files+37 added−16 removed

modified:docs/templates.rst+11 added
662 unmodified lines
663 </dl>664 {% endcall %}665666667Filters668~~~~~~~669670Filter sections allow you to apply regular Jinja2 filters on a block of671template data. Just wrap the code in the special `filter` section::672673 {% filter upper %}674 This text becomes uppercase675 {% endfilter %}676677678Assignments679~~~~~~~~~~~680681Inside code blocks you can also assign values to variables. Assignments at
445 unmodified lines
modified:jinja2/filters.py+13 added-14 removed
262 unmodified lines
263 except IndexError:264 return environment.undefined('No random item, sequence was empty.')265266267def do_filesizeformat(value):267def do_filesizeformat(value, binary=False):268 """Format the value like a 'human-readable' file size (i.e. 13 KB,269 4.1 MB, 102 bytes, etc).269 4.1 MB, 102 bytes, etc). Per default decimal prefixes are used (mega,270 giga etc.), if the second parameter is set to `True` the binary271 prefixes are (mebi, gibi).272 """271 # fail silently272 try:273 bytes = float(value)274 except TypeError:275 bytes = 0276277 if bytes < 1024:273 bytes = float(value)274 base = binary and 1024 or 1000275 middle = binary and 'i' or ''276 if bytes < base:277 return "%d Byte%s" % (bytes, bytes != 1 and 's' or '')279 elif bytes < 1024 * 1024:280 return "%.1f KB" % (bytes / 1024)281 elif bytes < 1024 * 1024 * 1024:282 return "%.1f MB" % (bytes / (1024 * 1024))283 return "%.1f GB" % (bytes / (1024 * 1024 * 1024))278 elif bytes < base * base:279 return "%.1f K%sB" % (bytes / base, middle)280 elif bytes < base * base * base:281 return "%.1f M%sB" % (bytes / (base * base), middle)282 return "%.1f G%sB" % (bytes / (base * base * base), middle)283284285def do_pprint(value, verbose=False):286 """Pretty print a variable. Useful for debugging.
401 unmodified lines
modified:tests/test_filters.py+10 added-2 removed
22 unmodified lines
23FILESIZEFORMAT = '{{ 100|filesizeformat }}|\24{{ 1000|filesizeformat }}|\25{{ 1000000|filesizeformat }}|\26{{ 1000000000|filesizeformat }}|\27{{ 1000000000000|filesizeformat }}'27{{ 1000000000000|filesizeformat }}|\28{{ 100|filesizeformat(true) }}|\29{{ 1000|filesizeformat(true) }}|\30{{ 1000000|filesizeformat(true) }}|\31{{ 1000000000|filesizeformat(true) }}|\32{{ 1000000000000|filesizeformat(true) }}'33FIRST = '''{{ foo|first }}'''34FLOAT = '''{{ "42"|float }}|{{ "ajsghasjgd"|float }}|{{ "32.32"|float }}'''35FORMAT = '''{{ "%s|%s"|format("a", "b") }}'''36INDENT = '''{{ foo|indent(2) }}|{{ foo|indent(2, true) }}'''
85 unmodified lines
122123def test_filesizeformat(env):124 tmpl = env.from_string(FILESIZEFORMAT)125 out = tmpl.render()121 assert out == '100 Bytes|1000 Bytes|976.6 KB|953.7 MB|931.3 GB'126 assert out == (127 '100 Bytes|1.0 KB|1.0 MB|1.0 GB|1000.0 GB|'128 '100 Bytes|1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'129 )130131132def test_first(env):133 tmpl = env.from_string(FIRST)
193 unmodified lines
modified:CHANGES+3 added
25 unmodified lines
26 to `false`.2728- improved error reporting for undefined values by providing a position.2930- `filesizeformat` filter uses decimal prefixes now per default and can be31 set to binary mode with the second parameter.3233Version 2.0rc134--------------35(no codename, released on June 9th 2008)36
1 unmodified line