| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import re |
| 20 |
from typing import Dict, List, Optional, Tuple, cast |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
HASH_ALGORITHMS = set(['sha1', 'sha224', 'sha384', 'sha256', 'sha512', 'md5']) |
| 25 |
|
| 26 |
extras_require_search = re.compile(r'(?P<name>.+)\[(?P<extras>[^\]]+)\]') |
| 27 |
|
| 28 |
|
| 29 |
def parse_fragment(fragment_string: str) -> Dict[str, str]: |
| 30 |
"""Takes a fragment string nd returns a dict of the components""" |
| 31 |
fragment_string = fragment_string.lstrip('#') |
| 32 |
|
| 33 |
try: |
| 34 |
return dict(cast(Tuple[str, str], tuple(key_value_string.split('='))) for key_value_string in |
| 35 |
fragment_string.split('&')) |
| 36 |
except ValueError: |
| 37 |
raise ValueError(f'Invalid fragment string {fragment_string}') |
| 38 |
|
| 39 |
|
| 40 |
def get_hash_info(d: Dict[str, str]) -> Tuple[Optional[str], Optional[str]]: |
| 41 |
"""Returns the first matching hashlib name and value from a dict""" |
| 42 |
for key in d.keys(): |
| 43 |
if key.lower() in HASH_ALGORITHMS: |
| 44 |
return key, d[key] |
| 45 |
|
| 46 |
return None, None |
| 47 |
|
| 48 |
|
| 49 |
def parse_extras_require(egg: Optional[str]) -> Tuple[Optional[str], List[str]]: |
| 50 |
if egg is not None: |
| 51 |
match = extras_require_search.search(egg) |
| 52 |
if match is not None: |
| 53 |
name = match.group('name') |
| 54 |
extras = match.group('extras') |
| 55 |
return name, [extra.strip() for extra in extras.split(',')] |
| 56 |
return egg, [] |