@@ -117,8 +117,8 @@ def search(self):
117117
118118 def __search (self , input_path , output_file = None ):
119119 """
120- :param input_path: path
121- :param output_file: file
120+ :param input_path: input file path
121+ :param output_file: output file path
122122 :return:
123123 """
124124 if os .path .isdir (input_path ):
@@ -135,16 +135,16 @@ def __search(self, input_path, output_file=None):
135135 elif os .path .isfile (input_path ):
136136 self .__format_output (input_path , output_file )
137137
138- def __format_output (self , file_path , output_file ):
138+ def __format_output (self , file_path , output_file = None ):
139139 """
140- :param file_path: file path
141- :param output_file: file
140+ :param file_path: input file path
141+ :param output_file: output file path
142142 :return:
143143 """
144144 try :
145145 res = re .match (self .pattern , file_path )
146146 if res :
147- single = self .count_single (file_path )
147+ single = self .count_single (file_path , output_file )
148148 file_lines = single ['file_lines' ]
149149 code_lines = single ['code_lines' ]
150150 blank_lines = single ['blank_lines' ]
@@ -166,9 +166,10 @@ def __format_output(self, file_path, output_file):
166166 except AttributeError as e :
167167 print (e )
168168
169- def count_single (self , file_path ):
169+ def count_single (self , file_path , output_file = None ):
170170 """
171171 :param file_path: the file you want to count
172+ :param output_file: output file path
172173 :return: single { file_lines, code_lines, blank_lines, comment_lines }
173174 """
174175 assert os .path .isfile (file_path ), "Function: 'code_counter' need a file path, but {} is not." .format (file_path )
@@ -179,13 +180,23 @@ def count_single(self, file_path):
179180 'blank_lines' : 0 ,
180181 'comment_lines' : 0 ,
181182 }
183+
182184 with open (file_path , 'rb' ) as handle :
183- for l in handle :
185+ for line_number , raw_line in enumerate ( handle ) :
184186 try :
185- line = l .strip ().decode ('utf8' )
187+ line = raw_line .strip ().decode ('utf8' )
186188 except UnicodeDecodeError :
187- # If the code line contain Chinese string, decode it as gbk
188- line = l .strip ().decode ('gbk' )
189+ try :
190+ # If the code line contain Chinese string, decode it as gbk
191+ line = raw_line .strip ().decode ('gbk' )
192+ except UnicodeDecodeError :
193+ if self .search_args .verbose :
194+ print ('\n \t {:>10} | decode line occurs a problem, non-count it, at File "{}", line {}:'
195+ .format ('WARN' , file_path , line_number ),
196+ file = output_file )
197+ print ('\t {:>10} | {}\n '
198+ .format (' ' , raw_line ))
199+ continue
189200
190201 single ['file_lines' ] += 1
191202 if not line :
0 commit comments