Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:separate decorators to another file for better reading #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 1 addition & 74 deletions tinyflux/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import copy
from datetime import datetime, timezone
from functools import wraps
from typing import (
Any,
Callable,
Expand All @@ -16,6 +15,7 @@
Union,
)

from .decorators import read_op, write_op, temp_storage_op, append_op
from .index import Index
from .measurement import Measurement
from .point import (
Expand All @@ -36,79 +36,6 @@
from .storages import CSVStorage, Storage


def append_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate an append operation with assertion.

Ensures storage can be appended to before doing anything.
"""

@wraps(method)
def op(self: Any, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
assert self._storage.can_append
return method(self, *args, **kwargs)

return op


def read_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate a read operation with assertion.

Ensures storage can be read from before doing anything.
"""

@wraps(method)
def op(self: Any, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
assert self._storage.can_read

if self._auto_index and not self._index.valid:
self.reindex()

return method(self, *args, **kwargs)

return op


def temp_storage_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate a db operation that requires auxiliary storage.

Initializes temporary storage, invokes method, and cleans-up storage after
op has run.
"""

@wraps(method)
def op(self: Any, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
# Init temp storage in the storage class.
self._storage._init_temp_storage()

# Invoke op.
rst = method(self, *args, **kwargs)

# Clean-up temp storage.
self._storage._cleanup_temp_storage()

return rst

return op


def write_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate a write operation with assertion.

Ensures storage can be written to before doing anything.
"""

@wraps(method)
def op(self: Any, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
assert self._storage.can_write
return method(self, *args, **kwargs)

return op


class TinyFlux:
"""The TinyFlux class containing the interface for the TinyFlux package.

Expand Down
94 changes: 94 additions & 0 deletions tinyflux/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
This module contains decorators for various operations related to \
storage management.

The decorators ensure certain preconditions are met before executing methods,
such as checking if the storage is appendable, readable, or writable. They also
handle temporary storage initialization and cleanup when required.

Decorators included:
- `append_op`: Ensures storage can be appended to before executing the method.
- `read_op`: Ensures storage can be read from, with optional reindexing.
- `temp_storage_op`: Handles temporary storage initialization and cleanup for
operations.
- `write_op`: Ensures storage can be written to before executing the method.
"""

from functools import wraps
from typing import (
Any,
Callable,
)


def append_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate an append operation with assertion.

Ensures storage can be appended to before doing anything.
"""

@wraps(method)
def op(self: Any, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
assert self._storage.can_append
return method(self, *args, **kwargs)

return op


def read_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate a read operation with assertion.

Ensures storage can be read from before doing anything.
"""

@wraps(method)
def op(self: Any, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
assert self._storage.can_read

if self._auto_index and not self._index.valid:
self.reindex()

return method(self, *args, **kwargs)

return op


def temp_storage_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate a db operation that requires auxiliary storage.

Initializes temporary storage, invokes method, and cleans-up storage after
op has run.
"""

@wraps(method)
def op(self: Any, seprate=None, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
# Init temp storage in the storage class.
self._storage._init_temp_storage()

# Invoke op.
rst = method(self, *args, **kwargs)

# Clean-up temp storage.
self._storage._cleanup_temp_storage()

return rst

return op


def write_op(method: Callable[..., Any]) -> Callable[..., Any]:
"""Decorate a write operation with assertion.

Ensures storage can be written to before doing anything.
"""

@wraps(method)
def op(self: Any, *args: Any, **kwargs: Any) -> Any:
"""Decorate."""
assert self._storage.can_write
return method(self, *args, **kwargs)

return op