From 2442e8fd43e8b4c8721a34e47c57d2c4f3ee5dab Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sat, 21 Apr 2018 17:10:29 -0700 Subject: [PATCH] Turn download_filesize into a class Instead of implicitly stashing c.seen on the template context, explicitly stash it on a class. This is mainly to avoid one of the two uses of c in h.pokedex, which will help during the move to pyramid. Updates #115. --- splinext/pokedex/content/dex/downloads.html | 3 +- splinext/pokedex/helpers.py | 66 +++++++++++---------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/splinext/pokedex/content/dex/downloads.html b/splinext/pokedex/content/dex/downloads.html index a3d35af4..e0cac18e 100644 --- a/splinext/pokedex/content/dex/downloads.html +++ b/splinext/pokedex/content/dex/downloads.html @@ -10,6 +10,7 @@ +<% c.sizer = h.pokedex.DownloadSizer() %> <%def name="dl_link(path, text=None)"> %if text: @@ -17,7 +18,7 @@ %else: ${caller.body()} %endif -(${h.pokedex.download_filesize(path)})\ +(${c.sizer.compute(path)})\ \ diff --git a/splinext/pokedex/helpers.py b/splinext/pokedex/helpers.py index bb07af83..3716f84c 100644 --- a/splinext/pokedex/helpers.py +++ b/splinext/pokedex/helpers.py @@ -836,37 +836,39 @@ def apply_move_template(template, move): return h.literal(template.safe_substitute(d)) -_file_size_units = 'B KB MB GB TB'.split() -def download_filesize(path): - try: - seen = c.seen - except AttributeError: - seen = c.seen = set() - if path in seen: - # Two download links for the same thing on one page - # Remove the "seen" stuff if this is ever legitimate - raise AssertionError('Copy/paste oversight! Two equal download links on one page') - seen.add(path) - root, me = os.path.split(__file__) - path = os.path.join(root, 'public', path) - try: - size = os.stat(path).st_size - except EnvironmentError: - raise EnvironmentError("Could not stat %s. Make sure to run spline-pokedex's bin/create-downloads.py script." % path) - def str_no_trailing_zero(num): - s = str(num) - if s.endswith('.0'): - s = s[:-2] - return s - for unit in _file_size_units: - if size < 1024: - if size >= 100: - return str(int(round(size))) + unit - elif size >= 10: - return str_no_trailing_zero(round(size, 1)) + unit + +class DownloadSizer(object): + file_size_units = 'B KB MB GB TB'.split() + + def __init__(self): + self.seen = set() + + def compute(self, path): + if path in self.seen: + # Two download links for the same thing on one page + # Remove the "seen" stuff if this is ever legitimate + raise AssertionError('Copy/paste oversight! Two equal download links on one page') + self.seen.add(path) + root, me = os.path.split(__file__) + path = os.path.join(root, 'public', path) + try: + size = os.stat(path).st_size + except EnvironmentError: + raise EnvironmentError("Could not stat %s. Make sure to run spline-pokedex's bin/create-downloads.py script." % path) + def str_no_trailing_zero(num): + s = str(num) + if s.endswith('.0'): + s = s[:-2] + return s + for unit in self.file_size_units: + if size < 1024: + if size >= 100: + return str(int(round(size))) + unit + elif size >= 10: + return str_no_trailing_zero(round(size, 1)) + unit + else: + return str_no_trailing_zero(round(size, 2)) + unit else: - return str_no_trailing_zero(round(size, 2)) + unit + size = size / 1024. else: - size = size / 1024. - else: - raise AssertionError('Serving a file of %s petabytes', size) + raise AssertionError('Serving a file of %s petabytes', size)