2019-12-08 22:43:49 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
class ID:
|
|
|
|
@staticmethod
|
|
|
|
def format_extid(extid: int) -> str:
|
|
|
|
"""
|
|
|
|
Take an ExtID as an integer, format it as a string.
|
|
|
|
|
|
|
|
If we had the ExtID 12345678, this would format as '1234-5678'
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
extid - The ID as an integer
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A string suitable for display to the user.
|
|
|
|
"""
|
|
|
|
extid_str = str(extid)
|
|
|
|
while len(extid_str) < 8:
|
2022-10-15 20:56:30 +02:00
|
|
|
extid_str = "0" + extid_str
|
|
|
|
return f"{extid_str[0:4]}-{extid_str[4:8]}"
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_extid(extid: str) -> Optional[int]:
|
|
|
|
"""
|
|
|
|
Take an ExtID as a string, and return the integer.
|
|
|
|
|
|
|
|
If we had the ExtID '1234-5678', this would return 12345678.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
extid - The string ID as shown to a suer
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
An integer extid suitable for looking up in a DB.
|
|
|
|
"""
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
if len(extid) == 9 and extid[4:5] == "-":
|
2019-12-08 22:43:49 +01:00
|
|
|
return int(extid[0:4] + extid[5:9])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
2022-10-15 20:56:30 +02:00
|
|
|
def format_machine_id(machine_id: int, region: str = "US") -> str:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Take a machine ID as an integer, format it as a string.
|
|
|
|
"""
|
2022-10-16 00:05:08 +02:00
|
|
|
if region not in {
|
|
|
|
"JP",
|
|
|
|
"KR",
|
|
|
|
"TW",
|
|
|
|
"HK",
|
|
|
|
"US",
|
|
|
|
"GB",
|
|
|
|
"IT",
|
|
|
|
"ES",
|
|
|
|
"FR",
|
|
|
|
"PT",
|
|
|
|
"XX",
|
|
|
|
}:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception(f"Invalid region {region}!")
|
|
|
|
return f"{region}-{machine_id}"
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_machine_id(machine_id: str) -> Optional[int]:
|
|
|
|
"""
|
|
|
|
Take a formatted machine ID as a string, returning an int.
|
|
|
|
"""
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
if (
|
|
|
|
machine_id[:2]
|
2022-10-16 00:05:08 +02:00
|
|
|
in {"JP", "KR", "TW", "HK", "US", "GB", "IT", "ES", "FR", "PT", "XX"}
|
2022-10-15 20:56:30 +02:00
|
|
|
and machine_id[2] == "-"
|
|
|
|
):
|
2019-12-08 22:43:49 +01:00
|
|
|
return int(machine_id[3:])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return None
|