API: rename {open_file, open_clan_folder} into {get_system_file, get_clan_folder}
This commit is contained in:
committed by
a-kenji
parent
1403f47b0d
commit
5bf1f06244
@@ -24,7 +24,7 @@ def remove_none(_list: list) -> list:
|
|||||||
RESULT: dict[str, SuccessDataClass[list[str] | None] | ErrorDataClass] = {}
|
RESULT: dict[str, SuccessDataClass[list[str] | None] | ErrorDataClass] = {}
|
||||||
|
|
||||||
|
|
||||||
def open_clan_folder(*, op_key: str) -> SuccessDataClass[Flake] | ErrorDataClass:
|
def get_clan_folder(*, op_key: str) -> SuccessDataClass[Flake] | ErrorDataClass:
|
||||||
"""
|
"""
|
||||||
Opens the clan folder using the GTK file dialog.
|
Opens the clan folder using the GTK file dialog.
|
||||||
Returns the path to the clan folder or an error if it fails.
|
Returns the path to the clan folder or an error if it fails.
|
||||||
@@ -34,7 +34,7 @@ def open_clan_folder(*, op_key: str) -> SuccessDataClass[Flake] | ErrorDataClass
|
|||||||
title="Select Clan Folder",
|
title="Select Clan Folder",
|
||||||
initial_folder=str(Path.home()),
|
initial_folder=str(Path.home()),
|
||||||
)
|
)
|
||||||
response = open_file(file_request, op_key=op_key)
|
response = get_system_file(file_request, op_key=op_key)
|
||||||
|
|
||||||
if isinstance(response, ErrorDataClass):
|
if isinstance(response, ErrorDataClass):
|
||||||
return response
|
return response
|
||||||
@@ -47,7 +47,7 @@ def open_clan_folder(*, op_key: str) -> SuccessDataClass[Flake] | ErrorDataClass
|
|||||||
ApiError(
|
ApiError(
|
||||||
message="No folder selected",
|
message="No folder selected",
|
||||||
description="You must select a folder to open.",
|
description="You must select a folder to open.",
|
||||||
location=["open_clan_folder"],
|
location=["get_clan_folder"],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -61,7 +61,7 @@ def open_clan_folder(*, op_key: str) -> SuccessDataClass[Flake] | ErrorDataClass
|
|||||||
ApiError(
|
ApiError(
|
||||||
message="Invalid clan folder",
|
message="Invalid clan folder",
|
||||||
description=f"The selected folder '{clan_folder}' is not a valid clan folder.",
|
description=f"The selected folder '{clan_folder}' is not a valid clan folder.",
|
||||||
location=["open_clan_folder"],
|
location=["get_clan_folder"],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -69,7 +69,7 @@ def open_clan_folder(*, op_key: str) -> SuccessDataClass[Flake] | ErrorDataClass
|
|||||||
return SuccessDataClass(op_key=op_key, data=clan_folder, status="success")
|
return SuccessDataClass(op_key=op_key, data=clan_folder, status="success")
|
||||||
|
|
||||||
|
|
||||||
def open_file(
|
def get_system_file(
|
||||||
file_request: FileRequest, *, op_key: str
|
file_request: FileRequest, *, op_key: str
|
||||||
) -> SuccessDataClass[list[str] | None] | ErrorDataClass:
|
) -> SuccessDataClass[list[str] | None] | ErrorDataClass:
|
||||||
GLib.idle_add(gtk_open_file, file_request, op_key)
|
GLib.idle_add(gtk_open_file, file_request, op_key)
|
||||||
@@ -106,7 +106,7 @@ def gtk_open_file(file_request: FileRequest, op_key: str) -> bool:
|
|||||||
ApiError(
|
ApiError(
|
||||||
message=e.__class__.__name__,
|
message=e.__class__.__name__,
|
||||||
description=str(e),
|
description=str(e),
|
||||||
location=["open_file"],
|
location=["get_system_file"],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -134,7 +134,7 @@ def gtk_open_file(file_request: FileRequest, op_key: str) -> bool:
|
|||||||
ApiError(
|
ApiError(
|
||||||
message=e.__class__.__name__,
|
message=e.__class__.__name__,
|
||||||
description=str(e),
|
description=str(e),
|
||||||
location=["open_file"],
|
location=["get_system_file"],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -162,7 +162,7 @@ def gtk_open_file(file_request: FileRequest, op_key: str) -> bool:
|
|||||||
ApiError(
|
ApiError(
|
||||||
message=e.__class__.__name__,
|
message=e.__class__.__name__,
|
||||||
description=str(e),
|
description=str(e),
|
||||||
location=["open_file"],
|
location=["get_system_file"],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -190,7 +190,7 @@ def gtk_open_file(file_request: FileRequest, op_key: str) -> bool:
|
|||||||
ApiError(
|
ApiError(
|
||||||
message=e.__class__.__name__,
|
message=e.__class__.__name__,
|
||||||
description=str(e),
|
description=str(e),
|
||||||
location=["open_file"],
|
location=["get_system_file"],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@@ -239,7 +239,7 @@ def gtk_open_file(file_request: FileRequest, op_key: str) -> bool:
|
|||||||
dialog.select_folder(callback=on_folder_select)
|
dialog.select_folder(callback=on_folder_select)
|
||||||
if file_request.mode == "open_multiple_files":
|
if file_request.mode == "open_multiple_files":
|
||||||
dialog.open_multiple(callback=on_file_select_multiple)
|
dialog.open_multiple(callback=on_file_select_multiple)
|
||||||
elif file_request.mode == "open_file":
|
elif file_request.mode == "get_system_file":
|
||||||
dialog.open(callback=on_file_select)
|
dialog.open(callback=on_file_select)
|
||||||
elif file_request.mode == "save":
|
elif file_request.mode == "save":
|
||||||
dialog.save(callback=on_save_finish)
|
dialog.save(callback=on_save_finish)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from clan_lib.dirs import user_data_dir
|
|||||||
from clan_lib.log_manager import LogGroupConfig, LogManager
|
from clan_lib.log_manager import LogGroupConfig, LogManager
|
||||||
from clan_lib.log_manager import api as log_manager_api
|
from clan_lib.log_manager import api as log_manager_api
|
||||||
|
|
||||||
from clan_app.api.file_gtk import open_clan_folder, open_file
|
from clan_app.api.file_gtk import get_clan_folder, get_system_file
|
||||||
from clan_app.api.middleware import (
|
from clan_app.api.middleware import (
|
||||||
ArgumentParsingMiddleware,
|
ArgumentParsingMiddleware,
|
||||||
LoggingMiddleware,
|
LoggingMiddleware,
|
||||||
@@ -113,8 +113,8 @@ def app_run(app_opts: ClanAppOptions) -> int:
|
|||||||
shared_threads=shared_threads,
|
shared_threads=shared_threads,
|
||||||
)
|
)
|
||||||
|
|
||||||
API.overwrite_fn(open_file)
|
API.overwrite_fn(get_system_file)
|
||||||
API.overwrite_fn(open_clan_folder)
|
API.overwrite_fn(get_clan_folder)
|
||||||
|
|
||||||
# Add middleware to the webview
|
# Add middleware to the webview
|
||||||
webview.add_middleware(ArgumentParsingMiddleware(api=API))
|
webview.add_middleware(ArgumentParsingMiddleware(api=API))
|
||||||
|
|||||||
@@ -103,16 +103,16 @@ class MethodRegistry:
|
|||||||
---
|
---
|
||||||
# Example
|
# Example
|
||||||
|
|
||||||
The function 'open_file()' depends on the platform.
|
The function 'get_system_file()' depends on the platform.
|
||||||
|
|
||||||
def open_file(file_request: FileRequest) -> str | None:
|
def get_system_file(file_request: FileRequest) -> str | None:
|
||||||
# In GTK we open a file dialog window
|
# In GTK we open a file dialog window
|
||||||
# In Android we open a file picker dialog
|
# In Android we open a file picker dialog
|
||||||
# and so on.
|
# and so on.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# At runtime the clan-app must override platform specific functions
|
# At runtime the clan-app must override platform specific functions
|
||||||
API.register(open_file)
|
API.register(get_system_file)
|
||||||
---
|
---
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError(msg)
|
raise NotImplementedError(msg)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class FileFilter:
|
|||||||
@dataclass
|
@dataclass
|
||||||
class FileRequest:
|
class FileRequest:
|
||||||
# Mode of the os dialog window
|
# Mode of the os dialog window
|
||||||
mode: Literal["open_file", "select_folder", "save", "open_multiple_files"]
|
mode: Literal["get_system_file", "select_folder", "save", "open_multiple_files"]
|
||||||
# Title of the os dialog window
|
# Title of the os dialog window
|
||||||
title: str | None = field(default=None)
|
title: str | None = field(default=None)
|
||||||
# Pre-applied filters for the file dialog
|
# Pre-applied filters for the file dialog
|
||||||
@@ -30,25 +30,25 @@ class FileRequest:
|
|||||||
|
|
||||||
|
|
||||||
@API.register_abstract
|
@API.register_abstract
|
||||||
def open_file(file_request: FileRequest) -> list[str] | None:
|
def get_system_file(file_request: FileRequest) -> list[str] | None:
|
||||||
"""
|
"""
|
||||||
Api method to open a file dialog window.
|
Api method to open a file dialog window.
|
||||||
|
|
||||||
Implementations is specific to the platform and
|
Implementations is specific to the platform and
|
||||||
returns the name of the selected file or None if no file was selected.
|
returns the name of the selected file or None if no file was selected.
|
||||||
"""
|
"""
|
||||||
msg = "open_file() is not implemented"
|
msg = "get_system_file() is not implemented"
|
||||||
raise NotImplementedError(msg)
|
raise NotImplementedError(msg)
|
||||||
|
|
||||||
|
|
||||||
@API.register_abstract
|
@API.register_abstract
|
||||||
def open_clan_folder() -> Flake:
|
def get_clan_folder() -> Flake:
|
||||||
"""
|
"""
|
||||||
Api method to open the clan folder.
|
Api method to open the clan folder.
|
||||||
|
|
||||||
Implementations is specific to the platform and returns the path to the clan folder.
|
Implementations is specific to the platform and returns the path to the clan folder.
|
||||||
"""
|
"""
|
||||||
msg = "open_clan_folder() is not implemented"
|
msg = "get_clan_folder() is not implemented"
|
||||||
raise NotImplementedError(msg)
|
raise NotImplementedError(msg)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -324,10 +324,10 @@ def test_private_public_fields() -> None:
|
|||||||
def test_literal_field() -> None:
|
def test_literal_field() -> None:
|
||||||
@dataclass
|
@dataclass
|
||||||
class Person:
|
class Person:
|
||||||
name: Literal["open_file", "select_folder", "save"]
|
name: Literal["get_system_file", "select_folder", "save"]
|
||||||
|
|
||||||
data = {"name": "open_file"}
|
data = {"name": "get_system_file"}
|
||||||
expected = Person(name="open_file")
|
expected = Person(name="get_system_file")
|
||||||
assert from_dict(Person, data) == expected
|
assert from_dict(Person, data) == expected
|
||||||
|
|
||||||
assert dataclass_to_dict(expected) == data
|
assert dataclass_to_dict(expected) == data
|
||||||
|
|||||||
Reference in New Issue
Block a user