<s> from __future__ import absolute_import <EOL> from flask import request <EOL> from flask . ext . restful import reqparse , abort , Resource , fields , marshal_with <EOL> from ceryx . db import RedisRouter <EOL> resource_fields = { <EOL> '<STR_LIT:source>' : fields . String , <EOL> '<STR_LIT:target>' : fields . String , <EOL> } <EOL> parser = reqparse . RequestParser ( ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:source>' , type = str , required = True , help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:target>' , type = str , required = True , help = '<STR_LIT>' <EOL> ) <EOL> router = RedisRouter . from_config ( ) <EOL> def lookup_or_abort ( source ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return { '<STR_LIT:source>' : source , '<STR_LIT:target>' : router . lookup ( source ) } <EOL> except RedisRouter . LookupNotFound : <EOL> abort ( <EOL> <NUM_LIT> , message = '<STR_LIT>' . format ( source ) <EOL> ) <EOL> class Route ( Resource ) : <EOL> """<STR_LIT>""" <EOL> @ marshal_with ( resource_fields ) <EOL> def get ( self , source ) : <EOL> """<STR_LIT>""" <EOL> route = lookup_or_abort ( source ) <EOL> return route <EOL> @ marshal_with ( resource_fields ) <EOL> def delete ( self , source ) : <EOL> """<STR_LIT>""" <EOL> route = lookup_or_abort ( source ) <EOL> router . delete ( source ) <EOL> return route , <NUM_LIT> <EOL> @ marshal_with ( resource_fields ) <EOL> def put ( self , source ) : <EOL> """<STR_LIT>""" <EOL> args = parser . parse_args ( ) <EOL> router . insert ( args [ '<STR_LIT:source>' ] , args [ '<STR_LIT:target>' ] ) <EOL> return args , <NUM_LIT> <EOL> class RoutesList ( Resource ) : <EOL> """<STR_LIT>""" <EOL> @ marshal_with ( resource_fields ) <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> args = parser . parse_args ( ) <EOL> router . insert ( args [ '<STR_LIT:source>' ] , args [ '<STR_LIT:target>' ] ) <EOL> return args , <NUM_LIT> <EOL> @ marshal_with ( resource_fields ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> pattern = request . args . get ( '<STR_LIT>' ) <EOL> routes = router . lookup_routes ( pattern ) <EOL> return routes </s>
