<s> from abc import abstractmethod <EOL> import pyglet . resource <EOL> from . templates import TextureTemplate , FrameTextureTemplate <EOL> class Parser : <EOL> @ abstractmethod <EOL> def condition_fulfilled ( self , key ) : <EOL> return False <EOL> @ abstractmethod <EOL> def parse_element ( self , element ) : <EOL> pass <EOL> class TextureParser ( Parser ) : <EOL> def __init__ ( self , resources_path ) : <EOL> self . _textures = { } <EOL> self . _loader = pyglet . resource . Loader ( resources_path ) <EOL> def condition_fulfilled ( self , key ) : <EOL> return key . startswith ( '<STR_LIT:image>' ) <EOL> def _get_texture ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if filename not in self . _textures : <EOL> texture = self . _loader . texture ( filename ) <EOL> self . _textures [ filename ] = texture <EOL> return self . _textures [ filename ] <EOL> def _get_texture_region ( self , filename , x , y , width , height ) : <EOL> """<STR_LIT>""" <EOL> texture = self . _get_texture ( filename ) <EOL> retval = texture . get_region ( x , y , width , height ) . get_texture ( ) <EOL> return retval <EOL> def parse_element ( self , element ) : <EOL> if isinstance ( element , dict ) : <EOL> if '<STR_LIT>' in element : <EOL> texture = self . _get_texture_region ( element [ '<STR_LIT:source>' ] , * element [ '<STR_LIT>' ] ) <EOL> else : <EOL> texture = self . _get_texture ( element [ '<STR_LIT:source>' ] ) <EOL> if '<STR_LIT>' in element : <EOL> return FrameTextureTemplate ( <EOL> texture , <EOL> element [ '<STR_LIT>' ] , <EOL> element . get ( '<STR_LIT>' , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> ) <EOL> else : <EOL> return TextureTemplate ( texture ) <EOL> else : <EOL> texture = self . _get_texture ( element ) <EOL> return TextureTemplate ( texture ) </s>
