Skip to content

db

plantdb.commons.db Link

API for the database module in the ROMI project.

The following classes are defined:

  • A database DB contains a list of scans Scan distinguishable by their id.
  • A Scan can be made of several list of files Fileset.
  • A Fileset is made of a list of files Files.
  • A File can be an image, text of bytes.

It should be subclassed to implement an actual database interface.

DB Link

DB()

Bases: object

Class defining the database object DB.

Abstract class defining the API used to communicate with a database in the ROMI project.

Source code in plantdb/commons/db.py
47
48
def __init__(self):
    pass

connect Link

connect(login_data=None)

Connect to the database.

Parameters:

Name Type Description Default
login_data list or dict

Use this to access to a DB with credentials.

None

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
50
51
52
53
54
55
56
57
58
59
60
61
62
def connect(self, login_data=None):
    """Connect to the database.

    Parameters
    ----------
    login_data : list or dict, optional
        Use this to access to a ``DB`` with credentials.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

create_scan Link

create_scan(id)

Create a new scan object in the database.

Parameters:

Name Type Description Default
id str

Id of the scan to retrieve

required

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def create_scan(self, id):
    """Create a new scan object in the database.

    Parameters
    ----------
    id : str
        Id of the scan to retrieve

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

delete_scan Link

delete_scan(id)

Delete a scan from the DB.

Parameters:

Name Type Description Default
id str

Id of the scan to delete

required

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
112
113
114
115
116
117
118
119
120
121
122
123
124
def delete_scan(self, id):
    """Delete a scan from the DB.

    Parameters
    ----------
    id : str
        Id of the scan to delete

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

disconnect Link

disconnect()

Disconnect from the database.

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
64
65
66
67
68
69
70
71
def disconnect(self):
    """Disconnect from the database.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_scan Link

get_scan(id, create=False)

Get a scan saved in the database.

Parameters:

Name Type Description Default
id str

Id of the scan instance to retrieve.

required
create bool

Create the scan if it does not exist, default to False.

False

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def get_scan(self, id, create=False):
    """Get a scan saved in the database.

    Parameters
    ----------
    id : str
        Id of the scan instance to retrieve.
    create : bool, optional
        Create the scan if it does not exist, default to ``False``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_scans Link

get_scans()

Get the list of scans saved in the database.

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
73
74
75
76
77
78
79
80
def get_scans(self):
    """Get the list of scans saved in the database.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

DBBusyError Link

DBBusyError(message)

Bases: OSError

Raises an error if the database is busy.

This error is raised when the database is busy and an operation cannot be done on it.

Source code in plantdb/commons/db.py
608
609
def __init__(self, message):
    self.message = message

File Link

File(fileset, id, **kwargs)

Bases: object

Class defining a file File contained in a Fileset.

Abstract class defining the API used to represent a file in the ROMI project.

Attributes:

Name Type Description
db DB

Database instance where to find the scan, fileset and file.

scan Scan

Scan instance containing the fileset and file.

fileset Fileset

Fileset instance containing the file.

id str

Id of the file instance.

Constructor.

Parameters:

Name Type Description Default
fileset Fileset

Instance containing the file.

required
id str

Id of the file instance.

required

Other Parameters:

Name Type Description
ext str

The extension of the file.

Source code in plantdb/commons/db.py
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
def __init__(self, fileset, id, **kwargs):
    """Constructor.

    Parameters
    ----------
    fileset : plantdb.commons.db.Fileset
        Instance containing the file.
    id : str
        Id of the file instance.

    Other Parameters
    ----------------
    ext : str
        The extension of the file.
    """
    self.db = fileset.get_db()
    self.scan = fileset.get_scan()
    self.fileset = fileset
    self.id = id
    ext = kwargs.get('ext', '')
    self.filename = id + ext if ext != '' else None

get_db Link

get_db()

Get parent database instance.

Returns:

Type Description
DB

The parent database instance.

Source code in plantdb/commons/db.py
466
467
468
469
470
471
472
473
474
def get_db(self):
    """Get parent database instance.

    Returns
    -------
    plantdb.commons.db.DB
        The parent database instance.
    """
    return self.db

