#!/usr/Local/pyroot/PyTangoRoot/bin/python """ Command Line Interface to use ArchiveExtractor module """ import argparse import core.ArchiveExtractor as AE import logging import datetime import numpy as np import PyTango as tango # Name the logger after the filename logger = logging.getLogger("ArchiveExtractor") # Default stop date dateStop = datetime.datetime.now() # Default start date dateStart = datetime.datetime.now()-datetime.timedelta(days=1) ####################################################### # Install argument parser parser = argparse.ArgumentParser(description="Extract attributes from the extractor devices.\nVersion %s"%AE.__version__) parser.add_argument("--from", type=AE.ArchiveExtractor.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=AE.ArchiveExtractor.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"%AE.__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 ####################################################### # Instanciate Extractor if args.DB == "L": AE = AE.ArchiveExtractor(extractorPath="archiving/extractor/%d"%(args.DBN), logger=logger) else: AE = AE.ArchiveExtractor(args.DB, args.DBN, logger=logger) ####################################################### # Prepare dictionnary for result results = dict() ####################################################### # Extract from database for attr in attributes: logger.info("Extracting attribute %s..."%attr) logger.info("Extract from %s to %s."%(args.dateStart, args.dateStop)) for attempt in range(3): try: datevalue = AE.betweenDates(attr, args.dateStart, args.dateStop) # 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)