Skip to content

pid_record

PIDRecord

" This class represents a PID record with a PID and entries. For more information on the PID record format, see the documentation of the Typed PID Maker (https://kit-data-manager.github.io/webpage/typed-pid-maker/openapi.html)

Attributes:

Name Type Description
_pid str

The PID of the PID record

_entries dict[str, list[PIDRecordEntry]]

The entries of the PID record. The entries are stored in a dictionary with the key as the key of the entry and the value as a list of values for the entry. Each value is a dictionary with the key "value" and the value of the entry as the value. The value can also be accessed with the key "@value"

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 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
 92
 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
class PIDRecord:
    """ "
    This class represents a PID record with a PID and entries.
    For more information on the PID record format, see the documentation of the Typed PID Maker (https://kit-data-manager.github.io/webpage/typed-pid-maker/openapi.html)

    Attributes:
        _pid (str): The PID of the PID record
        _entries (dict[str, list[PIDRecordEntry]]): The entries of the PID record. The entries are stored in a dictionary with the key as the key of the entry and the value as a list of values for the entry. Each value is a dictionary with the key "value" and the value of the entry as the value. The value can also be accessed with the key "@value"
    """

    _pid: str
    _entries: dict[str, list[PIDRecordEntry]]

    def __init__(self, pid: str, entries: list[PIDRecordEntry] = None):
        """
        Creates a PID record

        Args:
            pid (str): The PID of the PID record
            entries (list[PIDRecordEntry]): The entries of the PID record (optional) Entries is a dictionary with the key as the key of the entry and the value as a list of values for the entry. Each value is a dictionary with the key "value" and the value of the entry as the value. The value can also be accessed with the key "@value"

        Raises:
            ValueError: If the PID is None
        """
        if pid is None:
            raise ValueError("PID must not be None")

        self._pid = pid

        self._entries = {}
        if entries is not None and isinstance(
            entries, list
        ):  # Check if entries is not None and a list
            for entry in entries:
                if isinstance(entry, PIDRecordEntry):
                    self.addPIDRecordEntry(entry)
                elif isinstance(entry, dict) and "key" in entry and "value" in entry:
                    self.addEntry(
                        entry["key"],
                        entry["value"],
                        entry["name"] if "name" in entry else None,
                    )

    def addPIDRecordEntry(self, entry: PIDRecordEntry):
        """
        Adds a PID record entry to the PID record

        Args:
            entry (PIDRecordEntry): The PID record entry to add

        Raises:
            ValueError: If the key of the entry is None or the value of the entry is None
        """

        if entry.key is None:  # Check if the key is None
            raise ValueError("Key must not be None")
        if entry.value is None:  # Check if the value is None
            raise ValueError("Value must not be None")

        if (
            entry.key not in self._entries
        ):  # Check if the key is not already in the PID record
            logger.debug(f"Adding entry {entry} to PID record")
            self._entries[entry.key] = [entry]  # Add the entry to the PID record
        elif isinstance(
            self._entries[entry.key], list
        ):  # Check if the entry is already a list
            if not any(
                e.value == entry.value
                for e in self._entries[
                    entry.key
                ]  # Check if the entry value is already in the list
            ):
                logger.debug(
                    f"Adding entry {entry} to PID record. Entry with key {entry.key} already exists. Adding to list"
                )
                self._entries[entry.key].append(
                    entry
                )  # Add the entry to the list iff the value is not already in the list
            logger.debug(
                f"Entry with key {entry.key} and value {entry.value} already exists. Skipping"
            )
        else:  # If the entry is not a list
            logger.debug(
                f"Adding entry {entry} to PID record. Entry with key {entry.key} already exists. Converting to list"
            )
            self._entries[entry.key] = [
                self._entries[entry.key],
                entry,
            ]  # Convert the entry to a list

    def addEntry(self, key: str, value: str | dict, name: str = None):
        """
        Adds an entry to the PID record
        If the entry already exists, it is not added again (no duplicates)

        Args:
            key (str): The key of the entry
            value (str|dict): The value of the entry
            name (str): The name of the entry (optional)

        Raises:
            ValueError: If the key is None or the values are None
        """

        entry = PIDRecordEntry(key, value, name)  # Create a PIDRecordEntry object
        self.addPIDRecordEntry(entry)  # Add the PIDRecordEntry object to the PID record

    def addListOfEntries(self, entries: list[PIDRecordEntry]):
        """
        Adds multiple PID record entries to the PID record

        Args:
            entries (list[PIDRecordEntry]): The PID record entries to add

        Raises:
            ValueError: If the entries are None
        """

        if entries is None:
            raise ValueError("Entries must not be None")

        for entry in entries:  # Add each entry to the PID record
            self.addPIDRecordEntry(entry)

    def addEntries(self, key: str, values: list[str], name: str = None):
        """
        Adds multiple entries to the PID record

        Args:
            key (str): The key of the entries
            values (list[str]): The values of the entries. All values are added to the PID record with the same key.
            name (str): The name of the entries (optional)

        Raises:
            ValueError: If the key is None or the values are None
        """
        if key is None:  # Check if the key is None
            raise ValueError("Key must not be None")

        if values is None:  # Check if the values are None
            raise ValueError("Values must not be None")

        for value in values:  # Add each value to the PID record
            self.addEntry(key, value, name)

    def updateEntry(self, key: str, value: str | dict, name: str = None):
        """
        Updates an entry in the PID record
        If the entry does not exist, it is added

        Args:
            key (str): The key of the entry
            value (str|dict): The value of the entry. If the value is a dictionary, it is converted to a string internally.
            name (str): The name of the entry (optional)

        Raises:
            ValueError: If the key is None
        """
        entry = PIDRecordEntry(key, value, name)  # Create a PIDRecordEntry object
        self.deleteEntry(key)  # Delete the entry with the given key
        self.addPIDRecordEntry(entry)  # Add the PIDRecordEntry object to the PID record

    def getEntries(self) -> dict:
        """
        Returns the entries of the PID record

        Returns:
            dict: The entries of the PID record
        """
        return self._entries

    def getPID(self) -> str:
        """
        Returns the PID of the PID record

        Returns:
            str: The PID of the PID record
        """
        return self._pid

    def getEntry(self, key: str) -> list[PIDRecordEntry] | PIDRecordEntry | None:
        """
        Returns all entries with the given key

        Args:
            key (str): The key of the entries

        Returns:
            list[PIDRecordEntry]: The entries with the given key
            PIDRecordEntry: The entry with the given key if only one entry is found
            None: If no entry is found

        Raises:
            ValueError: If the key is None
        """
        if key is None:  # Check if the key is None
            raise ValueError("Key must not be None")

        if key in self._entries:  # Check if the key is in the PID record
            return self._entries[key]
        else:  # If the key is not in the PID record
            return None

    def deleteEntry(self, key: str, value: str | dict = None):
        """
        Deletes an entry from the PID record

        Args:
            key (str): The key of the entry
            value (str|dict): The value of the entry (optional) If the value is None, all entries with the given key are deleted. If the value is not None, only the entry with the given key and value is deleted.

        Raises:
            ValueError: If the key is None
        """
        if key is None:  # Check if the key is None
            raise ValueError("Key must not be None")

        if key in self._entries:
            if value is None:  # Delete all entries with the given key
                del self._entries[key]
            else:
                self._entries[key] = [
                    entry for entry in self._entries[key] if entry["value"] != value
                ]

    def deleteAllEntries(self):
        """
        Deletes all entries from the PID record
        """
        self._entries = {}

    def entryExists(self, key: str, value: str | dict = None) -> bool:
        """
        Checks if an entry exists

        Args:
            key (str): The key of the entry
            value (str|dict): The value of the entry (optional) If the value is None, the method checks if an entry with the given key exists. If the value is not None, the method checks if an entry with the given key and value exists.

        Returns:
            bool: True if the entry exists, False otherwise

        Raises:
            ValueError: If the key is None
        """
        if key is None:  # Check if the key is None
            raise ValueError("Key must not be None")

        if key in self._entries:  # Check if the key is in the PID record
            if value is None:  # Check if the value argument is not specified (None)
                return True
            else:  # If the value argument is specified, check if the value is in the list of entries
                return any(entry["value"] == value for entry in self._entries[key])
        else:  # If the key is not in the PID record
            return False

    def toJSON(self) -> dict:
        """
        Exports the PID record as JSON object

        Returns:
            dict: The PID record as JSON object
        """
        entries = {}

        for key, value in self._entries.items():  # Iterate over all entries
            entries[key] = [
                entry.toJSON() for entry in value
            ]  # Convert the entries to JSON

        return {"pid": self._pid, "entries": entries}

    def exportSimpleFormatJSON(self) -> dict:
        """
        Exports the PID record as a simple JSON object

        Returns:
            dict: The PID record as a simple JSON object
        """
        kv_pairs = []

        for key, value in self._entries.items():  # Iterate over all entries
            for entry in value:  # Iterate over all values of the entry
                kv_pairs.append(
                    {"key": key, "value": entry["value"]}
                )  # Add the key and value to the list

        return {"pid": self._pid, "record": kv_pairs}

    @staticmethod
    def fromJSON(input_json: dict) -> "PIDRecord":
        """
        Creates a PID record from a JSON object

        Args:
            input_json (dict): The JSON object to create the PID record from

        Returns:
            PIDRecord: The PID record created from the JSON object

        Raises:
            ValueError: If the JSON object is None or invalid
        """
        logger.debug("Trying to extract PID record from JSON", input_json)

        if input_json is None:  # Check if the JSON object is None
            raise ValueError("JSON must not be None")

        if "pid" not in input_json:  # Check if the JSON object contains a PID
            raise ValueError("PID must be in JSON object")

        if "entries" not in input_json:  # Check if the JSON object contains entries
            return PIDRecord(input_json["pid"])
        else:
            entries = []

            for key, value in input_json[
                "entries"
            ].items():  # Iterate over all entries in the JSON object
                for entry in value:  # Iterate over all values of the entry. Each value consists of a dict with the keys "key", "value" and "name". Name is the only optional key.
                    if "value" not in entry or "key" not in entry:
                        # Skip this entry if it does not contain a key or value
                        logger.warning(
                            f"Skipping entry {entry} because it does not contain a key or value"
                        )
                        continue
                    elif "name" in entry:
                        # If the entry contains a name, add it to the PIDRecordEntry
                        entries.append(
                            PIDRecordEntry(key, entry["value"], entry["name"])
                        )
                    else:
                        # If the entry does not contain a name, add it without a name
                        entries.append(PIDRecordEntry(key, entry["value"]))

            logger.debug(
                f"Extracted PID record from JSON: {PIDRecord(input_json['pid'], entries)}"
            )
            return PIDRecord(input_json["pid"], entries)

    def merge(self, other: "PIDRecord") -> "PIDRecord":
        """
        Merges the PID record with another PID record

        Args:
            other (PIDRecord): The PID record to merge with

        Returns:
            PIDRecord: The merged PID record

        Raises:
            ValueError: If the other PID record is None
        """
        if other is None:  # Check if the other PID record is None
            raise ValueError("Other PID record must not be None")

        if (
            self._pid != other.getPID()
        ):  # Check if the PID of both PID records is the same
            raise ValueError("PID of both PID records must be the same")

        for (
            key,
            value,
        ) in (
            other.getEntries().items()
        ):  # Iterate over all entries in the other PID record
            for entry in value:  # Iterate over all values of the entry
                if not self.entryExists(
                    key, entry.value
                ):  # Check if the entry does not exist in this PID record
                    self.addPIDRecordEntry(entry)  # Add the entry to this PID record

        return self  # Return the merged PID record

    def __str__(self):
        return f"PIDRecord(pid={self._pid}, entries={self._entries})"

    def __repr__(self):
        return str(self.toJSON())

    def __dict__(self):
        return self.toJSON()

