<s> from __future__ import print_function <EOL> import sys <EOL> import os <EOL> import argparse <EOL> import re <EOL> def _fg ( file , pattern , ops ) : <EOL> with open ( file , '<STR_LIT:r>' ) as f : <EOL> text = f . readlines ( ) <EOL> z = len ( text ) <EOL> for i in range ( z ) : <EOL> line = text [ i ] <EOL> result = pattern . match ( line . strip ( ) ) <EOL> if not result : result = ( ops . pattern in line . strip ( ) ) <EOL> if result : <EOL> if ops . A : <EOL> if i < ops . A_num : <EOL> j = i <EOL> else : <EOL> j = ops . A_num <EOL> print ( '<STR_LIT>' . join ( text [ i - j : i ] ) , end = '<STR_LIT>' ) <EOL> print ( line , end = '<STR_LIT>' ) <EOL> if ops . B : <EOL> if i + ops . B_num > z : <EOL> j = z - i <EOL> else : <EOL> j = ops . B_num <EOL> print ( '<STR_LIT>' . join ( text [ i + <NUM_LIT:1> : i + j + <NUM_LIT:1> ] ) , end = '<STR_LIT>' ) <EOL> def _grep ( args ) : <EOL> pattern = re . compile ( args . pattern if not args . i else args . pattern . lower ( ) ) <EOL> for file in args . files : <EOL> _fg ( file , pattern , args ) <EOL> def main ( argv ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store>' , type = int , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store>' , type = int , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = argparse . REMAINDER ) <EOL> argv = parser . parse_args ( ) <EOL> argv . A = False <EOL> argv . B = False <EOL> if argv . A_num : <EOL> argv . A = True <EOL> if argv . B_num : <EOL> argv . B = True <EOL> if len ( argv . files ) < <NUM_LIT:2> : <EOL> parser . print_help ( ) <EOL> return <EOL> argv . pattern = argv . files [ <NUM_LIT:0> ] <EOL> argv . files = argv . files [ <NUM_LIT:1> : ] <EOL> _grep ( args = argv ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv ) </s>
