Skip to content

Commit

Permalink
pyDKB/common: move read_es_config() function from stage to library.
Browse files Browse the repository at this point in the history
Function like this seem to be needed in multiple places, so why not to
have it in the common library.
  • Loading branch information
mgolosova committed Apr 26, 2019
1 parent 4dd8a17 commit 2b4a50e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
29 changes: 1 addition & 28 deletions Utils/Dataflow/091_datasetsRucio/datasets_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions Utils/Dataflow/pyDKB/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2b4a50e

Please sign in to comment.