__init__

__init__(pid: str, entries: list[PIDRecordEntry] = None)

Creates a PID record

Parameters:

Name Type Description Default
pid str

The PID of the PID record

required
entries list[PIDRecordEntry]

The entries of the PID record (optional) Entries is a dictionary with the key as the key of the entry and the value as a list of values for the entry. Each value is a dictionary with the key "value" and the value of the entry as the value. The value can also be accessed with the key "@value"

None

Raises:

Type Description
ValueError

If the PID is None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def __init__(self, pid: str, entries: list[PIDRecordEntry] = None):
    """
    Creates a PID record

    Args:
        pid (str): The PID of the PID record
        entries (list[PIDRecordEntry]): The entries of the PID record (optional) Entries is a dictionary with the key as the key of the entry and the value as a list of values for the entry. Each value is a dictionary with the key "value" and the value of the entry as the value. The value can also be accessed with the key "@value"

    Raises:
        ValueError: If the PID is None
    """
    if pid is None:
        raise ValueError("PID must not be None")

    self._pid = pid

    self._entries = {}
    if entries is not None and isinstance(
        entries, list
    ):  # Check if entries is not None and a list
        for entry in entries:
            if isinstance(entry, PIDRecordEntry):
                self.addPIDRecordEntry(entry)
            elif isinstance(entry, dict) and "key" in entry and "value" in entry:
                self.addEntry(
                    entry["key"],
                    entry["value"],
                    entry["name"] if "name" in entry else None,
                )

