From: Roland Häder Date: Sun, 18 Jun 2023 00:53:52 +0000 (+0200) Subject: WIP: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=961f9756eab01ee3bb910e89467f6175cf6c9567;p=fba.git WIP: - removed geneic-try/except blocks around SQL statements, let them fail and not sys.exit() uncleanly - raise an exception if no row has been updated, e.g. when record isn't found --- diff --git a/fba/helpers/version.py b/fba/helpers/version.py index 6e16661..15b05b3 100644 --- a/fba/helpers/version.py +++ b/fba/helpers/version.py @@ -115,8 +115,8 @@ def strip_hosted_on(software: str) -> str: end = software.find("hosted on ") # DEBUG: print(f"DEBUG: end[{type(end)}]='{end}'") - software = software[0, end].strip() - # DEBUG: print(f"DEBUG: software='{software}'") + software = software[0:end].strip() + # DEBUG: print(f"DEBUG: software[{type(software)}]='{software}'") software = strip_until(software, " - ") diff --git a/fba/models/blocks.py b/fba/models/blocks.py index b0b0fa8..d8a8129 100644 --- a/fba/models/blocks.py +++ b/fba/models/blocks.py @@ -38,30 +38,23 @@ def update_reason(reason: str, blocker: str, blocked: str, block_level: str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") - elif block_level == "accept"; + elif block_level == "accept": raise ValueError("Accepted domains are not wanted here") # DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level) - try: - fba.cursor.execute( - "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND (reason IS NULL OR reason = '') LIMIT 1", - ( - reason, - time.time(), - blocker, - blocked, - block_level - ), - ) - - # DEBUG: print(f"DEBUG: fba.cursor.rowcount={fba.cursor.rowcount}") - if fba.cursor.rowcount == 0: - # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',reason='{reason}' - EXIT!") - return - - except BaseException as exception: - print(f"ERROR: failed SQL query: reason='{reason}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(exception)}]:'{str(exception)}'") - sys.exit(255) + fba.cursor.execute( + "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND (reason IS NULL OR reason = '') LIMIT 1", + [ + reason, + time.time(), + blocker, + blocked, + block_level + ]) + + # DEBUG: print(f"DEBUG: fba.cursor.rowcount={fba.cursor.rowcount}") + if fba.cursor.rowcount == 0: + raise Exception(f"Did not update any rows: domain='{domain}',fields()={len(fields)}") # DEBUG: print("DEBUG: EXIT!") @@ -79,28 +72,21 @@ def update_last_seen(blocker: str, blocked: str, block_level: str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") - elif block_level == "accept"; + elif block_level == "accept": raise ValueError("Accepted domains are not wanted here") - try: - fba.cursor.execute( - "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", - ( - time.time(), - blocker, - blocked, - block_level - ) - ) - - # DEBUG: print(f"DEBUG: fba.cursor.rowcount={fba.cursor.rowcount}") - if fba.cursor.rowcount == 0: - # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' - EXIT!") - return - - except BaseException as exception: - print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(exception)}]:'{str(exception)}'") - sys.exit(255) + fba.cursor.execute( + "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", + [ + time.time(), + blocker, + blocked, + block_level + ]) + + # DEBUG: print(f"DEBUG: fba.cursor.rowcount={fba.cursor.rowcount}") + if fba.cursor.rowcount == 0: + raise Exception(f"Did not update any rows: domain='{domain}',fields()={len(fields)}") # DEBUG: print("DEBUG: EXIT!") @@ -118,7 +104,7 @@ def is_instance_blocked(blocker: str, blocked: str, block_level: str) -> bool: raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") - elif block_level == "accept"; + elif block_level == "accept": raise ValueError("Accepted domains are not wanted here") fba.cursor.execute( @@ -157,7 +143,7 @@ def add_instance(blocker: str, blocked: str, reason: str, block_level: str): raise Exception(f"blocker='{blocker}' is blacklisted but function invoked") elif blacklist.is_blacklisted(blocked): raise Exception(f"blocked='{blocked}' is blacklisted but function invoked") - elif block_level == "accept"; + elif block_level == "accept": raise ValueError("Accepted domains are not wanted here") if reason is not None: @@ -165,20 +151,16 @@ def add_instance(blocker: str, blocked: str, reason: str, block_level: str): reason = tidyup.reason(reason) print(f"INFO: New block: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}'") - try: - fba.cursor.execute( - "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES (?, ?, ?, ?, ?, ?)", - ( - blocker, - blocked, - reason, - block_level, - time.time(), - time.time() - ), - ) - except BaseException as exception: - print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}',exception[{type(exception)}]:'{str(exception)}'") - sys.exit(255) + + fba.cursor.execute( + "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES (?, ?, ?, ?, ?, ?)", + [ + blocker, + blocked, + reason, + block_level, + time.time(), + time.time() + ]) # DEBUG: print("DEBUG: EXIT!") diff --git a/fba/models/instances.py b/fba/models/instances.py index def8401..2042253 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -101,6 +101,8 @@ def update_data(domain: str): raise ValueError("Parameter 'domain' is empty") elif not has_pending(domain): raise Exception(f"domain='{domain}' has no pending instance data, but function invoked") + elif not is_registered(domain): + raise Exception(f"domain='{domain}' cannot be updated while not being registered") # DEBUG: print(f"DEBUG: Updating instance data for domain='{domain}' ...") sql_string = "" @@ -126,27 +128,21 @@ def update_data(domain: str): sql_string = "UPDATE instances SET" + sql_string + " last_updated = ? WHERE domain = ? LIMIT 1" # DEBUG: print("DEBUG: sql_string:", sql_string) - try: - # DEBUG: print("DEBUG: Executing SQL:", sql_string) - fba.cursor.execute(sql_string, fields) - - # DEBUG: print(f"DEBUG: Success! (rowcount={fba.cursor.rowcount })") - if fba.cursor.rowcount == 0: - print(f"WARNING: Did not update any rows: domain='{domain}',fields()={len(fields)} - EXIT!") - return + # DEBUG: print("DEBUG: Executing SQL:", sql_string) + fba.cursor.execute(sql_string, fields) - # DEBUG: print("DEBUG: Committing changes ...") - fba.connection.commit() + # DEBUG: print(f"DEBUG: Success! (rowcount={fba.cursor.rowcount })") + if fba.cursor.rowcount == 0: + raise Exception(f"Did not update any rows: domain='{domain}',fields()={len(fields)}") - # DEBUG: print(f"DEBUG: Deleting _pending for domain='{domain}'") - for key in _pending: - # DEBUG: print(f"DEBUG: domain='{domain}',key='{key}'") - if domain in _pending[key]: - del _pending[key][domain] + # DEBUG: print("DEBUG: Committing changes ...") + fba.connection.commit() - except BaseException as exception: - print(f"ERROR: failed SQL query: domain='{domain}',sql_string='{sql_string}',exception[{type(exception)}]:'{str(exception)}'") - sys.exit(255) + # DEBUG: print(f"DEBUG: Deleting _pending for domain='{domain}'") + for key in _pending: + # DEBUG: print(f"DEBUG: domain='{domain}',key='{key}'") + if domain in _pending[key]: + del _pending[key][domain] # DEBUG: print("DEBUG: EXIT!") diff --git a/fba/networks/mastodon.py b/fba/networks/mastodon.py index 99de141..7d836ed 100644 --- a/fba/networks/mastodon.py +++ b/fba/networks/mastodon.py @@ -231,7 +231,7 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str): if block_level == "": print("WARNING: block_level is empty, domain:", domain) continue - elif block_level == "accept"; + elif block_level == "accept": print(f"DEBUG: domain='{domain}' skipping block_level='accept'") continue diff --git a/fba/networks/pleroma.py b/fba/networks/pleroma.py index aed1783..f017df3 100644 --- a/fba/networks/pleroma.py +++ b/fba/networks/pleroma.py @@ -92,7 +92,7 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str): if block_level == "": print("WARNING: block_level is now empty!") continue - elif block_level == "accept"; + elif block_level == "accept": print(f"DEBUG: domain='{domain}' skipping block_level='accept'") continue @@ -256,7 +256,7 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str): if block_level == "": print("WARNING: block_level is now empty!") continue - elif block_level == "accept"; + elif block_level == "accept": print(f"DEBUG: domain='{domain}' skipping block_level='accept'") continue