Skip to content

py client version check fixes and cleanup #1497

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

Merged
merged 8 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions integrations/client/test_delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,16 @@ def raise_for_status(self): pass
def json(self): return json.loads(self.content)
get.reset_mock()
get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200)

# "back up" the value of this private class var and replace w/ default
# so the ._version_check() method runs unencumbered:
e_vdc__save = Epidata._version_checked
Epidata._version_checked = False
# run version check:
Epidata._version_check()
# "restore" class var:
Epidata._version_checked = e_vdc__save

captured = self.capsys.readouterr()
output = captured.err.splitlines()
self.assertEqual(len(output), 1)
Expand Down
35 changes: 24 additions & 11 deletions src/client/delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,38 @@ class Epidata:
debug = False # if True, prints extra logging statements
sandbox = False # if True, will not execute any queries

_version_checked = False

@staticmethod
def log(evt, **kwargs):
kwargs['event'] = evt
kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z")
return sys.stderr.write(str(kwargs) + "\n")

# Check that this client's version matches the most recent available, runs just once per program execution (on initial module load).
# Check that this client's version matches the most recent available.
# This is indended to run just once per program execution, on initial module load.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This is indended to run just once per program execution, on initial module load.
# This is intended to run just once per program execution, on initial module load.

# See the bottom of this file for the ultimate call to this method.
@staticmethod
def _version_check():
if Epidata._version_checked:
# already done; nothing to do!
return

Epidata._version_checked = True

try:
latest_version = requests.get('https://pypi.org/pypi/delphi-epidata/json').json()['info']['version']
if latest_version != __version__:
Epidata.log(
"Client version not up to date",
client_version=__version__,
latest_version=latest_version
)
request = requests.get('https://pypi.org/pypi/delphi-epidata/json', timeout=5)
latest_version = request.json()['info']['version']
except Exception as e:
Epidata.log("Error getting latest client version", exception=str(e))
return

# Run this once on module load. Use dunder method for Python <= 3.9 compatibility
# https://stackoverflow.com/a/12718272
_version_check.__func__()
if latest_version != __version__:
Epidata.log(
"Client version not up to date",
client_version=__version__,
latest_version=latest_version
)

# Helper function to cast values and/or ranges to strings
@staticmethod
Expand Down Expand Up @@ -708,3 +717,7 @@ async def async_make_calls(param_combos):
future = asyncio.ensure_future(async_make_calls(param_list))
responses = loop.run_until_complete(future)
return responses



Epidata._version_check()
Loading