<s> """<STR_LIT>""" <EOL> import re <EOL> from pyeapi . api import EntityCollection <EOL> ROUTES_RE = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . M ) <EOL> class StaticRoute ( EntityCollection ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def get ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . getall ( ) . get ( name ) <EOL> def getall ( self ) : <EOL> """<STR_LIT>""" <EOL> matches = ROUTES_RE . findall ( self . config ) <EOL> routes = dict ( ) <EOL> for match in matches : <EOL> ip_dest = match [ <NUM_LIT:0> ] <EOL> next_hop = match [ <NUM_LIT:1> ] <EOL> next_hop_ip = None if match [ <NUM_LIT:2> ] is '<STR_LIT>' else match [ <NUM_LIT:2> ] <EOL> distance = int ( match [ <NUM_LIT:3> ] ) <EOL> data = { } <EOL> data [ '<STR_LIT>' ] = None if match [ <NUM_LIT:4> ] is '<STR_LIT>' else int ( match [ <NUM_LIT:4> ] ) <EOL> data [ '<STR_LIT>' ] = None if match [ <NUM_LIT:5> ] is '<STR_LIT>' else match [ <NUM_LIT:5> ] <EOL> ip_dict = routes [ ip_dest ] = routes . get ( ip_dest , { } ) <EOL> nh_dict = ip_dict [ next_hop ] = ip_dict . get ( next_hop , { } ) <EOL> nhip_dict = nh_dict [ next_hop_ip ] = nh_dict . get ( next_hop_ip , { } ) <EOL> nhip_dict [ distance ] = data <EOL> return routes <EOL> def create ( self , ip_dest , next_hop , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_route ( ip_dest , next_hop , ** kwargs ) <EOL> def delete ( self , ip_dest , next_hop , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . update ( { '<STR_LIT>' : True } ) <EOL> return self . _set_route ( ip_dest , next_hop , ** kwargs ) <EOL> def default ( self , ip_dest , next_hop , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . update ( { '<STR_LIT:default>' : True } ) <EOL> return self . _set_route ( ip_dest , next_hop , ** kwargs ) <EOL> def set_tag ( self , ip_dest , next_hop , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_route ( ip_dest , next_hop , ** kwargs ) <EOL> def set_route_name ( self , ip_dest , next_hop , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_route ( ip_dest , next_hop , ** kwargs ) <EOL> def _build_commands ( self , ip_dest , next_hop , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> commands = "<STR_LIT>" % ( ip_dest , next_hop ) <EOL> next_hop_ip = kwargs . get ( '<STR_LIT>' , None ) <EOL> distance = kwargs . get ( '<STR_LIT>' , None ) <EOL> tag = kwargs . get ( '<STR_LIT>' , None ) <EOL> route_name = kwargs . get ( '<STR_LIT>' , None ) <EOL> if next_hop_ip is not None : <EOL> commands += "<STR_LIT>" % next_hop_ip <EOL> if distance is not None : <EOL> commands += "<STR_LIT>" % distance <EOL> if tag is not None : <EOL> commands += "<STR_LIT>" % tag <EOL> if route_name is not None : <EOL> commands += "<STR_LIT>" % route_name <EOL> return commands <EOL> def _set_route ( self , ip_dest , next_hop , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> commands = self . _build_commands ( ip_dest , next_hop , ** kwargs ) <EOL> delete = kwargs . get ( '<STR_LIT>' , False ) <EOL> default = kwargs . get ( '<STR_LIT:default>' , False ) <EOL> if delete : <EOL> commands = "<STR_LIT>" + commands <EOL> else : <EOL> if default : <EOL> commands = "<STR_LIT>" + commands <EOL> return self . configure ( commands ) <EOL> def instance ( node ) : <EOL> """<STR_LIT>""" <EOL> return StaticRoute ( node ) </s>
