<s> import os <EOL> import json <EOL> import mimeparse <EOL> import inspect <EOL> from jinja2 import Template , DebugUndefined , Environment , PackageLoader , FileSystemLoader <EOL> from jinja2 . exceptions import TemplateNotFound <EOL> from giotto import get_config <EOL> from giotto . exceptions import NoViewMethod <EOL> from giotto . utils import Mock , htmlize , htmlize_list , pre_process_json , super_accept_to_mimetype , jsonify <EOL> from giotto . control import GiottoControl , Redirection <EOL> def renders ( * mimetypes ) : <EOL> def decorator ( func ) : <EOL> func . mimetypes = [ ] <EOL> for mimetype in mimetypes : <EOL> func . mimetypes . append ( mimetype ) <EOL> return func <EOL> return decorator <EOL> class GiottoView ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , persist = None , ** kwargs ) : <EOL> self . persist = persist <EOL> self . render_map = { } <EOL> self . reject_map = { } <EOL> class_defined_renderers = [ x for x in dir ( self ) if not x . startswith ( '<STR_LIT>' ) ] <EOL> self . _register_renderers ( class_defined_renderers ) <EOL> for format , function in kwargs . items ( ) : <EOL> mime = super_accept_to_mimetype ( format ) <EOL> setattr ( function , '<STR_LIT>' , [ mime or format ] ) <EOL> setattr ( self , format , function ) <EOL> self . _register_renderers ( kwargs . keys ( ) ) <EOL> def _register_renderers ( self , attrs ) : <EOL> """<STR_LIT>""" <EOL> for method in attrs : <EOL> func = getattr ( self , method ) <EOL> mimetypes = getattr ( func , '<STR_LIT>' , [ ] ) <EOL> for mimetype in mimetypes : <EOL> if not '<STR_LIT:/>' in mimetype : <EOL> self . reject_map [ mimetype ] = func <EOL> if mimetype not in self . render_map : <EOL> self . render_map [ mimetype ] = func <EOL> else : <EOL> base_classes = self . __class__ . mro ( ) [ <NUM_LIT:1> : ] <EOL> from_baseclass = any ( [ x for x in base_classes if func . __name__ in dir ( x ) ] ) <EOL> if not from_baseclass : <EOL> self . render_map [ mimetype ] = func <EOL> def can_render ( self , partial_mimetype ) : <EOL> """<STR_LIT>""" <EOL> for mime in self . render_map . keys ( ) : <EOL> if mime == '<STR_LIT>' : <EOL> return True <EOL> if partial_mimetype in mime : <EOL> return True <EOL> return False <EOL> def render ( self , result , mimetype , errors = None ) : <EOL> """<STR_LIT>""" <EOL> available_mimetypes = [ x for x in self . render_map . keys ( ) if '<STR_LIT:/>' in x ] <EOL> render_func = None <EOL> if '<STR_LIT:/>' not in mimetype : <EOL> render_func = self . reject_map . get ( mimetype , None ) <EOL> if not render_func : <EOL> raise NoViewMethod ( "<STR_LIT>" % mimetype ) <EOL> if not render_func and available_mimetypes : <EOL> target_mimetype = mimeparse . best_match ( available_mimetypes , mimetype ) <EOL> render_func = self . render_map . get ( target_mimetype , None ) <EOL> if not render_func : <EOL> raise NoViewMethod ( "<STR_LIT>" % mimetype ) <EOL> principle_mimetype = render_func . mimetypes [ <NUM_LIT:0> ] <EOL> if GiottoControl in render_func . __class__ . mro ( ) : <EOL> return { '<STR_LIT:body>' : render_func , '<STR_LIT>' : render_func . persist } <EOL> if callable ( self . persist ) : <EOL> persist = self . persist ( result ) <EOL> else : <EOL> persist = self . persist <EOL> arg_names = inspect . getargspec ( render_func ) . args <EOL> num_args = len ( set ( arg_names ) - set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> if num_args == <NUM_LIT:2> : <EOL> data = render_func ( result , errors or Mock ( ) ) <EOL> else : <EOL> data = render_func ( result ) <EOL> if GiottoControl in data . __class__ . mro ( ) : <EOL> return { '<STR_LIT:body>' : data , '<STR_LIT>' : persist } <EOL> if not hasattr ( data , '<STR_LIT>' ) : <EOL> data = { '<STR_LIT:body>' : data , '<STR_LIT>' : principle_mimetype } <EOL> else : <EOL> if not '<STR_LIT>' in data and target_mimetype == '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not '<STR_LIT>' in data : <EOL> data [ '<STR_LIT>' ] = target_mimetype <EOL> data [ '<STR_LIT>' ] = persist <EOL> return data <EOL> class BasicView ( GiottoView ) : <EOL> """<STR_LIT>""" <EOL> @ renders ( '<STR_LIT:application/json>' ) <EOL> def generic_json ( self , result , errors ) : <EOL> return { '<STR_LIT:body>' : jsonify ( result ) , '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> @ renders ( '<STR_LIT>' ) <EOL> def generic_html ( self , result , errors ) : <EOL> """<STR_LIT>""" <EOL> h1 = htmlize ( type ( result ) ) <EOL> out = [ ] <EOL> result = pre_process_json ( result ) <EOL> if not hasattr ( result , '<STR_LIT>' ) : <EOL> header = "<STR_LIT>" <EOL> if type ( result ) is list : <EOL> result = htmlize_list ( result ) <EOL> else : <EOL> result = htmlize ( result ) <EOL> out = [ "<STR_LIT>" + result + "<STR_LIT>" ] <EOL> elif hasattr ( result , '<STR_LIT>' ) : <EOL> out = [ "<STR_LIT>" + result + "<STR_LIT>" ] <EOL> else : <EOL> header = "<STR_LIT>" <EOL> for key , value in result . items ( ) : <EOL> v = htmlize ( value ) <EOL> row = "<STR_LIT>" . format ( key , v ) <EOL> out . append ( row ) <EOL> env = Environment ( loader = PackageLoader ( '<STR_LIT>' ) ) <EOL> template = env . get_template ( '<STR_LIT>' ) <EOL> rendered = template . render ( { '<STR_LIT>' : h1 , '<STR_LIT>' : header , '<STR_LIT>' : out } ) <EOL> return { '<STR_LIT:body>' : rendered , '<STR_LIT>' : '<STR_LIT>' } <EOL> @ renders ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def generic_text ( self , result , errors ) : <EOL> out = [ ] <EOL> kv_iterate = None <EOL> single_iterate = [ ] <EOL> if hasattr ( result , '<STR_LIT>' ) : <EOL> kv_iterate = result . items ( ) <EOL> elif hasattr ( result , '<STR_LIT>' ) : <EOL> return result <EOL> elif not result : <EOL> return '<STR_LIT>' <EOL> elif hasattr ( result , '<STR_LIT>' ) : <EOL> single_iterate = result <EOL> elif hasattr ( result , '<STR_LIT>' ) : <EOL> kv_iterate = result . todict ( ) . items ( ) <EOL> else : <EOL> kv_iterate = result . __dict__ . items ( ) <EOL> if kv_iterate : <EOL> for key , value in kv_iterate : <EOL> row = "<STR_LIT>" . format ( key , value ) <EOL> out . append ( row ) <EOL> else : <EOL> for value in single_iterate : <EOL> out . append ( self . generic_text ( value , None ) ) <EOL> return "<STR_LIT:\n>" . join ( out ) <EOL> def get_jinja_template ( template_name ) : <EOL> ppx = get_config ( '<STR_LIT>' ) <EOL> env = Environment ( loader = FileSystemLoader ( os . path . join ( ppx , '<STR_LIT>' ) ) ) <EOL> return env . get_template ( template_name ) <EOL> def jinja_template ( template_name , name = '<STR_LIT:data>' , mimetype = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> def jinja_renderer ( result , errors ) : <EOL> template = get_jinja_template ( template_name ) <EOL> context = { name : result or Mock ( ) , '<STR_LIT>' : errors , '<STR_LIT>' : enumerate } <EOL> rendered = template . render ( ** context ) <EOL> return { '<STR_LIT:body>' : rendered , '<STR_LIT>' : mimetype } <EOL> return jinja_renderer <EOL> def partial_jinja_template ( template_name , name = '<STR_LIT:data>' , mimetype = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> def partial_jinja_renderer ( result , errors ) : <EOL> template = get_jinja_template ( template_name ) <EOL> old = template . environment . undefined <EOL> template . environment . undefined = DebugUndefined <EOL> context = { name : result or Mock ( ) , '<STR_LIT>' : errors } <EOL> rendered = template . render ( ** context ) <EOL> template . environment . undefined = old <EOL> return { '<STR_LIT:body>' : rendered , '<STR_LIT>' : mimetype } <EOL> return partial_jinja_renderer <EOL> def lazy_jinja_template ( template_name , name = '<STR_LIT:data>' , mimetype = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> def lazy_jinja_renderer ( result , errors ) : <EOL> template = get_jinja_template ( template_name ) <EOL> context = { name : result or Mock ( ) , '<STR_LIT>' : errors } <EOL> data = ( '<STR_LIT>' , template , context ) <EOL> return { '<STR_LIT:body>' : data , '<STR_LIT>' : mimetype } <EOL> return lazy_jinja_renderer <EOL> class ImageViewer ( GiottoView ) : <EOL> """<STR_LIT>""" <EOL> @ renders ( '<STR_LIT>' ) <EOL> def plaintext ( self , result ) : <EOL> """<STR_LIT>""" <EOL> from PIL import Image <EOL> ascii_chars = [ '<STR_LIT:#>' , '<STR_LIT:A>' , '<STR_LIT:@>' , '<STR_LIT:%>' , '<STR_LIT:S>' , '<STR_LIT:+>' , '<STR_LIT:<>' , '<STR_LIT:*>' , '<STR_LIT::>' , '<STR_LIT:U+002C>' , '<STR_LIT:.>' ] <EOL> def image_to_ascii ( image ) : <EOL> image_as_ascii = [ ] <EOL> all_pixels = list ( image . getdata ( ) ) <EOL> for pixel_value in all_pixels : <EOL> index = pixel_value / <NUM_LIT> <EOL> image_as_ascii . append ( ascii_chars [ index ] ) <EOL> return image_as_ascii <EOL> img = Image . open ( result ) <EOL> width , heigth = img . size <EOL> new_width = <NUM_LIT> <EOL> new_heigth = int ( ( heigth * new_width ) / width ) <EOL> new_image = img . resize ( ( new_width , new_heigth ) ) <EOL> new_image = new_image . convert ( "<STR_LIT:L>" ) <EOL> img_as_ascii = image_to_ascii ( new_image ) <EOL> img_as_ascii = '<STR_LIT>' . join ( ch for ch in img_as_ascii ) <EOL> out = [ ] <EOL> for c in range ( <NUM_LIT:0> , len ( img_as_ascii ) , new_width ) : <EOL> out . append ( img_as_ascii [ c : c + new_width ] ) <EOL> return "<STR_LIT:\n>" . join ( out ) <EOL> def image ( self , result ) : <EOL> return result <EOL> def image_jpeg ( self , result ) : <EOL> return self . image ( result ) <EOL> def text_cmd ( self , result ) : <EOL> return result . read ( ) <EOL> def text_html ( self , result ) : <EOL> return """<STR_LIT>""" <EOL> class URLFollower ( BasicView ) : <EOL> """<STR_LIT>""" <EOL> @ renders ( "<STR_LIT>" ) <EOL> def text_html ( self , data ) : <EOL> return Redirection ( data ) <EOL> def cmd ( self , data ) : <EOL> return "<STR_LIT>" <EOL> class ForceJSONView ( GiottoView ) : <EOL> """<STR_LIT>""" <EOL> @ renders ( "<STR_LIT>" ) <EOL> def renderer ( self , data , errors ) : <EOL> return jsonify ( data ) <EOL> class ForceTextView ( GiottoView ) : <EOL> @ renders ( "<STR_LIT>" ) <EOL> def renderer ( self , data , errors ) : <EOL> return self . generic_text ( data ) </s>
