SLinfra 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
infra/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ __version__ = "0.1.2"
2
+ AUTHOR = "MJBM2510"
3
+
4
+ from infra.state_tracker import StateTracker, Status
5
+ from infra.file_logger import FileLogger
infra/file_logger.py ADDED
@@ -0,0 +1,64 @@
1
+ import os
2
+ from datetime import datetime
3
+
4
+
5
+ class FileLogger:
6
+
7
+ LEVELS = {"DEBUG": 10, "INFO": 20, "WARNING": 30, "ERROR": 40}
8
+
9
+ def __init__(self, log_dir="logs", log_file="app.log", level="INFO"):
10
+ self.level = self.LEVELS.get(level, 20)
11
+ self.log_dir = log_dir
12
+ self.log_path = os.path.join(log_dir, log_file)
13
+
14
+ self._prepare_environment()
15
+ self.file = open(self.log_path, "a", encoding="utf-8")
16
+
17
+ def _prepare_environment(self):
18
+ os.makedirs(self.log_dir, exist_ok=True)
19
+
20
+ if not os.path.exists(self.log_path):
21
+ with open(self.log_path, "w", encoding="utf-8") as file:
22
+ file.write(f"=== Log started at {self._timestamp()} ===\n")
23
+
24
+ def _timestamp(self):
25
+ return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
26
+
27
+ def _write(self, level, message):
28
+ if self.LEVELS[level] < self.level:
29
+ return
30
+
31
+ line = f"[{self._timestamp()}] [{level}] | {message}"
32
+ self.file.write(line + "\n")
33
+ self.file.flush()
34
+
35
+ def info(self, message):
36
+ self._write("INFO", message)
37
+
38
+ def warning(self, message):
39
+ self._write("WARNING", message)
40
+
41
+ def error(self, message):
42
+ self._write("ERROR", message)
43
+
44
+ def debug(self, message):
45
+ self._write("DEBUG", message)
46
+
47
+ def __enter__(self):
48
+ return self
49
+
50
+ def __exit__(self, exc_type, exc_val, exc_tb):
51
+ self.close()
52
+
53
+ def close(self):
54
+ if not self.file.closed:
55
+ self.file.close()
56
+
57
+
58
+ # test
59
+ if __name__ == "__main__":
60
+ with FileLogger(level="DEBUG") as logger:
61
+ logger.debug("Debug message")
62
+ logger.info("Info message")
63
+ logger.warning("Warning message")
64
+ logger.error("Error message")
infra/state_tracker.py ADDED
@@ -0,0 +1,85 @@
1
+ import os
2
+ import json
3
+ from datetime import datetime
4
+ import threading
5
+ from enum import Enum
6
+
7
+
8
+ class Status(Enum):
9
+ PENDING = "pending"
10
+ DOWNLOADING = "downloading"
11
+ DONE = "done"
12
+ ERROR = "error"
13
+
14
+
15
+ class StateTracker:
16
+
17
+ def __init__(self, state_file="state.json"):
18
+ self._lock = threading.RLock()
19
+ self.state_file = state_file
20
+ self.state = {}
21
+ self._load()
22
+
23
+ def _load(self):
24
+ with self._lock:
25
+ if os.path.exists(self.state_file):
26
+ try:
27
+ with open(self.state_file, "r", encoding="utf-8") as file:
28
+ self.state = json.load(file)
29
+ except Exception:
30
+ self.state = {}
31
+ else:
32
+ self.state = {}
33
+
34
+ def _timestamp(self):
35
+ return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
36
+
37
+ def save(self):
38
+ tmp_file = self.state_file + ".tmp"
39
+
40
+ with self._lock:
41
+ with open(tmp_file, "w", encoding="utf-8") as file:
42
+ json.dump(self.state, file, ensure_ascii=False, indent=4)
43
+
44
+ os.replace(tmp_file, self.state_file)
45
+
46
+ def set(self, item_id, status, **meta):
47
+ with self._lock:
48
+ self.state[item_id] = {
49
+ "status": status,
50
+ "updated_at": self._timestamp(),
51
+ "meta": meta,
52
+ }
53
+
54
+ self.save()
55
+
56
+ def get(self, item_id, default=None):
57
+ with self._lock:
58
+ return self.state.get(item_id, default)
59
+
60
+ def filter_by_status(self, status):
61
+ with self._lock:
62
+ return {
63
+ key: value
64
+ for key, value in self.state.items()
65
+ if value.get("status") == status
66
+ }
67
+
68
+ def remove(self, item_id):
69
+ with self._lock:
70
+ if item_id in self.state:
71
+ del self.state[item_id]
72
+ self.save()
73
+
74
+ def exists(self, item_id):
75
+ with self._lock:
76
+ return item_id in self.state
77
+
78
+ def clear(self):
79
+ with self._lock:
80
+ self.state = {}
81
+ self.save()
82
+
83
+ def all(self):
84
+ with self._lock:
85
+ return dict(self.state)
@@ -0,0 +1,121 @@
1
+ Metadata-Version: 2.4
2
+ Name: SLinfra
3
+ Version: 0.1.2
4
+ Summary: A lightweight Python utility for file-based logging and persistent state tracking.
5
+ Author-email: MJBM <mohamnarut@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/MJBM2510/infra
8
+ Project-URL: Source, https://github.com/MJBM2510/infra
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Operating System :: OS Independent
15
+ Requires-Python: >=3.8
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Dynamic: license-file
19
+
20
+ # infra
21
+
22
+ A lightweight Python utility for file-based logging and persistent state tracking.
23
+
24
+ ## Overview
25
+
26
+ - infra is a minimal Python package designed to help small to medium projects:
27
+ - Keep clean and readable logs
28
+ - Persist application state between runs
29
+ - Avoid heavy logging frameworks
30
+ - It is especially useful for scripts, automation tools, and long-running processes.
31
+
32
+ ---
33
+
34
+ ## Features
35
+
36
+ - Simple file-based logger
37
+ - Persistent state storage (JSON-based)
38
+ - Zero external dependencies
39
+ - Easy to integrate into existing projects
40
+
41
+ ---
42
+
43
+ ## Installation
44
+
45
+ ```bash
46
+ pip install infra
47
+ ```
48
+
49
+ ---
50
+
51
+ ## Quick Start
52
+
53
+ ### File Logging
54
+ ```python
55
+ from infra.file_logger import FileLogger
56
+
57
+ logger = FileLogger("app.log")
58
+
59
+ logger.info("Application started")
60
+ logger.warning("Low memory warning")
61
+ logger.error("Unexpected error occurred")
62
+ ```
63
+
64
+ This will create (or append to) a log file and store timestamped log messages.
65
+
66
+ ### State Tracking
67
+ ```python
68
+ from infra.state_tracker import StateTracker
69
+
70
+ state = StateTracker("state.json")
71
+
72
+ state["last_run"] = "2026-01-23T14:00"
73
+ state["counter"] = 5
74
+ state.save()
75
+ ```
76
+
77
+ This allows your application to persist important values between executions.
78
+
79
+ ---
80
+
81
+ ## Project Structure
82
+
83
+ ```
84
+ infra/
85
+ ├── infra/
86
+ │ ├── __init__.py
87
+ │ ├── file_logger.py
88
+ │ └── state_tracker.py
89
+ ├── README.md
90
+ ├── LICENSE
91
+ └── pyproject.toml
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Design Goals
97
+
98
+ - Keep the API small and intuitive
99
+ - Prefer simplicity over feature overload
100
+ - Avoid external dependencies
101
+ - Be suitable for educational and practical use
102
+
103
+ ---
104
+
105
+ ## Limitations
106
+
107
+ - Not thread-safe
108
+ - No log rotation support
109
+ - Designed for small to medium workloads
110
+
111
+ ---
112
+
113
+ ## Roadmap / TODO
114
+
115
+ - Add unit tests
116
+ - Add log rotation support
117
+ - Improve type hints and documentation
118
+
119
+ ## License
120
+
121
+ This project is licensed under the MIT License.
@@ -0,0 +1,8 @@
1
+ infra/__init__.py,sha256=36ctFNvJaQXOS93abRTM9JTqpkXBqcuOgxCTYE6xYTM,137
2
+ infra/file_logger.py,sha256=YF9f3CcBcSrn5hmL1zTnHtW0iei2bZcagJ-2qxltyZI,1749
3
+ infra/state_tracker.py,sha256=xZuFWUebWGvwjLY3jUYZ2aEUOVim9LKYq6gFwPugDxA,2184
4
+ slinfra-0.1.2.dist-info/licenses/LICENSE,sha256=oUx8d4ZH7tKK_srnI53jyuKwURFKe22Vd8LhzP4ECP0,1065
5
+ slinfra-0.1.2.dist-info/METADATA,sha256=9o2eSayb8p-o3qqoesqY_5B-A9bfV5QRByIk5k67K-s,2545
6
+ slinfra-0.1.2.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
7
+ slinfra-0.1.2.dist-info/top_level.txt,sha256=oUj4TteZGKCvGrRn61_VMWOw-XGJCHm3_ZqbfNLhSE0,6
8
+ slinfra-0.1.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MJBM2510
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ infra