-
Notifications
You must be signed in to change notification settings - Fork 190
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
Schema: rewrite to run local only and handle newer schema versions #3117
Open
mashehu
wants to merge
12
commits into
nf-core:dev
Choose a base branch
from
mashehu:schema-rewrite
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3e212cd
update urls for new builder API
mashehu 1746a22
Merge branch 'dev' of github.com:nf-core/tools into schema-rewrite
mashehu 4f61896
switch to local server
mashehu fecc9ee
handle post request
mashehu 0c84ca8
switch to localhost for schema builder and add web-gui
mashehu b05515a
update web-gui
mashehu 6718f16
write and read directly to the json file
mashehu ee741b7
remove fallback option on quit
mashehu 0fbdfa6
update web-gui
mashehu dbc697a
fix formatting in template nextflow_schema
mashehu 5c2d6e4
fix typo
mashehu d21bbb8
move web-gui inside nf_core dir to bundle it
mashehu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ nf_core/pipeline-template/tower.yml | |
# don't run on things handled by ruff | ||
*.py | ||
*.pyc | ||
|
||
web-gui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import json | ||
import logging | ||
import threading | ||
import urllib.parse as urlparse | ||
from http import HTTPStatus | ||
from http.server import HTTPServer, SimpleHTTPRequestHandler | ||
from pathlib import Path | ||
from typing import Dict | ||
from urllib.parse import parse_qsl | ||
|
||
import nf_core | ||
|
||
log: logging.Logger = logging.getLogger(__name__) | ||
|
||
|
||
def parse_qsld(query: str) -> Dict: | ||
return dict(parse_qsl(query)) | ||
|
||
|
||
class MyHandler(SimpleHTTPRequestHandler): | ||
status = "waiting_for_user" # Default status | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, directory=str(Path(nf_core.__file__).parent / "web-gui"), **kwargs) | ||
|
||
def send_cors_headers(self): | ||
self.send_header("Access-Control-Allow-Origin", "http://localhost:4321") | ||
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") | ||
self.send_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, message") | ||
|
||
def do_OPTIONS(self): # noqa: N802 | ||
self.send_response(HTTPStatus.NO_CONTENT) | ||
self.send_cors_headers() | ||
self.end_headers() | ||
|
||
def _send_response(self, status_code: int, body: Dict) -> None: | ||
self.send_response(status_code) | ||
self.send_header("Content-type", "application/json") | ||
self.send_cors_headers() | ||
self.end_headers() | ||
self.wfile.write(json.dumps(body).encode()) | ||
|
||
def do_POST(self) -> None: # noqa: N802 | ||
log.debug("POST request received") | ||
content_type = self.headers.get("Content-Type") | ||
content_length = int(str(self.headers.get("Content-Length"))) | ||
post_data = self.rfile.read(content_length) | ||
if urlparse.urlparse(self.path).path == "/process-schema": | ||
if content_type == "application/json": | ||
data = json.loads(post_data.decode()) | ||
schema_path = data.get("schema_path", None) | ||
# write data to local schema_file | ||
open(schema_path, "w").write(json.dumps(data["schema"], indent=4)) | ||
|
||
else: | ||
data = parse_qsld(post_data.decode()) | ||
|
||
data["schema"] = json.loads(data.get("schema", None)) | ||
schema_path = data.get("schema_path", None) | ||
# write data to local schema_file | ||
open(schema_path, "w").write(json.dumps(data["schema"], indent=4)) | ||
status = data.get("status", "received") | ||
MyHandler.status = status | ||
if status == "waiting_for_user": | ||
status = "received" | ||
|
||
self._send_response( | ||
200, | ||
{ | ||
"message": "Data stored successfully", | ||
"status": status, | ||
"schema_path": schema_path, | ||
"web_url": "http://localhost:8000/schema_builder.html?schema_path=" | ||
+ urlparse.quote(schema_path, safe=""), | ||
"api_url": "http://localhost:8000/process-schema?schema_path=" | ||
+ urlparse.quote(schema_path, safe=""), | ||
}, | ||
) | ||
else: | ||
self._send_response(404, {"error": "Not Found"}) | ||
|
||
def do_GET(self) -> None: # noqa: N802 | ||
parsed = urlparse.urlparse(self.path) | ||
if parsed.path == "/process-schema": | ||
schema_path: str | None = parse_qsld(parsed.query).get("schema_path", None) | ||
if schema_path is None: | ||
self._send_response(422, {"error": "schema_path parameter not found"}) | ||
|
||
else: | ||
with open(schema_path) as file: | ||
data = json.load(file) | ||
if data is None: | ||
self._send_response(404, {"error": "Not Found"}) | ||
else: | ||
self._send_response( | ||
200, {"message": "GET request received", "status": MyHandler.status, "data": data} | ||
) | ||
else: | ||
super().do_GET() | ||
|
||
def log_message(self, format, *args): | ||
log.debug(format % args) | ||
|
||
|
||
def run( | ||
server_class=HTTPServer, | ||
handler_class=MyHandler, | ||
): | ||
global server_instance | ||
|
||
server_address = ("localhost", 8000) | ||
log.info(f"Starting server on http://{server_address[0]}:{server_address[1]}") | ||
server_instance = server_class(server_address, handler_class) | ||
|
||
try: | ||
server_instance.serve_forever() | ||
except KeyboardInterrupt: | ||
pass | ||
finally: | ||
log.info("Server loop stopped") | ||
|
||
|
||
def start_server(): | ||
server_thread = threading.Thread(target=run, daemon=True) | ||
server_thread.start() | ||
return server_thread | ||
|
||
|
||
def stop_server(): | ||
global server_instance | ||
|
||
if server_instance: | ||
log.info("Stopping server...") | ||
server_instance.shutdown() | ||
server_instance.server_close() | ||
server_instance = None | ||
log.info("Server stopped") | ||
else: | ||
log.warning("No server instance to stop") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is always thrown? I just tested with a fresh pipeline and I see this logging: