Skip to content

Commit fcd7864

Browse files
committed
refactored library structure
1 parent 77863f6 commit fcd7864

File tree

14 files changed

+293
-41
lines changed

14 files changed

+293
-41
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,8 @@ docs/_build/
6969
# VSCode
7070
.vscode/
7171

72+
# testing
73+
tests/
74+
7275
# Pyenv
7376
.python-version

Cargo.lock

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ name = "_phaeton"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
pyo3 = { version = "0.20.0", features = ["extension-module"] }
11+
pyo3 = { version = "0.20.0", features = ["extension-module"] }
12+
rayon = "1.11.0"

python/phaeton/__init__.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
1-
# python/phaeton/__init__.py
1+
from .engine import Engine
2+
from .pipeline import Pipeline
3+
4+
__all__ = ["Engine", "Pipeline"]
5+
_DEFAULT_ENGINE = Engine(workers=0)
26

3-
# Coba import binary Rust (ini bakal gagal kalau belum dicompile,
4-
# jadi kita kasih try-except biar gak error pas lagi coding python doang)
57
try:
68
from ._phaeton import __version__ as _rust_version
79
except ImportError:
810
_rust_version = "0.0.0-dev"
911

10-
# Import submodule biar user bisa akses phaeton.text, dll
11-
from . import text, io, guard
12-
13-
# --- ROOT FUNCTIONS & CONFIG ---
14-
15-
_CONFIG = {
16-
"threads": 4,
17-
"strict": True
18-
}
19-
20-
def configure(threads: int = 4, strict: bool = True):
21-
"""Global configuration settings for Phaeton engine."""
22-
global _CONFIG
23-
_CONFIG["threads"] = threads
24-
_CONFIG["strict"] = strict
25-
# Nanti di sini kita pass config ke Rust
26-
print(f"DEBUG: Config updated -> {_CONFIG}")
27-
2812
def version() -> str:
29-
"""Check library and engine version."""
13+
"""Returns the library version string."""
3014
return f"Phaeton v0.1.0 (Engine: Rust v{_rust_version})"
3115

32-
def sanitize(text: str) -> str:
33-
"""
34-
[DUMMY] Otomatis mendeteksi PII umum dan menggantinya dengan ***.
35-
"""
36-
# Placeholder logic (Python side)
37-
if not text: return ""
38-
return text.replace("@", "***").replace("08", "**")
16+
def filter(source: str, filter: str) -> str:
17+
"""Shortcut function to filter data from source."""
18+
results = (
19+
_DEFAULT_ENGINE.ingest(source)
20+
.filter(filter)
21+
.run()
22+
)
23+
return results
24+
25+
def clean(source: str, operation: dict) -> str:
26+
"""Shortcut function to clean data from source."""
27+
pipeline = _DEFAULT_ENGINE.ingest(source)
28+
for op, mode in operation.items():
29+
if op == "sanitize":
30+
pipeline = pipeline.sanitize(mode)
31+
elif op == "filter":
32+
pipeline = pipeline.filter(mode)
33+
results = pipeline.run()
34+
return results

python/phaeton/engine.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from .pipeline import Pipeline
2+
3+
class Engine:
4+
"""
5+
Phaeton Engine.
6+
Manages resources and acts as the entry point for data ingestion.
7+
"""
8+
def __init__(self, workers: int = 0, verbose: bool = False):
9+
"""
10+
Initialize the engine.
11+
12+
Args:
13+
workers (int): Number of threads. 0 = Auto-detect.
14+
verbose (bool): Enable detailed logging.
15+
"""
16+
self.config = {
17+
"workers": workers,
18+
"verbose": verbose
19+
}
20+
21+
def ingest(self, source: str) -> Pipeline:
22+
"""
23+
Ingest data from a source file/path to start the pipeline.
24+
25+
Args:
26+
source (str): Path to the file (e.g., "server.log", "data.csv")
27+
28+
Returns:
29+
Pipeline: A new pipeline instance ready for chaining.
30+
"""
31+
return Pipeline(source, self.config)
32+
33+
def __repr__(self):
34+
return f"<Phaeton Engine | Workers: {self.config['workers']}>"

python/phaeton/guard.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

python/phaeton/io.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

python/phaeton/pipeline.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
class Pipeline:
3+
"""
4+
Data processing pipeline builder.
5+
"""
6+
def __init__(self, source: str, config: dict):
7+
self.source = source
8+
self.config = config
9+
self.steps = [] # List of instructions (Receipt)
10+
11+
def filter(self, pattern: str) -> "Pipeline":
12+
"""
13+
Keep lines containing the pattern.
14+
"""
15+
self.steps.append({
16+
"action": "filter",
17+
"pattern": pattern
18+
})
19+
return self
20+
21+
def sanitize(self, mode: str = "default") -> "Pipeline":
22+
"""
23+
Sanitize sensitive data based on mode.
24+
Modes: 'default', 'no-html', 'email-mask'.
25+
"""
26+
self.steps.append({
27+
"action": "sanitize",
28+
"mode": mode
29+
})
30+
return self
31+
32+
def save(self, path: str) -> "Pipeline":
33+
"""Save results to file immediately during execution."""
34+
self.steps.append({
35+
"action": "save",
36+
"path": path
37+
})
38+
return self
39+
40+
def run(self):
41+
"""
42+
Execute the pipeline.
43+
"""
44+
try:
45+
from . import _phaeton
46+
except ImportError:
47+
raise ImportError("Phaeton core is not compiled.")
48+
49+
# TODO: Kirim path, list of steps, dan config ke Rust
50+
return _phaeton.execute_pipeline(self.source, self.steps, self.config)
51+
52+
def __repr__(self):
53+
return f"<Pipeline | Source: {self.source} | Steps: {len(self.steps)}>"

python/phaeton/text.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/engine.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub fn setup(workers: usize) {
2+
if workers > 0 {
3+
// Setup rayon global pool
4+
let _ = rayon::ThreadPoolBuilder::new()
5+
.num_threads(workers)
6+
.build_global();
7+
}
8+
}

0 commit comments

Comments
 (0)