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
Provenance
- Commit
76f9aa4- Tree
3126228- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a0e3ac0
4 files changed+37 added−16 removed
modified:docs/templates.rst+11 added
662 unmodified lines663 </dl>664 {% endcall %}665 666 667+Filters668+~~~~~~~669+670+Filter sections allow you to apply regular Jinja2 filters on a block of671+template data. Just wrap the code in the special `filter` section::672+673+ {% filter upper %}674+ This text becomes uppercase675+ {% endfilter %}676+677+678 Assignments679 ~~~~~~~~~~~680 681 Inside code blocks you can also assign values to variables. Assignments at445 unmodified linesmodified:jinja2/filters.py+13 added-14 removed
262 unmodified lines263 except IndexError:264 return environment.undefined('No random item, sequence was empty.')265 266 267-def do_filesizeformat(value):267+def 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 = 0276-277- 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)283 284 285 def do_pprint(value, verbose=False):286 """Pretty print a variable. Useful for debugging.401 unmodified linesmodified:tests/test_filters.py+10 added-2 removed
22 unmodified lines23 FILESIZEFORMAT = '{{ 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) }}'33 FIRST = '''{{ foo|first }}'''34 FLOAT = '''{{ "42"|float }}|{{ "ajsghasjgd"|float }}|{{ "32.32"|float }}'''35 FORMAT = '''{{ "%s|%s"|format("a", "b") }}'''36 INDENT = '''{{ foo|indent(2) }}|{{ foo|indent(2, true) }}'''85 unmodified lines122 123 def 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+ )130 131 132 def test_first(env):133 tmpl = env.from_string(FIRST)193 unmodified linesmodified:CHANGES+3 added
25 unmodified lines26 to `false`.27 28 - improved error reporting for undefined values by providing a position.29 30+- `filesizeformat` filter uses decimal prefixes now per default and can be31+ set to binary mode with the second parameter.32+33 Version 2.0rc134 --------------35 (no codename, released on June 9th 2008)36 1 unmodified line