<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> from __future__ import with_statement <EOL> import sys <EOL> import dataproperty <EOL> import pyparsing as pp <EOL> import six <EOL> import thutils <EOL> import tcconfig <EOL> import tcconfig . traffic_control <EOL> def parse_option ( ) : <EOL> parser = thutils . option . ArgumentParserObject ( ) <EOL> parser . make ( version = tcconfig . VERSION ) <EOL> group = parser . parser . add_mutually_exclusive_group ( required = True ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> help = """<STR_LIT>""" ) <EOL> group = parser . add_argument_group ( "<STR_LIT>" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> group = parser . add_argument_group ( "<STR_LIT>" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , choices = tcconfig . traffic_control . TrafficDirection . LIST , <EOL> default = tcconfig . traffic_control . TrafficDirection . OUTGOING , <EOL> help = """<STR_LIT>""" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = float , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = float , default = <NUM_LIT:0> , <EOL> help = """<STR_LIT>""" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = float , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = float , default = <NUM_LIT:0> , <EOL> help = """<STR_LIT>""" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> group . add_argument ( <EOL> "<STR_LIT>" , type = int , <EOL> help = "<STR_LIT>" ) <EOL> return parser . parse_args ( ) <EOL> def _parse_tc_filter_network ( text ) : <EOL> network_pattern = ( <EOL> pp . SkipTo ( "<STR_LIT>" , include = True ) + <EOL> pp . Word ( pp . alphanums + "<STR_LIT:.>" + "<STR_LIT:/>" ) ) <EOL> return network_pattern . parseString ( text ) [ - <NUM_LIT:1> ] <EOL> def _parse_tc_filter_port ( text ) : <EOL> port_pattern = ( <EOL> pp . SkipTo ( "<STR_LIT>" , include = True ) + <EOL> pp . Word ( pp . nums ) ) <EOL> return port_pattern . parseString ( text ) [ - <NUM_LIT:1> ] <EOL> def load_tcconfig ( config_file ) : <EOL> from voluptuous import Schema , Required , Any , ALLOW_EXTRA <EOL> schema = Schema ( { <EOL> Required ( six . text_type ) : { <EOL> Any ( "<STR_LIT>" , "<STR_LIT>" ) : { <EOL> six . text_type : { <EOL> six . text_type : six . text_type , <EOL> } , <EOL> } <EOL> } , <EOL> } , extra = ALLOW_EXTRA ) <EOL> return thutils . loader . JsonLoader . load ( <EOL> config_file , schema ) <EOL> def get_tcconfig_command_list ( config_table , is_overwrite ) : <EOL> command_list = [ ] <EOL> for device , device_table in six . iteritems ( config_table ) : <EOL> if is_overwrite : <EOL> command_list . append ( "<STR_LIT>" + device ) <EOL> for direction , direction_table in six . iteritems ( device_table ) : <EOL> for tc_filter , filter_table in six . iteritems ( direction_table ) : <EOL> if filter_table == { } : <EOL> continue <EOL> option_list = [ <EOL> "<STR_LIT>" + device , <EOL> "<STR_LIT>" + direction , <EOL> ] + [ <EOL> "<STR_LIT>" % ( k , v ) <EOL> for k , v in six . iteritems ( filter_table ) <EOL> ] <EOL> try : <EOL> network = _parse_tc_filter_network ( tc_filter ) <EOL> if network != "<STR_LIT>" : <EOL> option_list . append ( "<STR_LIT>" + network ) <EOL> except pp . ParseException : <EOL> pass <EOL> try : <EOL> port = _parse_tc_filter_port ( tc_filter ) <EOL> option_list . append ( "<STR_LIT>" + port ) <EOL> except pp . ParseException : <EOL> pass <EOL> command_list . append ( "<STR_LIT:U+0020>" . join ( [ "<STR_LIT>" ] + option_list ) ) <EOL> return command_list <EOL> @ thutils . main . Main <EOL> def main ( ) : <EOL> options = parse_option ( ) <EOL> thutils . initialize_library ( __file__ , options ) <EOL> thutils . common . verify_install_command ( [ "<STR_LIT>" ] ) <EOL> subproc_wrapper = thutils . subprocwrapper . SubprocessWrapper ( ) <EOL> if dataproperty . is_not_empty_string ( options . config_file ) : <EOL> return_code = <NUM_LIT:0> <EOL> for tcconfig_command in get_tcconfig_command_list ( <EOL> load_tcconfig ( options . config_file ) , options . overwrite ) : <EOL> return_code |= subproc_wrapper . run ( tcconfig_command ) <EOL> return return_code <EOL> tc = tcconfig . traffic_control . TrafficControl ( <EOL> subproc_wrapper , options . device ) <EOL> tc . direction = options . direction <EOL> tc . bandwidth_rate = options . bandwidth_rate <EOL> tc . latency_ms = options . network_latency <EOL> tc . latency_distro_ms = options . latency_distro_ms <EOL> tc . packet_loss_rate = options . packet_loss_rate <EOL> tc . corruption_rate = options . corruption_rate <EOL> tc . network = options . network <EOL> tc . port = options . port <EOL> tc . validate ( ) <EOL> if options . overwrite : <EOL> tc . delete_tc ( ) <EOL> tc . set_tc ( ) <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
