diff --git a/core/ArchiveExtractor.py b/core/ArchiveExtractor.py index 80f952e4d97f467b91ea24f3b8fa94d81b4c52a0..ba40a9891447283d8dbf3a91312306df5e6c799d 100755 --- a/core/ArchiveExtractor.py +++ b/core/ArchiveExtractor.py @@ -10,7 +10,22 @@ import pandas as pd __version__ = "1.0.1" ########################################################################## -""" Commodity private variables """ +### Install logger for the module ### +########################################################################## +logger = logging.getLogger(__name__) +#logger.setLevel(getattr(logging, logger.upper())) + +if not logger.hasHandlers(): + # No handlers, create one + sh = logging.StreamHandler() + sh.setLevel(logger.level) + sh.setFormatter(logging.Formatter("%(levelname)s:%(message)s")) + logger.addHandler(sh) + + +########################################################################## +### Commodity private variables ### +########################################################################## # Extractor date format for GetAttDataBetweenDates _DBDFMT = "%Y-%m-%d %H:%M:%S" @@ -19,11 +34,106 @@ _DBDFMT = "%Y-%m-%d %H:%M:%S" _DBDFMT2 = "%d-%m-%Y %H:%M:%S" ########################################################################## -""" Commodity private functions """ +### Commodity private functions ### +########################################################################## # Vectorized fromtimestamp function _ArrayTimeStampToDatetime = np.vectorize(datetime.datetime.fromtimestamp) +def _check_initialized(): + """ + Check if the module is initialized, raise exception RuntimeError if not. + """ + global _extractors + if None in _extractors: + logger.error("Module {0} is not initialied. You should run {0}.init().".format(__name__)) + raise RuntimeError("Module not initialized") + +def _dateparse(datestr): + """ + Convenient function to parse date strings. + Global format is %Y-%m-%d-%H:%M:%S and it can be reduced to be less precise. + + Parameters + --------- + datestr : string + Date as a string, format %Y-%m-%d-%H:%M:%S or less precise. + + Exceptions + ---------- + ValueError + If the parsing failed. + + Returns + ------- + date : datetime.datetime + Parsed date + """ + + # This gives all format that will be tried, in order. + # Stop on first parse success. Raise error if none succeed. + fmt = [ + "%Y-%m-%d-%H:%M:%S", + "%Y-%m-%d-%H:%M", + "%Y-%m-%d-%H", + "%Y-%m-%d", + "%Y-%m", + ] + + date = None + for f in fmt: + try: + date = datetime.datetime.strptime(datestr, f) + except ValueError: + continue + else: + break + else: + raise ValueError("Could not parse argument to a date") + + return date + + +########################################################################## +### Module private variables ### +########################################################################## +# Tuple of extractor for HDB and TDB +_extractors = (None, None) + +# Tuple for attribute tables +_AttrTables = (None, None) + +########################################################################## +### Module initialisation functions ### +########################################################################## +def init( + HdbExtractorPath="archiving/hdbextractor/2", + TdbExtractorPath="archiving/tdbextractor/2", + ): + """ + Initialize the module. + Instanciate tango.DeviceProxy for extractors (TDB and HDB) + + HdbExtractorPath, TdbExtractorPath: string + Tango path to the extractors. + """ + global _extractors + + logger.debug("Instanciating extractors device proxy...") + + _extractors = (tango.DeviceProxy(HdbExtractorPath), tango.DeviceProxy(TdbExtractorPath)) + logger.debug("{} and {} instanciated.".format(*_extractors)) + + logger.debug("Configuring extractors device proxy...") + for e in _extractors: + # set timeout to 3 sec + e.set_timeout_millis(3000) + + logger.debug("Filling attributes lookup tables...") + _AttrTables = tuple(e.getattnameall() for e in _extractors) + logger.debug("HDB: {} TDB: {} attributes counted".format(len(_AttrTables[0]), len(_AttrTables[1]))) + + class ArchiveExtractor: