file_processer.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_icon, replace_background, hykb_dimens
  11. # 替换java/kotlin/xml文件中的颜色硬编码
  12. def replace_file_by_path(file_path: str, auto_include_callback=None):
  13. # 整理文件路径
  14. file_path = unify_path_separator(file_path)
  15. if not os.path.isfile(file_path):
  16. print(f"{file_path} 不是一个文件")
  17. return
  18. # 获取文件后缀
  19. suffix = get_file_suffix(file_path)
  20. if suffix not in get_included_suffix():
  21. print(f"忽略{suffix}格式文件")
  22. return
  23. # 获取文件名
  24. file_name = os.path.basename(file_path)
  25. if file_name in get_excluded_files():
  26. print(f"{file_name} 是忽略文件")
  27. return
  28. print(f"处理文件:{file_path}")
  29. with open(file_path, 'r', encoding='utf-8') as f:
  30. text = f.read()
  31. text = text.replace("\"#ffffffff\"", "\"#ffffff\"")
  32. text = text.replace("\"#FFFFFFFF\"", "\"#ffffff\"")
  33. # 自动解析include
  34. auto_parse_include(text, suffix, auto_include_callback)
  35. # fix过时方法
  36. text = update_deprecated(text)
  37. # 提取文件中的颜色硬编码
  38. text = extract_codes_colors(text, suffix)
  39. # 替换旧的颜色
  40. text = replace_old_color(text)
  41. # 更新主题
  42. text = update_themes(text)
  43. # 删除xml注释
  44. text = delete_xml_remark(text)
  45. # 替换旧的dimen
  46. text = hykb_dimens(text)
  47. text = bg_white_radius10(text)
  48. text = bg_homeindex_item_10(text)
  49. text = bg_btn_ffffff_10dp(text)
  50. # text = MoreButtonStyle(text)
  51. # text = BaseTextOneLineStyle(text)
  52. text = bg_btn_white_topr10(text)
  53. text = bg_btn_white_topr12(text)
  54. text = bg_btn_white_topr16(text)
  55. text = replace_xml_imageview(text)
  56. text = replace_xml_textview(text)
  57. text = replace_icon(text)
  58. text = replace_background(text)
  59. # 错误校正,非必须
  60. text = text.replace("green_brandDark", "green_brand")
  61. text = text.replace("green_brand_brand", "green_brand")
  62. text = text.replace("green_brand_brandDark", "green_brand")
  63. text = text.replace("green_brandDark", "green_brand")
  64. text = text.replace("green_brand_08", "green_brand_7")
  65. text = text.replace("green_brand_brand_word", "green_word")
  66. text = text.replace("green_brand_word", "green_word")
  67. text = text.replace("green_brand_hover", "green_hover")
  68. text = text.replace("green_brand_line", "green_line")
  69. text = text.replace("green_brand_e0fce7", "green_e0fce7")
  70. text = text.replace("bg_light_00", "transparent")
  71. text = text.replace("font_color_ff131715", "black_h1")
  72. text = text.replace("green_brand_bg", "green_bg")
  73. text = text.replace("color_ff131715", "black_h1")
  74. text = text.replace("color_green_", "green_brand_")
  75. # text = text.replace("Color.parseColor(\"#ffffff\")", "ResUtils.getColor(R.color.white)")
  76. # text = text.replace("Color.parseColor(\"#ffffffff\")", "ResUtils.getColor(R.color.white)")
  77. # text = text.replace("android:textColor=\"#ffffffff\"", "android:textColor=\"@color/white\"")
  78. # text = text.replace("android:background=\"#ffffffff\"", "android:background=\"@color/bg_white\"")
  79. # text = text.replace("setImageDrawable(ContextCompat.getDrawable(mActivity, ", "setImageResource((")
  80. # 添加xml头信息
  81. text = add_xml_res_auto(text)
  82. # 替换旧方法
  83. text = replace_res_utils(text, suffix)
  84. # 添加R文件引入
  85. text = add_import_R(text, suffix)
  86. # 删除Java多余空白行
  87. # text = delete_java_blank_line(text)
  88. # 删除xml多余空白行
  89. # text = delete_xml_blank_line(text, suffix)
  90. text = text.replace("color_333333", "black_h2")
  91. text = text.replace("color_999999", "black_h4")
  92. # 处理状态栏
  93. text = replace_system_bar(text)
  94. # 替换旧文件中的内容
  95. with open(file_path, 'w', encoding='utf-8') as f:
  96. f.write(text)