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.File inherits all methods from Pyjo.Asset and implements the following new ones.

add_chunk(chunk: bytes) → Self[source]
file = file.add_chunk(b'foo bar baz')

Add chunk of data.

cleanup
boolean = file.cleanup
file.cleanup = boolean

Delete path automatically once the file is not used anymore.

close() → None[source]
file.close()

Close handle and delete path with cleanup, 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 131072 bytes (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 on tmpdir.

property is_file: bool[source]
boolean = file.is_file

True, this is a Pyjo.Asset.File object.

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 mtime: float[source]
mtime = file.mtime

Modification time of asset.

path
path = file.path
file.path = '/home/sri/foo.txt'

File path used to create handle.

property size: int[source]
size = file.size

Size of asset data in bytes.

slurp() → bytes[source]
content = file.slurp()

Read all asset data at once.

property tmpdir: str[source]
tmpdir = file.tmpdir
file.tmpdir = '/tmp'

Temporary directory used to generate path, defaults to the value of the PYJO_TMPDIR environment 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.File object.