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

[WIP] Config refactoring #615

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 20 additions & 20 deletions cli/medperf/account_management/account_management.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from .token_storage import TokenStore
from medperf.config_management import read_config, write_config
from medperf import config
from medperf.config_management import config
from medperf import settings
from medperf.exceptions import MedperfException


def read_user_account():
config_p = read_config()
if config.credentials_keyword not in config_p.active_profile:
config_p = config.read_config()
if settings.credentials_keyword not in config_p.active_profile:
return

account_info = config_p.active_profile[config.credentials_keyword]
account_info = config_p.active_profile[settings.credentials_keyword]
return account_info


Expand All @@ -23,15 +23,15 @@ def set_credentials(
):
email = id_token_payload["email"]
TokenStore().set_tokens(email, access_token, refresh_token)
config_p = read_config()
config_p = config.read_config()

if login_event:
# Set the time the user logged in, so that we can track the lifetime of
# the refresh token
logged_in_at = token_issued_at
else:
# This means this is a refresh event. Preserve the logged_in_at timestamp.
logged_in_at = config_p.active_profile[config.credentials_keyword][
logged_in_at = config_p.active_profile[settings.credentials_keyword][
"logged_in_at"
]

Expand All @@ -42,8 +42,8 @@ def set_credentials(
"logged_in_at": logged_in_at,
}

config_p.active_profile[config.credentials_keyword] = account_info
write_config(config_p)
config_p.active_profile[settings.credentials_keyword] = account_info
config_p.write_config()


def read_credentials():
Expand All @@ -61,35 +61,35 @@ def read_credentials():


def delete_credentials():
config_p = read_config()
if config.credentials_keyword not in config_p.active_profile:
config_p = config.read_config()
if settings.credentials_keyword not in config_p.active_profile:
raise MedperfException("You are not logged in")

email = config_p.active_profile[config.credentials_keyword]["email"]
email = config_p.active_profile[settings.credentials_keyword]["email"]
TokenStore().delete_tokens(email)

config_p.active_profile.pop(config.credentials_keyword)
write_config(config_p)
config_p.active_profile.pop(settings.credentials_keyword)
config_p.write_config()


def set_medperf_user_data():
"""Get and cache user data from the MedPerf server"""
config_p = read_config()
config_p = config.read_config()
medperf_user = config.comms.get_current_user()

config_p.active_profile[config.credentials_keyword]["medperf_user"] = medperf_user
write_config(config_p)
config_p.active_profile[settings.credentials_keyword]["medperf_user"] = medperf_user
config_p.write_config()

return medperf_user


def get_medperf_user_data():
"""Return cached medperf user data. Get from the server if not found"""
config_p = read_config()
if config.credentials_keyword not in config_p.active_profile:
config_p = config.read_config()
if settings.credentials_keyword not in config_p.active_profile:
raise MedperfException("You are not logged in")

medperf_user = config_p.active_profile[config.credentials_keyword].get(
medperf_user = config_p.active_profile[settings.credentials_keyword].get(
"medperf_user", None
)
if medperf_user is None:
Expand Down
8 changes: 4 additions & 4 deletions cli/medperf/account_management/token_storage/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import base64
import logging
from medperf.utils import remove_path
from medperf import config
from medperf import settings


class FilesystemTokenStore:
def __init__(self):
self.creds_folder = config.creds_folder
self.creds_folder = settings.creds_folder
os.makedirs(self.creds_folder, mode=0o700, exist_ok=True)

def __get_paths(self, account_id):
Expand All @@ -19,9 +19,9 @@ def __get_paths(self, account_id):
account_folder = os.path.join(self.creds_folder, account_id_encoded)
os.makedirs(account_folder, mode=0o700, exist_ok=True)

access_token_file = os.path.join(account_folder, config.access_token_storage_id)
access_token_file = os.path.join(account_folder, settings.access_token_storage_id)
refresh_token_file = os.path.join(
account_folder, config.refresh_token_storage_id
account_folder, settings.refresh_token_storage_id
)

return access_token_file, refresh_token_file
Expand Down
14 changes: 7 additions & 7 deletions cli/medperf/account_management/token_storage/keyring_.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
users who connect to remote machines through passwordless SSH faced some issues."""

import keyring
from medperf import config
from medperf import settings


class KeyringTokenStore:
Expand All @@ -11,33 +11,33 @@ def __init__(self):

def set_tokens(self, account_id, access_token, refresh_token):
keyring.set_password(
config.access_token_storage_id,
settings.access_token_storage_id,
account_id,
access_token,
)
keyring.set_password(
config.refresh_token_storage_id,
settings.refresh_token_storage_id,
account_id,
refresh_token,
)

def read_tokens(self, account_id):
access_token = keyring.get_password(
config.access_token_storage_id,
settings.access_token_storage_id,
account_id,
)
refresh_token = keyring.get_password(
config.refresh_token_storage_id,
settings.refresh_token_storage_id,
account_id,
)
return access_token, refresh_token

def delete_tokens(self, account_id):
keyring.delete_password(
config.access_token_storage_id,
settings.access_token_storage_id,
account_id,
)
keyring.delete_password(
config.refresh_token_storage_id,
settings.refresh_token_storage_id,
account_id,
)
7 changes: 4 additions & 3 deletions cli/medperf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import logging.handlers

from medperf import __version__
import medperf.config as config
from medperf import settings
from medperf.config_management import config
from medperf.decorators import clean_except, add_inline_parameters
import medperf.commands.result.result as result
from medperf.commands.result.create import BenchmarkExecution
Expand Down Expand Up @@ -92,10 +93,10 @@ def main(
# Set inline parameters
inline_args = ctx.params
for param in inline_args:
setattr(config, param, inline_args[param])
setattr(settings, param, inline_args[param])

# Update logging level according to the passed inline params
loglevel = config.loglevel.upper()
loglevel = settings.loglevel.upper()
logging.getLogger().setLevel(loglevel)
logging.getLogger("requests").setLevel(loglevel)

Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/association/approval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from medperf import config
from medperf.config_management import config
from medperf.exceptions import InvalidArgumentError


Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/association/association.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typer
from typing import Optional

import medperf.config as config
from medperf.config_management import config
from medperf.decorators import clean_except
from medperf.commands.association.list import ListAssociations
from medperf.commands.association.approval import Approval
Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/association/list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tabulate import tabulate

from medperf import config
from medperf.config_management import config


class ListAssociations:
Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/association/priority.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from medperf import config
from medperf.config_management import config
from medperf.exceptions import InvalidArgumentError
from medperf.entities.benchmark import Benchmark

Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from medperf.commands.auth.logout import Logout
from medperf.commands.auth.status import Status
from medperf.decorators import clean_except
import medperf.config as config
from medperf.config_management import config
import typer

app = typer.Typer()
Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/auth/login.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import medperf.config as config
from medperf.config_management import config
from medperf.account_management import read_user_account
from medperf.exceptions import InvalidArgumentError, MedperfException
from email_validator import validate_email, EmailNotValidError
Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/auth/logout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import medperf.config as config
from medperf.config_management import config


class Logout:
Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/auth/status.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import medperf.config as config
from medperf.config_management import config
from medperf.account_management import read_user_account


Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/auth/synapse_login.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import synapseclient
from synapseclient.core.exceptions import SynapseAuthenticationError
from medperf import config
from medperf.config_management import config
from medperf.exceptions import CommunicationAuthenticationError


Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/benchmark/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typer
from typing import Optional

import medperf.config as config
from medperf.config_management import config
from medperf.decorators import clean_except
from medperf.entities.benchmark import Benchmark
from medperf.commands.list import EntityList
Expand Down
5 changes: 3 additions & 2 deletions cli/medperf/commands/benchmark/submit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import medperf.config as config
from medperf import settings
from medperf.config_management import config
from medperf.entities.benchmark import Benchmark
from medperf.exceptions import InvalidEntityError
from medperf.utils import remove_path
Expand Down Expand Up @@ -53,7 +54,7 @@ def __init__(
self.no_cache = no_cache
self.skip_data_preparation_step = skip_data_preparation_step
self.bmk.metadata["demo_dataset_already_prepared"] = skip_data_preparation_step
config.tmp_paths.append(self.bmk.path)
settings.tmp_paths.append(self.bmk.path)

def get_extra_information(self):
"""Retrieves information that must be populated automatically,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typer
from typing import Optional

import medperf.config as config
from medperf.config_management import config
from medperf.decorators import clean_except
from medperf.commands.view import EntityView
from medperf.entities.report import TestReport
Expand Down
19 changes: 10 additions & 9 deletions cli/medperf/commands/compatibility_test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from medperf.comms.entity_resources import resources
from medperf.entities.cube import Cube
import medperf.config as config
from medperf import settings
from medperf.config_management import config
import os
import yaml
from pathlib import Path
Expand All @@ -26,7 +27,7 @@ def download_demo_data(dset_url, dset_hash):

# It is assumed that all demo datasets contain a file
# which specifies the input of the data preparation step
paths_file = os.path.join(demo_dset_path, config.demo_dset_paths_file)
paths_file = os.path.join(demo_dset_path, settings.demo_dset_paths_file)
with open(paths_file, "r") as f:
paths = yaml.safe_load(f)

Expand All @@ -41,14 +42,14 @@ def download_demo_data(dset_url, dset_hash):

def prepare_local_cube(path):
temp_uid = get_folders_hash([path])
cubes_folder = config.cubes_folder
cubes_folder = settings.cubes_folder
dst = os.path.join(cubes_folder, temp_uid)
os.symlink(path, dst)
logging.info(f"local cube will be linked to path: {dst}")
config.tmp_paths.append(dst)
cube_metadata_file = os.path.join(path, config.cube_metadata_filename)
settings.tmp_paths.append(dst)
cube_metadata_file = os.path.join(path, settings.cube_metadata_filename)
if not os.path.exists(cube_metadata_file):
mlcube_yaml_path = os.path.join(path, config.cube_filename)
mlcube_yaml_path = os.path.join(path, settings.cube_filename)
mlcube_yaml_hash = get_file_hash(mlcube_yaml_path)
temp_metadata = {
"id": None,
Expand All @@ -63,7 +64,7 @@ def prepare_local_cube(path):
metadata = Cube(**temp_metadata).todict()
with open(cube_metadata_file, "w") as f:
yaml.dump(metadata, f)
config.tmp_paths.append(cube_metadata_file)
settings.tmp_paths.append(cube_metadata_file)

return temp_uid

Expand All @@ -90,7 +91,7 @@ def prepare_cube(cube_uid: str):
path = path.resolve()

if os.path.exists(path):
mlcube_yaml_path = os.path.join(path, config.cube_filename)
mlcube_yaml_path = os.path.join(path, settings.cube_filename)
if os.path.exists(mlcube_yaml_path):
logging.info("local path provided. Creating symbolic link")
temp_uid = prepare_local_cube(path)
Expand Down Expand Up @@ -137,7 +138,7 @@ def create_test_dataset(
data_creation.create_dataset_object()
# TODO: existing dataset could make problems
# make some changes since this is a test dataset
config.tmp_paths.remove(data_creation.dataset.path)
settings.tmp_paths.remove(data_creation.dataset.path)
if skip_data_preparation_step:
data_creation.make_dataset_prepared()
dataset = data_creation.dataset
Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/dataset/associate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from medperf import config
from medperf.config_management import config
from medperf.entities.dataset import Dataset
from medperf.entities.benchmark import Benchmark
from medperf.utils import dict_pretty_print, approval_prompt
Expand Down
2 changes: 1 addition & 1 deletion cli/medperf/commands/dataset/dataset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typer
from typing import Optional

import medperf.config as config
from medperf.config_management import config
from medperf.decorators import clean_except
from medperf.entities.dataset import Dataset
from medperf.commands.list import EntityList
Expand Down
Loading
Loading