|
| 1 | +# Copyright 2024 Brain Simulation Ecosystem Limited. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | + |
| 17 | +import glob |
| 18 | +import json |
| 19 | +import os |
| 20 | +import sys |
| 21 | + |
| 22 | + |
| 23 | +def fix_ipython2_lexer_in_notebooks(directory_path): |
| 24 | + """ |
| 25 | + 批量修复指定目录中所有 Jupyter Notebook 文件的 ipython2 lexer 问题 |
| 26 | + """ |
| 27 | + # 查找所有.ipynb文件 |
| 28 | + notebook_files = glob.glob(os.path.join(directory_path, "*.ipynb")) |
| 29 | + |
| 30 | + if not notebook_files: |
| 31 | + print(f"在目录 {directory_path} 中未找到任何 .ipynb 文件") |
| 32 | + return |
| 33 | + |
| 34 | + fixed_count = 0 |
| 35 | + |
| 36 | + for file_path in notebook_files: |
| 37 | + try: |
| 38 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 39 | + data = json.load(f) |
| 40 | + |
| 41 | + needs_fix = False |
| 42 | + |
| 43 | + # 检查并修复顶层元数据 |
| 44 | + if 'metadata' in data: |
| 45 | + # 修复 language_info |
| 46 | + if 'language_info' in data['metadata']: |
| 47 | + lang_info = data['metadata']['language_info'] |
| 48 | + if lang_info.get('name') == 'ipython2': |
| 49 | + lang_info['name'] = 'ipython3' |
| 50 | + needs_fix = True |
| 51 | + print(f"修复 {os.path.basename(file_path)}: 顶层 language_info.name") |
| 52 | + |
| 53 | + if lang_info.get('pygments_lexer') == 'ipython2': |
| 54 | + lang_info['pygments_lexer'] = 'ipython3' |
| 55 | + needs_fix = True |
| 56 | + print(f"修复 {os.path.basename(file_path)}: 顶层 language_info.pygments_lexer") |
| 57 | + |
| 58 | + # 修复 kernelspec |
| 59 | + if 'kernelspec' in data['metadata']: |
| 60 | + kernelspec = data['metadata']['kernelspec'] |
| 61 | + if kernelspec.get('language') == 'ipython2': |
| 62 | + kernelspec['language'] = 'python' |
| 63 | + needs_fix = True |
| 64 | + print(f"修复 {os.path.basename(file_path)}: 顶层 kernelspec.language") |
| 65 | + |
| 66 | + if kernelspec.get('name') == 'ipython2': |
| 67 | + kernelspec['name'] = 'python3' |
| 68 | + needs_fix = True |
| 69 | + print(f"修复 {os.path.basename(file_path)}: 顶层 kernelspec.name") |
| 70 | + |
| 71 | + # 检查并修复单元格元数据 |
| 72 | + for i, cell in enumerate(data.get('cells', [])): |
| 73 | + if 'metadata' in cell: |
| 74 | + # 修复单元格级别的语言设置 |
| 75 | + if 'language' in cell['metadata'] and cell['metadata']['language'] == 'ipython2': |
| 76 | + cell['metadata']['language'] = 'ipython3' |
| 77 | + needs_fix = True |
| 78 | + print(f"修复 {os.path.basename(file_path)}: 单元格 {i} 的语言设置") |
| 79 | + |
| 80 | + # 修复其他可能的 lexer 设置 |
| 81 | + if 'pygments_lexer' in cell['metadata'] and cell['metadata']['pygments_lexer'] == 'ipython2': |
| 82 | + cell['metadata']['pygments_lexer'] = 'ipython3' |
| 83 | + needs_fix = True |
| 84 | + print(f"修复 {os.path.basename(file_path)}: 单元格 {i} 的 pygments_lexer 设置") |
| 85 | + |
| 86 | + # 如果需要修复,保存文件 |
| 87 | + if needs_fix: |
| 88 | + # 创建备份 |
| 89 | + backup_path = file_path + '.backup' |
| 90 | + with open(backup_path, 'w', encoding='utf-8') as f: |
| 91 | + json.dump(data, f, indent=2, ensure_ascii=False) |
| 92 | + |
| 93 | + # 保存修复后的文件 |
| 94 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 95 | + json.dump(data, f, indent=2, ensure_ascii=False) |
| 96 | + |
| 97 | + fixed_count += 1 |
| 98 | + print(f"已修复并备份: {os.path.basename(file_path)}") |
| 99 | + else: |
| 100 | + print(f"无需修复: {os.path.basename(file_path)}") |
| 101 | + |
| 102 | + except Exception as e: |
| 103 | + print(f"处理文件 {file_path} 时出错: {str(e)}") |
| 104 | + |
| 105 | + print(f"\n处理完成! 共修复了 {fixed_count} 个文件") |
| 106 | + return fixed_count |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + import os |
| 111 | + print(os.path.dirname(os.path.abspath(__file__))) |
| 112 | + |
| 113 | + # 使用当前目录,或者指定您的文档目录路径 |
| 114 | + target_directory = input("请输入包含.ipynb文件的目录路径(直接回车使用当前目录): ").strip() |
| 115 | + |
| 116 | + if not target_directory: |
| 117 | + target_directory = "." |
| 118 | + |
| 119 | + if not os.path.isdir(target_directory): |
| 120 | + print(f"错误: 目录 '{target_directory}' 不存在") |
| 121 | + sys.exit(1) |
| 122 | + |
| 123 | + print(f"开始处理目录: {os.path.abspath(target_directory)}") |
| 124 | + fix_ipython2_lexer_in_notebooks(target_directory) |
0 commit comments