addPIDRecordEntry

addPIDRecordEntry(entry: PIDRecordEntry)

Adds a PID record entry to the PID record

Parameters:

Name Type Description Default
entry PIDRecordEntry

The PID record entry to add

required

Raises:

Type Description
ValueError

If the key of the entry is None or the value of the entry is None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 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
def addPIDRecordEntry(self, entry: PIDRecordEntry):
    """
    Adds a PID record entry to the PID record

    Args:
        entry (PIDRecordEntry): The PID record entry to add

    Raises:
        ValueError: If the key of the entry is None or the value of the entry is None
    """

    if entry.key is None:  # Check if the key is None
        raise ValueError("Key must not be None")
    if entry.value is None:  # Check if the value is None
        raise ValueError("Value must not be None")

    if (
        entry.key not in self._entries
    ):  # Check if the key is not already in the PID record
        logger.debug(f"Adding entry {entry} to PID record")
        self._entries[entry.key] = [entry]  # Add the entry to the PID record
    elif isinstance(
        self._entries[entry.key], list
    ):  # Check if the entry is already a list
        if not any(
            e.value == entry.value
            for e in self._entries[
                entry.key
            ]  # Check if the entry value is already in the list
        ):
            logger.debug(
                f"Adding entry {entry} to PID record. Entry with key {entry.key} already exists. Adding to list"
            )
            self._entries[entry.key].append(
                entry
            )  # Add the entry to the list iff the value is not already in the list
        logger.debug(
            f"Entry with key {entry.key} and value {entry.value} already exists. Skipping"
        )
    else:  # If the entry is not a list
        logger.debug(
            f"Adding entry {entry} to PID record. Entry with key {entry.key} already exists. Converting to list"
        )
        self._entries[entry.key] = [
            self._entries[entry.key],
            entry,
        ]  # Convert the entry to a list

