Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import os
  2. from pathlib import Path
  3.  
  4. # Presence of these means we should keep
  5. EXCLUSIONS = {"LIBTBX", "FIXME", "DIALS", "pytest"}
  6.  
  7. EXCLUDE_FILES = {
  8. "conftest.py",
  9. "util/image_viewer/slip_viewer/frame.py",
  10. "util/installer.py",
  11. }
  12.  
  13.  
  14. def should_remove_line(filename, line):
  15. if any(x in str(filename) for x in EXCLUDE_FILES):
  16. return False
  17. if not line.strip():
  18. return True
  19. if line.startswith("#!") and (
  20. "command_line" in str(filename)
  21. or "setup.py" in str(filename)
  22. or "show_version.py" in str(filename)
  23. ):
  24. return False
  25. if "coding:" in line:
  26. return False
  27. if line.startswith("#") and any(x in line for x in EXCLUSIONS):
  28. return False
  29. if line.startswith("#"):
  30. return True
  31.  
  32.  
  33. def filter_lines(filename, lines):
  34. lout = []
  35. line_pos = 0
  36. while lines and (not lines[line_pos].strip() or lines[line_pos].startswith("#")):
  37. # if "coding" in lines[line_pos]:
  38. # breakpoint()
  39. if not should_remove_line(filename, lines[line_pos]):
  40. if "coding" in lines[line_pos]:
  41. lout.append("# coding: utf-8\n")
  42. else:
  43. lout.append(lines[line_pos])
  44. line_pos += 1
  45. # If a block ended with newline, we might still have contents, so keep newline
  46. # Black will remove if was only file
  47. if line_pos > 0 and not lines[line_pos - 1].strip():
  48. lout.append(lines[line_pos - 1])
  49. lout.extend(lines[line_pos:])
  50. return lout
  51.  
  52.  
  53. headers = []
  54. for (path, dirs, files) in os.walk("."):
  55. for fname in files:
  56. if not fname.endswith(".py"):
  57. continue
  58. fullpath = Path(path) / fname
  59. lines = []
  60. with open(fullpath) as f:
  61. lines = f.readlines()
  62.  
  63. lines_out = filter_lines(fullpath, lines)
  64.  
  65. if not lines_out == lines:
  66. with open(fullpath, "w") as f:
  67. f.write("".join(lines_out))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement