Skip to content

Commit

Permalink
Add explicit config param to pokemon_has_media
Browse files Browse the repository at this point in the history
For the pyramid port, which doesn't have threadlocal globals like
config. Most of the other helpers are going to be moving to dexlib,
where they'll have access to the request context, but pokemon_has_media
has to stay because it is used in a controller, so we have to get the
context to it some other way.

Also add a convenient wrapper function to dexlib which passes the config
parameter implicitly, and update the template code to call it.

Updates #117
  • Loading branch information
magical committed Jul 19, 2019
1 parent 44b3d9c commit 9ab6a52
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion splinext/pokedex/controllers/pokedex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ def sprite_exists(directory):
# n.b. calling dict.setdefault always evaluates the default
if directory not in c.sprites:
c.sprites[directory] = pokedex_helpers.pokemon_has_media(
c.form, directory, extension)
c.form, directory, extension, config)
return c.sprites[directory]
c.sprite_exists = sprite_exists

Expand Down
6 changes: 3 additions & 3 deletions splinext/pokedex/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def version_group_icon(version_group):
title=names)


def pokemon_has_media(pokemon_form, prefix, ext, use_form=True):
def pokemon_has_media(pokemon_form, prefix, ext, config, use_form=True):
"""Determine whether a file exists in the specified directory for the
specified Pokémon form.
"""
Expand Down Expand Up @@ -305,7 +305,7 @@ def pokemon_form_image(pokemon_form, prefix=None, **attr):
if prefix is None:
prefix = 'main-sprites/ultra-sun-ultra-moon'
# FIXME what the hell is going on here
if not pokemon_has_media(pokemon_form, prefix, 'png'):
if not pokemon_has_media(pokemon_form, prefix, 'png', config):
prefix = 'main-sprites/black-white'

# Deal with Spiky-eared Pichu and ??? Arceus
Expand Down Expand Up @@ -334,7 +334,7 @@ def pokemon_icon(pokemon, alt=True):
return h.literal('<span class="sprite-icon sprite-icon-%d"></span>' % pokemon.species.id)

alt_text = pokemon.name if alt else u''
if pokemon_has_media(pokemon.default_form, 'icons', 'png'):
if pokemon_has_media(pokemon.default_form, 'icons', 'png', config):
return pokemon_form_image(pokemon.default_form, prefix='icons', alt=alt_text)

return pokedex_img('pokemon/icons/0.png', title=pokemon.species.name, alt=alt_text)
Expand Down
10 changes: 9 additions & 1 deletion splinext/pokedex/templates/pokedex/lib.mako
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<%! from splinext.pokedex import i18n, db %>\
<%def name="pokemon_has_media(pokemon_form, prefix, ext, use_form=True)"><%
"""Determine whether a file exists in the specified directory for the specified Pokémon form.
Convenience wrapper around splinext.pokedex.helpers.pokemon_has_media which implicitly passes the app config.
"""
return h.pokedex.pokemon_has_media(pokemon_form, prefix, ext, config, use_form=use_form)
%></%def>


<%def name="pokemon_page_header(icon_form=None, subpages=True)">
<div id="dex-header">
<a href="${url.current(name=c.prev_species.name.lower(), form=None)}" id="dex-header-prev" class="dex-box-link">
Expand Down Expand Up @@ -326,7 +334,7 @@ collapse_key = h.pokedex.collapse_flavor_text_key(literal=obdurate)
species = pokemon_form.species
# A handful of Pokémon have separate cries for each form; most don't
if not h.pokedex.pokemon_has_media(pokemon_form, 'cries', 'ogg'):
if not pokemon_has_media(pokemon_form, 'cries', 'ogg'):
pokemon_form = None
cry_url = url(controller='dex', action='media',
Expand Down
2 changes: 1 addition & 1 deletion splinext/pokedex/templates/pokedex/pokemon.mako
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ ${h.h1(_('Flavor'))}
<h2>${_("Sprites")}</h2>
${h.pokedex.pokemon_form_image(c.pokemon.default_form, prefix='main-sprites/ultra-sun-ultra-moon')}
${h.pokedex.pokemon_form_image(c.pokemon.default_form, prefix='main-sprites/ultra-sun-ultra-moon/shiny')}
% if h.pokedex.pokemon_has_media(c.pokemon.default_form, 'main-sprites/ultra-sun-ultra-moon/female', 'png'):
% if dexlib.pokemon_has_media(c.pokemon.default_form, 'main-sprites/ultra-sun-ultra-moon/female', 'png'):
${h.pokedex.pokemon_form_image(c.pokemon.default_form, prefix='main-sprites/ultra-sun-ultra-moon/female')}
${h.pokedex.pokemon_form_image(c.pokemon.default_form, prefix='main-sprites/ultra-sun-ultra-moon/shiny/female')}
% endif
Expand Down

0 comments on commit 9ab6a52

Please sign in to comment.