<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import datetime <EOL> from lib import aclgenerator <EOL> class Term ( aclgenerator . Term ) : <EOL> """<STR_LIT>""" <EOL> _ACTIONS = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , term , term_type ) : <EOL> self . term = term <EOL> self . term_type = term_type <EOL> def __str__ ( self ) : <EOL> if self . term . platform : <EOL> if '<STR_LIT>' not in self . term . platform : <EOL> return '<STR_LIT>' <EOL> if self . term . platform_exclude : <EOL> if '<STR_LIT>' in self . term . platform_exclude : <EOL> return '<STR_LIT>' <EOL> ret_str = [ ] <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:4> + '<STR_LIT>' + self . term . name + '<STR_LIT:{>' ) <EOL> if self . term . comment : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> for comment in self . term . comment : <EOL> for line in comment . split ( '<STR_LIT:\n>' ) : <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT:#>' + line ) <EOL> source_address = self . term . GetAddressOfVersion ( <EOL> '<STR_LIT>' , self . AF_MAP . get ( self . term_type ) ) <EOL> source_address_exclude = self . term . GetAddressOfVersion ( <EOL> '<STR_LIT>' , self . AF_MAP . get ( self . term_type ) ) <EOL> if source_address : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> for saddr in source_address : <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + str ( saddr ) ) <EOL> if source_address_exclude : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> for ex in source_address : <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + str ( ex ) ) <EOL> if self . term . source_port : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + self . _Group ( self . term . source_port ) ) <EOL> destination_address = self . term . GetAddressOfVersion ( <EOL> '<STR_LIT>' , self . AF_MAP . get ( self . term_type ) ) <EOL> destination_address_exclude = self . term . GetAddressOfVersion ( <EOL> '<STR_LIT>' , self . AF_MAP . get ( self . term_type ) ) <EOL> if destination_address : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> for daddr in destination_address : <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + str ( daddr ) ) <EOL> if destination_address_exclude : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> for ex in destination_address_exclude : <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + str ( ex ) ) <EOL> if self . term . destination_port : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + self . _Group ( self . term . destination_port ) ) <EOL> if self . term . protocol : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + self . _Group ( self . term . protocol ) ) <EOL> if self . term . option : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' ) <EOL> for option in self . term . option : <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + option ) <EOL> for action in self . term . action : <EOL> ret_str . append ( '<STR_LIT:U+0020>' ) <EOL> ret_str . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' <EOL> + self . _ACTIONS . get ( str ( action ) ) + '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( ret_str ) <EOL> def _Group ( self , group ) : <EOL> def _FormattedGroup ( el ) : <EOL> if isinstance ( el , str ) : <EOL> return el . lower ( ) <EOL> elif isinstance ( el , int ) : <EOL> return str ( el ) <EOL> elif el [ <NUM_LIT:0> ] == el [ <NUM_LIT:1> ] : <EOL> return '<STR_LIT>' % el [ <NUM_LIT:0> ] <EOL> else : <EOL> return '<STR_LIT>' % ( el [ <NUM_LIT:0> ] , el [ <NUM_LIT:1> ] ) <EOL> if len ( group ) > <NUM_LIT:1> : <EOL> rval = '<STR_LIT>' <EOL> for item in group : <EOL> rval = rval + str ( item [ <NUM_LIT:0> ] ) + '<STR_LIT:U+0020>' <EOL> else : <EOL> rval = _FormattedGroup ( group [ <NUM_LIT:0> ] ) <EOL> return rval <EOL> class Demo ( aclgenerator . ACLGenerator ) : <EOL> """<STR_LIT>""" <EOL> _PLATFORM = '<STR_LIT>' <EOL> _SUFFIX = '<STR_LIT>' <EOL> _OPTIONAL_SUPPORTED_KEYWORDS = set ( [ '<STR_LIT>' , ] ) <EOL> def _TranslatePolicy ( self , pol , exp_info ) : <EOL> current_date = datetime . date . today ( ) <EOL> exp_info_date = current_date + datetime . timedelta ( weeks = exp_info ) <EOL> self . demo_policies = [ ] <EOL> for header , terms in pol . filters : <EOL> if not self . _PLATFORM in header . platforms : <EOL> continue <EOL> filter_options = header . FilterOptions ( '<STR_LIT>' ) <EOL> filter_name = filter_options [ <NUM_LIT:0> ] <EOL> if len ( filter_options ) > <NUM_LIT:1> : <EOL> interface_specific = filter_options [ <NUM_LIT:1> ] <EOL> else : <EOL> interface_specific = '<STR_LIT:none>' <EOL> filter_type = '<STR_LIT>' <EOL> term_names = set ( ) <EOL> new_terms = [ ] <EOL> for term in terms : <EOL> if term . name in term_names : <EOL> raise DemoFilterError ( '<STR_LIT>' ) <EOL> term_names . add ( term . name ) <EOL> if term . expiration : <EOL> if term . expiration <= exp_info_date : <EOL> logging . info ( '<STR_LIT>' <EOL> '<STR_LIT>' , term . name , filter_name ) <EOL> if term . expiration <= current_date : <EOL> logging . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , term . name , filter_name ) <EOL> continue <EOL> new_terms . append ( Term ( term , filter_type ) ) <EOL> self . demo_policies . append ( ( header , filter_name , filter_type , <EOL> interface_specific , new_terms ) ) <EOL> def __str__ ( self ) : <EOL> target = [ ] <EOL> for ( header , filter_name , filter_type , <EOL> interface_specific , terms ) in self . demo_policies : <EOL> target . append ( '<STR_LIT>' ) <EOL> target . append ( '<STR_LIT:U+0020>' * <NUM_LIT:4> + '<STR_LIT>' % filter_name ) <EOL> target . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' % filter_type ) <EOL> for comment in header . comment : <EOL> for line in comment . split ( '<STR_LIT:\n>' ) : <EOL> target . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' % line ) <EOL> target . append ( '<STR_LIT:U+0020>' * <NUM_LIT:8> + '<STR_LIT>' % interface_specific ) <EOL> target . append ( '<STR_LIT:U+0020>' * <NUM_LIT:4> + '<STR_LIT:}>' ) <EOL> for term in terms : <EOL> target . append ( str ( term ) ) <EOL> target . append ( '<STR_LIT:U+0020>' * <NUM_LIT:4> + '<STR_LIT:}>' ) <EOL> target . append ( '<STR_LIT:U+0020>' ) <EOL> target . append ( '<STR_LIT:}>' ) <EOL> return '<STR_LIT:\n>' . join ( target ) <EOL> class Error ( Exception ) : <EOL> pass <EOL> class DemoFilterError ( Error ) : <EOL> pass </s>