addEntry

addEntry(key: str, value: str | dict, name: str = None)

Adds an entry to the PID record If the entry already exists, it is not added again (no duplicates)

Parameters:

Name Type Description Default
key str

The key of the entry

required
value str | dict

The value of the entry

required
name str

The name of the entry (optional)

None

Raises:

Type Description
ValueError

If the key is None or the values are None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def addEntry(self, key: str, value: str | dict, name: str = None):
    """
    Adds an entry to the PID record
    If the entry already exists, it is not added again (no duplicates)

    Args:
        key (str): The key of the entry
        value (str|dict): The value of the entry
        name (str): The name of the entry (optional)

    Raises:
        ValueError: If the key is None or the values are None
    """

    entry = PIDRecordEntry(key, value, name)  # Create a PIDRecordEntry object
    self.addPIDRecordEntry(entry)  # Add the PIDRecordEntry object to the PID record

addListOfEntries

addListOfEntries(entries: list[PIDRecordEntry])

Adds multiple PID record entries to the PID record

Parameters:

Name Type Description Default
entries list[PIDRecordEntry]

The PID record entries to add

required

Raises:

Type Description
ValueError

If the entries are None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def addListOfEntries(self, entries: list[PIDRecordEntry]):
    """
    Adds multiple PID record entries to the PID record

    Args:
        entries (list[PIDRecordEntry]): The PID record entries to add

    Raises:
        ValueError: If the entries are None
    """

    if entries is None:
        raise ValueError("Entries must not be None")

    for entry in entries:  # Add each entry to the PID record
        self.addPIDRecordEntry(entry)

