Skip to content

Commit

Permalink
Changed to default line length (88) for both black and ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcampbell committed Nov 22, 2023
1 parent 5b016d3 commit 7363990
Show file tree
Hide file tree
Showing 34 changed files with 194 additions and 172 deletions.
12 changes: 8 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand All @@ -26,14 +27,17 @@ dependencies = [
"typer",
]

[tool.hatch.version]
source = "vcs"

[project.scripts]
nsls2api = "nsls2api.cli.cli:app"

[tool.hatch]
version.source = "vcs"
build.hooks.vcs.version-file = "src/nsls2api/_version.py"

[tool.black]
line-length = 80
target_version = ['py311']
include = '\.pyi?$'

[tool.ruff]
target-version = "py311"

2 changes: 1 addition & 1 deletion src/nsls2api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__app_name__ = "nsls2api"
__app_version__ = "0.0.1"
__app_version__ = "0.0.1"
1 change: 1 addition & 0 deletions src/nsls2api/api/models/person_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Person(pydantic.BaseModel):
facility_name: Optional[str] = None
citizenship: Optional[str] = None


class PersonSummary(pydantic.BaseModel):
firstname: str
lastname: str
Expand Down
1 change: 0 additions & 1 deletion src/nsls2api/api/models/proposal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ class RecentProposalsModel(pydantic.BaseModel):
class ProposalSummary(pydantic.BaseModel):
proposal_id: str
title: str

4 changes: 1 addition & 3 deletions src/nsls2api/api/v1/admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@


@router.get("/admin/settings") # , include_in_schema=False)
async def info(
settings: Annotated[config.Settings, Depends(config.get_settings)]
):
async def info(settings: Annotated[config.Settings, Depends(config.get_settings)]):
return settings


Expand Down
53 changes: 37 additions & 16 deletions src/nsls2api/api/v1/beamline_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,86 @@
router = fastapi.APIRouter()


@router.get('/beamline/{name}', response_model=Beamline)
@router.get("/beamline/{name}", response_model=Beamline)
async def details(name: str):
beamline = await beamline_service.beamline_by_name(name)
if beamline is None:
raise HTTPException(status_code=451, detail=f'Beamline named {name} could not be found')
raise HTTPException(
status_code=451, detail=f"Beamline named {name} could not be found"
)
return beamline


@router.get('/beamline/{name}/services', response_model=list[BeamlineService])
@router.get("/beamline/{name}/services", response_model=list[BeamlineService])
async def get_beamline_services(name: str):
beamline_services = await beamline_service.all_services(name)
if beamline_services is None:
raise HTTPException(status_code=404, detail=f'Beamline named {name} could not be found')
raise HTTPException(
status_code=404, detail=f"Beamline named {name} could not be found"
)
return beamline_services


@router.get('/beamline/{name}/service-accounts')
@router.get("/beamline/{name}/service-accounts")
async def get_beamline_accounts(name: str, api_key: APIKey = Depends(get_api_key)):
service_accounts = await beamline_service.service_accounts(name)
if service_accounts is None:
raise HTTPException(status_code=404, detail=f"Beamline named {name} could not be found")
raise HTTPException(
status_code=404, detail=f"Beamline named {name} could not be found"
)
return service_accounts


@router.get('/beamline/{name}/accounts/workflow', response_model=str)
@router.get("/beamline/{name}/accounts/workflow", response_model=str)
async def get_beamline_workflow_username(name: str):
workflow_user = await beamline_service.workflow_username(name)
if workflow_user is None:
raise HTTPException(status_code=404, detail=f'No workflow user has been defined for the {name} beamline')
raise HTTPException(
status_code=404,
detail=f"No workflow user has been defined for the {name} beamline",
)
return workflow_user


