aiomegfile.filesystem.http module

class aiomegfile.filesystem.http.AioHttpAdaptiveReader(uri: str, mode: str, timeout: float, max_retries: int, encoding: str, errors: str, newline: str | None, block_size: int, max_buffer_size: int, block_forward: int | None)[source]

Bases: AioReadable, AioSeekable

Adaptive HTTP reader selecting prefetch or full-content strategy.

async close() None

Close selected reader if initialized.

property mode: str

Return open mode.

Returns:

File mode string.

Return type:

str

property name: str

Return source URI.

Returns:

URI string.

Return type:

str

async read(size: int | None = None) AnyStr[source]

Read content from current position.

Parameters:

size – Maximum size to read.

Returns:

Read bytes or text.

Return type:

T.AnyStr

async readline(size: int | None = None) AnyStr[source]

Read one line from current position.

Parameters:

size – Maximum size to read.

Returns:

One line in bytes or text.

Return type:

T.AnyStr

async seek(offset: int, whence: int = 0) int[source]

Move stream cursor and return new offset.

Parameters:
  • offset – Target offset.

  • whence – Seek reference position.

Returns:

New absolute offset.

Return type:

int

async tell() int[source]

Return current stream offset.

Returns:

Current stream offset.

Return type:

int

class aiomegfile.filesystem.http.AioHttpContentReader(uri: str, mode: str, timeout: float, max_retries: int, encoding: str, errors: str, newline: str | None)[source]

Bases: AioReadable

Async reader for streaming HTTP response content.

async close() None

Close reader and release HTTP resources.

property mode: str

Return open mode.

Returns:

File mode string.

Return type:

str

property name: str

Return source URI.

Returns:

URI string.

Return type:

str

async read(size: int | None = None) AnyStr[source]

Read content from current position.

Parameters:

size – Maximum size to read.

Returns:

Read bytes or text.

Return type:

T.AnyStr

async readline(size: int | None = None) AnyStr[source]

Read one line from current position.

Parameters:

size – Maximum size to read.

Returns:

One line in bytes or text.

Return type:

T.AnyStr

async tell() int[source]

Return current stream offset.

Returns:

Current stream offset.

Return type:

int

class aiomegfile.filesystem.http.HttpFileSystem(timeout: float = 60.0)[source]

Bases: BaseFileSystem

Filesystem adapter for read-only HTTP resources.

build_uri(path: str) str

Build URI from path.

Parameters:

path – Path without protocol.

Returns:

URI string.

Return type:

str

async exists(path: str, followlinks: bool = False) bool[source]

Return whether path points to an existing HTTP resource.

Parameters:
  • path – Target path without protocol.

  • followlinks – Ignored for HTTP protocol.

Returns:

True when resource exists, otherwise False.

Return type:

bool

classmethod from_uri(uri: str)

Create a new filesystem instance from URI.

Parameters:

uri – URI string.

Returns:

HttpFileSystem instance.

Return type:

HttpFileSystem

async is_dir(path: str, followlinks: bool = False) bool[source]

Return whether path points to a directory.

HTTP resources are treated as files only.

Parameters:
  • path – Target path without protocol.

  • followlinks – Ignored for HTTP protocol.

Returns:

Always False.

Return type:

bool

async is_file(path: str, followlinks: bool = False) bool[source]

Return whether path points to an existing HTTP resource.

Parameters:
  • path – Target path without protocol.

  • followlinks – Ignored for HTTP protocol.

Returns:

True when resource exists, otherwise False.

Return type:

bool

open(path: str, mode: str = 'r', buffering: int = -1, encoding: str | None = None, errors: str | None = None, newline: str | None = None, **kwargs: Any) AsyncContextManager[source]

Open a read-only HTTP resource.

Parameters:
  • path – Target path without protocol.

  • mode – Open mode, supports r/rt/rb.

  • buffering – Ignored, kept for API compatibility.

  • encoding – Text encoding in text mode.

  • errors – Text decoding error handling strategy.

  • newline – Newline handling in text mode.

  • kwargs

    Extra options for compatibility with megfile APIs.

    Supported options:

    • block_size: Prefetch block size in bytes.

    • max_buffer_size: Maximum in-memory prefetch buffer size in bytes.

    • block_forward: Number of blocks to prefetch ahead.

    • max_retries: Maximum retry attempts for transient HTTP errors.

Returns:

Async reader context manager.

Return type:

T.AsyncContextManager

parse_uri(uri: str) str[source]

Parse path part from URI.

Parameters:

uri – URI string.

Returns:

Path without protocol.

Return type:

str

protocol = 'http'
same_endpoint(other_filesystem: BaseFileSystem) bool[source]

Return whether this filesystem points to the same endpoint.

Parameters:

other_filesystem – Filesystem to compare.

Returns:

True when both are HTTP filesystems with same protocol.

Return type:

bool

scandir(path: str) AsyncContextManager[AsyncIterator[Any]][source]

Scan entries under a path.

HTTP protocol does not support directory listing.

Parameters:

path – Target path without protocol.

Raises:

NotADirectoryError – Always raised for HTTP resources.

scanfile(path: str, sort: bool = False) AsyncContextManager[AsyncIterator[Any]][source]

Scan files under a path.

HTTP protocol does not support directory traversal.

Parameters:
  • path – Target path without protocol.

  • sort – Compatibility flag for protocol-aligned scanfile APIs.

Raises:

NotADirectoryError – Always raised for HTTP resources.

async stat(path: str, followlinks: bool = False) StatResult[source]

Get metadata for an HTTP resource.

Parameters:
  • path – Target path without protocol.

  • followlinks – Ignored for HTTP protocol.

Returns:

StatResult for the HTTP resource.

Return type:

StatResult

Raises:
  • FileNotFoundError – When resource does not exist.

  • PermissionError – When request is denied.

  • OSError – When request fails for other reasons.

class aiomegfile.filesystem.http.HttpsFileSystem(timeout: float = 60.0)[source]

Bases: HttpFileSystem

Filesystem adapter for HTTPS resources.

protocol = 'https'
aiomegfile.filesystem.http.is_http(path: str | PathLike) bool[source]

Return whether the given path is an HTTP or HTTPS URL.

Parameters:

path – Path to be tested.

Returns:

True if path is an HTTP(S) URL, otherwise False.

Return type:

bool