get_fileset Link

get_fileset()

Get parent fileset.

Returns:

Type Description
Fileset

The parent fileset instance.

Source code in plantdb/commons/db.py
486
487
488
489
490
491
492
493
494
def get_fileset(self):
    """Get parent fileset.

    Returns
    -------
    plantdb.commons.db.Fileset
        The parent fileset instance.
    """
    return self.fileset

get_id Link

get_id()

Get file id.

Returns:

Type Description
str

The id of the file instance.

Source code in plantdb/commons/db.py
456
457
458
459
460
461
462
463
464
def get_id(self):
    """Get file id.

    Returns
    -------
    str
        The id of the file instance.
    """
    return deepcopy(self.id)

get_metadata Link

get_metadata(key=None, default=None)

Get metadata associated to scan.

Parameters:

Name Type Description Default
key str

Metadata key to retrieve (defaults to None)

None
default Any

The default value to return if the key do not exist in the metadata. Default is None.

None

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
def get_metadata(self, key=None, default=None):
    """Get metadata associated to scan.

    Parameters
    ----------
    key : str, optional
        Metadata key to retrieve (defaults to ``None``)
    default : Any, optional
        The default value to return if the key do not exist in the metadata.
        Default is ``None``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_scan Link

get_scan()

Get parent scan instance.

Returns:

Type Description
Scan

The parent scan instance.

Source code in plantdb/commons/db.py
476
477
478
479
480
481
482
483
484
def get_scan(self):
    """Get parent scan instance.

    Returns
    -------
    plantdb.commons.db.Scan
        The parent scan instance.
    """
    return self.scan

import_file Link

import_file(path)

Import an existing file to the File object.

Parameters:

Name Type Description Default
path str

Path of the file to import

required

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
532
533
534
535
536
537
538
539
540
541
542
543
544
def import_file(self, path):
    """Import an existing file to the ``File`` object.

    Parameters
    ----------
    path : str
        Path of the file to import

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

read Link

read()

Reads from a file.

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
592
593
594
595
596
597
598
599
def read(self):
    """Reads from a file.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

read_raw Link

read_raw()

Reads bytes from a file.

Returns:

Type Description
bytearray

File buffer

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
562
563
564
565
566
567
568
569
570
571
572
573
574
def read_raw(self):
    """Reads bytes from a file.

    Returns
    -------
    bytearray
        File buffer

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

set_metadata Link

set_metadata(data, value=None)

Get metadata associated to scan.

If value is None, scan metadata is set to data. If value is not None data is a key and is set to value.

Parameters:

Name Type Description Default
data str or dict

Key or value to set as metadata

required
value any

Value to set (default is None)

None

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
def set_metadata(self, data, value=None):
    """Get metadata associated to scan.

    If value is ``None``, scan metadata is set to data.
    If value is not ``None`` data is a key and is set to value.

    Parameters
    ----------
    data : str or dict
        Key or value to set as metadata
    value : any, optional
        Value to set (default is ``None``)

    Raises
    ------
    NotImplementedError
   """
    raise NotImplementedError

write Link

write(str, ext='')

Writes to a file.

Parameters:

Name Type Description Default
data str

Data to write

required
ext str

File extension to use

''

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
def write(self, str, ext=""):
    """Writes to a file.

    Parameters
    ----------
    data : str
        Data to write
    ext : str, optional
        File extension to use

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

write_raw Link

write_raw(buffer, ext='')

Writes bytes to a file.

Parameters:

Name Type Description Default
buffer bytearray

Data to write

required
ext str

File extension to use

''

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
def write_raw(self, buffer, ext=""):
    """Writes bytes to a file.

    Parameters
    ----------
    buffer : bytearray
        Data to write
    ext : str, optional
        File extension to use

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

Fileset Link

Fileset(scan, id)

Bases: object

Class defining a set of files Fileset contained in a Scan.

Abstract class defining the API used to represent a set of files in the ROMI project.

Notes

