From 2b4a50e35bdf5710962c8f9544830931ceb43b1c Mon Sep 17 00:00:00 2001 From: Marina Golosova Date: Fri, 26 Apr 2019 23:11:36 +0200 Subject: [PATCH] pyDKB/common: move `read_es_config()` function from stage to library. Function like this seem to be needed in multiple places, so why not to have it in the common library. --- .../091_datasetsRucio/datasets_processing.py | 29 +---------------- Utils/Dataflow/pyDKB/common/utils.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/Utils/Dataflow/091_datasetsRucio/datasets_processing.py b/Utils/Dataflow/091_datasetsRucio/datasets_processing.py index 6990851b3..1f750dc1a 100755 --- a/Utils/Dataflow/091_datasetsRucio/datasets_processing.py +++ b/Utils/Dataflow/091_datasetsRucio/datasets_processing.py @@ -37,6 +37,7 @@ sys.path.append(dkb_dir) import pyDKB from pyDKB import storages + from pyDKB.common.utils import read_es_config except Exception, err: sys.stderr.write("(ERROR) Failed to import pyDKB library: %s\n" % err) sys.exit(1) @@ -103,34 +104,6 @@ def main(argv): sys.exit(exit_code) -def read_es_config(cfg_file): - """ Read ES configuration file. - - :param cfg_file: open file descriptor with ES access configuration - :type cfg_file: file descriptor - """ - keys = {'ES_HOST': 'host', - 'ES_PORT': 'port', - 'ES_USER': 'user', - 'ES_PASSWORD': '__passwd', - 'ES_INDEX': 'index' - } - cfg = {} - for line in cfg_file.readlines(): - if line.strip().startswith('#'): - continue - line = line.split('#')[0].strip() - if '=' not in line: - continue - key, val = line.split('=')[:2] - try: - cfg[keys[key]] = val - except KeyError: - sys.stderr.write("(WARN) Unknown configuration parameter: " - "'%s'.\n" % key) - return cfg - - def init_rucio_client(): """ Initialize global variable `rucio_client`. """ global rucio_client diff --git a/Utils/Dataflow/pyDKB/common/utils.py b/Utils/Dataflow/pyDKB/common/utils.py index 4d942ffe3..dc5aa0d37 100644 --- a/Utils/Dataflow/pyDKB/common/utils.py +++ b/Utils/Dataflow/pyDKB/common/utils.py @@ -54,3 +54,34 @@ def custom_readline(f, newline): pos = buf.index(newline) yield buf[:pos] buf = buf[pos + len(newline):] + + +def read_es_config(cfg_file): + """ Read ES configuration file. + + We have ES config in form of file with shell variables declaration, + but sometimes need to parse it in Python as well. + + :param cfg_file: open file descriptor with ES access configuration + :type cfg_file: file descriptor + """ + keys = {'ES_HOST': 'host', + 'ES_PORT': 'port', + 'ES_USER': 'user', + 'ES_PASSWORD': '__passwd', + 'ES_INDEX': 'index' + } + cfg = {} + for line in cfg_file.readlines(): + if line.strip().startswith('#'): + continue + line = line.split('#')[0].strip() + if '=' not in line: + continue + key, val = line.split('=')[:2] + try: + cfg[keys[key]] = val + except KeyError: + sys.stderr.write("(WARN) Unknown configuration parameter: " + "'%s'.\n" % key) + return cfg