Skip to content
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

improve ux when trying to open sqlite without protocol #800

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion optuna_dashboard/_storage_url.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os.path
from pathlib import Path
import re
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -58,12 +59,25 @@
return guess_storage_from_url(storage)


def _has_sqlite_header(storage_url: str) -> bool:
storage_path = Path(storage_url)
SQLITE_HEADER = (
b"SQLite format 3\x00" # see https://github.com/optuna/optuna-dashboard/pull/800
)
with storage_path.open(mode="rb") as f:
header = f.read(len(SQLITE_HEADER))
return header == SQLITE_HEADER


def guess_storage_from_url(storage_url: str) -> BaseStorage:
if storage_url.startswith("redis"):
return get_journal_redis_storage(storage_url)

if os.path.isfile(storage_url):
return get_journal_file_storage(storage_url)
if _has_sqlite_header(storage_url):
return get_rdb_storage("sqlite:///" + storage_url)

Check warning on line 78 in optuna_dashboard/_storage_url.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_storage_url.py#L78

Added line #L78 was not covered by tests
else:
return get_journal_file_storage(storage_url)

if rfc1738_pattern.match(storage_url) is not None:
return get_rdb_storage(storage_url)
Expand Down
Loading