From 3b5f9e1a3660ee50cf723565f90b6e146da792e0 Mon Sep 17 00:00:00 2001
From: Romain Broucquart <romain.broucquart@synchrotron-soleil.fr>
Date: Wed, 16 Nov 2022 16:57:43 +0100
Subject: [PATCH] WIP move functions, variables to module

---
 core/ArchiveExtractor.py | 114 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 112 insertions(+), 2 deletions(-)

diff --git a/core/ArchiveExtractor.py b/core/ArchiveExtractor.py
index 80f952e..ba40a98 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:
 
-- 
GitLab