Skip to content

Commit f7db406

Browse files
committed
Define ConfigDirectory and ProgramDirectory.
1 parent 290c108 commit f7db406

8 files changed

Lines changed: 102 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/asimov/sdk.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
__version__: str
44
__version_tuple__: tuple[int, int, int, str | None]
55

6+
class ConfigDirectory:
7+
pass
8+
69
class ModuleDirectory:
710
pass
811

12+
class ProgramDirectory:
13+
pass
14+
915
class StateDirectory:
1016
pass

src/directory/config_directory.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This is free and unencumbered software released into the public domain.
2+
3+
use asimov_directory::fs::ConfigDirectory as FsConfigDirectory;
4+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyString};
5+
6+
/// A configuration directory stored on a file system (e.g., `$HOME/.asimov/configs/`).
7+
#[pyclass(frozen, module = "asimov", str = "{0}")]
8+
pub struct ConfigDirectory(FsConfigDirectory);
9+
10+
#[pymethods]
11+
impl ConfigDirectory {
12+
#[new]
13+
pub fn __new__(path: &str) -> PyResult<Self> {
14+
let Ok(result) = FsConfigDirectory::open(path) else {
15+
Err(PyErr::new::<PyRuntimeError, _>(
16+
"Failed to open configuration directory", // TODO
17+
))?
18+
};
19+
Ok(Self(result))
20+
}
21+
22+
pub fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> {
23+
let class_name: Bound<'_, PyString> = slf.get_type().qualname()?;
24+
Ok(format!("{}({:?})", class_name, slf.borrow().0.as_str()))
25+
}
26+
}

src/directory/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// This is free and unencumbered software released into the public domain.
22

3+
mod config_directory;
4+
pub use config_directory::*;
5+
36
mod module_directory;
47
pub use module_directory::*;
58

9+
mod program_directory;
10+
pub use program_directory::*;
11+
612
mod state_directory;
713
pub use state_directory::*;

src/directory/module_directory.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This is free and unencumbered software released into the public domain.
22

3-
use asimov_directory::fs::ModuleDirectory as FsModuleDirectory;
3+
use asimov_directory::{ModuleDirectory as _, fs::ModuleDirectory as FsModuleDirectory};
44
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyString};
55

6-
/// A module directory.
6+
/// A module directory stored on a file system (e.g., `$HOME/.asimov/modules/`).
77
#[pyclass(frozen, module = "asimov", str = "{0}")]
88
pub struct ModuleDirectory(FsModuleDirectory);
99

@@ -19,8 +19,18 @@ impl ModuleDirectory {
1919
Ok(Self(result))
2020
}
2121

22-
fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> {
22+
pub fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> {
2323
let class_name: Bound<'_, PyString> = slf.get_type().qualname()?;
2424
Ok(format!("{}({:?})", class_name, slf.borrow().0.as_str()))
2525
}
26+
27+
/// Checks if a module is installed.
28+
pub fn is_installed(&self, module_name: String) -> bool {
29+
self.0.is_installed(module_name)
30+
}
31+
32+
/// Checks if a module is installed and enabled.
33+
pub fn is_enabled(&self, module_name: String) -> bool {
34+
self.0.is_enabled(module_name)
35+
}
2636
}

src/directory/program_directory.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This is free and unencumbered software released into the public domain.
2+
3+
use asimov_directory::fs::ProgramDirectory as FsProgramDirectory;
4+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyString};
5+
6+
/// A program directory stored on a file system (e.g., `$HOME/.asimov/libexec/`).
7+
#[pyclass(frozen, module = "asimov", str = "{0}")]
8+
pub struct ProgramDirectory(FsProgramDirectory);
9+
10+
#[pymethods]
11+
impl ProgramDirectory {
12+
#[new]
13+
pub fn __new__(path: &str) -> PyResult<Self> {
14+
let Ok(result) = FsProgramDirectory::open(path) else {
15+
Err(PyErr::new::<PyRuntimeError, _>(
16+
"Failed to open program directory", // TODO
17+
))?
18+
};
19+
Ok(Self(result))
20+
}
21+
22+
pub fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> {
23+
let class_name: Bound<'_, PyString> = slf.get_type().qualname()?;
24+
Ok(format!("{}({:?})", class_name, slf.borrow().0.as_str()))
25+
}
26+
}

src/directory/state_directory.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This is free and unencumbered software released into the public domain.
22

3-
use asimov_directory::fs::StateDirectory as FsStateDirectory;
3+
use asimov_directory::{StateDirectory as _, fs::StateDirectory as FsStateDirectory};
44
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyString};
55

6-
/// A state directory.
6+
/// A state directory stored on a file system (e.g., `$HOME/.asimov/`).
77
#[pyclass(frozen, module = "asimov", str = "{0}")]
88
pub struct StateDirectory(FsStateDirectory);
99

@@ -19,8 +19,23 @@ impl StateDirectory {
1919
Ok(Self(result))
2020
}
2121

22-
fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> {
22+
pub fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> {
2323
let class_name: Bound<'_, PyString> = slf.get_type().qualname()?;
2424
Ok(format!("{}({:?})", class_name, slf.borrow().0.as_str()))
2525
}
26+
27+
/// Checks if any configurations are available.
28+
pub fn has_configs(&self) -> bool {
29+
self.0.has_configs()
30+
}
31+
32+
/// Checks if any modules are installed.
33+
pub fn has_modules(&self) -> bool {
34+
self.0.has_modules()
35+
}
36+
37+
/// Checks if any programs are installed.
38+
pub fn has_programs(&self) -> bool {
39+
self.0.has_programs()
40+
}
2641
}

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ mod sdk {
1313
Ok(())
1414
}
1515

16+
#[pymodule_export]
17+
use super::ConfigDirectory;
18+
1619
#[pymodule_export]
1720
use super::ModuleDirectory;
1821

22+
#[pymodule_export]
23+
use super::ProgramDirectory;
24+
1925
#[pymodule_export]
2026
use super::StateDirectory;
2127
}

0 commit comments

Comments
 (0)