1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-01-29 01:25:46 +01:00

Moving human_readable formatting function out of __init__.

There is a module, where this function is used - formatter_functions. It
is at the same time the only place where it is used. No need to clutter
__init__.py with it.
This commit is contained in:
2020-09-30 20:24:20 +02:00
parent a5880b2445
commit e213b7a64b
2 changed files with 18 additions and 20 deletions

View File

@@ -6,7 +6,6 @@ Created on 13 Jan 2011
import inspect, re, traceback, numbers
from math import trunc
from ebook_converter import human_readable
from ebook_converter.constants_old import DEBUG
from ebook_converter.ebooks.metadata import title_sort
from ebook_converter.utils.config_base import tweaks
@@ -860,15 +859,29 @@ class BuiltinHumanReadable(BuiltinFormatterFunction):
arg_count = 1
category = 'Formatting values'
__doc__ = doc = ('human_readable(v) -- return a string '
'representing the number v in KB, MB, GB, etc.'
)
'representing the number v in KB, MB, GB, etc.')
def evaluate(self, formatter, kwargs, mi, locals, val):
try:
return human_readable(round(float(val)))
except:
return self.human_readable(round(float(val)))
except Exception:
return ''
def _human_readable(self, size, sep=' '):
""" Convert a size in bytes into a human readable form """
divisor, suffix = 1, "B"
for i, candidate in enumerate(('B', 'KB', 'MB', 'GB', 'TB', 'PB',
'EB')):
if size < (1 << ((i + 1) * 10)):
divisor, suffix = (1 << (i * 10)), candidate
break
size = str(float(size)/divisor)
if size.find(".") > -1:
size = size[:size.find(".")+2]
if size.endswith('.0'):
size = size[:-2]
return size + sep + suffix
class BuiltinFormatNumber(BuiltinFormatterFunction):
name = 'format_number'