addEntries

addEntries(key: str, values: list[str], name: str = None)

Adds multiple entries to the PID record

Parameters:

Name Type Description Default
key str

The key of the entries

required
values list[str]

The values of the entries. All values are added to the PID record with the same key.

required
name str

The name of the entries (optional)

None

Raises:

Type Description
ValueError

If the key is None or the values are None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def addEntries(self, key: str, values: list[str], name: str = None):
    """
    Adds multiple entries to the PID record

    Args:
        key (str): The key of the entries
        values (list[str]): The values of the entries. All values are added to the PID record with the same key.
        name (str): The name of the entries (optional)

    Raises:
        ValueError: If the key is None or the values are None
    """
    if key is None:  # Check if the key is None
        raise ValueError("Key must not be None")

    if values is None:  # Check if the values are None
        raise ValueError("Values must not be None")

    for value in values:  # Add each value to the PID record
        self.addEntry(key, value, name)

updateEntry

updateEntry(key: str, value: str | dict, name: str = None)

Updates an entry in the PID record If the entry does not exist, it is added

Parameters:

Name Type Description Default
key str

The key of the entry

required
value str | dict

The value of the entry. If the value is a dictionary, it is converted to a string internally.

required
name str

The name of the entry (optional)

None

Raises:

Type Description
ValueError

If the key is None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def updateEntry(self, key: str, value: str | dict, name: str = None):
    """
    Updates an entry in the PID record
    If the entry does not exist, it is added

    Args:
        key (str): The key of the entry
        value (str|dict): The value of the entry. If the value is a dictionary, it is converted to a string internally.
        name (str): The name of the entry (optional)

    Raises:
        ValueError: If the key is None
    """
    entry = PIDRecordEntry(key, value, name)  # Create a PIDRecordEntry object
    self.deleteEntry(key)  # Delete the entry with the given key
    self.addPIDRecordEntry(entry)  # Add the PIDRecordEntry object to the PID record

getEntries

getEntries() -> dict

Returns the entries of the PID record

Returns:

Name Type Description
dict dict

The entries of the PID record

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
191
192
193
194
195
196
197
198
def getEntries(self) -> dict:
    """
    Returns the entries of the PID record

    Returns:
        dict: The entries of the PID record
    """
    return self._entries

getPID

getPID() -> str

Returns the PID of the PID record

Returns:

Name Type Description
str str

The PID of the PID record

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
200
201
202
203
204
205
206
207
def getPID(self) -> str:
    """
    Returns the PID of the PID record

    Returns:
        str: The PID of the PID record
    """
    return self._pid

getEntry

getEntry(
    key: str,
) -> list[PIDRecordEntry] | PIDRecordEntry | None

Returns all entries with the given key

Parameters:

Name Type Description Default
key str

The key of the entries

required

Returns:

Name Type Description
list[PIDRecordEntry] | PIDRecordEntry | None

