Skip to content

Commit

Permalink
Fixed filesizeformat
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Oct 5, 2011
1 parent 1898c24 commit b1b7b08
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -8,6 +8,7 @@ Version 2.7
- Choice and prefix loaders now dispatch source and template lookup
separately in order to work in combination with module loaders as
advertised.
- Fixed filesizeformat.

Version 2.6
-----------
Expand Down
30 changes: 15 additions & 15 deletions jinja2/filters.py
Expand Up @@ -15,7 +15,7 @@
from itertools import imap, groupby
from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode
from jinja2.runtime import Undefined
from jinja2.exceptions import FilterArgumentError, SecurityError
from jinja2.exceptions import FilterArgumentError


_word_re = re.compile(r'\w+(?u)')
Expand Down Expand Up @@ -346,25 +346,25 @@ def do_filesizeformat(value, binary=False):
bytes = float(value)
base = binary and 1024 or 1000
prefixes = [
(binary and "KiB" or "kB"),
(binary and "MiB" or "MB"),
(binary and "GiB" or "GB"),
(binary and "TiB" or "TB"),
(binary and "PiB" or "PB"),
(binary and "EiB" or "EB"),
(binary and "ZiB" or "ZB"),
(binary and "YiB" or "YB")
(binary and 'KiB' or 'kB'),
(binary and 'MiB' or 'MB'),
(binary and 'GiB' or 'GB'),
(binary and 'TiB' or 'TB'),
(binary and 'PiB' or 'PB'),
(binary and 'EiB' or 'EB'),
(binary and 'ZiB' or 'ZB'),
(binary and 'YiB' or 'YB')
]
if bytes == 1:
return "1 Byte"
return '1 Byte'
elif bytes < base:
return "%d Bytes" % bytes
return '%d Bytes' % bytes
else:
for i, prefix in enumerate(prefixes):
unit = base * base ** (i + 1)
if bytes < unit:
return "%.1f %s" % ((bytes / unit), prefix)
return "%.1f %s" % ((bytes / unit), prefix)
unit = base ** (i + 1)
if bytes <= unit:
return '%.1f %s' % ((bytes / unit), prefix)
return '%.1f %s' % ((bytes / unit), prefix)


def do_pprint(value, verbose=False):
Expand Down
8 changes: 4 additions & 4 deletions jinja2/testsuite/filters.py
Expand Up @@ -84,10 +84,10 @@ def test_filesizeformat(self):
'{{ 1000000000000|filesizeformat(true) }}'
)
out = tmpl.render()
assert out == (
'100 Bytes|0.0 kB|0.0 MB|0.0 GB|0.0 TB|100 Bytes|'
'1000 Bytes|1.0 KiB|0.9 MiB|0.9 GiB'
)
self.assert_equal(out, (
'100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'
'1000 Bytes|1.0 MiB|0.9 GiB|0.9 TiB'
))

def test_first(self):
tmpl = env.from_string('{{ foo|first }}')
Expand Down

0 comments on commit b1b7b08

Please sign in to comment.