|
5 | 5 | import os |
6 | 6 |
|
7 | 7 | from ..client import Client |
8 | | -from ..exceptions import StorageException |
| 8 | +from ..exceptions import (StorageException, NotFound) |
9 | 9 | from pyDKB.common.misc import (log, logLevel) |
10 | 10 | from pyDKB.common.misc import try_to_import |
11 | 11 |
|
@@ -50,3 +50,61 @@ class RucioClient(Client, ParentClientClass): |
50 | 50 | def __init__(self, *args, **kwargs): |
51 | 51 | """ Initialize parent client class. """ |
52 | 52 | ParentClientClass.__init__(self, *args, **kwargs) |
| 53 | + |
| 54 | + def get(self, oid, **kwargs): |
| 55 | + """ Get dataset metadata. |
| 56 | +
|
| 57 | + Implementation of interface method `Clent.get()`. |
| 58 | +
|
| 59 | + :param oid: dataset name |
| 60 | + :type oid: str |
| 61 | + :param fields: list of requested metadata fields |
| 62 | + (None = all metadata) |
| 63 | + :type fields: list |
| 64 | +
|
| 65 | + :return: dataset metadata |
| 66 | + :rtype: dict |
| 67 | + """ |
| 68 | + scope, name = self._scope_and_name(oid) |
| 69 | + try: |
| 70 | + result = self.get_metadata(scope=scope, name=name) |
| 71 | + except ValueError, err: |
| 72 | + raise StorageException("Failed to get metadata from Rucio: %s" |
| 73 | + % err) |
| 74 | + except RucioException, err: |
| 75 | + if 'Data identifier not found' in str(err): |
| 76 | + raise NotFound(scope=scope, name=name) |
| 77 | + raise StorageException("Failed to get metadata from Rucio: %s" |
| 78 | + % err) |
| 79 | + if kwargs.get('fields') is not None: |
| 80 | + result = {f: result.get(f, None) for f in kwargs['fields']} |
| 81 | + return result |
| 82 | + |
| 83 | + def _scope_and_name(self, dsn): |
| 84 | + """ Construct normalized scope and dataset name. |
| 85 | +
|
| 86 | + As input accepts dataset names in two forms: |
| 87 | + * dot-separated string: "<XXX>.<YYY>[.<...>]"; |
| 88 | + * dot-separated string with prefix: "<scope>:<XXX>.<YYY>[.<...>]". |
| 89 | +
|
| 90 | + In first case ID is taken as a canonical dataset name and scope is set |
| 91 | + to its first field (or two first fields, if the ID starts with 'user' |
| 92 | + or 'group'). |
| 93 | + In second case prefix is taken as scope, and removed from ID to get the |
| 94 | + canonical dataset name. |
| 95 | +
|
| 96 | + :param dsn: dataset name |
| 97 | + :type dsn: str |
| 98 | +
|
| 99 | + :return: scope, datasetname |
| 100 | + :rtype: tuple |
| 101 | + """ |
| 102 | + result = dsn.split(':') |
| 103 | + if len(result) < 2: |
| 104 | + splitted = dsn.split('.') |
| 105 | + if dsn.startswith('user') or dsn.startswith('group'): |
| 106 | + scope = '.'.join(splitted[0:2]) |
| 107 | + else: |
| 108 | + scope = splitted[0] |
| 109 | + result = (scope, dsn) |
| 110 | + return result |
0 commit comments