Skip to content

API turboframe.files

rafageist edited this page Dec 4, 2026 · 15 revisions

turboframe.files

Source: turboframe/files.py. Generated from the code of TurboFrame 0.3.0 by tools/wiki_api.py.

A project's files: list them, read them without altering a byte, save them without losing one.

Opening and saving (an editor's files):

  • A file is opened only if it is UTF-8 text (read_source). Binary or non-UTF-8 files are refused with a reason, never decoded with replacements: saving them would rewrite bytes the user never changed. The text keeps its \r\n if the file has it.
  • Or, asked for (read_source(path, lf=True)), the text comes with \n newlines and SourceFile.newline says which the file had; saving with write_source(..., newline=source.newline) writes them back. An application then counts offsets in \n text and the file keeps its newlines (newline_of, lf_text).
  • A save (write_source) writes a temporary file next to the target, then replaces the target. An interrupted save leaves the old file whole. The permissions are kept.
  • A save first checks that the file on disk is still the one that was opened (its Stamp). If another program changed it, the save is refused (ChangedOnDisk), so the interface can ask before overwriting.

Listing and searching (find in files, go to file):

  • project_files lists a folder's files: git's list when the folder is a repository (what it tracks and what it does not ignore), else a walk that skips HIDDEN_NAMES (caches, virtual environments, node_modules).
  • read_text reads a file to search it: anything that is not text comes back as None, and the newlines are made \n.

FileProblem messages are for the user, in TurboFrame's language.

Contents

MAX_BYTES, MAX_FILES, MAX_TEXT_BYTES, HIDDEN_NAMES, HIDDEN_SUFFIXES, FileProblem, ChangedOnDisk, Stamp, SourceFile, read_source, changed_on_disk, write_source, project_files, read_text, newline_of, lf_text

MAX_BYTES

5000000

MAX_FILES

20000

MAX_TEXT_BYTES

2000000

HIDDEN_NAMES

frozenset (13 entries)

HIDDEN_SUFFIXES

('.pyc', '.pyo')

class FileProblem

class FileProblem(Exception)

A file could not be opened or saved; the message says why, for the user.

class ChangedOnDisk

class ChangedOnDisk(FileProblem)

The file changed on disk since it was opened or last saved.

class Stamp

class Stamp
Stamp(mtime_ns: int, size: int)

What the file on disk was when the editor read or wrote it.

class SourceFile

class SourceFile
SourceFile(path: Path, text: str, stamp: Stamp | None, newline: str | None = None)

A file's text and its stamp; stamp is None for a new file, not on disk yet.

newline is None when text is the file's exactly (its newlines included); else text has \n newlines and newline is the one the file has ("\r\n", "\n" or "\r"): read_source(lf=True).

read_source

def read_source(path: Path, *, lf: bool = False) -> SourceFile

The text of path, exactly as on disk (newlines included). With lf, the text with \n newlines and, in newline, the one the file uses (a file of mixed newlines is saved with its most frequent).

changed_on_disk

def changed_on_disk(path: Path, stamp: Stamp | None) -> bool

The file on disk is not the one described by stamp (created, changed or deleted since).

write_source

def write_source(path: Path, text: str, *, expected: Stamp | None, overwrite: bool = False, newline: str | None = None) -> SourceFile

Save text to path, atomically.

expected is what the file was when it was read (None for a new one). If the disk no longer matches, ChangedOnDisk is raised unless overwrite says the user chose to replace it.

newline None writes text as it is. Given (a SourceFile.newline), text is taken as \n text and every newline is written as newline; the SourceFile returned has the text given and newline.

project_files

def project_files(root: Path, limit: int = 20000, *, hidden: Collection[str] = frozenset({'.eggs', '.git', '.hg', '.idea', '.mypy_cache', '.pytest_cache', '.ruff_cache', '.svn', '.tox', '.venv', '__pycache__', 'node_modules', 'venv'}), hidden_suffixes: tuple[str, ...] = ('.pyc', '.pyo')) -> list[str]

The folder's files relative to root, with /: git's list when it is a repository (the tracked ones and the new ones it does not ignore), else a walk without the hidden folders, the folders whose name starts with a dot or ends with .egg-info, and the files ending with hidden_suffixes.

read_text

def read_text(path: Path, max_bytes: int = 2000000) -> str | None

The file as text to search, with \n newlines, or None for what is not (too large, binary, unreadable). Bytes that are not UTF-8 are replaced: the text is read, never saved back.

newline_of

def newline_of(text: str) -> str

The newline text uses: the most frequent of \r\n, \n and \r (\n when it has none; the first one in the order above on a tie).

lf_text

def lf_text(text: str) -> str

text with \n newlines (\r\n and a lone \r become \n).

Clone this wiki locally