Spaces:
Sleeping
Sleeping
Merge pull request #10 from CintraAI/enhancement/code-parser-and-test-suite-fix
Browse filesFiles changed (2)
- CodeParser.py+16-12
- test_code_chunker.py+5-5
1 unmodified line23455689101112131478935 unmodified lines4546475648495051605253545510 unmodified lines66676877697071797273747576771 unmodified line79808182838485868788899091128 unmodified lines2202212222232242252262271 unmodified lineimport subprocessfrom typing import List, Dict, Union, Tuplefrom tree_sitter import Language, Parser, Nodefrom typing import Union, Listimport logging
def return_simple_line_numbers_with_code(code: str) -> str: code_lines = code.split('\n') code_with_line_numbers = [f"Line {i + 1}: {line}" for i, line in enumerate(code_lines)] joined_lines = "\n".join(code_with_line_numbers) return joined_lines
class CodeParser: # Added a CACHE_DIR class attribute for caching CACHE_DIR = os.path.expanduser("~/.code_parser_cache")35 unmodified lines try: if os.path.exists(repo_path): logging.info(f"Updating existing repository for {language}") update_command = f'cd "{repo_path}" && git pull' update_command = f"cd {repo_path} && git pull" subprocess.run(update_command, shell=True, check=True) else: logging.info(f"Cloning repository for {language}") clone_command = f'git clone https://github.com/tree-sitter/tree-sitter-{language} "{repo_path}"' clone_command = f"git clone https://github.com/tree-sitter/tree-sitter-{language} {repo_path}" subprocess.run(clone_command, shell=True, check=True) except subprocess.CalledProcessError as e: logging.error(f"Failed to clone/update repository for {language}. Error: {e}")10 unmodified lines Language.build_library(build_path, [ts_dir, tsx_dir]) else: raise FileNotFoundError(f"TypeScript or TSX directory not found in {repo_path}") if language == 'php': elif language == 'php': php_dir = os.path.join(repo_path, 'php') if os.path.exists(php_dir): Language.build_library(build_path, [php_dir]) Language.build_library(build_path, [php_dir]) else: raise FileNotFoundError(f"PHP directory not found in {repo_path}") else: Language.build_library(build_path, [repo_path]) 1 unmodified line logging.info(f"Successfully built and loaded {language} parser") except Exception as e: logging.error(f"Failed to build or load language {language}. Error: {str(e)}") logging.error(f"Repository path: {repo_path}") logging.error(f"Build path: {build_path}") if language == 'typescript': logging.error(f"TypeScript dir exists: {os.path.exists(ts_dir)}") logging.error(f"TSX dir exists: {os.path.exists(tsx_dir)}") elif language == 'php': logging.error(f"PHP dir exists: {os.path.exists(php_dir)}")
except Exception as e: logging.error(f"An unexpected error occurred during parser installation: {str(e)}")128 unmodified lines return node_types[file_extension] elif file_extension == "jsx": return node_types["js"] elif file_extension == "tsx": return node_types["ts"] else: raise ValueError("Unsupported file type") 103 unmodified lines10410510610710710810911070 unmodified lines18118218318418418518618726 unmodified lines21421521621721721821922016 unmodified lines23723823924024024124224316 unmodified lines260261262263263264265266103 unmodified linesclass TestCodeChunkerJavaScript(unittest.TestCase):
def setUp(self): self.patcher = patch('app.util.TextChunker.Chunker.count_tokens', side_effect=mock_count_tokens) self.patcher = patch('utils.count_tokens', side_effect=mock_count_tokens) self.mock_count_tokens = self.patcher.start() self.code_chunker = CodeChunker(file_extension='js') self.mock_codebase = load_json('mock_codefiles.json')70 unmodified linesclass TestCodeChunkerCSS(unittest.TestCase): def setUp(self): self.patcher = patch('app.util.TextChunker.Chunker.count_tokens', side_effect=mock_count_tokens) self.patcher = patch('utils.count_tokens', side_effect=mock_count_tokens) self.mock_count_tokens = self.patcher.start() self.code_chunker = CodeChunker(file_extension='css') #Load the JSON data26 unmodified linesclass TestCodeChunkerTypeScript(unittest.TestCase):
def setUp(self): self.patcher = patch('app.util.TextChunker.Chunker.count_tokens', side_effect=mock_count_tokens) self.patcher = patch('utils.count_tokens', side_effect=mock_count_tokens) self.mock_count_tokens = self.patcher.start() self.code_chunker = CodeChunker(file_extension='ts') self.mock_codebase = load_json('mock_codefiles.json')16 unmodified linesclass TestCodeChunkerRuby(unittest.TestCase):
def setUp(self): self.patcher = patch('app.util.TextChunker.Chunker.count_tokens', side_effect=mock_count_tokens) self.patcher = patch('utils.count_tokens', side_effect=mock_count_tokens) self.mock_count_tokens = self.patcher.start() self.code_chunker = CodeChunker(file_extension='rb') self.mock_codebase = load_json('mock_codefiles.json')16 unmodified linesclass TestCodeChunkerPHP(unittest.TestCase):
def setUp(self): self.patcher = patch('app.util.TextChunker.Chunker.count_tokens', side_effect=mock_count_tokens) self.patcher = patch('utils.count_tokens', side_effect=mock_count_tokens) self.mock_count_tokens = self.patcher.start() self.code_chunker = CodeChunker(file_extension='php') self.mock_codebase = load_json('mock_codefiles.json')CodeParser.py+16-12
test_code_chunker.py+5-5