gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7KB

  1. import re
  2. def upgrade(file_manager):
  3. """ Use double quote for redacted text and single quote for strings. """
  4. # Don't use this script in production, it is broken and only serve
  5. # as an example.
  6. # List all the files that might need to be upgraded, here we list
  7. # all python models.
  8. files = [
  9. file for file in file_manager
  10. if 'models' in file.path.parts
  11. if file.path.suffix == '.py'
  12. if file.path.name != '__init__.py'
  13. ]
  14. # Early return if case there are no file, so we don't compile
  15. # regexps for nothing.
  16. if not files:
  17. return
  18. # Python Regexp 101
  19. #
  20. # re.VERBOSE ignores all spaces inside the regexp so it is possible
  21. # to indent it, it also allows for comments at the end of each line.
  22. # to actually expect a space you have to escape it: "\ "
  23. #
  24. # a* vs a*? the first is greedy and the second is lazy, the first is
  25. # gonna match as most "a" as possible, the second will stop as soon
  26. # as possible. Lazy quantifiers are MUCH faster than greedy one.
  27. #
  28. # (?P<x>) it is your regular group, but you can access it via its
  29. # name "x": match = re.match(...); match.group('x'). Much better
  30. # than using numeric groups.
  31. #
  32. # (?:) it is a non-capturing group, for when you need a group inside
  33. # the regexp but you don't need to remember what was matched inside.
  34. # They are faster than regular (capturing) groups.
  35. # Assume that all redacted text:
  36. # - Start with a upper case
  37. # - Have multiples words
  38. # - End with a dot
  39. # This assumption is wrong for many cases, don't use this script!
  40. redacted_text_re = re.compile(r"""
  41. ' # Opening single quote
  42. (?P<text>
  43. [A-Z][^'\s]*?\ # First word
  44. (?:[^'\s]*?\ )* # All middle words
  45. [^'\s]*?\. # Final word
  46. )
  47. ' # Closing single quote
  48. """, re.VERBOSE)
  49. # Assume that all strings:
  50. # - Are fully lowercase
  51. # - Have a single word
  52. # - Have no ponctuation
  53. # This assumption is wrong for many cases, don't use this script!
  54. strings_re = re.compile(r'"(?P<string>[a-z]+)"')
  55. # Iterate over all the files and run the regexps
  56. for fileno, file in enumerate(files, start=1):
  57. # load the content
  58. content = file.content
  59. # do the operations
  60. content = redacted_text_re.sub(r'"\g<text>"', content)
  61. content = strings_re.sub(r"'\g<string>'", content)
  62. # write back the file, if nothing changed nothing is written
  63. # file.content = content # uncomment this line to test the script
  64. # have the progress bar to actually show the progression
  65. file_manager.print_progress(fileno, len(files))
上海开阖软件有限公司 沪ICP备12045867号-1