| 1 |
from .validation import valid_hostname, valid_port |
| 2 |
|
| 3 |
|
| 4 |
def resolve(path_or_address=None, address=None, *ignored): |
| 5 |
""" |
| 6 |
Returns (path, address) based on consecutive optional arguments, |
| 7 |
[path] [address]. |
| 8 |
""" |
| 9 |
if path_or_address is None or address is not None: |
| 10 |
return path_or_address, address |
| 11 |
|
| 12 |
path = None |
| 13 |
if split_address(path_or_address)[1] is not None: |
| 14 |
address = path_or_address |
| 15 |
else: |
| 16 |
path = path_or_address |
| 17 |
|
| 18 |
return path, address |
| 19 |
|
| 20 |
|
| 21 |
def split_address(address): |
| 22 |
""" |
| 23 |
Returns (host, port) with an integer port from the specified address |
| 24 |
string. (None, None) is returned if the address is invalid. |
| 25 |
""" |
| 26 |
invalid = None, None |
| 27 |
|
| 28 |
if not address and address != 0: |
| 29 |
return invalid |
| 30 |
|
| 31 |
components = str(address).split(':') |
| 32 |
if len(components) > 2: |
| 33 |
return invalid |
| 34 |
|
| 35 |
if components[0] and not valid_hostname(components[0]): |
| 36 |
return invalid |
| 37 |
|
| 38 |
if len(components) == 2 and not valid_port(components[1]): |
| 39 |
return invalid |
| 40 |
|
| 41 |
if len(components) == 1: |
| 42 |
components.insert(0 if valid_port(components[0]) else 1, None) |
| 43 |
|
| 44 |
host, port = components |
| 45 |
port = int(port) if port else None |
| 46 |
|
| 47 |
return host, port |