Pyjo.Asset.File - File storage for HTTP content¶
from Pyjo.Asset.File import File
# Temporary file
file = File()
file.add_chunk(b'foo bar baz')
if file.contains(b'bar') >= 0:
print('File contains "bar"')
print(file.slurp())
# Or import the module, as in Google Python Style Guide
import Pyjo.Asset.File
# Temporary file
file = Pyjo.Asset.File.new()
file.add_chunk(b'foo bar baz')
if file.contains(b'bar') >= 0:
print('File contains "bar"')
print(file.slurp())
# Existing file
file = Pyjo.Asset.File.new(path='/home/sri/foo.txt')
file.move_to('/yada.txt')
print(file.slurp())
Pyjo.Asset.File is a file storage backend for HTTP content.
The end of a with block closes the file, and deletes it with
cleanup.
Events¶
Pyjo.Asset.File inherits all events from Pyjo.Asset.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::Asset::File of Mojolicious 9.49.
- class Pyjo.Asset.File.Pyjo_Asset_File(*, cleanup: bool | None = None, end_range: int | None = None, handle: IO[bytes] | None = None, path: str | PathLike[str] | None = None, start_range: int = 0, tmpdir: str | PathLike[str] | None = None)[source]¶
Pyjo.Asset.Fileinherits all methods fromPyjo.Assetand implements the following new ones.- cleanup¶
boolean = file.cleanup file.cleanup = boolean
Delete
pathautomatically once the file is not used anymore.
- close() None[source]¶
file.close()
Close
handleand deletepathwithcleanup, as it happens when the file is freed.
- contains(string: bytes) int[source]¶
position = file.contains(b'bar')
Check if asset contains a specific string.
- get_chunk(offset: int, max: int | None = None) bytes[source]¶
chunk = file.get_chunk(offset) chunk = file.get_chunk(offset, max)
Get chunk of data starting from a specific position, defaults to a maximum chunk size of
131072bytes (128KiB).
- property handle: IO[bytes][source]¶
handle = file.handle file.handle = open('/home/sri/foo.txt', 'rb')
Filehandle, created on demand for
path, which can be generated automatically and safely based ontmpdir.
- property is_file: bool[source]¶
boolean = file.is_file
True, this is a
Pyjo.Asset.Fileobject.
- move_to(to: Anything) Self[source]¶
file = file.move_to('/home/sri/bar.txt')
Move asset data into a specific file and disable
cleanup.
- property tmpdir: str[source]¶
tmpdir = file.tmpdir file.tmpdir = '/tmp'
Temporary directory used to generate
path, defaults to the value of thePYJO_TMPDIRenvironment variable or auto-detection.
- to_file() Self[source]¶
file = file.to_file()
Does nothing but return the invocant, since we already have a
Pyjo.Asset.Fileobject.