File size: 9,869 Bytes
85a0eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# =============================================================================
# app/config.py
# .pyfun parser for app/* modules
# Universal MCP Hub (Sandboxed) - based on PyFundaments Architecture
# Copyright 2026 - Volkan Kücükbudak
# Apache License V. 2 + ESOL 1.1
# =============================================================================
# USAGE in any app/* module:
#   from . import config
#   cfg = config.get()
#   providers = cfg["LLM_PROVIDERS"]
# =============================================================================
# USAGE
# in providers.py
# from . import config

# active = config.get_active_llm_providers()
# → { "anthropic": { "base_url": "...", "env_key": "ANTHROPIC_API_KEY", ... }, ... }
# =============================================================================
# in models.py  
# from . import config

# anthropic_models = config.get_models_for_provider("anthropic")
# =============================================================================
# in tools.py
# from . import config

# active_tools = config.get_active_tools()
# =============================================================================
import os
import logging
from typing import Dict, Any, Optional

logger = logging.getLogger('app.config')

# Path to .pyfun — lives in app/ next to this file
PYFUN_PATH = os.path.join(os.path.dirname(__file__), ".pyfun")

# Internal cache — loaded once at first get()
_cache: Optional[Dict[str, Any]] = None


def _parse_value(value: str) -> str:
    """Strip quotes and inline comments from a value."""
    value = value.strip()
    # Remove inline comment
    if " #" in value:
        value = value[:value.index(" #")].strip()
    # Strip surrounding quotes
    if value.startswith('"') and value.endswith('"'):
        value = value[1:-1]
    return value


def _parse() -> Dict[str, Any]:
    """
    Parses the app/.pyfun file into a nested dictionary.

    Structure:
        [SECTION]
            [SUBSECTION]
                [BLOCK.name]
                key = "value"
                [BLOCK.name_END]
            [SUBSECTION_END]
        [SECTION_END]

    Returns nested dict:
        {
            "HUB": { "HUB_NAME": "...", ... },
            "LLM_PROVIDERS": {
                "anthropic": { "active": "true", "base_url": "...", ... },
                "gemini":    { ... },
            },
            "MODELS": {
                "claude-opus-4-6": { "provider": "anthropic", ... },
            },
            ...
        }
    """
    if not os.path.isfile(PYFUN_PATH):
        logger.critical(f".pyfun not found at: {PYFUN_PATH}")
        raise FileNotFoundError(f".pyfun not found at: {PYFUN_PATH}")

    result: Dict[str, Any] = {}

    # Parser state
    section: Optional[str]      = None   # e.g. "HUB", "PROVIDERS"
    subsection: Optional[str]   = None   # e.g. "LLM_PROVIDERS"
    block_type: Optional[str]   = None   # e.g. "LLM_PROVIDER", "MODEL", "TOOL"
    block_name: Optional[str]   = None   # e.g. "anthropic", "claude-opus-4-6"

    with open(PYFUN_PATH, "r", encoding="utf-8") as f:
        for raw_line in f:
            line = raw_line.strip()

            # Skip empty lines and full-line comments
            if not line or line.startswith("#"):
                continue

            # Skip file identifier
            if line.startswith("[PYFUN_FILE"):
                continue

            # --- Block END markers (most specific first) ---
            if line.endswith("_END]") and "." in line:
                # e.g. [LLM_PROVIDER.anthropic_END] or [MODEL.claude-opus-4-6_END]
                block_type = None
                block_name = None
                continue

            if line.endswith("_END]") and not "." in line:
                # e.g. [LLM_PROVIDERS_END], [HUB_END], [MODELS_END]
                inner = line[1:-1].replace("_END", "")
                if subsection and inner == subsection:
                    subsection = None
                elif section and inner == section:
                    section = None
                continue

            # --- Block START markers ---
            if line.startswith("[") and line.endswith("]"):
                inner = line[1:-1]

                # Named block: [LLM_PROVIDER.anthropic] or [MODEL.claude-opus-4-6]
                if "." in inner:
                    parts = inner.split(".", 1)
                    block_type = parts[0]   # e.g. LLM_PROVIDER, MODEL, TOOL
                    block_name = parts[1]   # e.g. anthropic, claude-opus-4-6

                    # Determine which top-level key to store under
                    if block_type == "LLM_PROVIDER":
                        result.setdefault("LLM_PROVIDERS", {})
                        result["LLM_PROVIDERS"].setdefault(block_name, {})
                    elif block_type == "SEARCH_PROVIDER":
                        result.setdefault("SEARCH_PROVIDERS", {})
                        result["SEARCH_PROVIDERS"].setdefault(block_name, {})
                    elif block_type == "WEB_PROVIDER":
                        result.setdefault("WEB_PROVIDERS", {})
                        result["WEB_PROVIDERS"].setdefault(block_name, {})
                    elif block_type == "MODEL":
                        result.setdefault("MODELS", {})
                        result["MODELS"].setdefault(block_name, {})
                    elif block_type == "TOOL":
                        result.setdefault("TOOLS", {})
                        result["TOOLS"].setdefault(block_name, {})
                    continue

                # Subsection: [LLM_PROVIDERS], [SEARCH_PROVIDERS] etc.
                if section and not subsection:
                    subsection = inner
                    result.setdefault(inner, {})
                    continue

                # Top-level section: [HUB], [PROVIDERS], [MODELS] etc.
                section = inner
                result.setdefault(inner, {})
                continue

            # --- Key = Value ---
            if "=" in line:
                key, _, val = line.partition("=")
                key = key.strip()
                val = _parse_value(val)

                # Strip provider prefix from key (e.g. "anthropic.base_url" → "base_url")
                if block_name and key.startswith(f"{block_name}."):
                    key = key[len(block_name) + 1:]

                # Store in correct location
                if block_type and block_name:
                    if block_type == "LLM_PROVIDER":
                        result["LLM_PROVIDERS"][block_name][key] = val
                    elif block_type == "SEARCH_PROVIDER":
                        result["SEARCH_PROVIDERS"][block_name][key] = val
                    elif block_type == "WEB_PROVIDER":
                        result["WEB_PROVIDERS"][block_name][key] = val
                    elif block_type == "MODEL":
                        result["MODELS"][block_name][key] = val
                    elif block_type == "TOOL":
                        result["TOOLS"][block_name][key] = val
                elif section:
                    result[section][key] = val

    logger.info(f".pyfun loaded. Sections: {list(result.keys())}")
    return result


