]> git.mxchange.org Git - fba.git/commitdiff
WIP:
authorRoland Häder <roland@mxchange.org>
Sun, 18 Jun 2023 00:53:52 +0000 (02:53 +0200)
committerRoland Häder <roland@mxchange.org>
Sun, 18 Jun 2023 00:56:33 +0000 (02:56 +0200)
- 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

fba/helpers/version.py
fba/models/blocks.py
fba/models/instances.py
fba/networks/mastodon.py
fba/networks/pleroma.py

index 6e1666187c5375a2877fa616c8431447dc8e0b49..15b05b366ba15f6ad0b53de57893e14130dbaf60 100644 (file)
@@ -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[0end].strip()
-    # DEBUG: print(f"DEBUG: software='{software}'")
+    software = software[0:end].strip()
+    # DEBUG: print(f"DEBUG: software[{type(software)}]='{software}'")
 
     software = strip_until(software, " - ")
 
index b0b0fa8e1df6a6f59371ec732742212b36944b10..d8a8129e101a5924f21e228cc8a6c9509f84a01d 100644 (file)
@@ -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!")
index def84016668ff6e7b95a2168eb28562fc103e6e6..204225327ad0fec9b6c4ffc1ac354edb3902c333 100644 (file)
@@ -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!")
 
index 99de1416c09418664397405ae2b6e06b42de1e54..7d836eda618371a34e346f8bb13046c81ae128e3 100644 (file)
@@ -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
 
index aed1783cce8d34c4ba43e64ef59271c525a222a7..f017df3c32dcbf56a0435aac2a7fa778615e968f 100644 (file)
@@ -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