<s> from os . path import dirname <EOL> from os . path import realpath <EOL> from random import randint <EOL> from wsgiref . simple_server import make_server <EOL> from pyramid . config import Configurator <EOL> from pyramid . events import NewRequest <EOL> from rest_toolkit import resource <EOL> todos = { <EOL> "<STR_LIT>" : { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT:title>" : "<STR_LIT>" } , <EOL> "<STR_LIT>" : { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT:title>" : "<STR_LIT>" } , <EOL> "<STR_LIT>" : { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT:title>" : "<STR_LIT>" } , <EOL> "<STR_LIT>" : { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT:title>" : "<STR_LIT>" } <EOL> } <EOL> @ resource ( '<STR_LIT>' ) <EOL> class TodoCollection ( object ) : <EOL> def __init__ ( self , request ) : <EOL> pass <EOL> @ TodoCollection . GET ( ) <EOL> def list_todos ( collection , request ) : <EOL> return { "<STR_LIT:data>" : list ( todos . values ( ) ) } <EOL> @ TodoCollection . POST ( ) <EOL> def add_todo ( collection , request ) : <EOL> todo = { <EOL> "<STR_LIT:id>" : "<STR_LIT>" + str ( randint ( <NUM_LIT:100> , <NUM_LIT> ) ) , <EOL> "<STR_LIT:title>" : request . json_body [ "<STR_LIT:title>" ] <EOL> } <EOL> todos [ todo [ "<STR_LIT:id>" ] ] = todo <EOL> return { "<STR_LIT:data>" : todo } <EOL> @ resource ( '<STR_LIT>' ) <EOL> class TodoResource ( object ) : <EOL> def __init__ ( self , request ) : <EOL> self . data = todos [ request . matchdict [ '<STR_LIT:id>' ] ] <EOL> @ TodoResource . GET ( ) <EOL> def view_todo ( todo , request ) : <EOL> return todo . data <EOL> @ TodoResource . PUT ( ) <EOL> def update_todo ( todo , request ) : <EOL> todo . data [ '<STR_LIT:title>' ] = request . json_body [ '<STR_LIT:title>' ] <EOL> return { } <EOL> @ TodoResource . DELETE ( ) <EOL> def delete_todo ( todo , request ) : <EOL> del todos [ todo . data [ "<STR_LIT:id>" ] ] <EOL> return { } <EOL> def add_cors_callback ( event ) : <EOL> headers = "<STR_LIT>" <EOL> def cors_headers ( request , response ) : <EOL> response . headers . update ( { <EOL> "<STR_LIT>" : "<STR_LIT:*>" , <EOL> "<STR_LIT>" : headers <EOL> } ) <EOL> event . request . add_response_callback ( cors_headers ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> config = Configurator ( ) <EOL> config . include ( '<STR_LIT>' ) <EOL> config . add_static_view ( '<STR_LIT>' , dirname ( realpath ( __file__ ) ) ) <EOL> config . add_subscriber ( add_cors_callback , NewRequest ) <EOL> config . scan ( "<STR_LIT:.>" ) <EOL> app = config . make_wsgi_app ( ) <EOL> server = make_server ( '<STR_LIT>' , <NUM_LIT> , app ) <EOL> server . serve_forever ( ) </s>
