From 902fecbe7c97674899ae35a5065d5e0f44387d56 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 4 Sep 2024 02:20:34 +0200 Subject: [PATCH] Continued: - added type-hint 'None' for all functions not returning a value, for example setters, add/update/delete functions --- daemon.py | 34 +++++++++++++++++----------------- fba/boot.py | 6 +++--- fba/helpers/cache.py | 4 ++-- fba/helpers/domain.py | 2 +- fba/helpers/locking.py | 4 ++-- fba/helpers/processing.py | 2 +- fba/http/federation.py | 2 +- fba/http/network.py | 2 +- fba/models/blocks.py | 12 ++++++------ fba/models/error_log.py | 2 +- fba/models/instances.py | 38 +++++++++++++++++++------------------- fba/models/sources.py | 2 +- 12 files changed, 55 insertions(+), 55 deletions(-) diff --git a/daemon.py b/daemon.py index 8a9b085..d7d13b8 100755 --- a/daemon.py +++ b/daemon.py @@ -55,7 +55,7 @@ router.mount( templates = Jinja2Templates(directory="templates") @router.get(config.get("base_url") + "/api/info.json", response_class=JSONResponse) -def api_info(): +def api_info() -> None: database.cursor.execute("SELECT (SELECT COUNT(domain) FROM instances) AS total_websites, (SELECT COUNT(domain) FROM instances WHERE software IN ('pleroma', 'mastodon', 'lemmy', 'friendica', 'misskey', 'peertube', 'takahe', 'gotosocial', 'brighteon', 'wildebeest', 'bookwyrm', 'mitra', 'areionskey', 'mammuthus', 'neodb', 'smithereen', 'vebinet', 'toki', 'snac', 'biblioreads', 'wordpress', 'oolong', 'diaspora')) AS supported_instances, (SELECT COUNT(blocker) FROM blocks) AS total_blocks, (SELECT COUNT(domain) FROM instances WHERE last_error_details IS NOT NULL) AS erroneous_instances") row = database.cursor.fetchone() @@ -69,7 +69,7 @@ def api_info(): @router.get(config.get("base_url") + "/api/scoreboard.json", response_class=JSONResponse) -def api_scoreboard(mode: str, amount: int): +def api_scoreboard(mode: str, amount: int) -> None: if amount > config.get("api_limit"): raise HTTPException(status_code=400, detail="Too many results") @@ -113,7 +113,7 @@ def api_scoreboard(mode: str, amount: int): return JSONResponse(status_code=200, content=scores) @router.get(config.get("base_url") + "/api/list.json", response_class=JSONResponse) -def api_list(request: Request, mode: str, value: str, amount: int): +def api_list(request: Request, mode: str, value: str, amount: int) -> None: if mode is None or value is None or amount is None: raise HTTPException(status_code=500, detail="No filter specified") elif amount > config.get("api_limit"): @@ -148,7 +148,7 @@ LIMIT ?", [amount] return domainlist @router.get(config.get("base_url") + "/api/top.json", response_class=JSONResponse) -def api_index(request: Request, mode: str, value: str, amount: int): +def api_index(request: Request, mode: str, value: str, amount: int) -> None: if mode is None or value is None or amount is None: raise HTTPException(status_code=500, detail="No filter specified") elif amount > config.get("api_limit"): @@ -235,7 +235,7 @@ LIMIT ?", [ return result @router.get(config.get("base_url") + "/api/domain.json", response_class=JSONResponse) -def api_domain(domain: str): +def api_domain(domain: str) -> None: if domain is None: raise HTTPException(status_code=400, detail="Invalid request, parameter 'domain' missing") @@ -255,7 +255,7 @@ def api_domain(domain: str): return JSONResponse(status_code=200, content=dict(domain_data)) @router.get(config.get("base_url") + "/api/mutual.json", response_class=JSONResponse) -def api_mutual(domains: list[str] = Query()): +def api_mutual(domains: list[str] = Query()) -> None: """Return 200 if federation is open between the two, 4xx otherwise""" database.cursor.execute( "SELECT block_level FROM blocks " \ @@ -278,7 +278,7 @@ def api_mutual(domains: list[str] = Query()): return JSONResponse(status_code=200, content={}) @router.get(config.get("base_url") + "/.well-known/nodeinfo", response_class=JSONResponse) -def wellknown_nodeinfo(request: Request): +def wellknown_nodeinfo(request: Request) -> None: return JSONResponse(status_code=200, content={ "links": ({ "rel" : "http://nodeinfo.diaspora.software/ns/schema/1.0", @@ -287,7 +287,7 @@ def wellknown_nodeinfo(request: Request): }) @router.get(config.get("base_url") + "/nodeinfo/1.0", response_class=JSONResponse) -def nodeinfo_1_0(request: Request): +def nodeinfo_1_0(request: Request) -> None: return JSONResponse(status_code=200, content={ "version": "1.0", "software": { @@ -329,7 +329,7 @@ def nodeinfo_1_0(request: Request): }) @router.get(config.get("base_url") + "/api/v1/instance/domain_blocks", response_class=JSONResponse) -def api_domain_blocks(request: Request): +def api_domain_blocks(request: Request) -> None: blocked = blacklist.get_all() blocking = list() @@ -344,7 +344,7 @@ def api_domain_blocks(request: Request): return JSONResponse(status_code=200, content=blocking) @router.get(config.get("base_url") + "/api/v1/instance/peers", response_class=JSONResponse) -def api_peers(request: Request): +def api_peers(request: Request) -> None: database.cursor.execute("SELECT domain FROM instances WHERE nodeinfo_url IS NOT NULL") peers = list() @@ -354,7 +354,7 @@ def api_peers(request: Request): return JSONResponse(status_code=200, content=peers) @router.get(config.get("base_url") + "/scoreboard") -def scoreboard(request: Request, mode: str, amount: int): +def scoreboard(request: Request, mode: str, amount: int) -> None: if mode == "": raise HTTPException(status_code=400, detail="No mode specified") elif amount <= 0: @@ -379,7 +379,7 @@ def scoreboard(request: Request, mode: str, amount: int): }) @router.get(config.get("base_url") + "/list") -def list_domains(request: Request, mode: str, value: str, amount: int = config.get("api_limit")): +def list_domains(request: Request, mode: str, value: str, amount: int = config.get("api_limit")) -> None: if mode == "detection_mode" and not instances.valid(value, "detection_mode"): raise HTTPException(status_code=500, detail="Invalid detection mode provided") @@ -405,7 +405,7 @@ def list_domains(request: Request, mode: str, value: str, amount: int = config.g }) @router.get(config.get("base_url") + "/top") -def top(request: Request, mode: str, value: str, amount: int = config.get("api_limit")): +def top(request: Request, mode: str, value: str, amount: int = config.get("api_limit")) -> None: if mode == "block_level" and not blocks.valid(value, "block_level"): raise HTTPException(status_code=500, detail="Invalid block level provided") elif mode in ["domain", "reverse"] and not domain_helper.is_wanted(value): @@ -437,7 +437,7 @@ def top(request: Request, mode: str, value: str, amount: int = config.get("api_l }) @router.get(config.get("base_url") + "/infos") -def infos(request: Request, domain: str): +def infos(request: Request, domain: str) -> None: if domain is None: raise HTTPException(status_code=400, detail="Invalid request, parameter 'domain' missing") @@ -474,7 +474,7 @@ def infos(request: Request, domain: str): }) @router.get(config.get("base_url") + "/rss") -def rss(request: Request, domain: str = None): +def rss(request: Request, domain: str = None) -> None: if domain is not None: domain = tidyup.domain(domain).encode("idna").decode("utf-8") @@ -524,14 +524,14 @@ LIMIT ?", [config.get("rss_limit")]) }) @router.get(config.get("base_url") + "/robots.txt", response_class=PlainTextResponse) -def robots(request: Request): +def robots(request: Request) -> None: return templates.TemplateResponse("views/robots.txt", { "request" : request, "base_url": config.get("base_url") }) @router.get(config.get("base_url") + "/") -def index(request: Request): +def index(request: Request) -> None: # Get info response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/info.json") diff --git a/fba/boot.py b/fba/boot.py index 7dc826c..5b9f584 100644 --- a/fba/boot.py +++ b/fba/boot.py @@ -29,7 +29,7 @@ logger = logging.getLogger(__name__) # Argument parser _PARSER = None -def init_parser(): +def init_parser() -> None: logger.debug("CALLED!") global _PARSER @@ -276,7 +276,7 @@ def init_parser(): logger.debug("EXIT!") -def run_command(): +def run_command() -> None: logger.debug("CALLED!") args = _PARSER.parse_args() @@ -291,7 +291,7 @@ def run_command(): logger.debug("status=%d - EXIT!", status) return status -def shutdown(): +def shutdown() -> None: logger.debug("Closing database connection ...") database.connection.close() locking.release() diff --git a/fba/helpers/cache.py b/fba/helpers/cache.py index 325e583..ecdb3fc 100644 --- a/fba/helpers/cache.py +++ b/fba/helpers/cache.py @@ -32,7 +32,7 @@ def key_exists(key: str) -> bool: logger.debug("exists='%s' - EXIT!", exists) return exists -def set_all(key: str, rows: list, value: any): +def set_all(key: str, rows: list, value: any) -> None: logger.debug("key='%s',rows()=%d,value[]='%s' - CALLED!", key, len(rows), type(value)) if not isinstance(key, str): @@ -52,7 +52,7 @@ def set_all(key: str, rows: list, value: any): logger.debug("EXIT!") -def set_sub_key(key: str, sub: str, value: any): +def set_sub_key(key: str, sub: str, value: any) -> None: logger.debug("key='%s',sub='%s',value[]='%s' - CALLED!", key, sub, type(value)) if not isinstance(key, str): diff --git a/fba/helpers/domain.py b/fba/helpers/domain.py index 6b0fd10..8b8bfd3 100644 --- a/fba/helpers/domain.py +++ b/fba/helpers/domain.py @@ -30,7 +30,7 @@ from fba.models import instances logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) -def raise_on(domain: str): +def raise_on(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) if not isinstance(domain, str): diff --git a/fba/helpers/locking.py b/fba/helpers/locking.py index f553d04..c507f72 100644 --- a/fba/helpers/locking.py +++ b/fba/helpers/locking.py @@ -27,7 +27,7 @@ logger = logging.getLogger(__name__) lockfile = tempfile.gettempdir() + '/fba.lock' LOCK = None -def acquire(): +def acquire() -> None: global LOCK logger.debug("CALLED!") @@ -42,7 +42,7 @@ def acquire(): logger.debug("EXIT!") -def release(): +def release() -> None: logger.debug("CALLED!") if LOCK is not None: logger.debug("Releasing lock ...") diff --git a/fba/helpers/processing.py b/fba/helpers/processing.py index 464eded..1999b2b 100644 --- a/fba/helpers/processing.py +++ b/fba/helpers/processing.py @@ -120,7 +120,7 @@ def block(blocker: str, blocked: str, reason: str, block_level: str) -> bool: logger.debug("added='%s' - EXIT!", added) return added -def csv_block(blocker: str, url: str, command: str): +def csv_block(blocker: str, url: str, command: str) -> None: logger.debug("blocker='%s',url='%s',command='%s' - CALLED!", blocker, url, command) domain_helper.raise_on(blocker) diff --git a/fba/http/federation.py b/fba/http/federation.py index ad10c0c..71f0d57 100644 --- a/fba/http/federation.py +++ b/fba/http/federation.py @@ -52,7 +52,7 @@ _api_paths = [ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) -def fetch_instances(domain: str, origin: str, software: str, command: str, path: str = None): +def fetch_instances(domain: str, origin: str, software: str, command: str, path: str = None) -> None: global _DEPTH logger.debug("domain='%s',origin='%s',software='%s',command='%s',path='%s',_DEPTH=%d - CALLED!", domain, origin, software, command, path, _DEPTH) domain_helper.raise_on(domain) diff --git a/fba/http/network.py b/fba/http/network.py index 2daa27c..b823eaa 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -213,7 +213,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: logger.debug("Returning json_reply(%d)[]='%s' - EXIT!", len(json_reply), type(json_reply)) return json_reply -def send_bot_post(domain: str, blocklist: list): +def send_bot_post(domain: str, blocklist: list) -> None: logger.debug("domain='%s',blocklist()=%d - CALLED!", domain, len(blocklist)) domain_helper.raise_on(domain) diff --git a/fba/models/blocks.py b/fba/models/blocks.py index 1ebe3ea..d3e93a3 100644 --- a/fba/models/blocks.py +++ b/fba/models/blocks.py @@ -27,7 +27,7 @@ from fba.helpers import tidyup logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) -def get_reason(blocker: str, blocked: str, block_level: str): +def get_reason(blocker: str, blocked: str, block_level: str) -> str: logger.debug("blocker='%s',blocked='%s',block_level='%s' - CALLED!", blocker, blocked, block_level) domain_helper.raise_on(blocker) domain_helper.raise_on(blocked) @@ -60,7 +60,7 @@ def get_reason(blocker: str, blocked: str, block_level: str): logger.debug("row[reason]='%s' - EXIT!", row["reason"]) return row["reason"] -def update_reason(reason: str, blocker: str, blocked: str, block_level: str): +def update_reason(reason: str, blocker: str, blocked: str, block_level: str) -> None: logger.debug("reason='%s',blocker='%s',blocked='%s',block_level='%s' - CALLED!", reason, blocker, blocked, block_level) domain_helper.raise_on(blocker) domain_helper.raise_on(blocked) @@ -93,7 +93,7 @@ def update_reason(reason: str, blocker: str, blocked: str, block_level: str): logger.debug("EXIT!") -def update_last_seen(blocker: str, blocked: str, block_level: str): +def update_last_seen(blocker: str, blocked: str, block_level: str) -> None: logger.debug("blocker='%s',blocked='%s',block_level='%s' - CALLED!", blocker, blocked, block_level) domain_helper.raise_on(blocker) domain_helper.raise_on(blocked) @@ -161,7 +161,7 @@ def is_instance_blocked(blocker: str, blocked: str, block_level: str = None) -> logger.debug("is_blocked='%s' - EXIT!", is_blocked) return is_blocked -def add(blocker: str, blocked: str, reason: str, block_level: str): +def add(blocker: str, blocked: str, reason: str, block_level: str) -> None: logger.debug("blocker='%s',blocked='%s',reason='%s',block_level='%s' - CALLED!", blocker, blocked, reason, block_level) domain_helper.raise_on(blocker) domain_helper.raise_on(blocked) @@ -222,7 +222,7 @@ def valid(value: str, column: str) -> bool: logger.debug("is_valid='%s' - EXIT!", is_valid) return is_valid -def translate_idnas(rows: list, column: str): +def translate_idnas(rows: list, column: str) -> None: logger.debug("rows[]='%s',column='%s' - CALLED!", type(rows), column) if not isinstance(rows, list): @@ -276,7 +276,7 @@ def alias_block_level(block_level: str) -> str: logger.debug("block_level='%s' - EXIT!", block_level) return block_level -def delete(domain: str): +def delete(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) diff --git a/fba/models/error_log.py b/fba/models/error_log.py index 672c775..2e99161 100644 --- a/fba/models/error_log.py +++ b/fba/models/error_log.py @@ -26,7 +26,7 @@ from fba.helpers import domain as domain_helper logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) -def add(domain: str, error: dict): +def add(domain: str, error: dict) -> None: logger.debug("domain='%s',error[]='%s' - CALLED!", domain, type(error)) domain_helper.raise_on(domain) diff --git a/fba/models/instances.py b/fba/models/instances.py index 77e8922..2fe116d 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -76,7 +76,7 @@ _pending = { "software" : {}, } -def _set_data(key: str, domain: str, value: any): +def _set_data(key: str, domain: str, value: any) -> None: logger.debug("key='%s',domain='%s',value[]='%s' - CALLED!", key, domain, type(value)) domain_helper.raise_on(domain) @@ -117,7 +117,7 @@ def has_pending(domain: str) -> bool: logger.debug("has='%s' - EXIT!", has) return has -def update(domain: str): +def update(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) @@ -172,7 +172,7 @@ def update(domain: str): logger.debug("EXIT!") -def add(domain: str, origin: str, command: str, path: str = None, software: str = None): +def add(domain: str, origin: str, command: str, path: str = None, software: str = None) -> None: logger.debug("domain='%s',origin='%s',command='%s',path='%s',software='%s' - CALLED!", domain, origin, command, path, software) domain_helper.raise_on(domain) @@ -244,7 +244,7 @@ def add(domain: str, origin: str, command: str, path: str = None, software: str logger.debug("EXIT!") -def set_last_nodeinfo(domain: str): +def set_last_nodeinfo(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) @@ -253,7 +253,7 @@ def set_last_nodeinfo(domain: str): logger.debug("EXIT!") -def set_last_error(domain: str, error: dict): +def set_last_error(domain: str, error: dict) -> None: logger.debug("domain='%s',error[]='%s' - CALLED!", domain, type(error)) domain_helper.raise_on(domain) @@ -300,7 +300,7 @@ def set_last_error(domain: str, error: dict): logger.debug("EXIT!") -def set_success(domain: str): +def set_success(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) @@ -413,7 +413,7 @@ def deobfuscate(char: str, domain: str, blocked_hash: str = None) -> tuple: logger.debug("row[]='%s' - EXIT!", type(row)) return row -def set_last_blocked(domain: str): +def set_last_blocked(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) @@ -421,7 +421,7 @@ def set_last_blocked(domain: str): _set_data("last_blocked", domain, time.time()) logger.debug("EXIT!") -def set_last_instance_fetch(domain: str): +def set_last_instance_fetch(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) @@ -429,7 +429,7 @@ def set_last_instance_fetch(domain: str): _set_data("last_instance_fetch", domain, time.time()) logger.debug("EXIT!") -def set_last_response_time(domain: str, response_time: float): +def set_last_response_time(domain: str, response_time: float) -> None: logger.debug("domain='%s',response_time=%d - CALLED!", domain, response_time) domain_helper.raise_on(domain) @@ -442,7 +442,7 @@ def set_last_response_time(domain: str, response_time: float): _set_data("last_response_time", domain, response_time) logger.debug("EXIT!") -def set_total_peers(domain: str, peers: list): +def set_total_peers(domain: str, peers: list) -> None: logger.debug("domain='%s',peers()=%d - CALLED!", domain, len(peers)) domain_helper.raise_on(domain) @@ -453,7 +453,7 @@ def set_total_peers(domain: str, peers: list): _set_data("total_peers", domain, len(peers)) logger.debug("EXIT!") -def set_total_blocks(domain: str, blocks: list): +def set_total_blocks(domain: str, blocks: list) -> None: logger.debug("domain='%s',blocks()=%d - CALLED!", domain, len(blocks)) domain_helper.raise_on(domain) @@ -464,7 +464,7 @@ def set_total_blocks(domain: str, blocks: list): _set_data("total_blocks", domain, len(blocks)) logger.debug("EXIT!") -def set_obfuscated_blocks(domain: str, obfuscated: int): +def set_obfuscated_blocks(domain: str, obfuscated: int) -> None: logger.debug("domain='%s',obfuscated=%d - CALLED!", domain, obfuscated) domain_helper.raise_on(domain) @@ -477,7 +477,7 @@ def set_obfuscated_blocks(domain: str, obfuscated: int): _set_data("obfuscated_blocks", domain, obfuscated) logger.debug("EXIT!") -def set_nodeinfo_url(domain: str, url: str): +def set_nodeinfo_url(domain: str, url: str) -> None: logger.debug("domain='%s',url='%s' - CALLED!", domain, url) domain_helper.raise_on(domain) @@ -492,7 +492,7 @@ def set_nodeinfo_url(domain: str, url: str): _set_data("nodeinfo_url", domain, url) logger.debug("EXIT!") -def set_detection_mode(domain: str, mode: str): +def set_detection_mode(domain: str, mode: str) -> None: logger.debug("domain='%s',mode='%s' - CALLED!", domain, mode) domain_helper.raise_on(domain) @@ -505,7 +505,7 @@ def set_detection_mode(domain: str, mode: str): _set_data("detection_mode", domain, mode) logger.debug("EXIT!") -def set_has_obfuscation(domain: str, status: bool): +def set_has_obfuscation(domain: str, status: bool) -> None: logger.debug("domain='%s',status='%s' - CALLED!", domain, status) domain_helper.raise_on(domain) @@ -516,7 +516,7 @@ def set_has_obfuscation(domain: str, status: bool): _set_data("has_obfuscation", domain, status) logger.debug("EXIT!") -def set_original_software(domain: str, software: str): +def set_original_software(domain: str, software: str) -> None: logger.debug("domain='%s',software='%s' - CALLED!", domain, software) domain_helper.raise_on(domain) @@ -529,7 +529,7 @@ def set_original_software(domain: str, software: str): _set_data("original_software", domain, software) logger.debug("EXIT!") -def set_software(domain: str, software: str): +def set_software(domain: str, software: str) -> None: logger.debug("domain='%s',software='%s' - CALLED!", domain, software) domain_helper.raise_on(domain) @@ -563,7 +563,7 @@ def valid(value: str, column: str) -> bool: logger.debug("is_valid='%s' - EXIT!", is_valid) return is_valid -def delete(domain: str): +def delete(domain: str) -> None: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) @@ -574,7 +574,7 @@ def delete(domain: str): logger.debug("EXIT!") -def translate_idnas(rows: list, column: str): +def translate_idnas(rows: list, column: str) -> None: logger.debug("rows[]='%s' - CALLED!", type(rows)) if not isinstance(rows, list): diff --git a/fba/models/sources.py b/fba/models/sources.py index 0fec364..83a1b3c 100644 --- a/fba/models/sources.py +++ b/fba/models/sources.py @@ -51,7 +51,7 @@ def is_recent(source_domain: str) -> bool: logger.debug("recent='%s' - EXIT!", recent) return recent -def update (source_domain: str): +def update (source_domain: str) -> None: logger.debug("source_domain='%s' - CALLED!", source_domain) domain_helper.raise_on(source_domain) -- 2.39.5