Skip to content

Commit

Permalink
add pydantic validators for rust types coming from bridge (#184)
Browse files Browse the repository at this point in the history
* add pydantic validators for rust types coming from bridge

* cleanup logger
  • Loading branch information
saraswatpuneet authored Dec 11, 2023
1 parent 96457ea commit 9b89f50
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
21 changes: 20 additions & 1 deletion querent/config/collector/collector_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class CollectorConfig(BaseModel):
# Custom validator for ChannelCommandInterface
@validator("channel", pre=True, allow_reuse=True)
def validate_channel(cls, value):
# Perform any additional validation logic here
if not hasattr(value, "receive_in_python") or not hasattr(
value, "send_in_rust"
):
raise ValueError(
"Invalid type for channel. Must have 'receive_in_python' and 'send_in_rust' functions."
)
return value


Expand All @@ -36,6 +41,20 @@ class FSCollectorConfig(CollectorConfig):
id: str
root_path: str
chunk_size: int = 1024
channel: Any

# Custom validator for ChannelCommandInterface
@validator("channel", pre=True, allow_reuse=True)
def validate_channel(cls, value):
if (
value is None
or not hasattr(value, "receive_in_python")
or not hasattr(value, "send_in_rust")
):
raise ValueError(
"Invalid type for channel. Must have 'receive_in_python' and 'send_in_rust' functions."
)
return value


class AzureCollectConfig(CollectorConfig):
Expand Down
7 changes: 6 additions & 1 deletion querent/config/engine/engine_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ class EngineConfig(BaseModel):
# Custom validator for ChannelCommandInterface
@validator("channel", pre=True, allow_reuse=True)
def validate_channel(cls, value):
# Perform any additional validation logic here
if not hasattr(value, "receive_in_python") or not hasattr(
value, "send_in_rust"
):
raise ValueError(
"Invalid type for channel. Must have 'receive_in_python' and 'send_in_rust' functions."
)
return value
14 changes: 11 additions & 3 deletions querent/config/workflow/workflow_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ class WorkflowConfig(BaseModel):

@validator("channel", pre=True, allow_reuse=True)
def validate_channel(cls, value):
# Perform any additional validation logic here
if not hasattr(value, "receive_in_python") or not hasattr(
value, "send_in_rust"
):
raise ValueError(
"Invalid type for channel. Must have 'receive_in_python' and 'send_in_rust' functions."
)
return value

@validator("event_handler", pre=True, allow_reuse=True)
def validate_event_handler(cls, value):
# Perform any additional validation logic here
return value
# value must have handle_event function
if not hasattr(value, "handle_event"):
raise ValueError(
"Invalid type for event_handler. Must have 'handle_event' function."
)
18 changes: 12 additions & 6 deletions querent/logging/logger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
from logging.handlers import RotatingFileHandler
from querent.config.logger.logger_config import (
LOGGING_LEVEL,
LOGGING_FORMAT,
LOGGING_DATE_FORMAT,
LOGGING_FILE,
)
from querent.logging.handlers import create_console_handler, create_file_handler


def setup_logger(logger_name: str, log_file_id: str) -> logging.Logger:
Expand All @@ -17,15 +17,21 @@ def setup_logger(logger_name: str, log_file_id: str) -> logging.Logger:
logger = logging.getLogger(logger_name)
logger.setLevel(LOGGING_LEVEL)

console_handler = create_console_handler(LOGGING_FORMAT, LOGGING_DATE_FORMAT)
file_handler = create_file_handler(
log_file_name, LOGGING_FORMAT, LOGGING_DATE_FORMAT
console_handler = logging.StreamHandler()
console_handler.setFormatter(
logging.Formatter(LOGGING_FORMAT, LOGGING_DATE_FORMAT)
)

logger.addHandler(console_handler)

file_handler = RotatingFileHandler(
log_file_name, maxBytes=1024 * 1024, backupCount=5
)
file_handler.setFormatter(
logging.Formatter(LOGGING_FORMAT, LOGGING_DATE_FORMAT)
)
logger.addHandler(file_handler)

return logger
except Exception as e:
print(f"Error while setting up logger: {e}")
logger.error(f"Error while setting up logger: {e}")
return None

0 comments on commit 9b89f50

Please sign in to comment.