Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 133 lines (109 sloc) 4.73 KB
#!/usr/bin/env python
#
# biscuit - Modular HTTP cookie parser
# Copyright (C) 2012 Andrea Cardaci <cyrus.and@gmail.com>
#
# This file is part of biscuit.
#
# biscuit is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# biscuit is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# biscuit. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import argparse
from core.loader import *
from core.system import *
from core.exception import *
from core import default_printer
# EXCEPTIONS ]------------------------------------------------------------------
class RuntimeModuleError( Exception ):
def __init__( self , module ):
self.module = module
class ServiceError( RuntimeModuleError ):
pass
class PrinterError( RuntimeModuleError ):
pass
# ACTIONS ]---------------------------------------------------------------------
def do_list_printers( args ):
print "\n".join( printers.keys() )
def do_list_services( args ):
print "\n".join( services.keys() )
def do_describe_printer( args ):
if args.printer not in printers:
raise PrinterError( args.printer )
print args.printer + "\n\n" + printers[ args.printer ].HELP
def do_parse( args ):
# get printer
if args.printer:
try:
printer = printers[ args.printer ]
except KeyError:
raise PrinterError( args.printer )
else:
printer = default_printer
# get selected services
if args.services:
try:
selected_services = dict( ( service , services[ service ] )
for service in args.services )
except KeyError as e:
raise ServiceError( *e )
# use all known services
else:
selected_services = services
# start parsing
system = System( printer , not args.no_use_default , selected_services )
for line in sys.stdin:
try:
system.process_line( line )
except ParserError:
pass # skipping line
system.dump( sys.stdout , ">>> " )
# COMMAND LINE PARSER ]---------------------------------------------------------
def setup_command_line_parser():
parser = argparse.ArgumentParser( description = "HTTP cookies parser" )
commands = parser.add_subparsers()
# list command
list_command = commands.add_parser( "list" , help = "list available printers or services" )
list_command_modes = list_command.add_subparsers()
list_printers = list_command_modes.add_parser( "printers" , add_help = False , help = "list available printers" )
list_printers.set_defaults( action = do_list_printers )
list_services = list_command_modes.add_parser( "services" , add_help = False , help = "list available services" )
list_services.set_defaults( action = do_list_services )
# help command
help_command = commands.add_parser( "describe" , help = "describe a printer" )
help_command.add_argument( "printer" , help = "printer to describe" )
help_command.set_defaults( action = do_describe_printer )
# parse command
parse_command = commands.add_parser( "parse" , help = "parse cookies form standard input" )
parse_command.add_argument( "-n" , "--no-use-default" , action = "store_true" , help = "do not process cookies for unknown services" )
parse_command.add_argument( "-p" , "--printer" , metavar = "printer" , help = "printer to use" )
parse_command.add_argument( "services" , nargs = "*" , metavar = "service" , help = "process only these services (defaults to all)" )
parse_command.set_defaults( action = do_parse )
return parser
# MAIN ]------------------------------------------------------------------------
if __name__ == "__main__":
try:
# load runtime modules
printers = load_module_dir( "printers" )
services = load_module_dir( "services" )
# perform action
parser = setup_command_line_parser()
args = parser.parse_args()
args.action( args )
except ServiceError as e:
sys.stderr.write( "No such service '{0}', must be one of: {1}.\n"
.format( e.module , ", ".join( sorted( services ) ) ) )
except PrinterError as e:
sys.stderr.write( "No such printer '{0}', must be one of: {1}.\n"
.format( e.module , ", ".join( sorted( printers ) ) ) )
except KeyboardInterrupt:
sys.exit( 1 )