Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Run make format #65

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/codegate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def cli() -> None:
"""Codegate - A configurable service gateway."""
pass


@cli.command()
@click.option(
"--port",
Expand Down Expand Up @@ -90,7 +91,7 @@ def serve(
"port": cfg.port,
"log_level": cfg.log_level.value,
"log_format": cfg.log_format.value,
}
},
)

# TODO: Jakub Implement actual server logic here
Expand All @@ -103,6 +104,7 @@ def serve(
click.echo(f"Error: {e}", err=True)
sys.exit(1)


def main() -> None:
"""Main entry point for the CLI."""
cli()
Expand Down
8 changes: 6 additions & 2 deletions src/codegate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LogLevel(str, Enum):
DEBUG = "DEBUG"

@classmethod
def _missing_(cls, value: str) -> Optional['LogLevel']:
def _missing_(cls, value: str) -> Optional["LogLevel"]:
"""Handle case-insensitive lookup of enum values."""
try:
# Convert to uppercase and look up directly
Expand All @@ -29,14 +29,15 @@ def _missing_(cls, value: str) -> Optional['LogLevel']:
f"Valid levels are: {', '.join(level.value for level in cls)}"
)


class LogFormat(str, Enum):
"""Valid log formats."""

JSON = "JSON"
TEXT = "TEXT"

@classmethod
def _missing_(cls, value: str) -> Optional['LogFormat']:
def _missing_(cls, value: str) -> Optional["LogFormat"]:
"""Handle case-insensitive lookup of enum values."""
try:
# Convert to uppercase and look up directly
Expand All @@ -50,10 +51,12 @@ def _missing_(cls, value: str) -> Optional['LogFormat']:

class ConfigurationError(Exception):
"""Raised when there's an error in configuration."""

def __init__(self, message: str) -> None:
super().__init__(message)
# You can add additional logging or handling here if needed


@dataclass
class Config:
"""Application configuration with priority resolution."""
Expand Down Expand Up @@ -174,6 +177,7 @@ def load(
except ConfigurationError as e:
# Log warning but continue with defaults
import logging

logging.warning(f"Failed to load config file: {e}")

# Override with environment variables
Expand Down
39 changes: 27 additions & 12 deletions src/codegate/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,28 @@ def format(self, record: logging.LogRecord) -> str:
extra_attrs = {}
for key, value in record.__dict__.items():
if key not in {
"args", "asctime", "created", "exc_info", "exc_text", "filename",
"funcName", "levelname", "levelno", "lineno", "module", "msecs",
"msg", "name", "pathname", "process", "processName", "relativeCreated",
"stack_info", "thread", "threadName", "extra"
"args",
"asctime",
"created",
"exc_info",
"exc_text",
"filename",
"funcName",
"levelname",
"levelno",
"lineno",
"module",
"msecs",
"msg",
"name",
"pathname",
"process",
"processName",
"relativeCreated",
"stack_info",
"thread",
"threadName",
"extra",
}:
extra_attrs[key] = value

Expand Down Expand Up @@ -87,13 +105,11 @@ def __init__(self) -> None:
"""Initialize the text formatter."""
super().__init__(
fmt="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S.%03dZ"
datefmt="%Y-%m-%dT%H:%M:%S.%03dZ",
)

def formatTime( # noqa: N802
self,
record: logging.LogRecord,
datefmt: Optional[str] = None
self, record: logging.LogRecord, datefmt: Optional[str] = None
) -> str:
"""Format the time with millisecond precision.

Expand All @@ -109,8 +125,7 @@ def formatTime( # noqa: N802


def setup_logging(
log_level: Optional[LogLevel] = None,
log_format: Optional[LogFormat] = None
log_level: Optional[LogLevel] = None, log_format: Optional[LogFormat] = None
) -> None:
"""Configure the logging system.

Expand Down Expand Up @@ -160,6 +175,6 @@ def setup_logging(
extra={
"log_level": log_level.value,
"log_format": log_format.value,
"handlers": ["stdout", "stderr"]
}
"handlers": ["stdout", "stderr"],
},
)
18 changes: 11 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pytest
import yaml

from codegate.config import Config


Expand All @@ -19,7 +20,7 @@ def temp_config_file(tmp_path: Path) -> Iterator[Path]:
"port": 8989,
"host": "localhost",
"log_level": "DEBUG",
"log_format": "JSON"
"log_format": "JSON",
}
config_file = tmp_path / "config.yaml"

Expand All @@ -34,12 +35,14 @@ def env_vars() -> Generator[None, None, None]:
"""Set up test environment variables."""
original_env = dict(os.environ)

os.environ.update({
"CODEGATE_APP_PORT": "8989",
"CODEGATE_APP_HOST": "localhost",
"CODEGATE_APP_LOG_LEVEL": "WARNING",
"CODEGATE_LOG_FORMAT": "TEXT"
})
os.environ.update(
{
"CODEGATE_APP_PORT": "8989",
"CODEGATE_APP_HOST": "localhost",
"CODEGATE_APP_LOG_LEVEL": "WARNING",
"CODEGATE_LOG_FORMAT": "TEXT",
}
)

yield

Expand Down Expand Up @@ -73,6 +76,7 @@ def capture_logs(tmp_path: Path) -> Iterator[Path]:

# Create a file handler
import logging

handler = logging.FileHandler(log_file)
logger = logging.getLogger()
logger.addHandler(handler)
Expand Down
51 changes: 29 additions & 22 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from click.testing import CliRunner

