Skip to content

Commit

Permalink
core: helper: add sanitized_name to services metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Williangalvani committed Jul 3, 2023
1 parent 4c1fcdd commit 2f02725
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/services/helper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import http
import logging
import os
import re
from enum import Enum
from pathlib import Path
from typing import Any, Dict, List, Optional
Expand Down Expand Up @@ -58,6 +59,7 @@ class ServiceMetadata(BaseModel):
route: Optional[str]
new_page: Optional[bool]
api: str
sanitized_name: Optional[str]


class ServiceInfo(BaseModel):
Expand Down Expand Up @@ -97,6 +99,7 @@ def detect_service(port: int) -> ServiceInfo:
with requests.get(f"http://127.0.0.1:{port}/register_service", timeout=0.2) as response:
if response.status_code == http.HTTPStatus.OK:
info.metadata = ServiceMetadata.parse_obj(response.json())
info.metadata.sanitized_name = re.sub(r"[^a-z0-9]", "", info.metadata.name.lower())
except Exception:
# This should be avoided by the first try block, but better safe than sorry
pass
Expand Down Expand Up @@ -140,7 +143,7 @@ def scan_ports() -> List[ServiceInfo]:
# And check if there is a webpage available that is not us
# Use it as a set to remove duplicated ports
ports = set(connection.laddr.port for connection in connections)
services = (Helper.detect_service(port) for port in ports if port != PORT)
services = [Helper.detect_service(port) for port in ports if port != PORT]
return [service for service in services if service.valid]

@staticmethod
Expand Down

0 comments on commit 2f02725

Please sign in to comment.