Skip to content
Snippets Groups Projects
cli_archiveextractor.py 4.85 KiB
Newer Older


##########################################################################
""" Command Line Interface """
if __name__ == "__main__":

    # Name the logger after the filename
    logger = logging.getLogger("ArchiveExtractor")

    # Default stop date
    dateStop = datetime.datetime.now()

    # Default stop date
    dateStart = datetime.datetime.now()-datetime.timedelta(days=1)

    #######################################################
    # Install argument parser
    import argparse

    parser = argparse.ArgumentParser(description="Extract attributes from the extractor devices.\nVersion %s"%__version__)

    parser.add_argument("--from", type=dateparse, dest="dateStart",
        help="Start date for extraction, format '1990-12-13-22:33:45'. "+
        "It is possible to be less precise and drop, seconds, minutes, hours or even day."+
        " Default is one day ago",
        default=dateStart)

    parser.add_argument("--to", type=dateparse, dest="dateStop",
        help="Stop date for extraction, format '1990-12-13-22:33:45'. It is possible to be less precise and drop, seconds, minutes, hours or even day."+
        " Default is now.",
        default=dateStop)

    parser.add_argument("--DB", choices=["H", "T", "L"],
        default="T", help="Database to extract from. HDB (H) or TDB (T), default: %(default)s")

    parser.add_argument("--DBN", type=int, default=2,
            help="Extractor device number, default: %(default)s")

    parser.add_argument("--fileout", type=str, default="extracted_%s.npy"%datetime.datetime.now().strftime("%Y%m%d_%H%M%S"),
            help="filename of the extraction destination. Default: %(default)s"),

    parser.add_argument('--log', type=str, default="INFO",
            help="Log level. Default: %(default)s.")


    parser.add_argument('--filemode', action="store_true",
            help="Set attribute to filemode."+
            " Instead of specifying attributes, put a path to a file containing a list of attributes."+
            " The file contains one attribute per line.")

    parser.add_argument('attributes', type=str, nargs='+',
                        help="List of attributes to extract. Full tango path.")

    args = parser.parse_args()


    #######################################################
    # Configure logger

    # Add a stream handler
    s_handler = logging.StreamHandler()
    s_handler.setFormatter(logging.Formatter("%(levelname)s\t[%(funcName)s] \t%(message)s"))

    # Set level according to command line attribute
    s_handler.setLevel(level=getattr(logging, args.log.upper()))
    logger.setLevel(level=getattr(logging, args.log.upper()))
    logger.addHandler(s_handler)

    logger.debug("Parsed arguments: %s"%args)

    logger.info("Archive Extractor %s"%__version__)

    #######################################################
    # Filemode or not
    if args.filemode:
        logger.info("Filemode, openning file %s"%args.attributes[0])
        # Read the file. Each line is an attribute
        with open(args.attributes[0], "r") as fp:
            attributes = fp.readlines()

        logger.debug("Read lines : %s"%attributes)

        # Clean end of line
        for i_a in range(len(attributes)):
            attributes[i_a] = attributes[i_a].rstrip()

    else:
        attributes = args.attributes

    #######################################################
    # Select Extractor
    if args.DB == "L":
        extractor = "archiving/extractor/%d"%(args.DBN)
    else:
        extractor = "archiving/%sDBExtractor/%d"%(args.DB, args.DBN)

    #######################################################
    # Prepare dictionnary for result
    results = dict()

    #######################################################
    # Extract from database
    logger.info("Extract from %s to %s."%(args.dateStart, args.dateStop))

    for attr in attributes:
        logger.info("Extracting attribute %s..."%attr)

        for attempt in range(3):
            try:
                datevalue = query_ADB_BetweenDates(attr, args.dateStart, args.dateStop, extractor)

                # Add to result dictionnary
                results[attr] = datevalue

            except ValueError as e:
                logger.debug("ErrorMsg: %s"%e)
                logger.warning("Failed to extract %s. Skipping..."%attr)
            except (tango.CommunicationFailed, tango.DevFailed) as e:
                # retry
                logger.debug("ErrorMsg: %s"%e)
                logger.warning("Failed to extract %s. Retry..."%attr)
            break

        else:
            logger.error("The device %s might have crash.\n"%extractor+
                    "You should check with Jive and probably restart with Astor.\n")

        # Save all at each step
        np.save(args.fileout, results)

    logger.info("Extraction done, saved in file %s"%args.fileout)

else:
    # Name the logger after the module name
    logger = logging.getLogger(__name__)