Skip to content

Commit

Permalink
Merge pull request #4 from PADAS/async-cli-approach
Browse files Browse the repository at this point in the history
Async CLI suite approach
  • Loading branch information
vgarcia13 authored Nov 5, 2024
2 parents 1108d2d + fc79f68 commit 0ad9def
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 59 deletions.
115 changes: 57 additions & 58 deletions movebank_client/cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import click
import asyncclick as click
import json
from dateparser import parse as dp

from dateparser import parse as dp
from datetime import datetime, timezone, timedelta

import asyncio
from client import MovebankClient


@click.group(help="A group of commands for getting data from a SMART Connect Server")
@click.group(help="A group of commands for getting data from Movebank account")
def cli():
pass

Expand All @@ -32,16 +30,37 @@ def _add_options(func):
@add_options(common_options)
@click.option('--study_id', '-s', help='Study ID', required=True)
@click.option('--list-individuals', '-l', help='List all individuals', is_flag=True, default=False)
def get_study(username: str, password: str, study_id: str, list_individuals: bool):
client = MovebankClient(username=username, password=password)
async def get_study(username: str, password: str, study_id: str, list_individuals: bool):
async with MovebankClient(username=username, password=password) as client:
study = await client.get_study(study_id=study_id)

study = asyncio.run(client.get_study(study_id=study_id))
print("Study:\n")
print(json.dumps(study, indent=2) + "\n")

print("Study:\n")
print(json.dumps(study, indent=2) + "\n")
if list_individuals:
individuals = [individual for individual in await client.get_individuals_by_study(study_id=study_id)]

if list_individuals:
individuals = [individual for individual in asyncio.run(client.get_individuals_by_study(study_id=study_id))]
print("Individuals:\n")

for individual in individuals:
if individual["timestamp_end"]:
timestamp_end = dp(individual["timestamp_end"])
timestamp_end = timestamp_end.replace(tzinfo=timezone.utc)
ended = (datetime.now(tz=timezone.utc) - timedelta(days=7)) > timestamp_end
else:
ended = False
print(
f'{individual["id"]}: {individual["nick_name"]}, {individual["local_identifier"]}, {individual["nick_name"]}, {individual["ring_id"]}, {individual["timestamp_end"]}, {ended}\n'
)


@cli.command(help="Get Events for a Study")
@add_options(common_options)
@click.option('--study_id', '-s', help='Study ID', required=True)
async def get_events_for_study(username, password, study_id):
async with MovebankClient(username=username, password=password) as client:

individuals = [individual for individual in await client.get_individuals_by_study(study_id=study_id)]

print("Individuals:\n")

Expand All @@ -56,63 +75,43 @@ def get_study(username: str, password: str, study_id: str, list_individuals: boo
f'{individual["id"]}: {individual["nick_name"]}, {individual["local_identifier"]}, {individual["nick_name"]}, {individual["ring_id"]}, {individual["timestamp_end"]}, {ended}\n'
)

end = datetime.now(tz=timezone.utc)
start = end - timedelta(days=1)

@cli.command(help="Get Events for a Study")
@add_options(common_options)
@click.option('--study_id', '-s', help='Study ID', required=True)
def get_events_for_study(username, password, study_id):
client = MovebankClient(username=username, password=password)

individuals = [individual for individual in asyncio.run(client.get_individuals_by_study(study_id=study_id))]

print("Individuals:\n")

for individual in individuals:
if individual["timestamp_end"]:
timestamp_end = dp(individual["timestamp_end"])
timestamp_end = timestamp_end.replace(tzinfo=timezone.utc)
ended = (datetime.now(tz=timezone.utc) - timedelta(days=7)) > timestamp_end
else:
ended = False
print(
f'{individual["id"]}: {individual["nick_name"]}, {individual["local_identifier"]}, {individual["nick_name"]}, {individual["ring_id"]}, {individual["timestamp_end"]}, {ended}\n'
)
async def get_events():
for individual in individuals:
print(f'Getting events for individual: {individual["id"]}')
async for item in client.get_individual_events_by_time(study_id=study_id, individual_id=individual["id"],
timestamp_start=start, timestamp_end=end):
print(item)

end = datetime.now(tz=timezone.utc)
start = end - timedelta(days=1)

async def get_events():
for individual in individuals:
print(f'Getting events for individual: {individual["id"]}')
async for item in client.get_individual_events_by_time(study_id=study_id, individual_id=individual["id"],
timestamp_start=start, timestamp_end=end):
print(item)

asyncio.run(get_events())
await get_events()


@cli.command(help="Get Events for an Individual")
@add_options(common_options)
@click.option('--study_id', '-s', help='Study ID', required=True)
@click.option('--individual_id', '-i', help='Individual ID', required=True)
def get_individual_events(username, password, study_id, individual_id):
client = MovebankClient(username=username, password=password)
end = datetime.now(tz=timezone.utc)
start = end - timedelta(days=1)

async def get_events():
items = []
async for item in client.get_individual_events_by_time(study_id=study_id, individual_id=individual_id,
timestamp_start=start, timestamp_end=end):
items.append(item)
return items
async def get_individual_events(username, password, study_id, individual_id):
async with MovebankClient(username=username, password=password) as client:
end = datetime.now(tz=timezone.utc)
start = end - timedelta(days=1)


items = [
item async for item in client.get_individual_events_by_time(
study_id=study_id,
individual_id=individual_id,
timestamp_start=start,
timestamp_end=end
)
]

items = asyncio.run(get_events())

print("Events:\n")
print("Events:\n")

for item in items:
print(json.dumps(item, indent=2) + "\n")
for item in items:
print(json.dumps(item, indent=2) + "\n")


if __name__ == '__main__':
Expand Down
18 changes: 17 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ httpx = "^0.24.0"
respx = "^0.20.1"
backoff = "^2.2.1"
dateparser = "^1.2.0"
asyncclick = "^8.1.7.2"

[tool.poetry.group.dev]
optional = true
Expand Down

0 comments on commit 0ad9def

Please sign in to comment.