-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
56 lines (44 loc) · 1.61 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import logging
from url_normalize import url_normalize
def printErrorMessageAndExitWithErrorCode(exceptionRaised: Exception, errorCode: int):
printJoinedErrorMessage(exceptionRaised)
exit(errorCode)
def printJoinedErrorMessage(exceptionRaised: Exception):
logging.exception(f"{type(exceptionRaised)} ","".join(exceptionRaised.args))
def getHostAndResourcesFromLink(link:str):
host = getHostOfLink(link)
resources = getResourcesFromLink(link)
return host, resources
def getHostWithSchemaAndResourcesFromLink(link:str):
normalizedLink = normalizeLinkIfCan(link)
hostWithSchema = getHostWithSchemaOfLink(normalizedLink)
resources = getResourcesFromLink(normalizedLink)
return hostWithSchema, resources
def normalizeLinkIfCan(link:str) -> str:
newLink = ""
try:
newLink = url_normalize(link)
except:
newLink = link
return newLink
def getHostOfLink(link:str) -> str:
"""
Assumes the link is in the format:
https://host/resources
"""
return link.split("/")[2]
def getHostWithSchemaOfLink(link:str) -> str:
schemaAndHost = ""
try:
schemaAndHostParts = link.split("/")[:3]
schemaAndHost = f"{schemaAndHostParts[0]}//{schemaAndHostParts[2]}"
except Exception as e:
pass
return schemaAndHost
def getResourcesFromLink(link: str) -> str:
return f"/{'/'.join(link.split('/')[3:])}"
def threadOfHost(numThreads:int, host:str) -> int:
return abs(hash(host)%numThreads)
def getCompleteLinkFromHostAndResource(host:str, resource:str) -> str:
completeLink = f"{host}{resource}"
return completeLink