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

158 notebooks should have access to requesters oda token #248

Merged
1 change: 1 addition & 0 deletions oda_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@

conf_file=os.path.join(conf_dir,'data_server_conf.yml')

context_file = ".oda_api_context"

29 changes: 23 additions & 6 deletions oda_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,17 +1345,34 @@

return d

def get_context():
"""
load context from file .oda_api_context in the notebook dir
"""
from oda_api import context_file
if not os.path.isfile(context_file):
return {}
with open(context_file, 'r') as file:
context = json.load(file)
return context

class ProgressReporter(object):
"""
The class allows to report task progress to end user
"""
def __init__(self):
self._callback = None
callback_file = ".oda_api_callback" # perhaps it would be better to define this constant in a common lib
volodymyrss marked this conversation as resolved.
Show resolved Hide resolved
if not os.path.isfile(callback_file):
return
with open(callback_file, 'r') as file:
self._callback = file.read().strip()
callback = get_context().get('callback', None)
if callback:
callback = callback.strip()
else:
# backward compatibility
callback_file = ".oda_api_callback"
if os.path.isfile(callback_file):
logger.warning(f'reading callback from the deprecated location: {callback_file}')
with open(callback_file, 'r') as file:
callback = file.read().strip()

Check warning on line 1373 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1371-L1373

Added lines #L1371 - L1373 were not covered by tests

self._callback = callback

@property
def enabled(self):
Expand Down
2 changes: 2 additions & 0 deletions oda_api/plot_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def show(self, ext_sig=None, meta=None, header=None, sources=None,
def build_fig(self, ext_sig=None, meta=None, header=None, sources=None,
levels=None, cmap=cm.gist_earth,
unit_ID=4, det_sigma=3, sliders=True):
if self.data is None:
raise ValueError('data is required')

if levels is None:
levels = numpy.linspace(1, 10, 10)
Expand Down
4 changes: 4 additions & 0 deletions oda_api/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TokenLocation(Enum):
ODA_ENV_VAR = "environment variable ODA_TOKEN"
FILE_CUR_DIR = "file in current directory"
FILE_HOME = "file in home"
CONTEXT_FILE = "context file current directory"

try:
import jwt
Expand Down Expand Up @@ -314,6 +315,7 @@ def rewrite_token(new_token,
def discover_token_and_method(
allow_invalid=False,
token_discovery_methods=None):
from oda_api.api import get_context
failed_methods = []
token = None
if token_discovery_methods is None:
Expand All @@ -332,6 +334,8 @@ def discover_token_and_method(
elif n == TokenLocation.FILE_HOME:
with open(path.join(environ["HOME"], ".oda-token")) as ft:
token = ft.read().strip()
elif n == TokenLocation.CONTEXT_FILE:
token = get_context()['token'].strip()

logger.debug("searching for token in %s", n)
decoded_token = decode_oda_token(token, allow_invalid=allow_invalid)
Expand Down
12 changes: 7 additions & 5 deletions tests/test_plot_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@

@pytest.mark.xfail(reason="fails")
def test_image_no_sources():
import numpy as np
from astropy.wcs import WCS
from oda_api.plot_tools import OdaImage

oda_image = OdaImage(None)
oda_image.build_fig(header=WCS().to_header(),
meta={"src_name": "testsource"},
sources=[])

try:
oda_image.build_fig(header=WCS().to_header(),
meta={"src_name": "testsource"},
sources=[])
assert False, 'the code above should have raised the ValueError exception'

Check warning on line 31 in tests/test_plot_tools.py

View check run for this annotation

Codecov / codecov/patch

tests/test_plot_tools.py#L31

Added line #L31 was not covered by tests
except ValueError:
pass

def test_lc_adjustend_bins(request):
import numpy as np
Expand Down
17 changes: 10 additions & 7 deletions tests/test_progress_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import os
import json

callback_file = ".oda_api_callback"
context_file = ".oda_api_context"
request_params = dict(stage='simulation', progress=50., progress_max=100., substage='spectra', subprogress=30., subprogress_max=100., message='some message')

def test_progress_reporter_disabled():
if os.path.isfile(callback_file):
os.remove(callback_file)
if os.path.isfile(context_file):
os.remove(context_file)

Check warning on line 10 in tests/test_progress_report.py

View check run for this annotation

Codecov / codecov/patch

tests/test_progress_report.py#L10

Added line #L10 was not covered by tests
# if callback is not available
pr = ProgressReporter()
assert not pr.enabled
Expand All @@ -21,8 +21,11 @@
# if callback is available
try:
dump_file = 'callback'
with open(callback_file, 'w') as file:
print(f'file://{os.getcwd()}/{dump_file}', file=file)
context = {
"callback": f'file://{os.getcwd()}/{dump_file}'
}
with open(context_file, 'w') as output:
json.dump(context, output)

pr = ProgressReporter()
assert pr.enabled
Expand All @@ -36,8 +39,8 @@
request_params['action'] = 'progress' # this key is added by report_progress
assert saved_params == request_params
finally:
if os.path.isfile(callback_file):
os.remove(callback_file)
if os.path.isfile(context_file):
os.remove(context_file)
if os.path.isfile(dump_file):
os.remove(dump_file)

Loading