<s> """<STR_LIT>""" <EOL> import argparse <EOL> import sys <EOL> import csv <EOL> import re <EOL> def _cli ( ) : <EOL> epilog = r"""<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( <EOL> description = globals ( ) [ '<STR_LIT>' ] , epilog = epilog , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , nargs = '<STR_LIT:?>' , type = argparse . FileType ( '<STR_LIT:r>' ) , default = sys . stdin , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , default = sys . stdout , type = argparse . FileType ( '<STR_LIT:w>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> help = r"<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , default = '<STR_LIT:U+002C>' ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , required = True , help = "<STR_LIT>" ) <EOL> spec = parser . add_mutually_exclusive_group ( required = True ) <EOL> spec . add_argument ( <EOL> "<STR_LIT:-c>" , "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> spec . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> spec . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , action = '<STR_LIT:store_true>' , default = False , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , action = '<STR_LIT:store_true>' , default = False , <EOL> help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> if args . delimiter in [ '<STR_LIT:t>' , '<STR_LIT>' , '<STR_LIT:\t>' , '<STR_LIT>' ] : <EOL> args . delimiter = '<STR_LIT:\t>' <EOL> for mode in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if args . __dict__ [ mode ] : <EOL> match_str = args . __dict__ [ mode ] <EOL> break <EOL> filter_file ( <EOL> args . infile , args . outfile , args . name , mode , match_str , args . delimiter , <EOL> args . invert , args . ignorecase ) <EOL> def filter_file ( infile , outfile , name , mode , match_str , delimiter , invert , <EOL> ignorecase ) : <EOL> """<STR_LIT>""" <EOL> reader = csv . DictReader ( infile , delimiter = delimiter ) <EOL> writer = csv . DictWriter ( <EOL> outfile , delimiter = delimiter , fieldnames = reader . fieldnames ) <EOL> writer . writeheader ( ) <EOL> mode_fun = { <EOL> '<STR_LIT>' : _check_contains , '<STR_LIT>' : _check_equals , <EOL> '<STR_LIT>' : _check_regex } <EOL> for row in reader : <EOL> if mode_fun [ mode ] ( row [ name ] , match_str , ignorecase ) != invert : <EOL> writer . writerow ( row ) <EOL> def _check_contains ( item , match_str , ignorecase ) : <EOL> if ignorecase : <EOL> return match_str . lower ( ) in item . lower ( ) <EOL> else : <EOL> return match_str in item <EOL> def _check_equals ( item , match_str , ignorecase ) : <EOL> if ignorecase : <EOL> return match_str . lower ( ) == item . lower ( ) <EOL> else : <EOL> return match_str == item <EOL> def _check_regex ( item , match_str , ignorecase ) : <EOL> flags = re . IGNORECASE if ignorecase else <NUM_LIT:0> <EOL> return bool ( re . search ( match_str , item , flags = flags ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _cli ( ) </s>
