Skip to content

Instantly share code, notes, and snippets.

@gtors
Last active October 27, 2017 09:31
Show Gist options
  • Save gtors/8b7c6b62b63ee5c79780d38c6a353813 to your computer and use it in GitHub Desktop.
Save gtors/8b7c6b62b63ee5c79780d38c6a353813 to your computer and use it in GitHub Desktop.
Remover of multiline comments
#!/usr/bin/env python3
import re
import os
import sys
multiline_coment = re.compile(r'/\*\*.+?\*/\n?', flags=re.DOTALL)
extensions = tuple('.' + ext for ext in (
'java',
))
arg_path = sys.argv[1]
def is_supported_filetype(file_name: str) -> bool:
return any(map(file_name.endswith, extensions))
def apply_tranformations(file_name: str, transforms: tuple) -> None:
with open(file_name, 'r+') as fh:
file_content = fh.read()
for trans in transforms:
file_content = trans(file_content)
fh.seek(0)
fh.truncate()
fh.write(file_content)
def remove_multiline_comments(s: str) -> str:
return multiline_coment.sub('', s)
if os.path.isdir(arg_path):
for path, _, files in os.walk(arg_path):
for file in files:
if is_supported_filetype(file):
apply_tranformations(os.path.join(path, file), (remove_multiline_comments,))
elif os.path.isfile(arg_path):
apply_tranformations(arg_path, (remove_multiline_comments,))
else:
print(f'Are you kidding me? {arg_path}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment