#NEWLINE# def main( ) : #NEWLINE# #INDENT# parser = argparse . ArgumentParser ( ) #NEWLINE# parser . add_argument ( '-n' , '--nth' , metavar = 'N' , help = 'select the Nth entry (1-based)' , type = int , default = None ) #NEWLINE# parser . add_argument ( '-f' , '--first' , metavar = 'N' , help = 'select the N first entires' , type = int , default = None ) #NEWLINE# parser . add_argument ( '-v' , '--invert-match' , default = False , action = 'store_true' , help = 'select all entries but the ones that match' ) #NEWLINE# parser . add_argument ( '-k' , '--key' , default = 'name' , help = 'change the key used for the match (default is name)' ) #NEWLINE# parser . add_argument ( '-s' , '--case-sensitive' , default = False , action = 'store_true' ) #NEWLINE# parser . add_argument ( 'value' , metavar = 'VALUE' , nargs = '?' , default = None , help = 'string or numerical value to match' ) #NEWLINE# group = parser . add_mutually_exclusive_group ( required = False ) #NEWLINE# group . add_argument ( '-e' , '--numeric-equals' , default = False , action = 'store_true' ) #NEWLINE# group . add_argument ( '-d' , '--numeric-differs' , default = False , action = 'store_true' ) #NEWLINE# group . add_argument ( '-G' , '--greater' , default = False , action = 'store_true' ) #NEWLINE# group . add_argument ( '-g' , '--greater-or-equal' , default = False , action = 'store_true' ) #NEWLINE# group . add_argument ( '-l' , '--less-or-equal' , default = False , action = 'store_true' ) #NEWLINE# group . add_argument ( '-L' , '--less' , default = False , action = 'store_true' ) #NEWLINE# group . add_argument ( '-T' , '--true' , default = False , action = 'store_true' ) #NEWLINE# group . add_argument ( '-F' , '--false' , default = False , action = 'store_true' ) #NEWLINE# args = parser . parse_args ( ) #NEWLINE# if ( ( args . value is not None ) and ( args . false or args . true ) ) : #NEWLINE# #INDENT# parser . error ( 'cannot specify value for boolean matching' ) #NEWLINE# #UNINDENT# if sys . stdin . isatty ( ) : #NEWLINE# #INDENT# parser . error ( 'no input, pipe another btc command output into this command' ) #NEWLINE# #UNINDENT# l = sys . stdin . read ( ) #NEWLINE# if ( len ( l . strip ( ) ) == 0 ) : #NEWLINE# #INDENT# exit ( 1 ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# l = decoder . decode ( l ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# error ( ( 'unexpected input: %s' % l ) ) #NEWLINE# #UNINDENT# new = list ( ) #NEWLINE# for o in l : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if args . numeric_equals : #NEWLINE# #INDENT# if ( float ( o [ args . key ] ) == float ( args . value ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if args . numeric_differs : #NEWLINE# #INDENT# if ( float ( o [ args . key ] ) != float ( args . value ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if args . greater : #NEWLINE# #INDENT# if ( float ( o [ args . key ] ) > float ( args . value ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if args . greater_or_equal : #NEWLINE# #INDENT# if ( float ( o [ args . key ] ) >= float ( args . value ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if args . less_or_equal : #NEWLINE# #INDENT# if ( float ( o [ args . key ] ) <= float ( args . value ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if args . less : #NEWLINE# #INDENT# if ( float ( o [ args . key ] ) < float ( args . value ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if args . true : #NEWLINE# #INDENT# if bool ( o [ args . key ] ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if args . false : #NEWLINE# #INDENT# if ( not bool ( o [ args . key ] ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( args . value is not None ) : #NEWLINE# #INDENT# def case( x ) : #NEWLINE# #INDENT# if args . case_sensitive : #NEWLINE# #INDENT# return x #NEWLINE# #UNINDENT# return x . lower ( ) #NEWLINE# #UNINDENT# if fnmatch . fnmatch ( case ( unicode ( o [ args . key ] ) ) , case ( unicode ( args . value ) ) ) : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# new . append ( o ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except KeyError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# except ValueError as e : #NEWLINE# #INDENT# error ( ( 'value error: %s' % new ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( args . first is not None ) : #NEWLINE# #INDENT# new = new [ 0 : min ( args . first , len ( new ) ) ] #NEWLINE# #UNINDENT# if ( args . nth is not None ) : #NEWLINE# #INDENT# new = [ new [ ( args . nth - 1 ) ] ] #NEWLINE# #UNINDENT# if args . invert_match : #NEWLINE# #INDENT# new = [ o for o in l if ( o not in new ) ] #NEWLINE# #UNINDENT# print ( encoder . encode ( new ) )