list[PIDRecordEntry]: The entries with the given key

PIDRecordEntry list[PIDRecordEntry] | PIDRecordEntry | None

The entry with the given key if only one entry is found

None list[PIDRecordEntry] | PIDRecordEntry | None

If no entry is found

Raises:

Type Description
ValueError

If the key is None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
def getEntry(self, key: str) -> list[PIDRecordEntry] | PIDRecordEntry | None:
    """
    Returns all entries with the given key

    Args:
        key (str): The key of the entries

    Returns:
        list[PIDRecordEntry]: The entries with the given key
        PIDRecordEntry: The entry with the given key if only one entry is found
        None: If no entry is found

    Raises:
        ValueError: If the key is None
    """
    if key is None:  # Check if the key is None
        raise ValueError("Key must not be None")

    if key in self._entries:  # Check if the key is in the PID record
        return self._entries[key]
    else:  # If the key is not in the PID record
        return None

deleteEntry

deleteEntry(key: str, value: str | dict = None)

Deletes an entry from the PID record

Parameters:

Name Type Description Default
key str

The key of the entry

required
value str | dict

The value of the entry (optional) If the value is None, all entries with the given key are deleted. If the value is not None, only the entry with the given key and value is deleted.

None

Raises:

Type Description
ValueError

If the key is None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def deleteEntry(self, key: str, value: str | dict = None):
    """
    Deletes an entry from the PID record

    Args:
        key (str): The key of the entry
        value (str|dict): The value of the entry (optional) If the value is None, all entries with the given key are deleted. If the value is not None, only the entry with the given key and value is deleted.

    Raises:
        ValueError: If the key is None
    """
    if key is None:  # Check if the key is None
        raise ValueError("Key must not be None")

    if key in self._entries:
        if value is None:  # Delete all entries with the given key
            del self._entries[key]
        else:
            self._entries[key] = [
                entry for entry in self._entries[key] if entry["value"] != value
            ]

deleteAllEntries

deleteAllEntries()

Deletes all entries from the PID record

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
254
255
256
257
258
def deleteAllEntries(self):
    """
    Deletes all entries from the PID record
    """
    self._entries = {}

entryExists

entryExists(key: str, value: str | dict = None) -> bool

Checks if an entry exists

Parameters:

Name Type Description Default
key str

The key of the entry

required
value str | dict

The value of the entry (optional) If the value is None, the method checks if an entry with the given key exists. If the value is not None, the method checks if an entry with the given key and value exists.

None

Returns:

Name Type Description
bool bool

True if the entry exists, False otherwise

Raises:

Type Description
ValueError

If the key is None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
def entryExists(self, key: str, value: str | dict = None) -> bool:
    """
    Checks if an entry exists

    Args:
        key (str): The key of the entry
        value (str|dict): The value of the entry (optional) If the value is None, the method checks if an entry with the given key exists. If the value is not None, the method checks if an entry with the given key and value exists.

    Returns:
        bool: True if the entry exists, False otherwise

    Raises:
        ValueError: If the key is None
    """
    if key is None:  # Check if the key is None
        raise ValueError("Key must not be None")

    if key in self._entries:  # Check if the key is in the PID record
        if value is None:  # Check if the value argument is not specified (None)
            return True
        else:  # If the value argument is specified, check if the value is in the list of entries
            return any(entry["value"] == value for entry in self._entries[key])
    else:  # If the key is not in the PID record
        return False

toJSON

toJSON() -> dict

Exports the PID record as JSON object

Returns:

Name Type Description
dict dict

The PID record as JSON object

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def toJSON(self) -> dict:
    """
    Exports the PID record as JSON object

    Returns:
        dict: The PID record as JSON object
    """
    entries = {}

    for key, value in self._entries.items():  # Iterate over all entries
        entries[key] = [
            entry.toJSON() for entry in value
        ]  # Convert the entries to JSON

    return {"pid": self._pid, "entries": entries}

exportSimpleFormatJSON

exportSimpleFormatJSON() -> dict

