nvim-treesitter grammars updater: avoid silent fails (#408463)

authored by Matthieu Coudron and committed by GitHub e4424cba 559a668d

+27 -33
+27 -33
pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py
··· 1 1 #!/usr/bin/env nix-shell 2 - #!nix-shell update-shell.nix -i python 2 + #!nix-shell ./update-shell.nix -i python 3 3 4 4 import json 5 5 import logging 6 6 import os 7 7 import subprocess 8 8 from concurrent.futures import ThreadPoolExecutor 9 + from pathlib import Path 9 10 10 11 import requests 11 12 12 13 log = logging.getLogger("vim-updater") 13 14 14 - NURR_JSON_URL = "https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json" 15 + NURR_JSON_URL = ( 16 + "https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json" 17 + ) 18 + 15 19 16 20 def generate_grammar(lang, parser_info): 17 21 """Generate grammar for a language based on the parser info""" 18 - try: 19 - if "install_info" not in parser_info: 20 - log.warning(f"Parser {lang} does not have install_info, skipping") 21 - return "" 22 + if "install_info" not in parser_info: 23 + log.warning(f"Parser {lang} does not have install_info, skipping") 24 + return "" 22 25 23 - install_info = parser_info["install_info"] 26 + install_info = parser_info["install_info"] 24 27 25 - url = install_info["url"] 26 - rev = install_info["revision"] 28 + url = install_info["url"] 29 + rev = install_info["revision"] 27 30 28 - generated = f""" {lang} = buildGrammar {{ 31 + generated = f""" {lang} = buildGrammar {{ 29 32 language = "{lang}"; 30 33 version = "0.0.0+rev={rev[:7]}"; 31 34 src = """ 32 35 33 - generated += subprocess.check_output(["nurl", url, rev, "--indent=4"], text=True) 34 - generated += ";" 36 + generated += subprocess.check_output( 37 + ["nurl", url, rev, "--indent=4"], text=True 38 + ) 39 + generated += ";" 35 40 36 - location = install_info.get("location", "") 37 - if location: 38 - generated += f""" 41 + location = install_info.get("location", "") 42 + if location: 43 + generated += f""" 39 44 location = "{location}";""" 40 45 41 - if install_info.get("generate", False): 42 - generated += """ 46 + if install_info.get("generate", False): 47 + generated += """ 43 48 generate = true;""" 44 49 45 - generated += f""" 50 + generated += f""" 46 51 meta.homepage = "{url}"; 47 52 }}; 48 53 """ 49 54 50 - return generated 51 - except Exception as e: 52 - log.error(f"Error generating grammar for {lang}: {e}") 53 - return "" 55 + return generated 54 56 55 57 56 58 def fetch_nurr_parsers(): ··· 81 83 82 84 def process_parser_info(parser_info): 83 85 """Process a single parser info entry and generate grammar for it""" 84 - try: 85 - lang = parser_info["lang"] 86 - return generate_grammar(lang, parser_info) 87 - except Exception as e: 88 - log.error(f"Error processing parser: {e}") 89 - return "" 86 + return generate_grammar(parser_info["lang"], parser_info) 90 87 91 88 92 89 def update_grammars(): ··· 119 116 120 117 121 118 if __name__ == "__main__": 122 - logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') 119 + logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") 123 120 124 121 generated = update_grammars() 125 - output_path = os.path.join( 126 - os.path.dirname(__file__), 127 - "../../nvim-treesitter/generated.nix" 128 - ) 122 + output_path = Path(__file__).parent.parent / "nvim-treesitter/generated.nix" 129 123 log.info("Writing output to %s", output_path) 130 124 with open(output_path, "w") as f: 131 125 f.write(generated)