filer.templatetags.filer_tags: 53 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/filer/templatetags/filer_tags.py

Stats: 0 executed, 51 missed, 2 excluded, 49 ignored

  1. #-*- coding: utf-8 -*-
  2. from django.template import Library
  3. import math
  4. register = Library()
  5. # The templatetag below is copied from sorl.thumbnail
  6. filesize_formats = ['k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
  7. filesize_long_formats = {
  8. 'k': 'kilo', 'M': 'mega', 'G': 'giga', 'T': 'tera', 'P': 'peta',
  9. 'E': 'exa', 'Z': 'zetta', 'Y': 'yotta',
  10. }
  11. def filesize(bytes, format='auto1024'):
  12. """
  13. Returns the number of bytes in either the nearest unit or a specific unit
  14. (depending on the chosen format method).
  15. Acceptable formats are:
  16. auto1024, auto1000
  17. convert to the nearest unit, appending the abbreviated unit name to the
  18. string (e.g. '2 KiB' or '2 kB').
  19. auto1024 is the default format.
  20. auto1024long, auto1000long
  21. convert to the nearest multiple of 1024 or 1000, appending the correctly
  22. pluralized unit name to the string (e.g. '2 kibibytes' or '2 kilobytes').
  23. kB, MB, GB, TB, PB, EB, ZB or YB
  24. convert to the exact unit (using multiples of 1000).
  25. KiB, MiB, GiB, TiB, PiB, EiB, ZiB or YiB
  26. convert to the exact unit (using multiples of 1024).
  27. The auto1024 and auto1000 formats return a string, appending the correct
  28. unit to the value. All other formats return the floating point value.
  29. If an invalid format is specified, the bytes are returned unchanged.
  30. """
  31. format_len = len(format)
  32. # Check for valid format
  33. if format_len in (2, 3):
  34. if format_len == 3 and format[0] == 'K':
  35. format = 'k%s' % format[1:]
  36. if not format[-1] == 'B' or format[0] not in filesize_formats:
  37. return bytes
  38. if format_len == 3 and format[1] != 'i':
  39. return bytes
  40. elif format not in ('auto1024', 'auto1000',
  41. 'auto1024long', 'auto1000long'):
  42. return bytes
  43. # Check for valid bytes
  44. try:
  45. bytes = long(bytes)
  46. except (ValueError, TypeError):
  47. return bytes
  48. # Auto multiple of 1000 or 1024
  49. if format.startswith('auto'):
  50. if format[4:8] == '1000':
  51. base = 1000
  52. else:
  53. base = 1024
  54. logarithm = bytes and math.log(bytes, base) or 0
  55. index = min(int(logarithm) - 1, len(filesize_formats) - 1)
  56. if index >= 0:
  57. if base == 1000:
  58. bytes = bytes and bytes / math.pow(1000, index + 1)
  59. else:
  60. bytes = bytes >> (10 * (index))
  61. bytes = bytes and bytes / 1024.0
  62. unit = filesize_formats[index]
  63. else:
  64. # Change the base to 1000 so the unit will just output 'B' not 'iB'
  65. base = 1000
  66. unit = ''
  67. if bytes >= 10 or ('%.1f' % bytes).endswith('.0'):
  68. bytes = '%.0f' % bytes
  69. else:
  70. bytes = '%.1f' % bytes
  71. if format.endswith('long'):
  72. unit = filesize_long_formats.get(unit, '')
  73. if base == 1024 and unit:
  74. unit = '%sbi' % unit[:2]
  75. unit = '%sbyte%s' % (unit, bytes != '1' and 's' or '')
  76. else:
  77. unit = '%s%s' % (base == 1024 and unit.upper() or unit,
  78. base == 1024 and 'iB' or 'B')
  79. return '%s %s' % (bytes, unit)
  80. if bytes == 0:
  81. return bytes
  82. base = filesize_formats.index(format[0]) + 1
  83. # Exact multiple of 1000
  84. if format_len == 2:
  85. return bytes / (1000.0 ** base)
  86. # Exact multiple of 1024
  87. elif format_len == 3:
  88. bytes = bytes >> (10 * (base - 1))
  89. return bytes / 1024.0
  90. register.filter(filesize)