Skip to content

Commit

Permalink
Try to fix under MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Nov 15, 2024
1 parent d27a2e6 commit d9bdaac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
33 changes: 22 additions & 11 deletions docs/developer-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ helper class.

.. code-block:: python
import audbackend
import os
import shelve
import pickle
import audbackend
class UserDB:
r"""User database.
Expand All @@ -65,20 +67,24 @@ helper class.
"""
def __init__(self, backend: audbackend.backend.Base):
self.backend = backend
self.remote_file = "/.db.pkl"
self.local_file = audeer.path(".db.pkl")
def __enter__(self) -> shelve.Shelf:
if self.backend.exists("/user.db"):
self.backend.get_file("/user.db", "~.db")
self._map = shelve.open("~.db", flag="w", writeback=True)
if self.backend.exists(self.remote_file):
self.backend.get_file(self.remote_file, self.local_file)
if os.path.exists(self.local_file):
with open(self.local_file, "rb") as file:
self._map = pickle.load(file)
else:
self._map = shelve.open("~.db", writeback=True)
self._map = {}
return self._map
def __exit__(self, exc_type, exc_val, exc_tb):
self._map.close()
self.backend.put_file("~.db", "/user.db")
os.remove("~.db")
with open(self.local_file, "wb") as file:
pickle.dump(self._map, file, protocol=pickle.HIGHEST_PROTOCOL)
self.backend.put_file(self.local_file, self.remote_file)
os.remove(self.local_file)
Now,
we implement the interface.
Expand Down Expand Up @@ -176,9 +182,11 @@ in the constructor:

.. code-block:: python
import audbackend
import os
import audbackend
class SQLite(audbackend.backend.Base):
def __init__(
Expand All @@ -203,6 +211,7 @@ using a dedicated decorator:
import functools
def add_method(cls):
def decorator(func):
@functools.wraps(func)
Expand Down Expand Up @@ -249,6 +258,7 @@ stored on our backend:
import os
import sqlite3 as sl
@add_method(SQLite)
def _create(
self,
Expand Down Expand Up @@ -340,6 +350,7 @@ a file to our backend.
import datetime
import getpass
@add_method(SQLite)
def _put_file(
self,
Expand Down
2 changes: 2 additions & 0 deletions docs/legacy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ you have to list those extensions explicitly.
import audbackend
import audeer
host = audeer.mkdir("host")
audbackend.backend.FileSystem.create(host, "repo")
backend = audbackend.backend.FileSystem(host, "repo")
Expand All @@ -49,6 +50,7 @@ Afterwards we upload an TAR.GZ archive.
import tempfile
with tempfile.TemporaryDirectory() as tmp:
audeer.touch(audeer.path(tmp, "file.txt"))
interface.put_archive(tmp, "/file.tar.gz", "1.0.0")
Expand Down

0 comments on commit d9bdaac

Please sign in to comment.