-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
139 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .mongodb import MongoDB | ||
from .tinydb import TinyDB |
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,25 +1,37 @@ | ||
from abc import ABC, abstractmethod | ||
from abc import ABCMeta, abstractmethod | ||
from collections.abc import Generator | ||
from pathlib import Path | ||
|
||
from docstring_inheritance import NumpyDocstringInheritanceMeta | ||
|
||
class DB(ABC): | ||
|
||
# Support docstring inheritance. | ||
class ABCNumpyDocstringInheritanceMeta(ABCMeta, NumpyDocstringInheritanceMeta): | ||
pass | ||
|
||
|
||
class DB(metaclass=ABCNumpyDocstringInheritanceMeta): | ||
@abstractmethod | ||
def count(self) -> int: | ||
"""Return the number of tracks.""" | ||
pass | ||
|
||
@abstractmethod | ||
def empty(self) -> None: | ||
"""Delete all tracks.""" | ||
pass | ||
|
||
@abstractmethod | ||
def insert_one(self, track: dict) -> None: | ||
def insert_many(self, tracks: list[dict]) -> None: | ||
"""Insert multiple tracks.""" | ||
pass | ||
|
||
@abstractmethod | ||
def search(self, search_filters: dict) -> Generator[dict, None, None]: | ||
"""Return an iterator of matching tracks based on the search filters.""" | ||
pass | ||
|
||
@abstractmethod | ||
def update_one(self, path: Path, field: str, value: str | int) -> None: | ||
"""Update one field in one track.""" | ||
pass |
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,54 @@ | ||
import re | ||
from collections.abc import Generator | ||
from pathlib import Path | ||
|
||
from tinydb import Query, TinyDB as BaseTinyDB, where | ||
|
||
from .db import DB | ||
from rolabesti.conf.settings import TINY_FILE | ||
from rolabesti.models import FIELD_FILTERS | ||
|
||
|
||
class TinyDB(DB): | ||
def __init__(self) -> None: | ||
self.db = BaseTinyDB(TINY_FILE) | ||
|
||
def count(self) -> int: | ||
return len(self.db.all()) | ||
|
||
def empty(self) -> None: | ||
self.db.truncate() | ||
|
||
def insert_many(self, tracks: list[dict]) -> None: | ||
self.db.insert_multiple(tracks) | ||
|
||
def search(self, search_filters: dict) -> Generator[dict, None, None]: | ||
tinydb_query = self._get_tinydb_query(search_filters) | ||
for track in self.db.search(tinydb_query): | ||
yield track | ||
|
||
def update_one(self, path: Path, field: str, value: str | int) -> None: | ||
self.db.update({field: value}, Query()["path"] == str(path)) | ||
|
||
@staticmethod | ||
def _get_tinydb_query(search_filters: dict) -> Query: | ||
"""Get TinyDB query from search filters.""" | ||
query = Query().noop() | ||
|
||
# Get length queries. | ||
if (max_track_length := search_filters.get("max_track_length")) is not None: | ||
query = query & (where('length') <= max_track_length) | ||
if (min_track_length := search_filters.get("min_track_length")) is not None: | ||
query = query & (where('length') >= min_track_length) | ||
|
||
# Get field queries. | ||
for field_filter in set.intersection(set(FIELD_FILTERS), set(search_filters)): | ||
filter_value = search_filters[field_filter] | ||
fields = FIELD_FILTERS[field_filter] | ||
if len(fields) == 1: | ||
query = query & where(fields[0]).search(filter_value, flags=re.IGNORECASE) | ||
else: | ||
query = query & (where(fields[0]).search(filter_value, flags=re.IGNORECASE) | | ||
where(fields[1]).search(filter_value, flags=re.IGNORECASE)) | ||
|
||
return query |