Skip to content

Commit 3a2b347

Browse files
committed
Refactor environment variable handling in configuration files
- Introduced a new helper function `_get_int_env_var` to retrieve environment variables as integers with default fallback and error handling. - Updated `config.py` and `server_config.py` to use the new helper function for better clarity and maintainability. - Simplified `_get_env_var` in `config_utils.py` to focus solely on retrieving string values without type casting.
1 parent b11aafa commit 3a2b347

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/gitingest/config.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55

66
from gitingest.utils.config_utils import _get_env_var
77

8-
MAX_FILE_SIZE = _get_env_var("MAX_FILE_SIZE", 10 * 1024 * 1024, int) # Max file size to process in bytes (10 MB)
9-
MAX_FILES = _get_env_var("MAX_FILES", 10_000, int) # Max number of files to process
10-
MAX_TOTAL_SIZE_BYTES = _get_env_var("MAX_TOTAL_SIZE_BYTES", 500 * 1024 * 1024, int) # Max output file size (500 MB)
11-
MAX_DIRECTORY_DEPTH = _get_env_var("MAX_DIRECTORY_DEPTH", 20, int) # Max depth of directory traversal
8+
def _get_int_env_var(key: str, default: int) -> int:
9+
"""Get environment variable as integer with fallback to default."""
10+
try:
11+
return int(_get_env_var(key, str(default)))
12+
except ValueError:
13+
print(f"Warning: Invalid value for GITINGEST_{key}. Using default: {default}")
14+
return default
1215

13-
DEFAULT_TIMEOUT = _get_env_var("DEFAULT_TIMEOUT", 60, int) # Default timeout for git operations in seconds
16+
MAX_FILE_SIZE = _get_int_env_var("MAX_FILE_SIZE", 10 * 1024 * 1024) # Max file size to process in bytes (10 MB)
17+
MAX_FILES = _get_int_env_var("MAX_FILES", 10_000) # Max number of files to process
18+
MAX_TOTAL_SIZE_BYTES = _get_int_env_var("MAX_TOTAL_SIZE_BYTES", 500 * 1024 * 1024) # Max output file size (500 MB)
19+
MAX_DIRECTORY_DEPTH = _get_int_env_var("MAX_DIRECTORY_DEPTH", 20) # Max depth of directory traversal
20+
21+
DEFAULT_TIMEOUT = _get_int_env_var("DEFAULT_TIMEOUT", 60) # Default timeout for git operations in seconds
1422

1523
OUTPUT_FILE_NAME = _get_env_var("OUTPUT_FILE_NAME", "digest.txt")
1624
TMP_BASE_PATH = Path(_get_env_var("TMP_BASE_PATH", tempfile.gettempdir())) / "gitingest"

src/gitingest/utils/config_utils.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import Callable
76

87

9-
def _get_env_var(key: str, default: int | str, cast_func: Callable[[str], int | str] | None = None) -> int | str:
10-
"""Get environment variable with ``GITINGEST_`` prefix and optional type casting.
8+
def _get_env_var(key: str, default: str) -> str:
9+
"""Get environment variable with ``GITINGEST_`` prefix.
1110
1211
Parameters
1312
----------
1413
key : str
1514
The name of the environment variable.
16-
default : int | str
15+
default : str
1716
The default value to return if the environment variable is not set.
18-
cast_func : Callable[[str], int | str] | None
19-
The function to cast the environment variable to the desired type.
2017
2118
Returns
2219
-------
23-
int | str
24-
The value of the environment variable, cast to the desired type if provided.
20+
str
21+
The value of the environment variable as a string.
2522
2623
"""
2724
env_key = f"GITINGEST_{key}"
@@ -30,11 +27,4 @@ def _get_env_var(key: str, default: int | str, cast_func: Callable[[str], int |
3027
if value is None:
3128
return default
3229

33-
if cast_func:
34-
try:
35-
return cast_func(value)
36-
except (ValueError, TypeError):
37-
print(f"Warning: Invalid value for {env_key}: {value}. Using default: {default}")
38-
return default
39-
4030
return value

src/server/server_config.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66

77
from gitingest.utils.config_utils import _get_env_var
88

9-
MAX_DISPLAY_SIZE: int = _get_env_var("MAX_DISPLAY_SIZE", 300_000, int)
10-
DELETE_REPO_AFTER: int = _get_env_var("DELETE_REPO_AFTER", 60 * 60, int) # In seconds (1 hour)
9+
def _get_int_env_var(key: str, default: int) -> int:
10+
"""Get environment variable as integer with fallback to default."""
11+
try:
12+
return int(_get_env_var(key, str(default)))
13+
except ValueError:
14+
print(f"Warning: Invalid value for GITINGEST_{key}. Using default: {default}")
15+
return default
16+
17+
MAX_DISPLAY_SIZE: int = _get_int_env_var("MAX_DISPLAY_SIZE", 300_000)
18+
DELETE_REPO_AFTER: int = _get_int_env_var("DELETE_REPO_AFTER", 60 * 60) # In seconds (1 hour)
1119

1220
# Slider configuration (if updated, update the logSliderToSize function in src/static/js/utils.js)
13-
MAX_FILE_SIZE_KB: int = _get_env_var("MAX_FILE_SIZE_KB", 100 * 1024, int) # 100 MB
14-
MAX_SLIDER_POSITION: int = _get_env_var("MAX_SLIDER_POSITION", 500, int) # Maximum slider position
21+
MAX_FILE_SIZE_KB: int = _get_int_env_var("MAX_FILE_SIZE_KB", 100 * 1024) # 100 MB
22+
MAX_SLIDER_POSITION: int = _get_int_env_var("MAX_SLIDER_POSITION", 500) # Maximum slider position
1523

1624
EXAMPLE_REPOS: list[dict[str, str]] = [
1725
{"name": "Gitingest", "url": "https://github.com/cyclotruc/gitingest"},

0 commit comments

Comments
 (0)