Skip to content

Commit

Permalink
Misc updates related to foursight-smaht.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Aug 29, 2023
1 parent 8aecbd3 commit 47d3c44
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 160 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ foursight-core
Change Log
----------

4.4.1
4.5.0
=====
* 2023-07-17
* 2023-08
* Support local-check-runner utility.
* Minor change to respect REDIS_HOST_LOCAL environment variable (for local dev/testing),
as well as allowing override of Auth0 client/secret (AUTH0_CLIENT_LOCAL, AUTH0_SECRET_LOCAL).
* Miscellaneous changes to get foursight-smaht working properly.
* Support to get consorita/submission_centers, as well as for
awards/labs for foursight-fourfront (previous oversight).
* Added UI warning bar about inability to connect to ElasticSearch.


4.4.0
Expand Down
67 changes: 25 additions & 42 deletions foursight_core/captured_output.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import builtins
# TODO: Move to dcicutils.
from collections import namedtuple
from contextlib import contextmanager
import io
import sys
from typing import Optional

_original_print = builtins.print
_original_stdout = sys.stdout
_original_stderr = sys.stderr
_real_stdout = sys.stdout
_real_stderr = sys.stderr

@contextmanager
def captured_output(capture: bool = True):
Expand All @@ -23,65 +22,49 @@ def captured_output(capture: bool = True):
of the with-clause with this context manager, pass False as an argument to this context manager.
"""

save_print = _original_print
save_stdout = _original_stdout
save_stderr = _original_stderr
original_stdout = _real_stdout
original_stderr = _real_stderr
captured_output = io.StringIO()

def captured_print(*args, **kwargs) -> None:
captured_output.write(*args)
captured_output.write("\n")
def set_original_output() -> None:
sys.stdout = original_stdout
sys.stderr = original_stderr

def uncaptured_print(*args, **kwargs) -> None:
builtins.print = save_print
sys.stdout = save_stdout
sys.stderr = save_stderr
print(*args, **kwargs)
def set_captured_output() -> None:
if capture:
builtins.print = captured_print
sys.stdout = captured_output
sys.stderr = captured_output

def uncaptured_print(*args, **kwargs) -> None:
set_original_output()
print(*args, **kwargs)
set_captured_output()

def uncaptured_input(message: str) -> str:
builtins.print = save_print
sys.stdout = save_stdout
sys.stderr = save_stderr
set_original_output()
value = input(message)
if capture:
builtins.print = captured_print
sys.stdout = captured_output
sys.stderr = captured_output
set_captured_output()
return value

def get_captured_output() -> Optional[str]:
return captured_output.getvalue() if capture else None

if capture:
builtins.print = captured_print
sys.stdout = captured_output
sys.stderr = captured_output

Result = namedtuple("Result", ["get_captured_output", "uncaptured_print", "uncaptured_input"])

try:
set_captured_output()
Result = namedtuple("Result", ["get_captured_output", "uncaptured_print", "uncaptured_input"])
yield Result(get_captured_output, uncaptured_print, uncaptured_input)
finally:
builtins.print = save_print
sys.stdout = save_stdout
sys.stderr = save_stderr
set_original_output()


@contextmanager
def uncaptured_output():
save_print = builtins.print
save_stdout = sys.stdout
save_stderr = sys.stderr
builtins.print = _original_print
sys.stdout = _original_stdout
sys.stderr = _original_stderr
original_stdout = sys.stdout
original_stderr = sys.stderr
sys.stdout = _real_stdout
sys.stderr = _real_stderr
try:
yield
finally:
builtins.print = save_print
sys.stdout = save_stdout
sys.stderr = save_stderr
sys.stdout = original_stdout
sys.stderr = original_stderr
40 changes: 0 additions & 40 deletions foursight_core/check_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,46 +362,6 @@ def get_grouped_check_results(self, connection):
grouped_list = [group for group in grouped_results.values()]
return sorted(grouped_list, key=lambda v: v['_name'])

def obsolete_run_check_or_action(self, connection, check_str, check_kwargs):
"""
Does validation of provided check_str, it's module, and kwargs.
Determines by decorator whether the method is a check or action, then runs
it. All errors are taken care of within the running of the check/action.
Takes a FS_connection object, a check string formatted as: <str check module/name>
and a dictionary of check arguments.
For example:
check_str: 'system_checks/my_check'
check_kwargs: '{"foo":123}'
Fetches the check function and runs it (returning whatever it returns)
Return a string for failed results, CheckResult/ActionResult object otherwise.
"""
# make sure parameters are good
error_str = ' '.join(['Info: CHECK:', str(check_str), 'KWARGS:', str(check_kwargs)])
if len(check_str.strip().split('/')) != 2:
return ' '.join(['ERROR. Check string must be of form module/check_name.', error_str])
mod_name = check_str.strip().split('/')[0]
check_name = check_str.strip().split('/')[1]
if not isinstance(check_kwargs, dict):
return ' '.join(['ERROR. Check kwargs must be a dict.', error_str])
check_mod = None
for package_name in [self.check_package_name, 'foursight_core']:
try:
check_mod = self.import_check_module(package_name, mod_name)
except ModuleNotFoundError:
continue
except Exception as e:
raise e
if not check_mod:
return ' '.join(['ERROR. Check module is not valid.', error_str])
check_method = check_mod.__dict__.get(check_name)
if not check_method:
return ' '.join(['ERROR. Check name is not valid.', error_str])
if not self.check_method_deco(check_method, self.CHECK_DECO) and \
not self.check_method_deco(check_method, self.ACTION_DECO):
return ' '.join(['ERROR. Check or action must use a decorator.', error_str])
return check_method(connection, **check_kwargs)

def run_check_or_action(self, connection, check_str, check_kwargs):
"""
Does validation of provided check_str, it's module, and kwargs.
Expand Down
2 changes: 1 addition & 1 deletion foursight_core/react/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def create_authtoken(self, jwt: str, jwt_expires_at: int, domain: str, request:
# For testing only, we simulate a portal access key error (e.g. due to expiration),
# which would manifest itself, primarily and most importantly, here, on login.
raise Exception("test_mode_access_key_simulate_error")
allowed_envs, first_name, last_name = self._envs.get_user_auth_info(email, raise_exception=True)
allowed_envs, first_name, last_name = self._envs.get_user_auth_info(email)
user_exception = False
except Exception as e:
#
Expand Down
9 changes: 7 additions & 2 deletions foursight_core/react/api/envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dcicutils.env_utils import foursight_env_name
from dcicutils.function_cache_decorator import function_cache
from dcicutils.misc_utils import find_association
from ...app import app
from .gac import Gac

logging.basicConfig()
Expand Down Expand Up @@ -81,15 +82,19 @@ def get_user_auth_info(self, email: str, raise_exception: bool = False) -> Tuple
try:
# Note we must lower case the email to find the user. This is because all emails
# in the database are lowercased; it causes issues with OAuth if we don't do this.
known_env_name = known_env["full_name"]
envs = app.core.init_environments(known_env_name)
connection = app.core.init_connection(known_env_name, envs)
user = ff_utils.get_metadata('users/' + email.lower(),
ff_env=known_env["full_name"], add_on="frame=object&datastore=database")
key=connection.ff_keys,
add_on="frame=object&datastore=database")
if self._is_user_allowed_access(user):
# Since this is in a loop, for each env, this setup here will end up getting first/last name
# from the last env in the loop; doesn't really matter, just pick one set; this is just for
# informational/display purposes in the UI.
first_name = user.get("first_name")
last_name = user.get("last_name")
allowed_envs.append(known_env["full_name"])
allowed_envs.append(known_env_name)
except Exception as e:
if raise_exception:
raise
Expand Down
Loading

0 comments on commit 47d3c44

Please sign in to comment.