Python black format fixes

This commit is contained in:
2023-12-01 23:50:27 +09:00
parent 4f3d8d7800
commit 1e4b210670
5 changed files with 398 additions and 488 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,2 @@
.venv/ .venv/
bin/utils/__pycache__/* **/__pycache__/**

File diff suppressed because it is too large Load Diff

View File

@@ -5,6 +5,7 @@ latitude/longitude functions
import re import re
from math import radians, sin, cos, atan2, sqrt from math import radians, sin, cos, atan2, sqrt
def convert_lat_long_to_dms(lat_long, is_latitude=False, is_longitude=False): def convert_lat_long_to_dms(lat_long, is_latitude=False, is_longitude=False):
""" """
convert the LR format of N.N to the Exif GPS format convert the LR format of N.N to the Exif GPS format
@@ -21,13 +22,14 @@ def convert_lat_long_to_dms(lat_long, is_latitude=False, is_longitude=False):
degree = int(abs(lat_long)) degree = int(abs(lat_long))
minutes = round((float(abs(lat_long)) - int(abs(lat_long))) * 60, 10) minutes = round((float(abs(lat_long)) - int(abs(lat_long))) * 60, 10)
if is_latitude is True: if is_latitude is True:
direction = 'S' if int(lat_long) < 0 else 'N' direction = "S" if int(lat_long) < 0 else "N"
elif is_longitude is True: elif is_longitude is True:
direction = 'W' if int(lat_long) < 0 else 'E' direction = "W" if int(lat_long) < 0 else "E"
else: else:
direction = '(INVALID)' direction = "(INVALID)"
return f"{degree},{minutes}{direction}" return f"{degree},{minutes}{direction}"
def convert_lat_to_dms(lat_long): def convert_lat_to_dms(lat_long):
""" """
wrapper functions for Long/Lat calls: latitude wrapper functions for Long/Lat calls: latitude
@@ -54,6 +56,7 @@ def convert_long_to_dms(lat_long):
""" """
return convert_lat_long_to_dms(lat_long, is_longitude=True) return convert_lat_long_to_dms(lat_long, is_longitude=True)
def long_lat_reg(longitude, latitude): def long_lat_reg(longitude, latitude):
""" """
converts the XMP/EXIF formatted GPS Long/Lat coordinates converts the XMP/EXIF formatted GPS Long/Lat coordinates
@@ -68,12 +71,9 @@ def long_lat_reg(longitude, latitude):
dictionary: dict with converted lat/long dictionary: dict with converted lat/long
""" """
# regex # regex
latlong_re = re.compile(r'^(\d+),(\d+\.\d+)([NESW]{1})$') latlong_re = re.compile(r"^(\d+),(\d+\.\d+)([NESW]{1})$")
# dict for loop # dict for loop
lat_long = { lat_long = {"longitude": longitude, "latitude": latitude}
'longitude': longitude,
'latitude': latitude
}
# for element in lat_long: # for element in lat_long:
for index, element in lat_long.items(): for index, element in lat_long.items():
# match if it is exif GPS format # match if it is exif GPS format
@@ -82,10 +82,11 @@ def long_lat_reg(longitude, latitude):
# convert from Degree, Min.Sec into float format # convert from Degree, Min.Sec into float format
lat_long[index] = float(_match.group(1)) + (float(_match.group(2)) / 60) lat_long[index] = float(_match.group(1)) + (float(_match.group(2)) / 60)
# if S or W => inverse to negative # if S or W => inverse to negative
if _match.group(3) == 'S' or _match.group(3) == 'W': if _match.group(3) == "S" or _match.group(3) == "W":
lat_long[index] *= -1 lat_long[index] *= -1
return lat_long return lat_long
def convert_dms_to_lat(lat_long): def convert_dms_to_lat(lat_long):
""" """
rapper calls for DMS to Lat/Long: latitude rapper calls for DMS to Lat/Long: latitude
@@ -96,7 +97,8 @@ def convert_dms_to_lat(lat_long):
Returns: Returns:
dict: dict with converted lat/long dict: dict with converted lat/long
""" """
return long_lat_reg('0,0.0N', lat_long)['latitude'] return long_lat_reg("0,0.0N", lat_long)["latitude"]
def convert_dms_to_long(lat_long): def convert_dms_to_long(lat_long):
""" """
@@ -108,7 +110,8 @@ def convert_dms_to_long(lat_long):
Returns: Returns:
dict: dict with converted lat/long dict: dict with converted lat/long
""" """
return long_lat_reg(lat_long, '0,0.0N')['longitude'] return long_lat_reg(lat_long, "0,0.0N")["longitude"]
def get_distance(from_longitude, from_latitude, to_longitude, to_latitude): def get_distance(from_longitude, from_latitude, to_longitude, to_latitude):
""" """
@@ -134,7 +137,8 @@ def get_distance(from_longitude, from_latitude, to_longitude, to_latitude):
distance_longitude = from_longitude - to_longitude distance_longitude = from_longitude - to_longitude
distance_latitude = from_latitude - to_latitude distance_latitude = from_latitude - to_latitude
# main distance calculation # main distance calculation
distance = sin(distance_latitude / 2)**2 + cos(from_latitude) * \ distance = (
cos(to_latitude) * sin(distance_longitude / 2)**2 sin(distance_latitude / 2) ** 2 + cos(from_latitude) * cos(to_latitude) * sin(distance_longitude / 2) ** 2
)
distance = 2 * atan2(sqrt(distance), sqrt(1 - distance)) distance = 2 * atan2(sqrt(distance), sqrt(1 - distance))
return earth_radius * distance return earth_radius * distance

View File

@@ -2,11 +2,12 @@
reverse geolacte functions reverse geolacte functions
""" """
import requests
import re import re
import requests
from utils.long_lat import long_lat_reg from utils.long_lat import long_lat_reg
from utils.string_helpers import only_latin_chars from utils.string_helpers import only_latin_chars
def reverse_geolocate(longitude, latitude, map_type, args): def reverse_geolocate(longitude, latitude, map_type, args):
""" """
wrapper to call to either the google or openstreetmap wrapper to call to either the google or openstreetmap
@@ -27,16 +28,13 @@ def reverse_geolocate(longitude, latitude, map_type, args):
# detect and convert # detect and convert
lat_long = long_lat_reg(longitude=longitude, latitude=latitude) lat_long = long_lat_reg(longitude=longitude, latitude=latitude)
# which service to use # which service to use
if map_type == 'google': if map_type == "google":
return reverse_geolocate_google(lat_long['longitude'], lat_long['latitude'], args) return reverse_geolocate_google(lat_long["longitude"], lat_long["latitude"], args)
elif map_type == 'openstreetmap': elif map_type == "openstreetmap":
return reverse_geolocate_open_street_map(lat_long['longitude'], lat_long['latitude'], args) return reverse_geolocate_open_street_map(lat_long["longitude"], lat_long["latitude"], args)
else: else:
return { return {"Country": "", "status": "ERROR", "error": "Map type not valid"}
'Country': '',
'status': 'ERROR',
'error': 'Map type not valid'
}
def reverse_geolocate_init(longitude, latitude): def reverse_geolocate_init(longitude, latitude):
""" """
@@ -52,22 +50,23 @@ def reverse_geolocate_init(longitude, latitude):
""" """
# basic dict format # basic dict format
geolocation = { geolocation = {
'CountryCode': '', "CountryCode": "",
'Country': '', "Country": "",
'State': '', "State": "",
'City': '', "City": "",
'Location': '', "Location": "",
# below for error reports # below for error reports
'status': '', "status": "",
'error_message': '' "error_message": "",
} }
# error if long/lat is not valid # error if long/lat is not valid
latlong_re = re.compile(r'^\d+\.\d+$') latlong_re = re.compile(r"^\d+\.\d+$")
if not latlong_re.match(str(longitude)) or not latlong_re.match(str(latitude)): if not latlong_re.match(str(longitude)) or not latlong_re.match(str(latitude)):
geolocation['status'] = 'ERROR' geolocation["status"] = "ERROR"
geolocation['error_message'] = f"Latitude {latitude} or Longitude {longitude} are not valid" geolocation["error_message"] = f"Latitude {latitude} or Longitude {longitude} are not valid"
return geolocation return geolocation
def reverse_geolocate_open_street_map(longitude, latitude, args): def reverse_geolocate_open_street_map(longitude, latitude, args):
""" """
OpenStreetMap reverse lookcation lookup OpenStreetMap reverse lookcation lookup
@@ -87,24 +86,19 @@ def reverse_geolocate_open_street_map(longitude, latitude, args):
""" """
# init # init
geolocation = reverse_geolocate_init(longitude, latitude) geolocation = reverse_geolocate_init(longitude, latitude)
if geolocation['status'] == 'ERROR': if geolocation["status"] == "ERROR":
return geolocation return geolocation
# query format # query format
query_format = 'jsonv2' query_format = "jsonv2"
# language to return (english) # language to return (english)
language = 'en-US,en' language = "en-US,en"
# build query # build query
base = 'https://nominatim.openstreetmap.org/reverse.php?' base = "https://nominatim.openstreetmap.org/reverse.php?"
# parameters # parameters
payload = { payload = {"format": query_format, "lat": latitude, "lon": longitude, "accept-language": language}
'format': query_format,
'lat': latitude,
'lon': longitude,
'accept-language': language
}
# if we have an email, add it here # if we have an email, add it here
if args.email: if args.email:
payload['email'] = args.email payload["email"] = args.email
url = f"{base}" url = f"{base}"
# timeout in seconds # timeout in seconds
timeout = 60 timeout = 60
@@ -117,16 +111,16 @@ def reverse_geolocate_open_street_map(longitude, latitude, args):
# type map # type map
# Country to Location and for each in order of priority # Country to Location and for each in order of priority
type_map = { type_map = {
'CountryCode': ['country_code'], "CountryCode": ["country_code"],
'Country': ['country'], "Country": ["country"],
'State': ['state'], "State": ["state"],
'City': ['city', 'city_district', 'state_district'], "City": ["city", "city_district", "state_district"],
'Location': ['county', 'town', 'suburb', 'hamlet', 'neighbourhood', 'road'] "Location": ["county", "town", "suburb", "hamlet", "neighbourhood", "road"],
} }
# if not error # if not error
if 'error' not in response.json(): if "error" not in response.json():
# get address block # get address block
addr = response.json()['address'] addr = response.json()["address"]
# loop for locations # loop for locations
for loc_index, sub_index in type_map.items(): for loc_index, sub_index in type_map.items():
for index in sub_index: for index in sub_index:
@@ -137,12 +131,13 @@ def reverse_geolocate_open_street_map(longitude, latitude, args):
# if index in addr and not geolocation[loc_index]: # if index in addr and not geolocation[loc_index]:
# geolocation[loc_index] = addr[index] # geolocation[loc_index] = addr[index]
else: else:
geolocation['status'] = 'ERROR' geolocation["status"] = "ERROR"
geolocation['error_message'] = response.json()['error'] geolocation["error_message"] = response.json()["error"]
print(f"Error in request: {geolocation['error']}") print(f"Error in request: {geolocation['error']}")
# return # return
return geolocation return geolocation
def reverse_geolocate_google(longitude, latitude, args): def reverse_geolocate_google(longitude, latitude, args):
""" """
Google Maps reverse location lookup Google Maps reverse location lookup
@@ -163,25 +158,21 @@ def reverse_geolocate_google(longitude, latitude, args):
# init # init
geolocation = reverse_geolocate_init(longitude, latitude) geolocation = reverse_geolocate_init(longitude, latitude)
temp_geolocation = geolocation.copy() temp_geolocation = geolocation.copy()
if geolocation['status'] == 'ERROR': if geolocation["status"] == "ERROR":
return geolocation return geolocation
# sensor (why?) # sensor (why?)
sensor = 'false' sensor = "false"
# language, so we get ascii en back # language, so we get ascii en back
language = 'en' language = "en"
# request to google # request to google
# if a google api key is used, the request has to be via https # if a google api key is used, the request has to be via https
protocol = 'https://' if args.google_api_key else 'http://' protocol = "https://" if args.google_api_key else "http://"
base = "maps.googleapis.com/maps/api/geocode/json?" base = "maps.googleapis.com/maps/api/geocode/json?"
# build the base params # build the base params
payload = { payload = {"latlng": f"{latitude},{longitude}", "language": language, "sensor": sensor}
'latlng': f"{latitude},{longitude}",
'language': language,
'sensor': sensor
}
# if we have a google api key, add it here # if we have a google api key, add it here
if args.google_api_key: if args.google_api_key:
payload['key'] = args.google_api_key payload["key"] = args.google_api_key
# build the full url and send it to google # build the full url and send it to google
url = f"{protocol}{base}" url = f"{protocol}{base}"
# timeout in seconds # timeout in seconds
@@ -195,22 +186,22 @@ def reverse_geolocate_google(longitude, latitude, args):
# type map # type map
# For automated return of correct data into set to return # For automated return of correct data into set to return
type_map = { type_map = {
'CountryCode': ['country'], "CountryCode": ["country"],
'Country': ['country'], "Country": ["country"],
'State': ['administrative_area_level_1', 'administrative_area_level_2'], "State": ["administrative_area_level_1", "administrative_area_level_2"],
'City': ['locality', 'administrative_area_level_3'], "City": ["locality", "administrative_area_level_3"],
'Location': ['sublocality_level_1', 'sublocality_level_2', 'route'], "Location": ["sublocality_level_1", "sublocality_level_2", "route"],
} }
# print("Error: {}".format(response.json()['status'])) # print("Error: {}".format(response.json()['status']))
if response.json()['status'] == 'OK': if response.json()["status"] == "OK":
# first entry for type = premise # first entry for type = premise
for entry in response.json()['results']: for entry in response.json()["results"]:
for sub_entry in entry: for sub_entry in entry:
if sub_entry == 'types' and ( if sub_entry == "types" and (
'premise' in entry[sub_entry] or "premise" in entry[sub_entry]
'route' in entry[sub_entry] or or "route" in entry[sub_entry]
'street_address' in entry[sub_entry] or or "street_address" in entry[sub_entry]
'sublocality' in entry[sub_entry] or "sublocality" in entry[sub_entry]
): ):
# print("Entry {}: {}".format(sub_entry, entry[sub_entry])) # print("Entry {}: {}".format(sub_entry, entry[sub_entry]))
# print("Address {}".format(entry['address_components'])) # print("Address {}".format(entry['address_components']))
@@ -225,33 +216,33 @@ def reverse_geolocate_google(longitude, latitude, args):
for loc_index, sub_index in type_map.items(): for loc_index, sub_index in type_map.items():
for index in sub_index: for index in sub_index:
# this is an array, so we need to loop through each # this is an array, so we need to loop through each
for addr in entry['address_components']: for addr in entry["address_components"]:
# in types check that index is in there # in types check that index is in there
# and the location is not yet set # and the location is not yet set
# also check that entry is in LATIN based # also check that entry is in LATIN based
# NOTE: fallback if all are non LATIN? # NOTE: fallback if all are non LATIN?
if index in addr['types'] and not geolocation[loc_index]: if index in addr["types"] and not geolocation[loc_index]:
# for country code we need to use short name, # for country code we need to use short name,
# else we use long name # else we use long name
if loc_index == 'CountryCode': if loc_index == "CountryCode":
if only_latin_chars(addr['short_name']): if only_latin_chars(addr["short_name"]):
geolocation[loc_index] = addr['short_name'] geolocation[loc_index] = addr["short_name"]
elif not temp_geolocation[loc_index]: elif not temp_geolocation[loc_index]:
temp_geolocation[loc_index] = addr['short_name'] temp_geolocation[loc_index] = addr["short_name"]
else: else:
if only_latin_chars(addr['long_name']): if only_latin_chars(addr["long_name"]):
geolocation[loc_index] = addr['long_name'] geolocation[loc_index] = addr["long_name"]
elif not temp_geolocation[loc_index]: elif not temp_geolocation[loc_index]:
temp_geolocation[loc_index] = addr['long_name'] temp_geolocation[loc_index] = addr["long_name"]
# check that all in geoloaction are filled and if not fille from temp_geolocation dictionary # check that all in geoloaction are filled and if not fille from temp_geolocation dictionary
for loc_index in type_map: for loc_index in type_map:
if not geolocation[loc_index] and temp_geolocation[loc_index]: if not geolocation[loc_index] and temp_geolocation[loc_index]:
geolocation[loc_index] = temp_geolocation[loc_index] geolocation[loc_index] = temp_geolocation[loc_index]
# write OK status # write OK status
geolocation['status'] = response.json()['status'] geolocation["status"] = response.json()["status"]
else: else:
geolocation['error_message'] = response.json()['error_message'] geolocation["error_message"] = response.json()["error_message"]
geolocation['status'] = response.json()['status'] geolocation["status"] = response.json()["status"]
print(f"Error in request: {geolocation['status']} {geolocation['error_message']}") print(f"Error in request: {geolocation['status']} {geolocation['error_message']}")
# return # return
return geolocation return geolocation

View File

@@ -8,7 +8,8 @@ import unicodedata
# this is used by isLatin and onlyLatinChars # this is used by isLatin and onlyLatinChars
cache_latin_letters = {} cache_latin_letters = {}
def shorten_string(string, width, placeholder='..'):
def shorten_string(string, width, placeholder=".."):
""" """
shortens a string to width and attached placeholder shortens a string to width and attached placeholder
@@ -26,7 +27,7 @@ def shorten_string(string, width, placeholder='..'):
if string_length_cjk > width: if string_length_cjk > width:
# set current length and output string # set current length and output string
cur_len = 0 cur_len = 0
out_string = '' out_string = ""
# loop through each character # loop through each character
for char in str(string): for char in str(string):
# set the current length if we add the character # set the current length if we add the character
@@ -39,6 +40,7 @@ def shorten_string(string, width, placeholder='..'):
else: else:
return str(string) return str(string)
def string_len_cjk(string): def string_len_cjk(string):
""" """
because len on string in python counts characters but we need the width because len on string in python counts characters but we need the width
@@ -53,6 +55,7 @@ def string_len_cjk(string):
# return string len including double count for double width characters # return string len including double count for double width characters
return sum(1 + (unicodedata.east_asian_width(c) in "WF") for c in string) return sum(1 + (unicodedata.east_asian_width(c) in "WF") for c in string)
def is_latin(uchr): def is_latin(uchr):
""" """
checks via the unciode class if a character is LATIN char based checks via the unciode class if a character is LATIN char based
@@ -71,7 +74,8 @@ def is_latin(uchr):
return cache_latin_letters[uchr] return cache_latin_letters[uchr]
except KeyError: except KeyError:
# find LATIN in uncide type returned and set in dictionary for this character # find LATIN in uncide type returned and set in dictionary for this character
return cache_latin_letters.setdefault(uchr, 'LATIN' in unicodedata.name(uchr)) return cache_latin_letters.setdefault(uchr, "LATIN" in unicodedata.name(uchr))
def only_latin_chars(unistr): def only_latin_chars(unistr):
""" """
@@ -88,6 +92,7 @@ def only_latin_chars(unistr):
""" """
return all(is_latin(uchr) for uchr in unistr if uchr.isalpha()) return all(is_latin(uchr) for uchr in unistr if uchr.isalpha())
def format_len(string, length): def format_len(string, length):
""" """
in case of CJK characters we need to adjust the format length dynamically in case of CJK characters we need to adjust the format length dynamically