Skip to content

Commit beca9e7

Browse files
committed
♻️ 🎨 decouple CodeCounter and Config
1 parent 20f71e3 commit beca9e7

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

code_counter/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def main():
1515
config.invoke(args.config())
1616
return
1717

18-
code_counter = CodeCounter(config)
18+
code_counter = CodeCounter()
1919

2020
search_args = args.search()
2121
code_counter.setSearchArgs(search_args)

code_counter/core/counter.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#!/usr/bin/env python3
2-
# coding:utf8
2+
# -*- coding: utf-8 -*-
3+
34
import os
45
import re
56
from collections import defaultdict
7+
from code_counter.conf.config import Config
68

79

810
class CodeCounter:
911

10-
def __init__(self, config):
12+
def __init__(self):
13+
self.config = Config()
14+
1115
self.total_file_lines = 0
1216
self.total_code_lines = 0
1317
self.total_blank_lines = 0
1418
self.total_comment_lines = 0
1519
self.files_of_language = defaultdict(int)
16-
self.config = config
1720

1821
self.search_args = None
19-
self.comment_symbol = ()
20-
self.ignore = ()
2122
self.lines_of_language = {}
2223
self.pattern = None
2324

@@ -33,14 +34,16 @@ def __init__(self, config):
3334

3435
def setSearchArgs(self, args):
3536
self.search_args = args
37+
if args.suffix:
38+
self.config.suffix = set(args.suffix)
39+
if args.comment:
40+
self.config.comment = set(args.comment)
41+
if args.ignore:
42+
self.config.ignore = set(args.ignore)
3643

37-
suffix = args.suffix if args.suffix else self.config.suffix
38-
self.comment_symbol = tuple(args.comment if args.comment else self.config.comment)
39-
self.ignore = tuple(args.ignore if args.ignore else self.config.ignore)
40-
41-
self.lines_of_language = {suffix: 0 for suffix in suffix}
44+
self.lines_of_language = {suffix: 0 for suffix in self.config.suffix}
4245

43-
regex = '.*\.({})$'.format('|'.join(suffix))
46+
regex = r'.*\.({})$'.format('|'.join(self.config.suffix))
4447
self.pattern = re.compile(regex)
4548

4649
def search(self):
@@ -126,7 +129,7 @@ def __search(self, input_path, output_file=None):
126129
for file in files:
127130
file_path = os.path.join(input_path, file)
128131
if os.path.isdir(file_path):
129-
if os.path.split(file_path)[-1] in self.ignore:
132+
if os.path.split(file_path)[-1] in self.config.ignore:
130133
continue
131134
self.__search(file_path, output_file)
132135
elif os.path:
@@ -201,7 +204,7 @@ def count_single(self, file_path, output_file=None):
201204
single['file_lines'] += 1
202205
if not line:
203206
single['blank_lines'] += 1
204-
elif line.startswith(self.comment_symbol):
207+
elif line.startswith(tuple(self.config.comment)):
205208
single['comment_lines'] += 1
206209
else:
207210
single['code_lines'] += 1

0 commit comments

Comments
 (0)