@router.get('/beamline/{name}/accounts/ioc', response_model=str)
@router.get("/beamline/{name}/accounts/ioc", response_model=str)
async def get_beamline_ioc_username(name: str):
ioc_user = await beamline_service.ioc_username(name)
if ioc_user is None:
raise HTTPException(status_code=404, detail=f'No IOC user has been defined for the {name} beamline')
raise HTTPException(
status_code=404,
detail=f"No IOC user has been defined for the {name} beamline",
)
return ioc_user


@router.get('/beamline/{name}/accounts/bluesky', response_model=str)
@router.get("/beamline/{name}/accounts/bluesky", response_model=str)
async def get_beamline_bluesky_username(name: str):
bluesky_user = await beamline_service.bluesky_username(name)
if bluesky_user is None:
raise HTTPException(status_code=404, detail=f'No bluesky user has been defined for the {name} beamline')
raise HTTPException(
status_code=404,
detail=f"No bluesky user has been defined for the {name} beamline",
)
return bluesky_user


@router.get('/beamline/{name}/accounts/epics-services', response_model=str)
@router.get("/beamline/{name}/accounts/epics-services", response_model=str)
async def get_beamline_epics_services_username(name: str):
epics_user = await beamline_service.epics_services_username(name)
if epics_user is None:
raise HTTPException(status_code=404, detail=f'No EPICS services user has been defined for the {name} beamline')
raise HTTPException(
status_code=404,
detail=f"No EPICS services user has been defined for the {name} beamline",
)
return epics_user


@router.get('/beamline/{name}/accounts/operator', response_model=str)
@router.get("/beamline/{name}/accounts/operator", response_model=str)
async def get_beamline_operator_username(name: str):
operator_user = await beamline_service.operator_username(name)
if operator_user is None:
raise HTTPException(status_code=404, detail=f'No operator user has been defined for the {name} beamline')
raise HTTPException(
status_code=404,
detail=f"No operator user has been defined for the {name} beamline",
)
return operator_user
2 changes: 1 addition & 1 deletion src/nsls2api/api/v1/facility_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
router = fastapi.APIRouter()


@router.get('/facility/{facility}/cycles/current', response_model=str)
@router.get("/facility/{facility}/cycles/current", response_model=str)
async def get_current_facilty_cycle(facility: FacilityName):
return "2023-3"
8 changes: 6 additions & 2 deletions src/nsls2api/api/v1/proposal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ async def get_proposals_for_cycle(cycle: str):
proposal_list = await proposal_service.fetch_proposals_for_cycle(cycle)
if proposal_list is None:
return fastapi.responses.JSONResponse(
{"error": f"No proposals were found for cycle {cycle}"}, status_code=404
{"error": f"No proposals were found for cycle {cycle}"},
status_code=404,
)

data = {"cycle": cycle, "proposals": proposal_list}
return data


@router.get("/proposals/")
async def get_proposals():
proposal_list = await proposal_service.fetch_proposals()
return proposal_list


@router.get("/proposal/{proposal_id}", response_model=Proposal)
async def get_proposal(proposal_id: int):
proposal = await proposal_service.proposal_by_id(proposal_id)
Expand Down Expand Up @@ -98,7 +101,8 @@ async def get_proposal_principle_invesigator(proposal_id: int):

