Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl. duplicates identification #151

Draft
wants to merge 20 commits into
base: prescient/new_server
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 62 additions & 39 deletions lochness/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@
"""
Module to read Lochness configuration file and keyring file.
"""

import getpass as gp
import logging
import os
import string
from typing import Any, Dict

import cryptease as crypt
import yaml
import logging
import yaml.reader
import getpass as gp
import cryptease as crypt
import string

logger = logging.getLogger(__name__)


def load(f: 'location', archive_base=None):
'''load configuration file and keyring'''
logger.debug('loading configuration')
def load(path: str, archive_base=None) -> Dict[str, Any]:
"""
Load configuration file and keyring

Uses passphrase from environment variable NRG_KEYRING_PASS if available.
Otherwise, prompts user for passphrase.

Args:
path (str): path to configuration file (yaml)
archive_base (str): path to the root of the archive

with open(os.path.expanduser(f), 'rb') as fp:
Lochness = _read_config_file(fp)
Returns:
Dict[str, Any]: configuration dictionary
"""
logger.debug("loading configuration")
Lochness = _read_config_file(path)

if archive_base:
Lochness['phoenix_root'] = archive_base
if 'phoenix_root' not in Lochness:
raise ConfigError('need either --archive-base or '
'\'phoenix_root\' in config file')
Lochness['phoenix_root'] = os.path.expanduser(Lochness['phoenix_root'])
Lochness['keyring_file'] = os.path.expanduser(Lochness['keyring_file'])
Lochness["phoenix_root"] = archive_base
if "phoenix_root" not in Lochness:
raise ConfigError(
"need either --archive-base or 'phoenix_root' in config file"
)
Lochness["phoenix_root"] = os.path.expanduser(Lochness["phoenix_root"])
Lochness["keyring_file"] = os.path.expanduser(Lochness["keyring_file"])

# box file pattern strings from the config to string template
# regardless of the selected study in the args
if 'box' in Lochness:
for _, study_dict in Lochness['box'].items():
for _, modality_values in study_dict['file_patterns'].items():
if "box" in Lochness:
for _, study_dict in Lochness["box"].items():
for _, modality_values in study_dict["file_patterns"].items():
for modality_dict in modality_values:
modality_dict['pattern'] = \
string.Template(modality_dict['pattern'])
modality_dict["pattern"] = string.Template(modality_dict["pattern"])

with open(Lochness['keyring_file'], 'rb') as fp:
logger.info('reading keyring file {0}'.format(Lochness['keyring_file']))
if 'NRG_KEYRING_PASS' in os.environ:
load.passphrase = os.environ['NRG_KEYRING_PASS']
with open(Lochness["keyring_file"], "rb") as fp:
logger.info(f"reading keyring file {Lochness["keyring_file"]}")
if "NRG_KEYRING_PASS" in os.environ:
load.passphrase = os.environ["NRG_KEYRING_PASS"]
if load.passphrase is None:
load.passphrase = gp.getpass('enter passphrase: ')
load.passphrase = gp.getpass("enter passphrase: ")
key = crypt.key_from_file(fp, load.passphrase)
content = b''
content = b""
for chunk in crypt.decrypt(fp, key):
content += chunk
try:
Lochness['keyring'] = yaml.load(content, Loader=yaml.FullLoader)
Lochness["keyring"] = yaml.load(content, Loader=yaml.FullLoader)
except yaml.reader.ReaderError:
raise KeyringError('could not decrypt keyring {0} (wrong passphrase?)'.format(Lochness['keyring_file']))
raise KeyringError(
f"could not decrypt keyring {Lochness["keyring_file"]} (wrong passphrase?)"
)

return Lochness

Expand All @@ -55,19 +73,24 @@ def load(f: 'location', archive_base=None):


class KeyringError(Exception):
pass
"""
Generic keyring error.
"""


def _read_config_file(fp):
'''helper to read lochness configuration file'''
try:
cfg = yaml.load(fp.read(), Loader=yaml.FullLoader)
except Exception as e:
raise ConfigError('failed to parse {0} with error: {1}'.format(fp.name, e))
return cfg
def _read_config_file(path: str) -> Dict[str, Any]:
"""helper to read lochness configuration file"""


class ConfigError(Exception):
pass
expanded_path = os.path.expanduser(path)
with open(expanded_path, "rb") as fp:
try:
cfg = yaml.load(fp.read(), Loader=yaml.FullLoader)
except Exception as e:
raise ConfigError(f"failed to parse {expanded_path} with error: {e}")
return cfg


class ConfigError(Exception):
"""
Malformed configuration file.
"""
Loading