Skip to content

Commit

Permalink
feat: enable extensions-path arg
Browse files Browse the repository at this point in the history
  • Loading branch information
eri24816 committed Apr 21, 2024
1 parent c239f16 commit 9cc0c7b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 40 deletions.
4 changes: 1 addition & 3 deletions backend/src/grapycal/extension/extensionManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
class ExtensionManager:
def __init__(self,objectsync_server:objectsync.Server) -> None:
self._objectsync = objectsync_server
cwd = sys.path[0]
self._local_extension_dir = join(cwd,'.grapycal','extensions')
sys.path.append(self._local_extension_dir)
self._extensions: Dict[str, Extension] = {}

# Use this topic to inform the client about the extensions
Expand Down Expand Up @@ -195,6 +192,7 @@ def _scan_available_extensions(self) -> list[dict]:
if pkg.name not in self._extensions:
# find pyproject.toml and get package version
available_extensions.append(get_extension_info(pkg.name))

return available_extensions

async def _check_extension_compatible(self, extension_name: str):
Expand Down
50 changes: 20 additions & 30 deletions entry/args.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
import argparse
from typing import Any
import usersettings
import os


def parse_args():
parser = argparse.ArgumentParser(description="Grapycal backend server")
parser.add_argument(
"path", type=str, help="path to workspace file", nargs="?", default=None
)
parser.add_argument("--backend-path", type=str, help="path to backend code")
parser.add_argument("--frontend-path", type=str, help="path to frontend code")
parser.add_argument("--backend-path", type=str, help="path to backend code", required=True)
parser.add_argument("--frontend-path", type=str, help="path to frontend code", required=True)
parser.add_argument("--extensions-path", type=str, help="path to extensions folder")
parser.add_argument("--port", type=int, help="port to listen on")
parser.add_argument(
"--http-port", type=int, help="http port to listen on (to serve webpage)"
)
parser.add_argument("--host", type=str, help="host to listen on")
parser.add_argument(
"--no-http",
action="store_true",
help="if set, the server does not serve the webpage",
)
parser.add_argument(
"--restart",
action="store_true",
help="if set, the workspace restarts when it exits. Convenient for development",
)
args = parser.parse_args()
s = usersettings.Settings("Grapycal")
s.add_setting("port", int, default=8765) # type: ignore
s.add_setting("http_port", int, default=9001) # type: ignore
s.add_setting("host", str, default="localhost") # type: ignore
s.add_setting("path", str, default=os.path.join("workspace.grapycal")) # type: ignore

sync_args_with_usersettings(args, {
"path": "workspace.grapycal",
"port": 7943,
"host": "localhost"
})

return args

def sync_args_with_usersettings(args: argparse.Namespace, defaults: dict[str, Any]):
s = usersettings.Settings("Grapycal")
s.load_settings()
for name in ["port", "host", "path", "http_port"]:
if getattr(args, name):

for name, default in defaults.items():
if getattr(args, name) is not None:
s[name] = getattr(args, name)
else:
setattr(args, name, s.get(name, default))
s.save_settings()

for name in ["backend_path", "frontend_path", "no_http", "restart"]:
if getattr(args, name):
s[name] = getattr(args, name)

return s
return args
13 changes: 7 additions & 6 deletions entry/run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from contextlib import asynccontextmanager
import threading
from typing import Awaitable, Callable
from fastapi.staticfiles import StaticFiles
from grapycal.core import workspace
from args import parse_args
from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse
Expand Down Expand Up @@ -72,14 +70,17 @@ def main():
print("Exiting")
sys.exit(1)

# make extensions available
if args.extensions_path is not None:
sys.path.append(args.extensions_path)

# before importing workspace, we need to add the backend path to sys.path
sys.path.append(args.backend_path)
from grapycal.core.workspace import Workspace

workspace = Workspace(args.path, "")

app = make_app(workspace, args.get("frontend_path", None))
thread = threading.Thread(target=run_uvicorn, args=(app, args.host, args.port),daemon=True)
thread.start()
app = make_app(workspace, args.frontend_path)
threading.Thread(target=run_uvicorn, args=(app, args.host, args.port),daemon=True).start()

try:
workspace.run()
Expand Down
2 changes: 1 addition & 1 deletion scripts/dev.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python scripts/build_frontend.py &&\

python entry/run.py --backend-path "backend/src" --frontend-path "frontend/dist" --port 7943
python entry/run.py --backend-path "backend/src" --frontend-path "frontend/dist" --extensions-path "extensions" --port 7943

0 comments on commit 9cc0c7b

Please sign in to comment.