if len(principle_invesigator) == 0:
return fastapi.responses.JSONResponse(
{"error": f"PI not found for proposal {proposal_id}"}, status_code=404
{"error": f"PI not found for proposal {proposal_id}"},
status_code=404,
)
elif len(principle_invesigator) > 1:
return fastapi.responses.JSONResponse(
Expand Down
16 changes: 12 additions & 4 deletions src/nsls2api/api/v1/stats_api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import fastapi

from nsls2api.api.models.stats_model import StatsModel
from nsls2api.services import proposal_service, facility_service, beamline_service
from nsls2api.services import (
proposal_service,
facility_service,
beamline_service,
)

router = fastapi.APIRouter()


@router.get('/stats', response_model=StatsModel)
@router.get("/stats", response_model=StatsModel)
async def stats():
proposals = await proposal_service.proposal_count()
beamlines = await beamline_service.beamline_count()
facilities = await facility_service.facilities_count()
commissioning = len(await proposal_service.commissioning_proposals())

model = StatsModel(facility_count=facilities, beamline_count=beamlines, proposal_count=proposals,
commissioning_proposal_count=commissioning)
model = StatsModel(
facility_count=facilities,
beamline_count=beamlines,
proposal_count=proposals,
commissioning_proposal_count=commissioning,
)
return model
12 changes: 3 additions & 9 deletions src/nsls2api/api/v1/user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,17 @@ async def get_person_from_email(email: str):

@router.get("/person/department/{department}")
async def get_person_by_department(department_code: str = "PS"):
bnl_people = await bnlpeople_service.get_people_by_department(
department_code
)
bnl_people = await bnlpeople_service.get_people_by_department(department_code)
if bnl_people:
return bnl_people


@router.get("/person/me", response_model=str)
async def get_myself(
current_user: Annotated[Person, Depends(get_current_user)]
):
async def get_myself(current_user: Annotated[Person, Depends(get_current_user)]):
return current_user


@router.get(
"/data-session/{username}", response_model=DataSessionAccess, tags=["data"]
)
@router.get("/data-session/{username}", response_model=DataSessionAccess, tags=["data"])
@router.get(
"/data_session/{username}",
response_model=DataSessionAccess,
Expand Down
2 changes: 1 addition & 1 deletion src/nsls2api/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__logged_in_username = None
__logged_in_username = None
2 changes: 0 additions & 2 deletions src/nsls2api/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def login():
print("Logged in as anonymous")
return


# Let's test this token
url = f"{BASE_URL}/v1/person/me"
try:
Expand All @@ -64,7 +63,6 @@ def login():
print("Logging in...")



@app.command()
def logout():
print("Logging you out...")
Expand Down
2 changes: 1 addition & 1 deletion src/nsls2api/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ def main(
help="Show the application's version and exit.",
callback=_version_callback,
is_eager=True,
)
),
) -> None:
return
6 changes: 3 additions & 3 deletions src/nsls2api/cli/proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def view(proposal: int):
def search(proposal: int):
print(f"Searching for Proposal: {proposal}")
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
progress.add_task(description="Searching...", total=None)
time.sleep(3)
Expand Down
5 changes: 4 additions & 1 deletion src/nsls2api/infrastructure/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Settings(BaseSettings):
settings.n2sn_group_search = "group@example.com"
settings.bnlroot_ca_certs_file = "/path/to/ca_certs.pem"
"""

pass_api_key: str
pass_api_url: HttpUrl = "https://passservices.bnl.gov/passapi"
active_directory_server: str
Expand All @@ -51,7 +52,9 @@ class Settings(BaseSettings):
use_socks_proxy: bool = False
socks_proxy: str

model_config = SettingsConfigDict(env_file=str(Path(__file__).parent.parent / ".env"))
model_config = SettingsConfigDict(
env_file=str(Path(__file__).parent.parent / ".env")
)


@lru_cache()
Expand Down
10 changes: 7 additions & 3 deletions src/nsls2api/infrastructure/mongodb_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@


async def init_connection(mongodb_dsn: MongoDsn):

pprint(f"Attempting to connect to {str(mongodb_dsn)}")

client = motor.motor_asyncio.AsyncIOMotorClient(mongodb_dsn, uuidRepresentation="standard")
await beanie.init_beanie(database=client.get_default_database(), document_models=models.all_models)
client = motor.motor_asyncio.AsyncIOMotorClient(
mongodb_dsn, uuidRepresentation="standard"
)
await beanie.init_beanie(
database=client.get_default_database(),
document_models=models.all_models,
)

pprint(f"Connected to {client.get_default_database().name} on localhost.")
Loading

0 comments on commit 7363990

Please sign in to comment.