def load() -> Dict[str, Any]:
    """Force (re)load of .pyfun — clears cache."""
    global _cache
    _cache = _parse()
    return _cache


def get() -> Dict[str, Any]:
    """
    Returns parsed .pyfun config as nested dict.
    Loads and caches on first call — subsequent calls return cache.
    """
    global _cache
    if _cache is None:
        _cache = _parse()
    return _cache


def get_section(section: str) -> Dict[str, Any]:
    """
    Returns a specific top-level section.
    Returns empty dict if section not found.
    """
    return get().get(section, {})


def get_llm_providers() -> Dict[str, Any]:
    """Returns all LLM providers (active and inactive)."""
    return get().get("LLM_PROVIDERS", {})


def get_active_llm_providers() -> Dict[str, Any]:
    """Returns only LLM providers where active = 'true'."""
    return {
        name: cfg
        for name, cfg in get_llm_providers().items()
        if cfg.get("active", "false").lower() == "true"
    }


def get_search_providers() -> Dict[str, Any]:
    """Returns all search providers."""
    return get().get("SEARCH_PROVIDERS", {})


def get_active_search_providers() -> Dict[str, Any]:
    """Returns only search providers where active = 'true'."""
    return {
        name: cfg
        for name, cfg in get_search_providers().items()
        if cfg.get("active", "false").lower() == "true"
    }


def get_models() -> Dict[str, Any]:
    """Returns all model definitions."""
    return get().get("MODELS", {})


def get_models_for_provider(provider_name: str) -> Dict[str, Any]:
    """Returns all models for a specific provider."""
    return {
        name: cfg
        for name, cfg in get_models().items()
        if cfg.get("provider", "") == provider_name
    }


def get_tools() -> Dict[str, Any]:
    """Returns all tool definitions."""
    return get().get("TOOLS", {})


def get_active_tools() -> Dict[str, Any]:
    """Returns only tools where active = 'true'."""
    return {
        name: cfg
        for name, cfg in get_tools().items()
        if cfg.get("active", "false").lower() == "true"
    }


def get_hub() -> Dict[str, Any]:
    """Returns [HUB] section."""
    return get_section("HUB")


def get_limits() -> Dict[str, Any]:
    """Returns [HUB_LIMITS] section."""
    return get_section("HUB_LIMITS")


def get_db_sync() -> Dict[str, Any]:
    """Returns [DB_SYNC] section."""
    return get_section("DB_SYNC")


def get_debug() -> Dict[str, Any]:
    """Returns [DEBUG] section."""
    return get_section("DEBUG")


def is_debug() -> bool:
    """Returns True if DEBUG = 'ON' in .pyfun."""
    return get_debug().get("DEBUG", "OFF").upper() == "ON"