Jelajahi Sumber

refactor: Simplify function signatures and improve readability in ACL and HTTP access services

Manuel 9 bulan lalu
induk
melakukan
c679fee218

+ 16 - 14
routes/admin_routes.py

@@ -927,14 +927,13 @@ def _enable_single_blocklist(source_url: str | None, cm) -> tuple[bool, str]:
                 lines = [
                     ln
                     for ln in lines
-                    if not (ln.strip() == acl_line or ln.strip() == acl_line.replace('"', "'"))
+                    if not (
+                        ln.strip() == acl_line
+                        or ln.strip() == acl_line.replace('"', "'")
+                    )
                 ]
                 # Remove orphaned comments for this label
-                lines = [
-                    ln
-                    for ln in lines
-                    if not ln.strip() == comment_line
-                ]
+                lines = [ln for ln in lines if not ln.strip() == comment_line]
                 lines.append(comment_line)
                 lines.append(acl_line)
                 new_content = "\n".join(lines)
@@ -947,13 +946,11 @@ def _enable_single_blocklist(source_url: str | None, cm) -> tuple[bool, str]:
             lines = [
                 ln
                 for ln in lines
-                if not (ln.strip() == acl_line or ln.strip() == acl_line.replace('"', "'"))
-            ]
-            lines = [
-                ln
-                for ln in lines
-                if not ln.strip() == comment_line
+                if not (
+                    ln.strip() == acl_line or ln.strip() == acl_line.replace('"', "'")
+                )
             ]
+            lines = [ln for ln in lines if not ln.strip() == comment_line]
             acl_section_end = -1
             for i, line in enumerate(lines):
                 if line.strip().startswith("acl "):
@@ -1013,7 +1010,9 @@ def _disable_single_blocklist(source_url: str | None, cm) -> tuple[bool, str]:
                     stripped = ln.strip()
                     if stripped == acl_line or stripped == acl_line.replace('"', "'"):
                         # Also remove the comment above
-                        if new_lines and new_lines[-1].strip().startswith("# Blocklist:"):
+                        if new_lines and new_lines[-1].strip().startswith(
+                            "# Blocklist:"
+                        ):
                             new_lines.pop()
                         continue
                     new_lines.append(ln)
@@ -1080,7 +1079,10 @@ def blocklist_toggle():
             show_details = bool(current_app.debug)
         except RuntimeError:
             show_details = False
-        resp = {"status": "error", "message": "Error interno al cambiar estado de blocklist"}
+        resp = {
+            "status": "error",
+            "message": "Error interno al cambiar estado de blocklist",
+        }
         if show_details:
             resp["details"] = str(e)
         return jsonify(resp), 500

+ 5 - 7
services/squid/acls_service.py

@@ -359,7 +359,9 @@ def _remove_old_blocklist_acls(lines: list[str], acl_name: str) -> list[str]:
     return filtered
 
 
-def _build_acl_lines(acl_name: str, file_paths: list[tuple[str, str, int]]) -> list[str]:
+def _build_acl_lines(
+    acl_name: str, file_paths: list[tuple[str, str, int]]
+) -> list[str]:
     """Build comment + acl directive lines for each blocklist file.
 
     *file_paths* is a list of ``(label, filepath, domain_count)`` tuples.
@@ -371,9 +373,7 @@ def _build_acl_lines(acl_name: str, file_paths: list[tuple[str, str, int]]) -> l
     return new_lines
 
 
-def add_acl_blocklist(
-    acl_name: str, config_manager
-) -> tuple[bool, str]:
+def add_acl_blocklist(acl_name: str, config_manager) -> tuple[bool, str]:
     """Create dstdomain ACLs backed by files with active blacklist domains.
 
     One file is created **per source list** (``source_url``).  Domains that
@@ -498,9 +498,7 @@ def add_acl_blocklist(
         return False, "Error interno al agregar ACL blocklist"
 
 
-def remove_acl_blocklist(
-    acl_name: str, config_manager
-) -> tuple[bool, str]:
+def remove_acl_blocklist(acl_name: str, config_manager) -> tuple[bool, str]:
     """Remove all blocklist ACL directives and their domain files.
 
     1. Remove all ``acl <acl_name> dstdomain …blocklist_…`` lines from Squid

+ 4 - 12
services/squid/http_access_service.py

@@ -132,9 +132,7 @@ def add_http_access(
         return False, "Error interno al agregar regla http_access"
 
 
-def add_http_deny_blocklist(
-    acl_name: str, config_manager
-) -> tuple[bool, str]:
+def add_http_deny_blocklist(acl_name: str, config_manager) -> tuple[bool, str]:
     """Add an ``http_access deny <acl_name>`` rule for the blocklist.
 
     The deny rule is inserted **before** the first ``http_access allow`` rule
@@ -157,9 +155,7 @@ def add_http_deny_blocklist(
 
     try:
         if config_manager.is_modular:
-            http_content = config_manager.read_modular_config(
-                "120_http_access.conf"
-            )
+            http_content = config_manager.read_modular_config("120_http_access.conf")
             if http_content is not None:
                 lines = http_content.split("\n")
 
@@ -216,9 +212,7 @@ def add_http_deny_blocklist(
         return False, "Error interno al agregar regla http_access deny"
 
 
-def remove_http_deny_blocklist(
-    acl_name: str, config_manager
-) -> tuple[bool, str]:
+def remove_http_deny_blocklist(acl_name: str, config_manager) -> tuple[bool, str]:
     """Remove the ``http_access deny <acl_name>`` rule and its comment.
 
     Supports both modular (``120_http_access.conf``) and monolithic config.
@@ -259,9 +253,7 @@ def remove_http_deny_blocklist(
         removed = False
 
         if config_manager.is_modular:
-            http_content = config_manager.read_modular_config(
-                "120_http_access.conf"
-            )
+            http_content = config_manager.read_modular_config("120_http_access.conf")
             if http_content is not None:
                 lines = http_content.split("\n")
                 cleaned = _strip_deny(lines)