forked from DmitryBurnaev/starlette-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from dolamroth/main
Release 0.1.1
- Loading branch information
Showing
43 changed files
with
725 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
*.pyc | ||
|
||
filecache/ | ||
filestorage/ | ||
.python-version | ||
.idea/ | ||
.env | ||
venv/ | ||
/static/ | ||
/media | ||
/templates/ | ||
asgi.py | ||
starlette_web.egg-info/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from starlette_web.common.app import get_app | ||
|
||
|
||
app = get_app() | ||
|
||
|
||
if __name__ == "__main__": | ||
import os | ||
import sys | ||
import uvicorn | ||
|
||
settings_module = "starlette_web.tests.settings" | ||
for arg in sys.argv: | ||
if arg.startswith("--settings="): | ||
settings_module = arg[11:] | ||
|
||
os.environ.setdefault("STARLETTE_SETTINGS_MODULE", settings_module) | ||
uvicorn.run(app, host="127.0.0.1", port=80) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## File storages | ||
|
||
File storages in `starlette_web` are dedicated to work with files, added by users | ||
(**media files** in Django terminology). | ||
They provide a unified mechanism for managing files, without direct access to file handlers/descriptors. | ||
|
||
File storages explicitly separate reading and writing from each other. | ||
Developer is encouraged to use `storage.reader` and `storage.writer` for all read/write operations. | ||
|
||
All supported methods are asynchronous. | ||
|
||
### Usage | ||
|
||
```python3 | ||
from typing import List | ||
|
||
from starlette_web.common.conf import settings | ||
from starlette_web.common.files.storages import MediaFileSystemStorage, FilesystemStorage | ||
|
||
|
||
async with MediaFileSystemStorage() as storage: | ||
async with storage.reader("/path/to/file", mode="b") as _reader: | ||
content = await _reader.read(1024) | ||
line = await _reader.readline() | ||
async for line in _reader: | ||
... | ||
|
||
async with storage.writer("/path/to/file", mode="b", append=True) as _writer: | ||
await _writer.write(b"12345") | ||
|
||
async with FilesystemStorage(BASE_DIR=settings.PROJECT_ROOT_DIR / "filestorage") as storage: | ||
url: str = await storage.get_url("/path/to/file") | ||
mtime: float = await storage.get_mtime("/path/to/file") | ||
size: int = await storage.size("/path/to/file") | ||
_ = await storage.delete("/path/to/file") | ||
exists: bool = await storage.exists("/path/to/file") | ||
files: List[str] = await storage.listdir("/path/to/directory") | ||
``` | ||
|
||
### Supported storages | ||
|
||
- `starlette_web.common.files.storages.filesystem.FilesystemStorage` | ||
- Base file storage. Requires settings BASE_DIR option, and does not define `get_url` by default. | ||
- Wraps synchronous FileIO with `anyio.to_thread.run_sync`, following | ||
[recommendation of Nathaniel Smith](https://trio.readthedocs.io/en/stable/reference-io.html#background-why-is-async-file-i-o-useful-the-answer-may-surprise-you). | ||
However, actual implementation [may be improved in the future.](https://github.com/dolamroth/starlette-web/issues/31) | ||
- **Important**: By default, uses asynchronous `Filelock` as cross-process mutex. | ||
For faster access, it is recommended to subclass default implementation and provide faster | ||
cross-process synchronization mechanism, if you have any (i.e. AioRedis Lock). | ||
- `starlette_web.common.files.storages.filesystem.MediaFileSystemStorage` | ||
- Inherits `FilesystemStorage`. **Recommended** way to store user files. | ||
Uses `settings.MEDIA["ROOT"]` as its base directory and `settings.MEDIA["URL"]` for `get_url`. | ||
|
||
### Implementing custom storage | ||
|
||
To implement a custom storage, subclass `starlette_web.common.files.storages.base.BaseStorage`. | ||
|
||
By default, `BaseStorage` wraps all operations with asynchronous dummy lock, which doesn't actually lock. | ||
You may leave it as is, or use your cross-process asynchronous lock of choice. | ||
The lock interface allows defining a RW-lock, though default implementation is not provided. | ||
|
||
Available input arguments: | ||
- blocking_timeout: float (default `600`) - timeout to acquire lock for reading/writing, in seconds | ||
- write_timeout: float (default: `300`) - lock expiration timeout for writing, in seconds | ||
- directory_create_mode: int (default: `0o755`) - octal permissions for `mkdir` | ||
- chunk_size: int (default: `65536`) - chunk size for reading/writing, in bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.