Exports the PID record as a simple JSON object

Returns:

Name Type Description
dict dict

The PID record as a simple JSON object

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
def exportSimpleFormatJSON(self) -> dict:
    """
    Exports the PID record as a simple JSON object

    Returns:
        dict: The PID record as a simple JSON object
    """
    kv_pairs = []

    for key, value in self._entries.items():  # Iterate over all entries
        for entry in value:  # Iterate over all values of the entry
            kv_pairs.append(
                {"key": key, "value": entry["value"]}
            )  # Add the key and value to the list

    return {"pid": self._pid, "record": kv_pairs}

fromJSON staticmethod

fromJSON(input_json: dict) -> PIDRecord

Creates a PID record from a JSON object

Parameters:

Name Type Description Default
input_json dict

The JSON object to create the PID record from

required

Returns:

Name Type Description
PIDRecord PIDRecord

The PID record created from the JSON object

Raises:

Type Description
ValueError

If the JSON object is None or invalid

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
@staticmethod
def fromJSON(input_json: dict) -> "PIDRecord":
    """
    Creates a PID record from a JSON object

    Args:
        input_json (dict): The JSON object to create the PID record from

    Returns:
        PIDRecord: The PID record created from the JSON object

    Raises:
        ValueError: If the JSON object is None or invalid
    """
    logger.debug("Trying to extract PID record from JSON", input_json)

    if input_json is None:  # Check if the JSON object is None
        raise ValueError("JSON must not be None")

    if "pid" not in input_json:  # Check if the JSON object contains a PID
        raise ValueError("PID must be in JSON object")

    if "entries" not in input_json:  # Check if the JSON object contains entries
        return PIDRecord(input_json["pid"])
    else:
        entries = []

        for key, value in input_json[
            "entries"
        ].items():  # Iterate over all entries in the JSON object
            for entry in value:  # Iterate over all values of the entry. Each value consists of a dict with the keys "key", "value" and "name". Name is the only optional key.
                if "value" not in entry or "key" not in entry:
                    # Skip this entry if it does not contain a key or value
                    logger.warning(
                        f"Skipping entry {entry} because it does not contain a key or value"
                    )
                    continue
                elif "name" in entry:
                    # If the entry contains a name, add it to the PIDRecordEntry
                    entries.append(
                        PIDRecordEntry(key, entry["value"], entry["name"])
                    )
                else:
                    # If the entry does not contain a name, add it without a name
                    entries.append(PIDRecordEntry(key, entry["value"]))

        logger.debug(
            f"Extracted PID record from JSON: {PIDRecord(input_json['pid'], entries)}"
        )
        return PIDRecord(input_json["pid"], entries)

merge

merge(other: PIDRecord) -> PIDRecord

Merges the PID record with another PID record

Parameters:

Name Type Description Default
other PIDRecord

The PID record to merge with

required

Returns:

Name Type Description
PIDRecord PIDRecord

The merged PID record

Raises:

Type Description
ValueError

If the other PID record is None

Source code in src/nmr_FAIR_DOs/domain/pid_record.py
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
def merge(self, other: "PIDRecord") -> "PIDRecord":
    """
    Merges the PID record with another PID record

    Args:
        other (PIDRecord): The PID record to merge with

    Returns:
        PIDRecord: The merged PID record

    Raises:
        ValueError: If the other PID record is None
    """
    if other is None:  # Check if the other PID record is None
        raise ValueError("Other PID record must not be None")

    if (
        self._pid != other.getPID()
    ):  # Check if the PID of both PID records is the same
        raise ValueError("PID of both PID records must be the same")

    for (
        key,
        value,
    ) in (
        other.getEntries().items()
    ):  # Iterate over all entries in the other PID record
        for entry in value:  # Iterate over all values of the entry
            if not self.entryExists(
                key, entry.value
            ):  # Check if the entry does not exist in this PID record
                self.addPIDRecordEntry(entry)  # Add the entry to this PID record

    return self  # Return the merged PID record