Metadata-Version: 2.4
Name: codechu-fs
Version: 0.1.0
Summary: Stdlib-only filesystem primitives — atomic write, XDG trash, safe walk, mount helpers.
Author: Codechu
License: MIT License
        
        Copyright (c) 2026 Codechu
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/codechu/fs-py
Project-URL: Source, https://github.com/codechu/fs-py
Project-URL: Issues, https://github.com/codechu/fs-py/issues
Keywords: filesystem,atomic,xdg,trash,walk,stdlib
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: System :: Filesystems
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.1; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Dynamic: license-file

```text
   ┌──────────────────────────────────────────────────┐
   │  c o d e c h u — f s                             │
   │   .                                              │
   │   ├── atomic_write()    safe.tmp -> fsync -> mv  │
   │   ├── move_to_trash()   ~/.local/share/Trash/    │
   │   ├── walk()            cancellable + loop-safe  │
   │   └── mountpoint()      /proc/mounts aware       │
   └──────────────────────────────────────────────────┘
```

> *Filesystem primitives for tools that must not lose data.*

# codechu-fs

Stdlib-only filesystem helpers — atomic writes, XDG trash, cancellable
walks, mount discovery — extracted from the
[Disk Cleaner](https://github.com/codechu/disk-cleaner) toolchain.
Zero third-party dependencies. Python 3.10+.

## What it gives you

- **`atomic_write`** — tempfile in the same dir, `fsync`, then `rename`.
  Preserves existing permissions. Either bytes or text (with encoding).
- **`move_to_trash`** — freedesktop XDG Trash Specification compliant.
  Honors `XDG_DATA_HOME`, writes a `.trashinfo`, resolves name
  collisions.
- **`walk`** — `os.walk` with three things `os.walk` is missing: a
  `cancel` Event for cooperative shutdown, an `on_error` callback (no
  silent swallow), and symlink-loop detection when following links.
- **`recursive_size`** — sum of bytes under a directory, cancellable.
- **`mountpoint`** — the mount that contains a path, parsed from
  `/proc/mounts` on Linux with a `st_dev` fallback elsewhere.
- **`is_safe_path`** — path-traversal guard for user-supplied inputs.
- **`temp_dir`** — context-managed temp directory, auto-removed.

## Install

```bash
pip install codechu-fs
```

## Quick examples

```python
from pathlib import Path
from threading import Event
from codechu_fs import (
    atomic_write, move_to_trash, walk, recursive_size,
    mountpoint, is_safe_path, temp_dir,
)

# Durable write — never leaves a half-written file even if you crash mid-write.
atomic_write("/etc/myapp/config.json", b'{"k":1}\n')
atomic_write("notes.txt", "hello\n", encoding="utf-8")

# Send a file to the desktop trash (recoverable from the file manager).
trashed = move_to_trash("/tmp/junk.log")
print("now at:", trashed)

# Cancellable walk — stop instantly when the user clicks Cancel.
cancel = Event()
for dirpath, dirnames, filenames in walk("/var/log", cancel=cancel):
    if some_condition():
        cancel.set()

# Total size of a directory tree (skips unreadable files).
print(recursive_size(Path.home() / "Downloads"))

# What partition is this on?
print(mountpoint("/var/log/syslog"))   # → /var, or / on most systems

# Safe extraction of an untrusted filename.
if is_safe_path(user_input, base="/srv/uploads"):
    ...

# Scratch space that always cleans up.
with temp_dir(prefix="myapp-") as scratch:
    (scratch / "work.bin").write_bytes(b"...")
```

## Design

- **Pure stdlib.** Zero third-party dependencies. Seven small modules.
- **Defensive.** `walk` never swallows errors silently. `atomic_write`
  fsyncs before rename. `move_to_trash` rolls back its `.trashinfo` on
  failure.
- **Cancellable.** Every traversal accepts a `threading.Event`. Long
  scans on slow disks remain responsive in GUI apps.

## Tests

```bash
pip install -e ".[dev]"
pytest -q
```

## Documentation

- [API reference](docs/API.md) — every public symbol, signatures, edge cases
- [Recipes](docs/RECIPES.md) — five idiomatic patterns for CLIs and GUIs

## License

MIT — see [LICENSE](LICENSE).