Files can be 2D images, RGB pictures, text,...

Attributes:

Name Type Description
db DB

Database instance where to find the scan and fileset.

scan Scan

Scan instance containing the fileset.

id int

Id of the fileset instance.

Constructor.

Parameters:

Name Type Description Default
scan Scan

Scan instance containing the fileset.

required
id str

Id of the fileset instance.

required
Source code in plantdb/commons/db.py
283
284
285
286
287
288
289
290
291
292
293
294
295
def __init__(self, scan, id):
    """Constructor.

    Parameters
    ----------
    scan : plantdb.commons.db.Scan
        Scan instance containing the fileset.
    id : str
        Id of the fileset instance.
    """
    self.db = scan.get_db()
    self.scan = scan
    self.id = id

create_file Link

create_file(id)

Create a file.

Parameters:

Name Type Description Default
id str

Id of the new file.

required

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
388
389
390
391
392
393
394
395
396
397
398
399
400
def create_file(self, id):
    """Create a file.

    Parameters
    ----------
    id : str
        Id of the new file.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

delete_file Link

delete_file(file_id)

Delete a file.

Parameters:

Name Type Description Default
id str

Id of the file to delete.

required

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
402
403
404
405
406
407
408
409
410
411
412
413
414
def delete_file(self, file_id):
    """Delete a file.

    Parameters
    ----------
    id : str
        Id of the file to delete.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_db Link

get_db()

Get parent database instance.

Returns:

Type Description
DB

The parent database instance.

Source code in plantdb/commons/db.py
307
308
309
310
311
312
313
314
315
def get_db(self):
    """Get parent database instance.

    Returns
    -------
    plantdb.commons.db.DB
        The parent database instance.
    """
    return self.db

get_file Link

get_file(id, create=False)

Get file with given id.

Parameters:

Name Type Description Default
id str

File instance id.

required
create bool

Create the file if it does not exist, default to False.

False

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
def get_file(self, id, create=False):
    """Get file with given id.

    Parameters
    ----------
    id : str
        File instance id.
    create : bool, optional
        Create the file if it does not exist, default to ``False``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_files Link

get_files()

Get all files.

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
327
328
329
330
331
332
333
334
def get_files(self):
    """Get all files.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_id Link

get_id()

Get the fileset instance id.

Returns:

Type Description
str

The id of the fileset instance.

Source code in plantdb/commons/db.py
297
298
299
300
301
302
303
304
305
def get_id(self):
    """Get the fileset instance id.

    Returns
    -------
    str
        The id of the fileset instance.
    """
    return deepcopy(self.id)

get_metadata Link

get_metadata(key=None, default=None)

Get metadata associated to scan.

Parameters:

Name Type Description Default
key str

Metadata key to retrieve, default to None.

None
default Any

The default value to return if the key do not exist in the metadata. Default is None.

None

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
def get_metadata(self, key=None, default=None):
    """Get metadata associated to scan.

    Parameters
    ----------
    key : str
        Metadata key to retrieve, default to ``None``.
    default : Any, optional
        The default value to return if the key do not exist in the metadata.
        Default is ``None``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_scan Link

get_scan()

Get parent scan instance.

Returns:

Type Description
Scan

The parent scan instance.

Source code in plantdb/commons/db.py
317
318
319
320
321
322
323
324
325
def get_scan(self):
    """Get parent scan instance.

    Returns
    -------
    plantdb.commons.db.Scan
        The parent scan instance.
    """
    return self.scan

set_metadata Link

set_metadata(data, value=None)

Get metadata associated to scan.

If value is None, scan metadata is set to data. If value is not None data is a key and is set to value.

Parameters:

Name Type Description Default
data str or dict

Key or value to set as metadata.

required
value any

Value to set, default to None.

None

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
def set_metadata(self, data, value=None):
    """Get metadata associated to scan.

    If value is ``None``, scan metadata is set to data.
    If value is not ``None`` data is a key and is set to value.

    Parameters
    ----------
    data : str or dict
        Key or value to set as metadata.
    value : any, optional
        Value to set, default to ``None``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

