Generates a random UUID. By default, this generates a RFC 4122 version 4 UUID (totally random). See Python's ``uuid`` module documentation for more information. Args: template (optional): A template to build the UUID from. Not valid with any other
| 1137 | |
| 1138 | |
| 1139 | class RandUUID(RandField[uuid.UUID]): |
| 1140 | """Generates a random UUID. |
| 1141 | |
| 1142 | By default, this generates a RFC 4122 version 4 UUID (totally random). |
| 1143 | |
| 1144 | See Python's ``uuid`` module documentation for more information. |
| 1145 | |
| 1146 | Args: |
| 1147 | template (optional): A template to build the UUID from. Not valid with |
| 1148 | any other option. |
| 1149 | node (optional): A 48-bit Host ID. Only valid for version 1 (where it |
| 1150 | is optional). |
| 1151 | clock_seq (optional): An integer of up to 14-bits for the sequence |
| 1152 | number. Only valid for version 1 (where it is |
| 1153 | optional). |
| 1154 | namespace: A namespace identifier, which is also a UUID. Required for |
| 1155 | versions 3 and 5, must be omitted otherwise. |
| 1156 | name: string, required for versions 3 and 5, must be omitted otherwise. |
| 1157 | version: Version of UUID to use (1, 3, 4 or 5). If omitted, attempts to |
| 1158 | guess which version to generate, defaulting to version 4 |
| 1159 | (totally random). |
| 1160 | |
| 1161 | Raises: |
| 1162 | ValueError: on invalid constructor arguments |
| 1163 | """ |
| 1164 | # This was originally scapy.contrib.dce_rpc.RandUUID. |
| 1165 | |
| 1166 | _BASE = "([0-9a-f]{{{0}}}|\\*|[0-9a-f]{{{0}}}:[0-9a-f]{{{0}}})" |
| 1167 | _REG = re.compile( |
| 1168 | r"^{0}-?{1}-?{1}-?{2}{2}-?{2}{2}{2}{2}{2}{2}$".format( |
| 1169 | _BASE.format(8), _BASE.format(4), _BASE.format(2) |
| 1170 | ), |
| 1171 | re.I |
| 1172 | ) |
| 1173 | VERSIONS = [1, 3, 4, 5] |
| 1174 | |
| 1175 | def __init__(self, |
| 1176 | template=None, # type: Optional[Any] |
| 1177 | node=None, # type: Optional[int] |
| 1178 | clock_seq=None, # type: Optional[int] |
| 1179 | namespace=None, # type: Optional[uuid.UUID] |
| 1180 | name=None, # type: Optional[str] |
| 1181 | version=None, # type: Optional[Any] |
| 1182 | ): |
| 1183 | # type: (...) -> None |
| 1184 | self._template = template |
| 1185 | self._ori_version = version |
| 1186 | |
| 1187 | self.uuid_template = None |
| 1188 | self.clock_seq = None |
| 1189 | self.namespace = None |
| 1190 | self.name = None |
| 1191 | self.node = None |
| 1192 | self.version = None |
| 1193 | |
| 1194 | if template: |
| 1195 | if node or clock_seq or namespace or name or version: |
| 1196 | raise ValueError("UUID template must be the only parameter, " |
no test coverage detected