Using f-strings

This commit is contained in:
theo@manjaro 2022-07-04 10:31:31 +02:00
parent 94059b415a
commit eb7ce39432

View file

@ -42,10 +42,10 @@ def is_ip_local(ip):
def get_public_ip(protocol=4): def get_public_ip(protocol=4):
assert protocol in [4, 6], ( assert protocol in [4, 6], (
"Invalid protocol version for get_public_ip: %s, expected 4 or 6" % protocol f"Invalid protocol version for get_public_ip: {protocol}, expected 4 or 6"
) )
cache_file = "/var/cache/yunohost/ipv%s" % protocol cache_file = f"/var/cache/yunohost/ipv{protocol}"
cache_duration = 120 # 2 min cache_duration = 120 # 2 min
if ( if (
os.path.exists(cache_file) os.path.exists(cache_file)
@ -56,7 +56,7 @@ def get_public_ip(protocol=4):
logger.debug(f"Reusing IPv{protocol} from cache: {ip}") logger.debug(f"Reusing IPv{protocol} from cache: {ip}")
else: else:
ip = get_public_ip_from_remote_server(protocol) ip = get_public_ip_from_remote_server(protocol)
logger.debug("IP fetched: %s" % ip) logger.debug(f"IP fetched: {protocol}")
write_to_file(cache_file, ip or "") write_to_file(cache_file, ip or "")
return ip return ip
@ -72,7 +72,7 @@ def get_public_ip_from_remote_server(protocol=4):
return None return None
# If we are indeed connected in ipv4 or ipv6, we should find a default route # If we are indeed connected in ipv4 or ipv6, we should find a default route
routes = check_output("ip -%s route show table all" % protocol).split("\n") routes = check_output(f"ip -{protocol} route show table all").split("\n")
def is_default_route(r): def is_default_route(r):
# Typically the default route starts with "default" # Typically the default route starts with "default"
@ -85,8 +85,7 @@ def get_public_ip_from_remote_server(protocol=4):
if not any(is_default_route(r) for r in routes): if not any(is_default_route(r) for r in routes):
logger.debug( logger.debug(
"No default route for IPv%s, so assuming there's no IP address for that version" f"No default route for IPv{protocol}, so assuming there's no IP address for that version"
% protocol
) )
return None return None
@ -111,7 +110,7 @@ def get_public_ips(protocol=4):
# Check URLS # Check URLS
for url in ip_url_yunohost_tab[:3]: for url in ip_url_yunohost_tab[:3]:
logger.debug("Fetching IP from %s " % url) logger.debug(f"Fetching IP from {url}")
try: try:
ip = download_text(url, timeout=15).strip() ip = download_text(url, timeout=15).strip()
if ip in ip_count.keys(): if ip in ip_count.keys():
@ -120,7 +119,7 @@ def get_public_ips(protocol=4):
ip_count[ip]=1 ip_count[ip]=1
except Exception as e: except Exception as e:
logger.debug( logger.debug(
"Could not get public IPv%s from %s : %s" % (str(protocol), url, str(e)) f"Could not get public IPv{protocol} from {url} : {e}"
) )
ip_list_with_count = [ (ip,ip_count[ip]) for ip in ip_count ] ip_list_with_count = [ (ip,ip_count[ip]) for ip in ip_count ]