file_processer.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import os
  4. from utils.include_utils import auto_parse_include
  5. from utils.other_utils import get_file_suffix, extract_codes_colors, replace_old_color, update_themes, \
  6. replace_res_utils, bg_white_radius10, bg_btn_ffffff_10dp, add_xml_res_auto, \
  7. add_import_R, bg_homeindex_item_10, unify_path_separator, \
  8. get_included_suffix, get_excluded_files, update_deprecated, bg_btn_white_topr10, \
  9. bg_btn_white_topr12, bg_btn_white_topr16, replace_xml_imageview, replace_xml_textview, \
  10. replace_system_bar, delete_xml_remark, replace_background, hykb_dimens, replace_back_icon, \
  11. replace_arrow_icon
  12. # 替换java/kotlin/xml文件中的颜色硬编码
  13. def replace_file_by_path(file_path: str, auto_include_callback=None):
  14. # 整理文件路径
  15. file_path = unify_path_separator(file_path)
  16. if not os.path.isfile(file_path):
  17. print(f"{file_path} 不是一个文件")
  18. return
  19. # 获取文件后缀
  20. suffix = get_file_suffix(file_path)
  21. if suffix not in get_included_suffix():
  22. print(f"忽略{suffix}格式文件")
  23. return
  24. # 获取文件名
  25. file_name = os.path.basename(file_path)
  26. if file_name in get_excluded_files():
  27. print(f"{file_name} 是忽略文件")
  28. return
  29. print(f"处理文件:{file_path}")
  30. with open(file_path, 'r', encoding='utf-8') as f:
  31. text = f.read()
  32. # 自动解析include
  33. auto_parse_include(text, suffix, auto_include_callback)
  34. # fix过时方法
  35. text = update_deprecated(text)
  36. # 提取文件中的颜色硬编码
  37. text = extract_codes_colors(text, suffix)
  38. # 替换旧的颜色
  39. text = replace_old_color(text)
  40. # 更新主题
  41. text = update_themes(text)
  42. # 删除xml注释
  43. # text = delete_xml_remark(text)
  44. # 替换旧的dimen
  45. # text = hykb_dimens(text)
  46. text = bg_white_radius10(text)
  47. text = bg_homeindex_item_10(text)
  48. text = bg_btn_ffffff_10dp(text)
  49. # text = MoreButtonStyle(text)
  50. # text = BaseTextOneLineStyle(text)
  51. text = bg_btn_white_topr10(text)
  52. text = bg_btn_white_topr12(text)
  53. text = bg_btn_white_topr16(text)
  54. text = replace_xml_imageview(text)
  55. text = replace_xml_textview(text)
  56. text = replace_arrow_icon(text)
  57. text = replace_back_icon(text)
  58. text = replace_background(text)
  59. # 添加xml头信息
  60. text = add_xml_res_auto(text)
  61. # 替换旧方法
  62. text = replace_res_utils(text, suffix)
  63. # 添加R文件引入
  64. text = add_import_R(text, suffix)
  65. # 删除Java多余空白行
  66. # text = delete_java_blank_line(text)
  67. # 删除xml多余空白行
  68. # text = delete_xml_blank_line(text, suffix)
  69. # 处理状态栏
  70. text = replace_system_bar(text)
  71. # 替换旧文件中的内容
  72. with open(file_path, 'w', encoding='utf-8') as f:
  73. f.write(text)