Skip to content

Commit

Permalink
Fixing args processing
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckierDodge committed May 9, 2024
1 parent 7907134 commit 13ff5c9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 150 deletions.
37 changes: 1 addition & 36 deletions pdm.lock

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

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies = [
'requests>=2.31.0',
'ulid-py>=1.1.0',
'uvicorn[standard]>=0.21',
"h2o-lightwave[web]>=1.1.2",
"websockets>=12.0",
]

Expand Down
5 changes: 5 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ executing==2.0.1
fastapi==0.110.2
filelock==3.14.0
h11==0.14.0
httptools==0.6.1
identify==2.5.36
idna==3.7
importlib-metadata==7.1.0; python_full_version < "3.10.2"
Expand All @@ -39,6 +40,7 @@ pydantic-core==2.18.2
pygments==2.17.2
pyproject-hooks==1.1.0
pytest==8.2.0
python-dotenv==1.0.1
python-multipart==0.0.9
pyyaml==6.0.1
redis==4.6.0
Expand Down Expand Up @@ -66,5 +68,8 @@ typing-extensions==4.11.0
ulid-py==1.1.0
urllib3==1.26.18
uvicorn==0.29.0
uvloop==0.19.0; (sys_platform != "cygwin" and sys_platform != "win32") and platform_python_implementation != "PyPy"
virtualenv==20.26.1
watchfiles==0.21.0
websockets==12.0
zipp==3.18.1; python_full_version < "3.10.2"
4 changes: 4 additions & 0 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exceptiongroup==1.2.1; python_version < "3.11"
executing==2.0.1
fastapi==0.110.2
h11==0.14.0
httptools==0.6.1
idna==3.7
imagesize==1.4.1
importlib-metadata==7.1.0; python_full_version < "3.10.2"
Expand Down Expand Up @@ -53,4 +54,7 @@ typing-extensions==4.11.0
ulid-py==1.1.0
urllib3==1.26.18
uvicorn==0.29.0
uvloop==0.19.0; (sys_platform != "cygwin" and sys_platform != "win32") and platform_python_implementation != "PyPy"
watchfiles==0.21.0
websockets==12.0
zipp==3.18.1; python_full_version < "3.10.2"
4 changes: 4 additions & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fastapi==0.110.2
filelock==3.14.0
globus-sdk==3.41.0
h11==0.14.0
httptools==0.6.1
identify==2.5.36
idna==3.7
imagesize==1.4.1
Expand Down Expand Up @@ -98,5 +99,8 @@ typing-extensions==4.11.0
ulid-py==1.1.0
urllib3==1.26.18
uvicorn==0.29.0
uvloop==0.19.0; (sys_platform != "cygwin" and sys_platform != "win32") and platform_python_implementation != "PyPy"
virtualenv==20.26.1
watchfiles==0.21.0
websockets==12.0
zipp==3.18.1; python_full_version < "3.10.2"
137 changes: 24 additions & 113 deletions wei/modules/rest_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@
from threading import Thread
from typing import Any, List, Optional, Union

from fastapi import (
APIRouter,
FastAPI,
Request,
Response,
UploadFile,
WebSocket,
WebSocketDisconnect,
status,
)
from fastapi import APIRouter, FastAPI, Request, Response, UploadFile, status
from fastapi.datastructures import State
from fastapi.staticfiles import StaticFiles
from h2o_lightwave import Q, ui, wave_serve
from h2o_lightwave_web import web_directory

from wei.types import ModuleStatus
from wei.types.module_types import (
Expand Down Expand Up @@ -191,30 +179,37 @@ def action_handler(
arg_dict["state"] = state
if parameters.__contains__("action"):
arg_dict["action"] = action
for arg_name, arg_value in action.args.items():
if (
parameters.__contains__(arg_name)
or list(parameters.values())[-1].kind
== inspect.Parameter.VAR_KEYWORD
):
arg_dict[arg_name] = arg_value
for file in action.files:
if (
parameters.__contains__(file.filename)
or list(parameters.values())[-1].kind
== inspect.Parameter.VAR_KEYWORD
):
arg_dict[file.filename] = file.file
if (
list(parameters.values())[-1].kind
== inspect.Parameter.VAR_KEYWORD
):
# * Function has **kwargs, so we can pass all action args and files
arg_dict = {**arg_dict, **action.args}
arg_dict = {
**arg_dict,
**{file.filename: file.file for file in action.files},
}
else:
for arg_name, arg_value in action.args.items():
print(arg_name, arg_value)
print(parameters.keys())
if arg_name in parameters.keys():
arg_dict[arg_name] = arg_value
for file in action.files:
if file.filename in parameters.keys():
arg_dict[file.filename] = file.file

for arg in module_action.args:
if arg.name not in arg_dict:
if arg.name not in action.args:
print(arg)
if arg.required:
return StepResponse.step_failed(
action_log=f"Missing required argument '{arg.name}'"
)
for file in module_action.files:
if file.name not in arg_dict:
if not any(
arg_file.filename == file.name for arg_file in action.files
):
if file.required:
return StepResponse.step_failed(
action_log=f"Missing required file '{file.name}'"
Expand Down Expand Up @@ -373,89 +368,6 @@ async def about(request: Request, response: Response) -> ModuleAbout:
traceback.print_exc()
return {"error": "Unable to generate module about"}

async def _serve_ui(q: Q):
# Paint our UI on the first page visit.
if not q.client.initialized:
# Create a local state.
q.client.count = 0
q.page["state"] = ui.form_card(
box="1 1 2 2",
title="State",
items=[
ui.text_xl("Status"),
ui.text_l(self.state.status),
],
)
q.page["about"] = ui.form_card(
box="1 3 2 8",
title="About",
items=[
ui.text_xl(self.name),
ui.text_l("Description"),
ui.text(self.description),
ui.text_l("Version"),
ui.text(self.version),
ui.text_l("Model"),
ui.text(self.model),
ui.text_l("Interface"),
ui.text(self.interface),
],
)
action_components = []
for action in self.actions:
action_components.extend(
[
ui.text_l(action.name),
ui.text(action.description),
]
)
action_components.extend(
[
ui.button(
name=action.name,
label=action.name,
primary=True,
icon="PLAY",
path="/action",
),
]
)
q.page["actions"] = ui.form_card(
box="3 1 2 10",
title="Actions",
items=action_components,
)
q.page["resources"] = ui.form_card(
box="5 1 2 10",
title="Resources",
items=[
# ui.text_xl("Resource"),
# ui.text_l(self.state.status),
],
)
q.page["admin"] = ui.form_card(
box="7 1 2 10",
title="Admin",
items=[
# ui.text_xl("Resource"),
# ui.text_l(self.state.status),
],
)

q.client.initialized = True

# Send the UI changes to the browser.
await q.page.save()

@self.router.websocket("/_s/")
async def ws(ws: WebSocket):
try:
await ws.accept()
await wave_serve(_serve_ui, ws.send_text, ws.receive_text)
await ws.close()
except WebSocketDisconnect:
print("Client disconnected")

@self.router.post("/action")
def action(
request: Request,
Expand Down Expand Up @@ -525,7 +437,6 @@ def start(self):
): # * Don't override already set attributes with None's
self.state.__setattr__(arg_name, getattr(args, arg_name))
self._configure_routes()
self.app.mount("/", StaticFiles(directory=web_directory, html=True), name="/")

# * Enforce a name
if not self.state.name:
Expand Down

0 comments on commit 13ff5c9

Please sign in to comment.