Skip to content

sshfsdb

plantdb.commons.sshfsdb Link

This module implement a database as a file structure on a remote server using SSHFS.

SSHFSDB Link

SSHFSDB(basedir, remotedir=None)

Bases: FSDB

Subclass of FSDB that first mounts a remote directory using SSHFS.

Implementation of a database on a remote file system.

Attributes:

Name Type Description
basedir str

Path to the local directory where to mount the remote directory.

remotedir (str, optional)

Path to the remote directory containing the database. Should be in the format user@server:path

scans list

The list of Scan objects found in the database

is_connected bool

True if the DB is connected (locked the directory), else False.

Database constructor.

Mount remotedir directory on the basedir directory and load accessible Scan objects.

Parameters:

Name Type Description Default
basedir str

Path to local directory of the database

required
remotedir str

Path to the remote directory containing the database. Should be in the format user@server:path

None

Examples:

>>> # EXAMPLE 1: Use a temporary dummy database:
>>> from plantdb.commons.sshfsdb import SSHFSDB
>>> db = SSHFSDB("db", "someone@example.com:/data")
>>> print(db.basedir)
db
>>> print(db.remotedir)
someone@example.com:/data
>>> # Now connecting to this remote DB...
>>> db.connect()
>>> # ...allows to create new `Scan` in it:
>>> new_scan = db.create_scan("007")
>>> print(type(new_scan))
<class 'plantdb.commons.fsdb.core.Scan'>
>>> db.disconnect()
Source code in plantdb/commons/sshfsdb.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def __init__(self, basedir, remotedir=None):
    """Database constructor.

    Mount ``remotedir`` directory on the ``basedir`` directory and load accessible ``Scan`` objects.

    Parameters
    ----------
    basedir : str
        Path to local directory of the database
    remotedir : str, optional
        Path to the remote directory containing the database.
        Should be in the format ``user@server:path``

    Examples
    --------
    >>> # EXAMPLE 1: Use a temporary dummy database:
    >>> from plantdb.commons.sshfsdb import SSHFSDB
    >>> db = SSHFSDB("db", "someone@example.com:/data")
    >>> print(db.basedir)
    db
    >>> print(db.remotedir)
    someone@example.com:/data
    >>> # Now connecting to this remote DB...
    >>> db.connect()
    >>> # ...allows to create new `Scan` in it:
    >>> new_scan = db.create_scan("007")
    >>> print(type(new_scan))
    <class 'plantdb.commons.fsdb.core.Scan'>
    >>> db.disconnect()
    """
    super().__init__(basedir)
    self.remotedir = remotedir

connect Link

connect(login=None, unsafe=False)

Connect to the remote database.

Handle DB "locking" system by adding a LOCK_FILE_NAME file in the DB.

Parameters:

Name Type Description Default
login bool

UNUSED

None

Examples:

>>> from plantdb.commons.sshfsdb import SSHFSDB
>>> db = SSHFSDB("db", "someone@example.com:/data")
>>> print(db.is_connected)
False
>>> db.connect()
>>> print(db.is_connected)
True
Source code in plantdb/commons/sshfsdb.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def connect(self, login=None, unsafe=False):
    """Connect to the remote database.

    Handle DB "locking" system by adding a ``LOCK_FILE_NAME`` file in the DB.

    Parameters
    ----------
    login : bool
        UNUSED

    Examples
    --------
    >>> from plantdb.commons.sshfsdb import SSHFSDB
    >>> db = SSHFSDB("db", "someone@example.com:/data")
    >>> print(db.is_connected)
    False
    >>> db.connect()
    >>> print(db.is_connected)
    True
    """
    if not self.path().is_dir():
        self.path().mkdir(exist_ok=True)
    if self.remotedir is not None:
        cmd = ["sshfs", "-o", "idmap=user", self.remotedir, self.path()]
        logger.info(f"Connecting with: '{cmd}'")
        p = subprocess.run(cmd)
        logger.debug(f"The exit code was: {p.returncode}")
    super().connect()

disconnect Link

disconnect()

Disconnect from the database.

Handle DB "locking" system by removing the LOCK_FILE_NAME file from the DB.

Examples:

>>> from plantdb.commons.sshfsdb import SSHFSDB
>>> db = SSHFSDB("db", "someone@example.com:/data")
>>> print(db.is_connected)
False
>>> db.connect()
>>> print(db.is_connected)
True
>>> db.disconnect()
>>> print(db.is_connected)
False
Source code in plantdb/commons/sshfsdb.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def disconnect(self):
    """Disconnect from the database.

    Handle DB "locking" system by removing the ``LOCK_FILE_NAME`` file from the DB.

    Examples
    --------
    >>> from plantdb.commons.sshfsdb import SSHFSDB
    >>> db = SSHFSDB("db", "someone@example.com:/data")
    >>> print(db.is_connected)
    False
    >>> db.connect()
    >>> print(db.is_connected)
    True
    >>> db.disconnect()
    >>> print(db.is_connected)
    False
    """
    super().disconnect()
    p = subprocess.run(["fusermount", "-u", self.path()])
    print(f"The exit code was: {p.returncode}")