ciyidogan commited on
Commit
c40bd8f
·
verified ·
1 Parent(s): f0673f4

Update config_provider.py

Browse files
Files changed (1) hide show
  1. config_provider.py +61 -20
config_provider.py CHANGED
@@ -1,14 +1,13 @@
1
  """
2
- Flare – ConfigProvider
3
- ~~~~~~~~~~~~~~~~~~~~~~
4
- service_config.jsonc dosyasını tek sefer parse eder, nesnelere map eder
5
- Pydantic v2 kullanımı (Field(..., pattern=…))
6
  """
7
 
8
  from __future__ import annotations
9
 
10
  import json
11
- import re
12
  from pathlib import Path
13
  from typing import Dict, List, Optional
14
 
@@ -17,9 +16,7 @@ from utils import log
17
 
18
  # -------------------- Model tanımları --------------------
19
  class RetryConfig(BaseModel):
20
- strategy: str = Field( # static | exponential
21
- "static", pattern=r"^(static|exponential)$"
22
- )
23
  retry_count: int = 3
24
  backoff_seconds: int = 2
25
 
@@ -79,8 +76,6 @@ class ServiceConfig(BaseModel):
79
 
80
  # -------------------- Provider singleton ----------------
81
  class ConfigProvider:
82
- """service_config.jsonc okur, tekil instance döndürür."""
83
-
84
  _config: Optional[ServiceConfig] = None
85
  _CONFIG_PATH = Path(__file__).parent / "service_config.jsonc"
86
 
@@ -105,17 +100,63 @@ class ConfigProvider:
105
  log(f"❌ Config validation error: {exc}")
106
  raise
107
 
 
108
  @staticmethod
109
- def _strip_jsonc(content: str) -> str:
110
- """Remove // and /* */ comments for JSONC."""
111
- # Remove // lines
112
- content = re.sub(r"//.*", "", content)
113
- # Remove /* … */ blocks
114
- content = re.sub(r"/\*.*?\*/", "", content, flags=re.S)
115
- return content
116
-
117
-
118
- # --------------- Convenience aliases for imports ---------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  RetryConfig = RetryConfig
120
  ParameterConfig = ParameterConfig
121
  IntentConfig = IntentConfig
 
1
  """
2
+ Flare – ConfigProvider (JSONC-safe)
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+ Pydantic v2 (pattern=…)
5
+ Yorum ayıklayıcı string literal’leri korur
6
  """
7
 
8
  from __future__ import annotations
9
 
10
  import json
 
11
  from pathlib import Path
12
  from typing import Dict, List, Optional
13
 
 
16
 
17
  # -------------------- Model tanımları --------------------
18
  class RetryConfig(BaseModel):
19
+ strategy: str = Field("static", pattern=r"^(static|exponential)$")
 
 
20
  retry_count: int = 3
21
  backoff_seconds: int = 2
22
 
 
76
 
77
  # -------------------- Provider singleton ----------------
78
  class ConfigProvider:
 
 
79
  _config: Optional[ServiceConfig] = None
80
  _CONFIG_PATH = Path(__file__).parent / "service_config.jsonc"
81
 
 
100
  log(f"❌ Config validation error: {exc}")
101
  raise
102
 
103
+ # -------- robust JSONC stripper ----------
104
  @staticmethod
105
+ def _strip_jsonc(text: str) -> str:
106
+ """Remove // line comments and /* block comments */ without touching string literals."""
107
+ OUT, IN_STR, ESC, IN_SLASH, IN_BLOCK = 0, 1, 2, 3, 4
108
+ state = OUT
109
+ res = []
110
+
111
+ i = 0
112
+ while i < len(text):
113
+ ch = text[i]
114
+
115
+ if state == OUT:
116
+ if ch == '"':
117
+ state = IN_STR
118
+ res.append(ch)
119
+ elif ch == '/':
120
+ # Could be // or /* – peek next char
121
+ nxt = text[i + 1] if i + 1 < len(text) else ""
122
+ if nxt == '/':
123
+ state = IN_SLASH
124
+ i += 1 # skip nxt in loop
125
+ elif nxt == '*':
126
+ state = IN_BLOCK
127
+ i += 1
128
+ else:
129
+ res.append(ch)
130
+ else:
131
+ res.append(ch)
132
+
133
+ elif state == IN_STR:
134
+ res.append(ch)
135
+ if ch == '\\':
136
+ state = ESC # escape next
137
+ elif ch == '"':
138
+ state = OUT
139
+
140
+ elif state == ESC:
141
+ res.append(ch)
142
+ state = IN_STR
143
+
144
+ elif state == IN_SLASH:
145
+ if ch == '\n':
146
+ res.append(ch) # keep newline
147
+ state = OUT
148
+
149
+ elif state == IN_BLOCK:
150
+ if ch == '*' and i + 1 < len(text) and text[i + 1] == '/':
151
+ i += 1 # skip /
152
+ state = OUT
153
+
154
+ i += 1
155
+
156
+ return ''.join(res)
157
+
158
+
159
+ # --------------- exports ---------------
160
  RetryConfig = RetryConfig
161
  ParameterConfig = ParameterConfig
162
  IntentConfig = IntentConfig