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 line23456789101112131415161735 unmodified lines535455565758596061626310 unmodified lines7475767778798081821 unmodified line848586878889128 unmodified lines2182192202212222231 unmodified lineimport subprocessfrom typing import List, Dict, Union, Tuplefrom tree_sitter import Language, Parser, Nodefrom typing import Union, Listimport loggingdef 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_linesclass CodeParser:# Added a CACHE_DIR class attribute for cachingCACHE_DIR = os.path.expanduser("~/.code_parser_cache")35 unmodified linestry:if os.path.exists(repo_path):logging.info(f"Updating existing repository for {language}")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}"'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 linesLanguage.build_library(build_path, [ts_dir, tsx_dir])else:raise FileNotFoundError(f"TypeScript or TSX directory not found in {repo_path}")if language == 'php':php_dir = os.path.join(repo_path, 'php')Language.build_library(build_path, [php_dir])else:Language.build_library(build_path, [repo_path])1 unmodified linelogging.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)}")except Exception as e:logging.error(f"An unexpected error occurred during parser installation: {str(e)}")128 unmodified linesreturn node_types[file_extension]elif file_extension == "jsx":return node_types["js"]else:raise ValueError("Unsupported file type")1 unmodified line2345678935 unmodified lines454647484950515253545510 unmodified lines6667686970717273747576771 unmodified line79808182838485868788899091128 unmodified lines2202212222232242252262271 unmodified lineimport subprocessfrom typing import List, Dict, Union, Tuplefrom tree_sitter import Language, Parser, Nodeimport loggingclass CodeParser:# Added a CACHE_DIR class attribute for cachingCACHE_DIR = os.path.expanduser("~/.code_parser_cache")35 unmodified linestry:if os.path.exists(repo_path):logging.info(f"Updating existing repository for {language}")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}"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 linesLanguage.build_library(build_path, [ts_dir, tsx_dir])else:raise FileNotFoundError(f"TypeScript or TSX directory not found in {repo_path}")elif language == 'php':php_dir = os.path.join(repo_path, 'php')if os.path.exists(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 linelogging.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 linesreturn 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 lines10410510610710810911070 unmodified lines18118218318418518618726 unmodified lines21421521621721821922016 unmodified lines23723823924024124224316 unmodified lines260261262263264265266103 unmodified linesclass TestCodeChunkerJavaScript(unittest.TestCase):def setUp(self):self.patcher = patch('app.util.TextChunker.Chunker.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.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.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.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.mock_count_tokens = self.patcher.start()self.code_chunker = CodeChunker(file_extension='php')self.mock_codebase = load_json('mock_codefiles.json')103 unmodified lines10410510610710810911070 unmodified lines18118218318418518618726 unmodified lines21421521621721821922016 unmodified lines23723823924024124224316 unmodified lines260261262263264265266103 unmodified linesclass TestCodeChunkerJavaScript(unittest.TestCase):def setUp(self):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('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('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('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('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