Skip to content

Commit

Permalink
fix: Set a logger for service_replacer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramimashkouk committed Nov 29, 2024
1 parent b62d9dd commit 11b5101
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
from ast import NodeTransformer
from typing import Dict, List
from pathlib import Path

import logging
from chatsky_ui.core.logger_config import get_logger

logger = get_logger(__name__)


class ServiceReplacer(NodeTransformer):
def __init__(self, new_services: List[str]):
self.new_services_classes = self._get_classes_def(new_services)
self._logger = None

@property
def logger(self):
if self._logger is None:
raise ValueError("Logger has not been configured. Call set_logger() first.")
return self._logger

def set_logger(self):
self._logger = get_logger(__name__)

def _get_classes_def(self, services_code: List[str]) -> Dict[str, ast.ClassDef]:
parsed_codes = [ast.parse(service_code) for service_code in services_code]
Expand All @@ -24,19 +32,19 @@ def _extract_class_defs(self, parsed_code: ast.Module, service_code: str):
if isinstance(node, ast.ClassDef):
classes[node.name] = node
else:
logger.error("No class definition found in new_service: %s", service_code)
self.logger.error("No class definition found in new_service: %s", service_code)
return classes

def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
logger.debug("Visiting class '%s' and comparing with: %s", node.name, self.new_services_classes.keys())
self.logger.debug("Visiting class '%s' and comparing with: %s", node.name, self.new_services_classes.keys())
if node.name in self.new_services_classes:
return self._get_class_def(node)
return node

def _get_class_def(self, node: ast.ClassDef) -> ast.ClassDef:
service = self.new_services_classes[node.name]
del self.new_services_classes[node.name]
logger.info("Updating class '%s'", node.name)
self.logger.info("Updating class '%s'", node.name)
return service

def generic_visit(self, node: ast.AST):
Expand All @@ -46,7 +54,7 @@ def generic_visit(self, node: ast.AST):
return node

def _append_new_services(self, node: ast.Module):
logger.info("Services not found, appending new services: %s", list(self.new_services_classes.keys()))
self.logger.info("Services not found, appending new services: %s", list(self.new_services_classes.keys()))
for _, service in self.new_services_classes.items():
node.body.append(service)

Expand All @@ -56,6 +64,7 @@ def store_custom_service(services_path: Path, services: List[str]):
conditions_tree = ast.parse(file.read())

replacer = ServiceReplacer(services)
replacer.set_logger()
replacer.visit(conditions_tree)

with open(services_path, "w") as file:
Expand Down
2 changes: 1 addition & 1 deletion backend/chatsky_ui/services/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async def check_telegram_readiness(stream, name):
return response.json()["status"] == "ok"
except Exception as e:
self.logger.info(
f"Process '{self.id}' isn't alive on port '{HTTP_INTERFACE_PORT}'. "
f"Process '{self.id}' isn't alive on port '{HTTP_INTERFACE_PORT}' yet. "
f"Ignore this if you're not connecting via HTTPInterface. Exception caught: {e}"
)

Expand Down

0 comments on commit 11b5101

Please sign in to comment.