Exemple #1
0
    def __init__(
        self,
        database_url: str = "",
        search_exception: Optional[bool] = None,
    ) -> None:
        """Initialize the repository attributes.

        Args:
            database_url: URL specifying the connection to the database.
        """
        super().__init__(database_url, search_exception)
        self.database_file = os.path.expanduser(
            database_url.replace("tinydb://", ""))
        if not os.path.isfile(self.database_file):
            try:
                with open(self.database_file, "a",
                          encoding="utf-8") as file_cursor:
                    file_cursor.close()
            except FileNotFoundError as error:
                raise ConnectionError(
                    f"Could not create the database file: {self.database_file}"
                ) from error

        serialization = SerializationMiddleware(JSONStorage)
        serialization.register_serializer(DateTimeSerializer(), "TinyDate")

        self.db_ = TinyDB(self.database_file,
                          storage=serialization,
                          sort_keys=True,
                          indent=4)
        self.staged: Dict[str, List[Any]] = {"add": [], "remove": []}
Exemple #2
0
def test_serializer_nondestructive(tmpdir):
    path = str(tmpdir.join('db.json'))

    serializer = SerializationMiddleware(JSONStorage)
    serializer.register_serializer(DateTimeSerializer(), 'TinyDate')
    db = TinyDB(path, storage=serializer)

    data = {'date': datetime.utcnow(), 'int': 3, 'list': []}
    data_before = dict(data)  # implicitly copy
    db.insert(data)
    assert data == data_before
Exemple #3
0
def test_serializer(tmpdir):
    path = str(tmpdir.join('db.json'))

    serializer = SerializationMiddleware(JSONStorage)
    serializer.register_serializer(DateTimeSerializer(), 'TinyDate')
    db = TinyDB(path, storage=serializer)

    date = datetime(2000, 1, 1, 12, 0, 0)

    db.insert({'date': date})
    db.insert({'int': 2})
    assert db.count(where('date') == date) == 1
    assert db.count(where('int') == 2) == 1
Exemple #4
0
def test_serializer_recursive(tmpdir):
    path = str(tmpdir.join('db.json'))

    serializer = SerializationMiddleware(JSONStorage)
    serializer.register_serializer(DateTimeSerializer(), 'TinyDate')
    db = TinyDB(path, storage=serializer)

    date = datetime(2000, 1, 1, 12, 0, 0)
    datenow = datetime.utcnow()
    dates = [{'date': date, 'hp': 100}, {'date': datenow, 'hp': 1}]
    data = {'dates': dates, 'int': 10}
    db.insert(data)
    db.insert({'int': 2})
    assert db.count(where('dates').any(where('date') == date)) == 1
    assert db.count(where('int') == 2) == 1
Exemple #5
0
def main(args):
    serialization = SerializationMiddleware(JSONStorage)
    serialization.register_serializer(DateTimeSerializer(), "TinyDate")
    db_path = args.output_dir / "feeds.json"
    db = TinyDB(db_path,
                sort_keys=True,
                indent=4,
                separators=(",", ": "),
                storage=serialization)
    feeds = db.table("feeds")

    if args.import_opml:
        import_opml(feeds, args.import_opml)

    update_feeds(feeds, force_updates=args.force_updates)
    for feed in feeds.all():
        download_feed(feed, args.output_dir, args.max_episodes)
Exemple #6
0
from tinydb import JSONStorage, TinyDB
from tinydb_serialization import SerializationMiddleware
from tinydb_serialization.serializers import DateTimeSerializer

from simple_note_taker.core.config import config

_serialization = SerializationMiddleware(JSONStorage)
_serialization.register_serializer(DateTimeSerializer(), "TinyDate")

tiny_db = TinyDB(
    path=config.db_file_path,
    storage=_serialization,
    # json.dump() kwargs
    sort_keys=True,
    indent=4,
    separators=(",", ": "),
)
 def __init__(self):
     serialization = SerializationMiddleware(JSONStorage)
     serialization.register_serializer(DateTimeSerializer(), 'TinyDate')
     dbFile = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                           Config['DEFAULT']['LOCAL_DB_NAME'])
     super().__init__(dbFile, storage=serialization)