22import os .path
33import shutil
44from pathlib import Path
5- from typing import Union , Iterable , List
5+ from typing import Union , Iterable , List , Optional
66
77from codestripper .errors import InvalidTagError , TokenizerError
88from codestripper .tags import IgnoreFileError
99from codestripper .tags .tag import Tag , RangeTag
1010from codestripper .tokenizer import Tokenizer
1111from codestripper .utils import get_working_directory
12+ from codestripper .utils .comments import comments_mapping , Comment
1213
1314logger = logging .getLogger ("codestripper" )
1415
1516
16- def strip_files (files : Iterable [str ], working_directory : Union [str , None ] = None , comment : str = "//" ,
17+ def strip_files (files : Iterable [str ], working_directory : Union [str , None ] = None , * , comments : Optional [ List [ str ]] = None ,
1718 output : Union [Path , str ] = "out" , dry_run : bool = False , fail_on_error : bool = False ) -> List [str ]:
19+
20+ if comments is not None :
21+ for comment in comments :
22+ parts = comment .split (":" )
23+ if len (parts ) == 2 :
24+ comments_mapping [parts [0 ]] = Comment (parts [1 ])
25+ else :
26+ comments_mapping [parts [0 ]] = Comment (parts [1 ], parts [2 ])
27+
1828 cwd = get_working_directory (working_directory )
1929 out = os .path .join (os .getcwd (), output )
2030 if os .path .isdir (out ):
@@ -26,7 +36,14 @@ def strip_files(files: Iterable[str], working_directory: Union[str, None] = None
2636 content = handle .read ()
2737 if content is not None :
2838 try :
29- stripped = CodeStripper (content , comment ).strip ()
39+ _ , file_extension = os .path .splitext (file )
40+ file_extension = file_extension .lower ()
41+ if not file_extension in comments_mapping :
42+ logger .error (f"Unknown extension: '{ file_extension } ', "
43+ f"please specify which comment to use for this file extension." )
44+ continue
45+ com = comments_mapping [file_extension ]
46+ stripped = CodeStripper (content , com ).strip ()
3047 except IgnoreFileError :
3148 logger .info (f"File '{ file } ' is ignored, because of ignore tag" )
3249 continue
@@ -50,7 +67,7 @@ def strip_files(files: Iterable[str], working_directory: Union[str, None] = None
5067
5168class CodeStripper :
5269
53- def __init__ (self , content : str , comment : str ) -> None :
70+ def __init__ (self , content : str , comment : Comment ) -> None :
5471 self .content = content
5572 self .comment = comment
5673
0 commit comments