droidrun 0.3.10.dev5__py3-none-any.whl → 0.3.10.dev6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,148 +0,0 @@
1
- """
2
- App card loading utility for package-specific prompts.
3
-
4
- Supports flexible file path resolution and caches loaded content.
5
- """
6
-
7
- import json
8
- from pathlib import Path
9
- from typing import Dict, Optional
10
-
11
- from droidrun.config_manager.path_resolver import PathResolver
12
-
13
-
14
- class AppCardLoader:
15
- """Load app cards based on package names with content caching."""
16
-
17
- _mapping_cache: Optional[Dict[str, str]] = None
18
- _cache_dir: Optional[str] = None
19
- _content_cache: Dict[str, str] = {}
20
-
21
- @staticmethod
22
- def load_app_card(
23
- package_name: str, app_cards_dir: str = "config/app_cards"
24
- ) -> str:
25
- """
26
- Load app card for a package name.
27
-
28
- Path resolution:
29
- - Checks working directory first (for user overrides)
30
- - Falls back to project directory (for default cards)
31
- - Supports absolute paths (used as-is)
32
-
33
- File loading from app_cards.json:
34
- 1. Relative to app_cards_dir (most common):
35
- {"com.google.gm": "gmail.md"}
36
- → {app_cards_dir}/gmail.md
37
-
38
- 2. Relative path (checks working dir, then project dir):
39
- {"com.google.gm": "config/custom_cards/gmail.md"}
40
-
41
- 3. Absolute path:
42
- {"com.google.gm": "/usr/share/droidrun/cards/gmail.md"}
43
-
44
- Args:
45
- package_name: Android package name (e.g., "com.google.android.gm")
46
- app_cards_dir: Directory path (relative or absolute)
47
-
48
- Returns:
49
- App card content or empty string if not found
50
- """
51
- if not package_name:
52
- return ""
53
-
54
- # Check content cache first (key: package_name:app_cards_dir)
55
- cache_key = f"{package_name}:{app_cards_dir}"
56
- if cache_key in AppCardLoader._content_cache:
57
- return AppCardLoader._content_cache[cache_key]
58
-
59
- # Load mapping (with cache)
60
- mapping = AppCardLoader._load_mapping(app_cards_dir)
61
-
62
- # Get file path from mapping
63
- if package_name not in mapping:
64
- # Cache the empty result to avoid repeated lookups
65
- AppCardLoader._content_cache[cache_key] = ""
66
- return ""
67
-
68
- file_path_str = mapping[package_name]
69
- file_path = Path(file_path_str)
70
-
71
- # Determine resolution strategy
72
- if file_path.is_absolute():
73
- # Absolute path: use as-is
74
- app_card_path = file_path
75
- elif file_path_str.startswith(("config/", "prompts/", "docs/")):
76
- # Project-relative path: resolve with unified resolver
77
- app_card_path = PathResolver.resolve(file_path_str)
78
- else:
79
- # App_cards-relative: resolve dir first, then append filename
80
- cards_dir_resolved = PathResolver.resolve(app_cards_dir)
81
- app_card_path = cards_dir_resolved / file_path_str
82
-
83
- # Read file
84
- try:
85
- if not app_card_path.exists():
86
- # Cache the empty result
87
- AppCardLoader._content_cache[cache_key] = ""
88
- return ""
89
-
90
- content = app_card_path.read_text(encoding="utf-8")
91
- # Cache the content
92
- AppCardLoader._content_cache[cache_key] = content
93
- return content
94
- except Exception:
95
- # Cache the empty result on error
96
- AppCardLoader._content_cache[cache_key] = ""
97
- return ""
98
-
99
- @staticmethod
100
- def _load_mapping(app_cards_dir: str) -> Dict[str, str]:
101
- """Load and cache the app_cards.json mapping."""
102
- # Cache invalidation: if dir changed, reload
103
- if (
104
- AppCardLoader._mapping_cache is not None
105
- and AppCardLoader._cache_dir == app_cards_dir
106
- ):
107
- return AppCardLoader._mapping_cache
108
-
109
- # Resolve app cards directory
110
- cards_dir_resolved = PathResolver.resolve(app_cards_dir)
111
- mapping_path = cards_dir_resolved / "app_cards.json"
112
-
113
- try:
114
- if not mapping_path.exists():
115
- AppCardLoader._mapping_cache = {}
116
- AppCardLoader._cache_dir = app_cards_dir
117
- return {}
118
-
119
- with open(mapping_path, "r", encoding="utf-8") as f:
120
- mapping = json.load(f)
121
-
122
- AppCardLoader._mapping_cache = mapping
123
- AppCardLoader._cache_dir = app_cards_dir
124
- return mapping
125
- except Exception:
126
- AppCardLoader._mapping_cache = {}
127
- AppCardLoader._cache_dir = app_cards_dir
128
- return {}
129
-
130
- @staticmethod
131
- def clear_cache() -> None:
132
- """Clear all caches (useful for testing or runtime reloading)."""
133
- AppCardLoader._mapping_cache = None
134
- AppCardLoader._cache_dir = None
135
- AppCardLoader._content_cache.clear()
136
-
137
- @staticmethod
138
- def get_cache_stats() -> Dict[str, int]:
139
- """
140
- Get cache statistics.
141
-
142
- Returns:
143
- Dict with cache stats (useful for debugging)
144
- """
145
- return {
146
- "mapping_cached": 1 if AppCardLoader._mapping_cache is not None else 0,
147
- "content_entries": len(AppCardLoader._content_cache),
148
- }