Scan Link

Scan(db, id)

Bases: object

Class defining the scan object Scan.

Abstract class defining the API used to represent a scan in the ROMI project.

Attributes:

Name Type Description
db DB

Database instance where to find the scan.

id int

Id of the scan instance.

Constructor.

Parameters:

Name Type Description Default
db DB

Database instance where to find the scan.

required
id str

Id of the scan instance.

required
Source code in plantdb/commons/db.py
140
141
142
143
144
145
146
147
148
149
150
151
def __init__(self, db, id):
    """Constructor.

    Parameters
    ----------
    db : plantdb.commons.db.DB
        Database instance where to find the scan.
    id : str
        Id of the scan instance.
    """
    self.db = db
    self.id = id

create_fileset Link

create_fileset(id)

Create a fileset.

Parameters:

Name Type Description Default
id str

Id of the new fileset.

required

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
234
235
236
237
238
239
240
241
242
243
244
245
246
def create_fileset(self, id):
    """Create a fileset.

    Parameters
    ----------
    id : str
        Id of the new fileset.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

delete_fileset Link

delete_fileset(fileset_id)

Delete a fileset from the DB.

Parameters:

Name Type Description Default
id str

Id of the fileset to delete.

required

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
248
249
250
251
252
253
254
255
256
257
258
259
260
def delete_fileset(self, fileset_id):
    """Delete a fileset from the DB.

    Parameters
    ----------
    id : str
        Id of the fileset to delete.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_db Link

get_db()

Get parent database instance.

Returns:

Type Description
DB

Database instance where to find the scan.

Source code in plantdb/commons/db.py
163
164
165
166
167
168
169
170
171
def get_db(self):
    """Get parent database instance.

    Returns
    -------
    plantdb.commons.db.DB
        Database instance where to find the scan.
    """
    return self.db

get_fileset Link

get_fileset(id, create=False)

Get a fileset with a given id.

Parameters:

Name Type Description Default
id str

Id of the fileset to be retrieved.

required
create bool

Create the fileset if it does not exist, default to False.

False

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
def get_fileset(self, id, create=False):
    """Get a fileset with a given id.

    Parameters
    ----------
    id : str
        Id of the fileset to be retrieved.
    create : bool, optional
        Create the fileset if it does not exist, default to ``False``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_filesets Link

get_filesets()

Get all sets of files.

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
173
174
175
176
177
178
179
180
def get_filesets(self):
    """Get all sets of files.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

get_id Link

get_id()

Get the scan instance id.

Returns:

Type Description
str

Id of the scan instance.

Source code in plantdb/commons/db.py
153
154
155
156
157
158
159
160
161
def get_id(self):
    """Get the scan instance id.

    Returns
    -------
    str
        Id of the scan instance.
    """
    return deepcopy(self.id)

get_metadata Link

get_metadata(key=None, default=None)

Get metadata associated to scan.

Parameters:

Name Type Description Default
key str

Metadata key to retrieve, default to None.

None
default Any

The default value to return if the key do not exist in the metadata. Default is None.

None

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def get_metadata(self, key=None, default=None):
    """Get metadata associated to scan.

    Parameters
    ----------
    key : str
        Metadata key to retrieve, default to ``None``.
    default : Any, optional
        The default value to return if the key do not exist in the metadata.
        Default is ``None``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError

set_metadata Link

set_metadata(data, value=None)

Get metadata associated to scan.

If value is None, scan metadata is set to data. If value is not None data is a key and is set to value.

Parameters:

Name Type Description Default
data str or dict

Key or value.

required
value any

Value to set, default to None.

None

Raises:

Type Description
NotImplementedError
Source code in plantdb/commons/db.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def set_metadata(self, data, value=None):
    """Get metadata associated to scan.

    If value is ``None``, scan metadata is set to data.
    If value is not ``None`` data is a key and is set to value.

    Parameters
    ----------
    data : str or dict
        Key or value.
    value : any, optional
        Value to set, default to ``None``.

    Raises
    ------
    NotImplementedError
    """
    raise NotImplementedError