Skip to content

Commit

Permalink
Turn download_filesize into a class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
magical committed Apr 27, 2018
1 parent 673f984 commit 2442e8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
3 changes: 2 additions & 1 deletion splinext/pokedex/content/dex/downloads.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
</ul>
</%def>

<% c.sizer = h.pokedex.DownloadSizer() %>
<%def name="dl_link(path, text=None)">
<a href="${h.static_uri('pokedex', path)}">
%if text:
${text}
%else:
${caller.body()}
%endif
(${h.pokedex.download_filesize(path)})\
(${c.sizer.compute(path)})\
</a>\
</%def>

Expand Down
66 changes: 34 additions & 32 deletions splinext/pokedex/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2442e8f

Please sign in to comment.