-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
mod scan; | ||
|
||
use pyo3::prelude::*; | ||
|
||
#[pymodule] | ||
#[pyo3(name = "rlib")] | ||
fn rlib(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
m.add_class::<scan::Scanner>()?; | ||
Ok(()) | ||
} |
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,58 @@ | ||
use pyo3::prelude::*; | ||
use rustypath::RPath; | ||
|
||
|
||
#[pyclass] | ||
pub struct Scanner{ | ||
directory: RPath, | ||
contents: Vec<RPath>, | ||
} | ||
|
||
#[pymethods] | ||
impl Scanner { | ||
#[new] | ||
pub fn new(directory: &str) -> PyResult<Self> { | ||
Ok(Self { | ||
directory: RPath::from(directory), | ||
contents: Vec::new(), | ||
}) | ||
} | ||
|
||
pub fn scan(&mut self) -> PyResult<()> { | ||
let mut content: Vec<RPath> = Vec::new(); | ||
let mut dirs: Vec<RPath> = vec![self.directory.clone()]; | ||
|
||
while let Some(current_dir) = dirs.pop() { | ||
for entry in current_dir.read_dir()? { | ||
let entry = entry?; | ||
let path = entry.path(); | ||
|
||
if path.is_dir() { | ||
dirs.push(RPath::from(path)); | ||
} else { | ||
content.push(RPath::from(path)); | ||
} | ||
} | ||
} | ||
|
||
self.contents = content; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn files(&self) -> PyResult<Vec<RPath>> { | ||
Ok(self.contents.clone()) | ||
} | ||
|
||
pub fn isolate(&mut self, r#type: &str) -> PyResult<Vec<RPath>> { | ||
let mut isolated: Vec<RPath> = Vec::new(); | ||
|
||
for file in &self.contents { | ||
if file.if_extension(r#type) { | ||
isolated.push(file.to_owned()); | ||
} | ||
} | ||
|
||
Ok(isolated) | ||
} | ||
} |