from codegate.cli import cli
from codegate.config import LogFormat, LogLevel

Expand Down Expand Up @@ -44,7 +45,7 @@ def test_serve_default_options(cli_runner: CliRunner, mock_logging: Any) -> None
"port": 8989,
"log_level": "INFO",
"log_format": "JSON",
}
},
)


Expand All @@ -56,11 +57,15 @@ def test_serve_custom_options(cli_runner: CliRunner, mock_logging: Any) -> None:
cli,
[
"serve",
"--port", "8989",
"--host", "localhost",
"--log-level", "DEBUG",
"--log-format", "TEXT"
]
"--port",
"8989",
"--host",
"localhost",
"--log-level",
"DEBUG",
"--log-format",
"TEXT",
],
)

assert result.exit_code == 0
Expand All @@ -72,7 +77,7 @@ def test_serve_custom_options(cli_runner: CliRunner, mock_logging: Any) -> None:
"port": 8989,
"log_level": "DEBUG",
"log_format": "TEXT",
}
},
)


Expand All @@ -95,9 +100,7 @@ def test_serve_invalid_log_level(cli_runner: CliRunner) -> None:


def test_serve_with_config_file(
cli_runner: CliRunner,
mock_logging: Any,
temp_config_file: Path
cli_runner: CliRunner, mock_logging: Any, temp_config_file: Path
) -> None:
"""Test serve command with config file."""
with patch("logging.getLogger") as mock_logger:
Expand All @@ -113,9 +116,10 @@ def test_serve_with_config_file(
"port": 8989,
"log_level": "DEBUG",
"log_format": "JSON",
}
},
)


def test_serve_with_nonexistent_config_file(cli_runner: CliRunner) -> None:
"""Test serve command with nonexistent config file."""
result = cli_runner.invoke(cli, ["serve", "--config", "nonexistent.yaml"])
Expand All @@ -124,10 +128,7 @@ def test_serve_with_nonexistent_config_file(cli_runner: CliRunner) -> None:


def test_serve_priority_resolution(
cli_runner: CliRunner,
mock_logging: Any,
temp_config_file: Path,
env_vars: Any
cli_runner: CliRunner, mock_logging: Any, temp_config_file: Path, env_vars: Any
) -> None:
"""Test serve command respects configuration priority."""
with patch("logging.getLogger") as mock_logger:
Expand All @@ -136,12 +137,17 @@ def test_serve_priority_resolution(
cli,
[
"serve",
"--config", str(temp_config_file),
"--port", "8080",
"--host", "example.com",
"--log-level", "ERROR",
"--log-format", "TEXT"
]
"--config",
str(temp_config_file),
"--port",
"8080",
"--host",
"example.com",
"--log-level",
"ERROR",
"--log-format",
"TEXT",
],
)

assert result.exit_code == 0
Expand All @@ -153,13 +159,14 @@ def test_serve_priority_resolution(
"port": 8080,
"log_level": "ERROR",
"log_format": "TEXT",
}
},
)


def test_main_function() -> None:
"""Test main entry point function."""
with patch("codegate.cli.cli") as mock_cli:
from codegate.cli import main

main()
mock_cli.assert_called_once()
8 changes: 3 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
import yaml

from codegate.config import Config, ConfigurationError, LogFormat, LogLevel


Expand Down Expand Up @@ -58,7 +59,7 @@ def test_config_priority_resolution(temp_config_file: Path, env_vars: None) -> N
cli_port=8080,
cli_host="example.com",
cli_log_level="WARNING",
cli_log_format="TEXT"
cli_log_format="TEXT",
)
assert config.port == 8080
assert config.host == "example.com"
Expand Down Expand Up @@ -122,10 +123,7 @@ def config_file_with_format(tmp_path: Path) -> Path:
"""Create a config file with log format."""
config_file = tmp_path / "config.yaml"
with open(config_file, "w") as f:
yaml.dump({
"log_format": "TEXT",
"log_level": "DEBUG"
}, f)
yaml.dump({"log_format": "TEXT", "log_level": "DEBUG"}, f)
return config_file


Expand Down
7 changes: 5 additions & 2 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_json_formatter():
lineno=10,
msg="Test message",
args=(),
exc_info=None
exc_info=None,
)
formatter = JSONFormatter()
formatted_log = formatter.format(log_record)
Expand All @@ -26,6 +26,7 @@ def test_json_formatter():
assert "timestamp" in log_entry
assert "extra" in log_entry


def test_text_formatter():
log_record = logging.LogRecord(
name="test",
Expand All @@ -34,7 +35,7 @@ def test_text_formatter():
lineno=10,
msg="Test message",
args=(),
exc_info=None
exc_info=None,
)
formatter = TextFormatter()
formatted_log = formatter.format(log_record)
Expand All @@ -43,6 +44,7 @@ def test_text_formatter():
assert "test" in formatted_log
assert "Test message" in formatted_log


def test_setup_logging_json_format():
setup_logging(log_level=LogLevel.DEBUG, log_format=LogFormat.JSON)
logger = logging.getLogger("codegate")
Expand All @@ -58,6 +60,7 @@ def test_setup_logging_json_format():
assert log_entry["level"] == "DEBUG"
assert log_entry["message"] == "Debug message"


def test_setup_logging_text_format():
setup_logging(log_level=LogLevel.DEBUG, log_format=LogFormat.TEXT)
logger = logging.getLogger("codegate")
Expand Down