Repository navigation
API 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\nif the file has it. - Or, asked for (
read_source(path, lf=True)), the text comes with\nnewlines andSourceFile.newlinesays which the file had; saving withwrite_source(..., newline=source.newline)writes them back. An application then counts offsets in\ntext 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_fileslists 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 skipsHIDDEN_NAMES(caches, virtual environments,node_modules). -
read_textreads 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.
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
5000000
20000
2000000
frozenset (13 entries)
('.pyc', '.pyo')
class FileProblem(Exception)A file could not be opened or saved; the message says why, for the user.
class ChangedOnDisk(FileProblem)The file changed on disk since it was opened or last saved.
class Stamp
Stamp(mtime_ns: int, size: int)What the file on disk was when the editor read or wrote it.
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).
def read_source(path: Path, *, lf: bool = False) -> SourceFileThe 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).
def changed_on_disk(path: Path, stamp: Stamp | None) -> boolThe file on disk is not the one described by stamp (created, changed or deleted since).
def write_source(path: Path, text: str, *, expected: Stamp | None, overwrite: bool = False, newline: str | None = None) -> SourceFileSave 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.
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.
def read_text(path: Path, max_bytes: int = 2000000) -> str | NoneThe 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.
def newline_of(text: str) -> strThe 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).
def lf_text(text: str) -> strtext with \n newlines (\r\n and a lone \r become \n).
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links