auto-coder 0.1.327__py3-none-any.whl → 0.1.329__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.

Potentially problematic release.


This version of auto-coder might be problematic. Click here for more details.

@@ -0,0 +1,271 @@
1
+ """
2
+ Module providing a factory for creating language-specific compilers.
3
+ """
4
+
5
+ import os
6
+ from typing import Optional, Dict, Any, List
7
+
8
+ from autocoder.compilers.base_compiler import BaseCompiler
9
+ from autocoder.compilers.reactjs_compiler import ReactJSCompiler
10
+ from autocoder.compilers.vue_compiler import VueCompiler
11
+ from autocoder.compilers.python_compiler import PythonCompiler
12
+ from autocoder.compilers.java_compiler import JavaCompiler
13
+ from autocoder.compilers.provided_compiler import ProvidedCompiler
14
+
15
+ class CompilerFactory:
16
+ """
17
+ Factory class for creating appropriate compiler instances based on file type or language.
18
+ """
19
+
20
+ @classmethod
21
+ def create_compiler(cls, language: Optional[str] = None,
22
+ file_path: Optional[str] = None,
23
+ config_path: Optional[str] = None,
24
+ verbose: bool = False, **kwargs) -> Optional[BaseCompiler]:
25
+ """
26
+ Create and return an appropriate compiler instance based on language or file path.
27
+
28
+ Args:
29
+ language (Optional[str]): Language identifier ('python', 'java', 'javascript', 'typescript', etc.).
30
+ file_path (Optional[str]): Path to a file to infer the language from.
31
+ config_path (Optional[str]): Path to a config file to use for the compiler.
32
+ verbose (bool): Whether to enable verbose output in the compiler.
33
+
34
+ Returns:
35
+ BaseCompiler: An instance of a compiler appropriate for the language.
36
+
37
+ Raises:
38
+ ValueError: If no language could be determined or if the language is not supported.
39
+ """
40
+ if language is None and file_path is None:
41
+ raise ValueError("Either language or file_path must be provided")
42
+
43
+ # If language is not provided, try to infer from file path
44
+ if language is None and file_path is not None:
45
+ language = cls._detect_language_from_file(file_path)
46
+
47
+ # Map language to compiler class
48
+ compiler_map = {
49
+ 'python': PythonCompiler,
50
+ 'java': JavaCompiler,
51
+ 'javascript': ReactJSCompiler,
52
+ 'typescript': ReactJSCompiler,
53
+ 'js': ReactJSCompiler,
54
+ 'ts': ReactJSCompiler,
55
+ 'jsx': ReactJSCompiler,
56
+ 'tsx': ReactJSCompiler,
57
+ 'react': ReactJSCompiler,
58
+ 'reactjs': ReactJSCompiler,
59
+ 'vue': VueCompiler,
60
+ 'provided': ProvidedCompiler,
61
+ }
62
+
63
+ compiler_class = compiler_map.get(language.lower() if language else None)
64
+
65
+ if compiler_class is None:
66
+ return None
67
+
68
+ return compiler_class(verbose=verbose, config_path=config_path, **kwargs)
69
+
70
+ @classmethod
71
+ def _detect_language_from_file(cls, file_path: str) -> Optional[str]:
72
+ """
73
+ Detect the programming language based on file extension.
74
+
75
+ Args:
76
+ file_path (str): Path to the file.
77
+
78
+ Returns:
79
+ str: Language identifier.
80
+
81
+ Raises:
82
+ ValueError: If the file extension is not recognized.
83
+ """
84
+ if not os.path.exists(file_path):
85
+ return None
86
+
87
+ _, ext = os.path.splitext(file_path)
88
+ ext = ext.lower()
89
+
90
+ # Map extensions to languages
91
+ extension_map = {
92
+ '.py': 'python',
93
+ '.java': 'java',
94
+ '.js': 'javascript',
95
+ '.ts': 'typescript',
96
+ '.jsx': 'react',
97
+ '.tsx': 'react',
98
+ '.vue': 'vue',
99
+ }
100
+
101
+ language = extension_map.get(ext)
102
+ return language
103
+
104
+ @classmethod
105
+ def get_supported_languages(cls) -> List[str]:
106
+ """
107
+ Get a list of supported programming languages.
108
+
109
+ Returns:
110
+ List[str]: List of supported language identifiers.
111
+ """
112
+ return ['python', 'java', 'javascript', 'typescript', 'react', 'vue']
113
+
114
+ @classmethod
115
+ def compile_file(cls, file_path: str, verbose: bool = False) -> Dict[str, Any]:
116
+ """
117
+ Compile a single file using the appropriate compiler.
118
+
119
+ Args:
120
+ file_path (str): Path to the file to compile.
121
+ verbose (bool): Whether to enable verbose output.
122
+
123
+ Returns:
124
+ Dict[str, Any]: Compilation results.
125
+ """
126
+ compiler = cls.create_compiler(file_path=file_path, verbose=verbose)
127
+ if compiler is None:
128
+ return None
129
+ return compiler.compile_file(file_path)
130
+
131
+ @classmethod
132
+ def compile_project(cls, project_path: str, language: Optional[str] = None, verbose: bool = False) -> Dict[str, Any]:
133
+ """
134
+ Compile a project using the appropriate compiler.
135
+
136
+ Args:
137
+ project_path (str): Path to the project directory.
138
+ language (Optional[str]): Language identifier to specify which compiler to use.
139
+ If not provided, will try to auto-detect.
140
+ verbose (bool): Whether to enable verbose output.
141
+
142
+ Returns:
143
+ Dict[str, Any]: Compilation results.
144
+ """
145
+ # If language not specified, try to detect from project contents
146
+ if language is None:
147
+ # First check for package.json (JavaScript/TypeScript/React/Vue)
148
+ if os.path.exists(os.path.join(project_path, 'package.json')):
149
+ # Check if it's a React or Vue project
150
+ try:
151
+ with open(os.path.join(project_path, 'package.json'), 'r') as f:
152
+ import json
153
+ package_data = json.load(f)
154
+
155
+ dependencies = {
156
+ **package_data.get('dependencies', {}),
157
+ **package_data.get('devDependencies', {})
158
+ }
159
+
160
+ if 'react' in dependencies:
161
+ language = 'react'
162
+ elif 'vue' in dependencies:
163
+ language = 'vue'
164
+ else:
165
+ language = 'javascript'
166
+ except:
167
+ language = 'javascript' # Default to JavaScript
168
+ # Check for pom.xml or build.gradle (Java)
169
+ elif (os.path.exists(os.path.join(project_path, 'pom.xml')) or
170
+ os.path.exists(os.path.join(project_path, 'build.gradle'))):
171
+ language = 'java'
172
+ # Check for setup.py or requirements.txt (Python)
173
+ elif (os.path.exists(os.path.join(project_path, 'setup.py')) or
174
+ os.path.exists(os.path.join(project_path, 'requirements.txt'))):
175
+ language = 'python'
176
+ else:
177
+ # Count file extensions to guess the dominant language
178
+ language_counts = {}
179
+ for root, _, files in os.walk(project_path):
180
+ for file in files:
181
+ _, ext = os.path.splitext(file)
182
+ ext = ext.lower()
183
+ language_counts[ext] = language_counts.get(ext, 0) + 1
184
+
185
+ # Find the most common relevant extension
186
+ relevant_extensions = {'.py', '.java', '.js', '.ts', '.jsx', '.tsx', '.vue'}
187
+ most_common = None
188
+ max_count = 0
189
+
190
+ for ext, count in language_counts.items():
191
+ if ext in relevant_extensions and count > max_count:
192
+ most_common = ext
193
+ max_count = count
194
+
195
+ if most_common is None:
196
+ raise ValueError(f"Could not detect project language in {project_path}")
197
+
198
+ language = cls._detect_language_from_file(f"dummy{most_common}")
199
+
200
+ compiler = cls.create_compiler(language=language, verbose=verbose)
201
+ if compiler is None:
202
+ return None
203
+ return compiler.compile_project(project_path)
204
+
205
+ @classmethod
206
+ def format_compile_result(cls, compile_result: Dict[str, Any], language: Optional[str] = None) -> str:
207
+ """
208
+ Format compilation results into a human-readable string.
209
+
210
+ Args:
211
+ compile_result (Dict[str, Any]): The compilation result dictionary.
212
+ language (Optional[str]): Language identifier to specify which formatter to use.
213
+ If not provided, will try to infer from compile_result.
214
+
215
+ Returns:
216
+ str: A formatted string representation of the compilation results.
217
+ """
218
+ # Try to infer language/framework from compile_result
219
+ if language is None:
220
+ if 'language' in compile_result:
221
+ language = compile_result['language']
222
+ elif 'framework' in compile_result:
223
+ language = compile_result['framework']
224
+ elif 'file_type' in compile_result:
225
+ language = compile_result['file_type']
226
+ else:
227
+ # Default to Python as a fallback
228
+ language = 'python'
229
+
230
+ compiler = cls.create_compiler(language=language)
231
+ return compiler.format_compile_result(compile_result)
232
+
233
+ def compile_file(file_path: str, verbose: bool = False) -> Dict[str, Any]:
234
+ """
235
+ Utility function to compile a single file.
236
+
237
+ Args:
238
+ file_path (str): Path to the file to compile.
239
+ verbose (bool): Whether to display verbose output.
240
+
241
+ Returns:
242
+ Dict[str, Any]: A dictionary containing compilation results.
243
+ """
244
+ return CompilerFactory.compile_file(file_path, verbose=verbose)
245
+
246
+ def compile_project(project_path: str, language: Optional[str] = None, verbose: bool = False) -> Dict[str, Any]:
247
+ """
248
+ Utility function to compile a project.
249
+
250
+ Args:
251
+ project_path (str): Path to the project directory.
252
+ language (Optional[str]): Language identifier to specify which compiler to use.
253
+ verbose (bool): Whether to display verbose output.
254
+
255
+ Returns:
256
+ Dict[str, Any]: A dictionary containing compilation results.
257
+ """
258
+ return CompilerFactory.compile_project(project_path, language=language, verbose=verbose)
259
+
260
+ def format_compile_result(compile_result: Dict[str, Any], language: Optional[str] = None) -> str:
261
+ """
262
+ Utility function to format compilation results into a human-readable string.
263
+
264
+ Args:
265
+ compile_result (Dict[str, Any]): The compilation result dictionary.
266
+ language (Optional[str]): Language identifier to specify which formatter to use.
267
+
268
+ Returns:
269
+ str: A formatted string representation of the compilation results.
270
+ """
271
+ return CompilerFactory.format_compile_result(compile_result, language=language)