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

feat(pytauri): add path::PathResolver #62

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
366 changes: 335 additions & 31 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ inherits = "dev"

[profile.bundle-release]
inherits = "release"


[patch.crates-io.tauri]
git = "https://github.com/tauri-apps/tauri"
branch = "dev"
1 change: 1 addition & 0 deletions crates/pytauri-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Added

- [#62](https://github.com/WSH032/pytauri/pull/62) - feat: add `path::PathResolver`, `Manager::path`.
- [#50](https://github.com/WSH032/pytauri/pull/50) - feat: add `ipc::Channel`, `ipc::JavaScriptChannelId`, `webview::Webview`, `webview::WebviewWindow::as_ref::<webview>` for [channels ipc](https://tauri.app/develop/calling-frontend/#channels).
- [#46](https://github.com/WSH032/pytauri/pull/46) - feat: add `webview::WebviewWindow`, `Manager`, `ImplManager`, `App::handle`.
- [#48](https://github.com/WSH032/pytauri/pull/48) - feat: accessing the `WebviewWindow` in `Commands`.
Expand Down
14 changes: 14 additions & 0 deletions crates/pytauri-core/src/ext_mod_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod ipc;
pub mod path;
pub mod webview;

use std::{
Expand Down Expand Up @@ -326,6 +327,19 @@ impl Manager {
}
manager_method_impl!(slf, webview_windows_impl)
}

#[staticmethod]
fn path(py: Python<'_>, slf: ImplManager) -> PyResult<path::PathResolver> {
macro_rules! path_impl {
($wrapper:expr) => {{
let py_ref = $wrapper.borrow(py);
let guard = py_ref.0.inner_ref_semver()??;
let path_resolver = guard.path().clone();
Ok(path::PathResolver::new(path_resolver))
}};
}
manager_method_impl!(slf, path_impl)
}
}

/// See also: [tauri::EventId].
Expand Down
63 changes: 63 additions & 0 deletions crates/pytauri-core/src/ext_mod_impl/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::path::PathBuf;

use pyo3::prelude::*;
use pyo3_utils::py_wrapper::{PyWrapper, PyWrapperT0};
use tauri::path;

use crate::{tauri_runtime::Runtime, utils::TauriError};

type TauriPathResolver = path::PathResolver<Runtime>;

/// see also: [tauri::path::PathResolver].
#[pyclass(frozen)]
#[non_exhaustive]
pub struct PathResolver(PyWrapper<PyWrapperT0<TauriPathResolver>>);

impl PathResolver {
pub(crate) fn new(path_resolver: TauriPathResolver) -> Self {
Self(PyWrapper::new0(path_resolver))
}
}

macro_rules! impl_path_resolver_method {
($($fn_name:ident),*) => {
#[pymethods]
impl PathResolver {
$(
fn $fn_name(&self) -> PyResult<PathBuf> {
self.0
.inner_ref()
.$fn_name()
.map_err(|e| PyErr::from(TauriError::from(e)))
}
)*
}
};

}

impl_path_resolver_method!(
audio_dir,
cache_dir,
config_dir,
data_dir,
local_data_dir,
desktop_dir,
document_dir,
download_dir,
executable_dir,
font_dir,
home_dir,
picture_dir,
public_dir,
runtime_dir,
template_dir,
video_dir,
resource_dir,
app_config_dir,
app_data_dir,
app_local_data_dir,
app_cache_dir,
app_log_dir,
temp_dir
);
9 changes: 9 additions & 0 deletions crates/pytauri-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ pub mod ext_mod {
#[pymodule_export]
pub use ext_mod_impl::webview::{Webview, WebviewWindow};
}

/// see also: [tauri::path]
#[pymodule]
pub mod path {
use super::*;

#[pymodule_export]
pub use ext_mod_impl::path::PathResolver;
}
}
1 change: 1 addition & 0 deletions python/pytauri/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

### Added

- [#62](https://github.com/WSH032/pytauri/pull/62) - feat: add `path::PathResolver`, `Manager::path`.
- [#50](https://github.com/WSH032/pytauri/pull/50) - feat: add `ipc::Channel`, `ipc::JavaScriptChannelId`, `webview::Webview`, `webview::WebviewWindow::as_ref::<webview>` for [channels ipc](https://tauri.app/develop/calling-frontend/#channels).
- [#46](https://github.com/WSH032/pytauri/pull/46) - feat: add `webview::WebviewWindow`, `Manager`, `ImplManager`, `App::handle`.
- [#48](https://github.com/WSH032/pytauri/pull/48) - feat: accessing the `WebviewWindow` in `Commands`.
Expand Down
7 changes: 6 additions & 1 deletion python/pytauri/src/pytauri/ffi/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __call__(self, invoke: "Invoke", /) -> Any: ...


if TYPE_CHECKING:
from pytauri.ffi import webview
from pytauri.ffi import path, webview

@final
class App:
Expand Down Expand Up @@ -226,6 +226,11 @@ def webview_windows(slf: "ImplManager", /) -> dict[str, webview.WebviewWindow]:
"""Fetch all managed webview windows."""
...

@staticmethod
def path(slf: "ImplManager", /) -> path.PathResolver:
"""The path resolver is a helper class for general and application-specific path APIs."""
...

class Event:
"""[tauri::Event](https://docs.rs/tauri/latest/tauri/struct.Event.html)"""

Expand Down
47 changes: 47 additions & 0 deletions python/pytauri/src/pytauri/ffi/path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ruff: noqa: D102

"""[tauri::path](https://docs.rs/tauri/latest/tauri/path/index.html)"""

from typing import (
TYPE_CHECKING,
final,
)

from pytauri.ffi._ext_mod import pytauri_mod

__all__ = ["PathResolver"]

_path_mod = pytauri_mod.path

if TYPE_CHECKING:

@final
class PathResolver:
"""[tauri::path::PathResolver](https://docs.rs/tauri/latest/tauri/path/struct.PathResolver.html)"""

def audio_dir(self) -> str: ...
def cache_dir(self) -> str: ...
def config_dir(self) -> str: ...
def data_dir(self) -> str: ...
def local_data_dir(self) -> str: ...
def desktop_dir(self) -> str: ...
def document_dir(self) -> str: ...
def download_dir(self) -> str: ...
def executable_dir(self) -> str: ...
def font_dir(self) -> str: ...
def home_dir(self) -> str: ...
def picture_dir(self) -> str: ...
def public_dir(self) -> str: ...
def runtime_dir(self) -> str: ...
def template_dir(self) -> str: ...
def video_dir(self) -> str: ...
def resource_dir(self) -> str: ...
def app_config_dir(self) -> str: ...
def app_data_dir(self) -> str: ...
def app_local_data_dir(self) -> str: ...
def app_cache_dir(self) -> str: ...
def app_log_dir(self) -> str: ...
def temp_dir(self) -> str: ...

else:
PathResolver = _path_mod.PathResolver
5 changes: 5 additions & 0 deletions python/pytauri/src/pytauri/path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""[tauri::path](https://docs.rs/tauri/latest/tauri/path/index.html)"""

from pytauri.ffi.path import PathResolver

__all__ = ["PathResolver"]