#!/usr/bin/env python
"""
    SDB KV batch insert

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

try:
    if os.environ.get("DEVMODE", False):
        raise    
    import jldaws #@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 jldaws #@UnusedImport

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

DESC="""SDB Batch Insert KV entry in a category space - version %s

Waits availability of source JSON files, writes to SDB in specified category space. 

Input type is auto-detected. Default input type is JSON with the following format:
{"category": $category, "key":$key, "value":$value} where the 'category' and 'value' fields are optional. If the 'category' isn't specified in the input JSON, then the one specified with '-cn' is assumed or "". If the value field is not present, then the default value is assumed. 
""" % __version__
DEFAULTS={
          }

def main():
    try:
        import jldaws.do_setup  #@UnusedImport
        import logging
        import jldaws.do_checks #@UnusedImport
        
        parser=argparse.ArgumentParser(description=DESC, fromfile_prefix_chars='@', formatter_class=argparse.RawTextHelpFormatter)
        
        parser.add_argument('-sp',  dest='src_path',       type=str, help="Source path",        required=True)        
        parser.add_argument('-p',   dest='polling',        type=int, help="Polling interval",   default=60)
        parser.add_argument('-rc',  dest='reset_count',    type=int, help="Reset count (n*polling)",   default=60)
        
        parser.add_argument('-bs',  dest='batch_size',     type=int, help="Batch size per interval",   default=1)
        parser.add_argument('-ext', dest='src_ext',        type=str, help="Source file extension e.g. json",  default=None)
        
        parser.add_argument('-dn',  dest='domain_name',    type=str, help="Domain name",        required=True)
        parser.add_argument('-cn',  dest='category_name',  type=str, help="Category name",      default="")
        parser.add_argument('-dv',  dest='default_value',  type=str, help="Default value",      default="1")
                
        from jldaws.script_sdbbatchinsert import run
        from jldaws.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())
