Guest User

Untitled

a guest
Oct 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import re
  4. import os
  5. import sys
  6.  
  7. multiline_coment = re.compile(r'/\*\*.+?\*/', flags=re.DOTALL)
  8. extensions = tuple('.' + ext for ext in (
  9. 'java',
  10. ))
  11.  
  12. arg_path = sys.argv[1]
  13.  
  14. def is_supported_filetype(file_name: str) -> bool:
  15. return any(map(file_name.endswith, extensions))
  16.  
  17. def apply_tranformations(file_name: str, transforms: tuple) -> None:
  18. with open(file_name, 'r+') as fh:
  19. file_content = fh.read()
  20. for trans in transforms:
  21. file_content = trans(file_content)
  22. fh.seek(0)
  23. fh.truncate()
  24. fh.write(file_content)
  25.  
  26. def remove_multiline_comments(s: str) -> str:
  27. return multiline_coment.sub('', s)
  28.  
  29. if os.path.isdir(arg_path):
  30. for path, _, files in os.walk(arg_path):
  31. for file in files:
  32. if is_supported_filetype(file):
  33. apply_tranformations(os.path.join(path, file), (remove_multiline_comments,))
  34. elif os.path.isfile(arg_path):
  35. apply_tranformations(arg_path, (remove_multiline_comments,))
  36. else:
  37. print(f'Are you kidding me? {arg_path}')
Add Comment
Please, Sign In to add comment