Skip to content

Commit 58d7b18

Browse files
authored
Merge pull request #10 from InnoFang/dev
🎨 fix the problem of program exit when decode lines error
2 parents 552641e + 7c15b1e commit 58d7b18

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ setup:
88
check:
99
python setup.py check
1010

11-
build:check
11+
build: check
1212
python setup.py sdist bdist_wheel
1313

14-
install:build
15-
pip install dist/*.whl
16-
17-
upload:build
14+
upload: build
1815
twine upload dist/*
1916

17+
install: build
18+
pip install dist/*.whl
19+
2020
uninstall:
2121
echo y | pip uninstall code-counter
2222

23+
reinstall: uninstall install
24+
2325
clean:
2426
rm -rf build code_counter.* dist

code_counter/core/counter.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)