Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/prov/identifier.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
| author | shellac |
|---|---|
| date | Thu, 14 May 2020 14:56:58 -0400 |
| parents | 26e78fe6e8c4 |
| children |
comparison
equal
deleted
inserted
replaced
| 1:75ca89e9b81c | 2:6af9afd405e9 |
|---|---|
| 1 from __future__ import (absolute_import, division, print_function, | |
| 2 unicode_literals) | |
| 3 | |
| 4 import six | |
| 5 | |
| 6 __author__ = 'Trung Dong Huynh' | |
| 7 __email__ = 'trungdong@donggiang.com' | |
| 8 | |
| 9 | |
| 10 @six.python_2_unicode_compatible | |
| 11 class Identifier(object): | |
| 12 """Base class for all identifiers and also represents xsd:anyURI.""" | |
| 13 # TODO: make Identifier an "abstract" base class and move xsd:anyURI | |
| 14 # into a subclass | |
| 15 | |
| 16 def __init__(self, uri): | |
| 17 """ | |
| 18 Constructor. | |
| 19 | |
| 20 :param uri: URI string for the long namespace identifier. | |
| 21 """ | |
| 22 self._uri = six.text_type(uri) # Ensure this is a unicode string | |
| 23 | |
| 24 @property | |
| 25 def uri(self): | |
| 26 """Identifier's URI.""" | |
| 27 return self._uri | |
| 28 | |
| 29 def __str__(self): | |
| 30 return self._uri | |
| 31 | |
| 32 def __eq__(self, other): | |
| 33 return self.uri == other.uri if isinstance(other, Identifier) else False | |
| 34 | |
| 35 def __hash__(self): | |
| 36 return hash((self.uri, self.__class__)) | |
| 37 | |
| 38 def __repr__(self): | |
| 39 return '<%s: %s>' % (self.__class__.__name__, self._uri) | |
| 40 | |
| 41 def provn_representation(self): | |
| 42 """PROV-N representation of qualified name in a string.""" | |
| 43 return '"%s" %%%% xsd:anyURI' % self._uri | |
| 44 | |
| 45 | |
| 46 @six.python_2_unicode_compatible | |
| 47 class QualifiedName(Identifier): | |
| 48 """Qualified name of an identifier in a particular namespace.""" | |
| 49 | |
| 50 def __init__(self, namespace, localpart): | |
| 51 """ | |
| 52 Constructor. | |
| 53 | |
| 54 :param namespace: Namespace to use for qualified name resolution. | |
| 55 :param localpart: Portion of identifier not part of the namespace prefix. | |
| 56 """ | |
| 57 Identifier.__init__(self, u''.join([namespace.uri, localpart])) | |
| 58 self._namespace = namespace | |
| 59 self._localpart = localpart | |
| 60 self._str = ( | |
| 61 ':'.join([namespace.prefix, localpart]) | |
| 62 if namespace.prefix else localpart | |
| 63 ) | |
| 64 | |
| 65 @property | |
| 66 def namespace(self): | |
| 67 """Namespace of qualified name.""" | |
| 68 return self._namespace | |
| 69 | |
| 70 @property | |
| 71 def localpart(self): | |
| 72 """Local part of qualified name.""" | |
| 73 return self._localpart | |
| 74 | |
| 75 def __str__(self): | |
| 76 return self._str | |
| 77 | |
| 78 def __repr__(self): | |
| 79 return '<%s: %s>' % (self.__class__.__name__, self._str) | |
| 80 | |
| 81 def __hash__(self): | |
| 82 return hash(self.uri) | |
| 83 | |
| 84 def provn_representation(self): | |
| 85 """PROV-N representation of qualified name in a string.""" | |
| 86 return "'%s'" % self._str | |
| 87 | |
| 88 | |
| 89 class Namespace(object): | |
| 90 """PROV Namespace.""" | |
| 91 | |
| 92 def __init__(self, prefix, uri): | |
| 93 """ | |
| 94 Constructor. | |
| 95 | |
| 96 :param prefix: String short hand prefix for the namespace. | |
| 97 :param uri: URI string for the long namespace identifier. | |
| 98 """ | |
| 99 self._prefix = prefix | |
| 100 self._uri = uri | |
| 101 self._cache = dict() | |
| 102 | |
| 103 @property | |
| 104 def uri(self): | |
| 105 """Namespace URI.""" | |
| 106 return self._uri | |
| 107 | |
| 108 @property | |
| 109 def prefix(self): | |
| 110 """Namespace prefix.""" | |
| 111 return self._prefix | |
| 112 | |
| 113 def contains(self, identifier): | |
| 114 """ | |
| 115 Indicates whether the identifier provided is contained in this namespace. | |
| 116 | |
| 117 :param identifier: Identifier to check. | |
| 118 :return: bool | |
| 119 """ | |
| 120 uri = identifier if isinstance(identifier, six.string_types) else ( | |
| 121 identifier.uri if isinstance(identifier, Identifier) else None | |
| 122 ) | |
| 123 return uri.startswith(self._uri) if uri else False | |
| 124 | |
| 125 def qname(self, identifier): | |
| 126 """ | |
| 127 Returns the qualified name of the identifier given using the namespace | |
| 128 prefix. | |
| 129 | |
| 130 :param identifier: Identifier to resolve to a qualified name. | |
| 131 :return: :py:class:`QualifiedName` | |
| 132 """ | |
| 133 uri = identifier if isinstance(identifier, six.string_types) else ( | |
| 134 identifier.uri if isinstance(identifier, Identifier) else None | |
| 135 ) | |
| 136 if uri and uri.startswith(self._uri): | |
| 137 return QualifiedName(self, uri[len(self._uri):]) | |
| 138 else: | |
| 139 return None | |
| 140 | |
| 141 def __eq__(self, other): | |
| 142 return ( | |
| 143 (self._uri == other.uri and self._prefix == other.prefix) | |
| 144 if isinstance(other, Namespace) else False | |
| 145 ) | |
| 146 | |
| 147 def __ne__(self, other): | |
| 148 return ( | |
| 149 not isinstance(other, Namespace) or | |
| 150 self._uri != other.uri or | |
| 151 self._prefix != other.prefix | |
| 152 ) | |
| 153 | |
| 154 def __hash__(self): | |
| 155 return hash((self._uri, self._prefix)) | |
| 156 | |
| 157 def __repr__(self): | |
| 158 return '<%s: %s {%s}>' % ( | |
| 159 self.__class__.__name__, self._prefix, self._uri | |
| 160 ) | |
| 161 | |
| 162 def __getitem__(self, localpart): | |
| 163 if localpart in self._cache: | |
| 164 return self._cache[localpart] | |
| 165 else: | |
| 166 qname = QualifiedName(self, localpart) | |
| 167 self._cache[localpart] = qname | |
| 168 return qname |
