#!/usr/bin/env python
"""
    jldfilter - filters stdin through Python module, out to stdout


    See https://www.systemical.com/doc/opensource/jlddk  for more details

    http://docs.python.org/dev/library/argparse.html#nargs
    nargs="n" : n arguments grouped in a list
    nargs="?" : 1 argument, optional
    nargs="*" : list of arguments
    nargs="+" : a list of at least 1 element

    @author: Jean-Lou Dupont
"""
__version__="0.3"
import os, sys, argparse
op=os.path

try:
    import jlddk #@UnusedImport
except:
    ### must be in dev mode then    
    this_dir=op.dirname(__file__)
    lib_path=op.abspath(op.join(this_dir, ".."))
    sys.path.insert(0, lib_path)
    import jlddk #@UnusedImport

########################################################################

DESC="""Filters stdin stream through Python function ==> stdout - v%s""" % __version__
DEFAULTS={
          
          }

def main():
    try:
        import jlddk.do_setup  #@UnusedImport
        import logging
        import jlddk.do_checks #@UnusedImport
        
        parser=argparse.ArgumentParser(description=DESC, fromfile_prefix_chars='@', formatter_class=argparse.RawTextHelpFormatter)
        
        parser.add_argument('-nfe', dest="no_filter_empty", action="store_true",  help="No filter empty string", default=False)
        
        parser.add_argument('-m',   dest="module",        type=str,  help="Python module", required=True)
        parser.add_argument('-f',   dest="function",      type=str,  help="Python function inside module", required=True)
        parser.add_argument('-a',   dest="function_args", nargs='+', help="Arguments to function inside module", default=[])
        
        from jlddk.script_filter import run
        from jlddk.tools_sys import process_command_line
        args=process_command_line(parser)
        run(**args)

    except KeyboardInterrupt:
        logging.info("..Exiting")
        sys.exit(0)##no probs
        
    except Exception,e:
        logging.error(str(e))
        sys.exit(1)
        
sys.exit(main())
