Skip to content

Commit 85e10ab

Browse files
authored
Merge pull request #6 from InnoFang/dev
♻️ 🎨 refactor, optimize and format a lot
2 parents 08996b1 + 1c8a904 commit 85e10ab

File tree

4 files changed

+109
-71
lines changed

4 files changed

+109
-71
lines changed

code_counter/__main__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22
# coding:utf8
33

44
import time
5-
from code_counter.core.codecounter import CodeCounter
6-
from code_counter.core.argspaser import CodeCounterArgsParser
5+
from code_counter.core.counter import CodeCounter
6+
from code_counter.core.args import CodeCounterArgs
77
from code_counter.conf.config import Config
88

99

1010
def main():
11-
parser = CodeCounterArgsParser()
12-
args = parser.args
11+
args = CodeCounterArgs()
1312

1413
config = Config()
15-
if 'config' in parser.args:
16-
config.invoke(args['config'])
14+
if args.has_config_args():
15+
config.invoke(args.config())
1716
return
1817

1918
code_counter = CodeCounter(config)
2019

21-
search_args = args['search']
20+
search_args = args.search()
2221
code_counter.setSearchArgs(search_args)
2322

2423
time_start = time.time()
Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ def split_args(args):
1010
return list(args.split(','))
1111

1212

13-
class CodeCounterArgsParser:
13+
class CodeCounterArgs:
14+
__SEARCH__ = 'search'
15+
__CONFIG__ = 'config'
16+
1417
def __init__(self):
15-
parser = argparse.ArgumentParser(prog="code-counter",
16-
description="A command-line interface (CLI) utility that can help you easily count code and display detailed results.",
17-
usage="""cocnt <command> [<args>]
18+
parser = argparse.ArgumentParser(
19+
prog="code-counter",
20+
description="A command-line interface (CLI) utility "
21+
"that can help you easily count code and display detailed results.",
22+
usage="""cocnt <command> [<args>]
1823
These are common Code-Counter commands used in various situations:
1924
search Search code in the given path(s)
2025
config Configure Code-Counter
@@ -27,13 +32,36 @@ def __init__(self):
2732
print("Unrecognized command")
2833
parser.print_help()
2934
exit(1)
30-
self.args = {args.command: getattr(self, args.command)()}
35+
self.__args = {args.command: argparse.Namespace()}
36+
getattr(self, args.command)()
37+
38+
def has_search_args(self):
39+
return self.__SEARCH__ in self.__args
40+
41+
def has_config_args(self):
42+
return self.__CONFIG__ in self.__args
3143

3244
def search(self):
45+
if not self.has_search_args():
46+
return None
47+
if self.__args[self.__SEARCH__] == argparse.Namespace():
48+
self.__args[self.__SEARCH__] = self.__search()
49+
return self.__args[self.__SEARCH__]
50+
51+
def config(self):
52+
if not self.has_config_args():
53+
return None
54+
if self.__args[self.__CONFIG__] == argparse.Namespace():
55+
self.__args[self.__CONFIG__] = self.__config()
56+
return self.__args[self.__CONFIG__]
57+
58+
def __search(self):
3359
parser = argparse.ArgumentParser(
34-
description="Search code in the given path(s)")
60+
description="Search code in the given path(s)",
61+
usage="cocnt search input_path [-h] [-v] [-g] "
62+
"[-o OUTPUT_PATH] [--suffix SUFFIX] [--comment COMMENT] [--ignore IGNORE]")
3563
parser.add_argument('input_path', type=split_args,
36-
help="specify a file or directory path you want to count or use CONFIG placeholder to configure")
64+
help="counting the code lines according the given path(s)")
3765
parser.add_argument('-v', '--verbose', dest="verbose", action='store_true',
3866
help="show verbose infomation")
3967
parser.add_argument('-g', '--graph', dest='graph', action='store_true',
@@ -43,14 +71,21 @@ def search(self):
4371
parser.add_argument('--suffix', dest='suffix', type=split_args,
4472
help="what code files do you want to count, this parameter is disposable")
4573
parser.add_argument('--comment', dest='comment', type=split_args,
46-
help="the comment symbol, which can be judged whether the current line is a comment, this parameter is disposable")
74+
help="the comment symbol, which can be judged whether the current line is a comment, "
75+
"this parameter is disposable")
4776
parser.add_argument('--ignore', dest='ignore', type=split_args,
48-
help="ignore some directories or files that you don't want to count, this parameter is disposable")
77+
help="ignore some directories or files that you don't want to count, "
78+
"this parameter is disposable")
4979
return parser.parse_args(sys.argv[2:])
5080

51-
def config(self):
52-
parser = argparse.ArgumentParser(prog="code-counter",
53-
description="A command-line interface (CLI) utility that can help you easily count code and display detailed results.")
81+
def __config(self):
82+
parser = argparse.ArgumentParser(
83+
prog="code-counter",
84+
description="configure code-counter",
85+
usage="cocnt config [-h] [--list] [--suffix-reset SUFFIX_RESET] "
86+
"[--suffix-add SUFFIX_ADD] [--comment-reset COMMENT_RESET] "
87+
"[--comment-add COMMENT_ADD] [--ignore-reset IGNORE_RESET] "
88+
"[--ignore-add IGNORE_ADD] [--restore] ")
5489
parser.add_argument('--list', dest='show_list', action='store_true',
5590
help="list all variables set in config file, along with their values")
5691
parser.add_argument('--suffix-reset', dest='suffix_reset', type=split_args,
@@ -61,12 +96,15 @@ def config(self):
6196
parser.add_argument('--comment-reset', dest='comment_reset', type=split_args,
6297
help="override 'comment' in config and count comment lines according to this value")
6398
parser.add_argument('--comment-add', dest='comment_add', type=split_args,
64-
help="append new value for 'comment' in config and count comment lines according to this value")
99+
help="append new value for 'comment' in config "
100+
"and count comment lines according to this value")
65101

66102
parser.add_argument('--ignore-reset', dest='ignore_reset', type=split_args,
67-
help="override 'ignore' in config and ignore some files or directory according to this value")
103+
help="override 'ignore' in config "
104+
"and ignore some files or directory according to this value")
68105
parser.add_argument('--ignore-add', dest='ignore_add', type=split_args,
69-
help="append new value for 'ignore' in config and ignore some files or directory according to this value")
106+
help="append new value for 'ignore' in config "
107+
"and ignore some files or directory according to this value")
70108

71109
parser.add_argument('--restore', dest='restore', action='store_true',
72110
help="restore default config")

test/test.py

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import unittest
77
from unittest.mock import patch
8-
from code_counter.core.argspaser import CodeCounterArgsParser
8+
from code_counter.core.args import CodeCounterArgs
99
from code_counter.conf.config import Config
1010
from code_counter.__main__ import main
1111

@@ -27,23 +27,23 @@ def test_print_help(self):
2727
options = ('python', app_path, '--help')
2828
sys.argv[1:] = options[2:]
2929
try:
30-
CodeCounterArgsParser()
30+
CodeCounterArgs()
3131
except SystemExit:
3232
pass
3333

3434
def test_print_search_help(self):
3535
options = ('python', app_path, 'search', '--help')
3636
sys.argv[1:] = options[2:]
3737
try:
38-
CodeCounterArgsParser()
38+
CodeCounterArgs()
3939
except SystemExit:
4040
pass
4141

4242
def test_print_config_help(self):
4343
options = ('python', app_path, 'config', '--help')
4444
sys.argv[1:] = options[2:]
4545
try:
46-
CodeCounterArgsParser()
46+
CodeCounterArgs()
4747
except SystemExit:
4848
pass
4949

@@ -58,11 +58,11 @@ def test_search_args(self):
5858
'--comment=//,#,/*',
5959
'--ignore=.vscode,.idea']
6060
sys.argv[1:] = options[2:]
61-
parser = CodeCounterArgsParser()
62-
self.assertTrue('config' not in parser.args, '"config" is in the "args"')
63-
self.assertTrue('search' in parser.args, '"search" is not in the "args"')
64-
search_args = parser.args['search']
65-
self.assertEqual(search_args.path, '../code_counter/', "search path parsed error.")
61+
args = CodeCounterArgs()
62+
self.assertFalse(args.has_config_args(), '"config" is in the "args"')
63+
self.assertTrue(args.has_search_args(), '"search" is not in the "args"')
64+
search_args = args.search()
65+
self.assertEqual(search_args.input_path, ['../code_counter/'], "search path parsed error.")
6666
self.assertTrue(search_args.verbose, '-v,--verbose flag parsed error.')
6767
self.assertTrue(search_args.graph, '-g,--graph flag parsed error.')
6868
self.assertEqual(search_args.output_path, 'output.txt', "output path parsed error.")
@@ -82,10 +82,10 @@ def test_config_args(self):
8282
'--ignore-reset=target',
8383
'--restore']
8484
sys.argv[1:] = options[2:]
85-
parser = CodeCounterArgsParser()
86-
self.assertTrue('config' in parser.args, '"config" is not in the "args"')
87-
self.assertTrue('search' not in parser.args, '"search" is in the "args"')
88-
config_args = parser.args['config']
85+
args = CodeCounterArgs()
86+
self.assertTrue(args.has_config_args(), '"config" is not in the "args"')
87+
self.assertFalse(args.has_search_args(), '"search" is in the "args"')
88+
config_args = args.config()
8989
self.assertTrue(config_args.show_list, '--list flag parsed error.')
9090
self.assertEqual(config_args.suffix_add, ['lisp'], "suffix_add flag and values parsed error.")
9191
self.assertEqual(config_args.suffix_reset, ['clj'], "suffix_reset flag and values parsed error.")
@@ -103,11 +103,12 @@ def test_Config_restore(self, mock_input):
103103
'config',
104104
'--restore']
105105
sys.argv[1:] = options[2:]
106-
parser = CodeCounterArgsParser()
107-
args = parser.args
106+
107+
args = CodeCounterArgs()
108+
self.assertTrue(args.has_config_args())
109+
108110
config = Config()
109-
self.assertTrue('config' in args)
110-
config.invoke(args['config'])
111+
config.invoke(args.config())
111112

112113
self.assertEqual(config.suffix, self.default_suffix, "the suffix doesn't equal")
113114
self.assertEqual(config.comment, self.default_comment, "the comment doesn't equal")
@@ -126,10 +127,10 @@ def test_Config_reset1(self, mock_input):
126127
'--comment-reset=//,#,/**',
127128
'--ignore-reset=target,build,node_modules,__pycache__']
128129
sys.argv[1:] = options[2:]
129-
parser = CodeCounterArgsParser()
130-
args = parser.args
131-
self.assertTrue('config' in args)
132-
config.invoke(args['config'])
130+
131+
args = CodeCounterArgs()
132+
self.assertTrue(args.has_config_args())
133+
config.invoke(args.config())
133134

134135
suffix = ['java', 'cpp', 'go', 'js', 'py']
135136
comment = ['//', '#', '/**']
@@ -154,10 +155,10 @@ def test_Config_reset2(self, mock_input):
154155
'--comment-reset=//,#,/**',
155156
'--ignore-reset=target,build,node_modules,__pycache__']
156157
sys.argv[1:] = options[2:]
157-
parser = CodeCounterArgsParser()
158-
args = parser.args
159-
self.assertTrue('config' in args)
160-
config.invoke(args['config'])
158+
159+
args = CodeCounterArgs()
160+
self.assertTrue(args.has_config_args())
161+
config.invoke(args.config())
161162

162163
suffix = ['java', 'cpp', 'go', 'js', 'py']
163164
comment = ['//', '#', '/**']
@@ -182,10 +183,10 @@ def test_Config_reset3(self, mock_input):
182183
'--comment-reset=//,#,/**',
183184
'--ignore-reset=target,build,node_modules,__pycache__']
184185
sys.argv[1:] = options[2:]
185-
parser = CodeCounterArgsParser()
186-
args = parser.args
187-
self.assertTrue('config' in args)
188-
config.invoke(args['config'])
186+
187+
args = CodeCounterArgs()
188+
self.assertTrue(args.has_config_args())
189+
config.invoke(args.config())
189190

190191
suffix = ['java', 'cpp', 'go', 'js', 'py']
191192
comment = ['//', '#', '/**']
@@ -210,10 +211,10 @@ def test_Config_reset4(self, mock_input):
210211
'--comment-reset=//,#,/**',
211212
'--ignore-reset=target,build,node_modules,__pycache__']
212213
sys.argv[1:] = options[2:]
213-
parser = CodeCounterArgsParser()
214-
args = parser.args
215-
self.assertTrue('config' in args)
216-
config.invoke(args['config'])
214+
215+
args = CodeCounterArgs()
216+
self.assertTrue(args.has_config_args())
217+
config.invoke(args.config())
217218

218219
suffix = ['java', 'cpp', 'go', 'js', 'py']
219220
comment = ['//', '#', '/**']
@@ -238,10 +239,10 @@ def test_Config_add1(self, mock_input):
238239
'--comment-add=TEST_COMMENT',
239240
'--ignore-add=TEST_IGNORE']
240241
sys.argv[1:] = options[2:]
241-
parser = CodeCounterArgsParser()
242-
args = parser.args
243-
self.assertTrue('config' in args)
244-
config.invoke(args['config'])
242+
243+
args = CodeCounterArgs()
244+
self.assertTrue(args.has_config_args())
245+
config.invoke(args.config())
245246

246247
suffix = 'TEST_SUFFIX'
247248
comment = 'TEST_COMMENT'
@@ -266,10 +267,10 @@ def test_Config_add2(self, mock_input):
266267
'--comment-add=TEST_COMMENT',
267268
'--ignore-add=TEST_IGNORE']
268269
sys.argv[1:] = options[2:]
269-
parser = CodeCounterArgsParser()
270-
args = parser.args
271-
self.assertTrue('config' in args)
272-
config.invoke(args['config'])
270+
271+
args = CodeCounterArgs()
272+
self.assertTrue(args.has_config_args())
273+
config.invoke(args.config())
273274

274275
suffix = 'TEST_SUFFIX'
275276
comment = 'TEST_COMMENT'
@@ -294,10 +295,10 @@ def test_Config_add3(self, mock_input):
294295
'--comment-add=TEST_COMMENT',
295296
'--ignore-add=TEST_IGNORE']
296297
sys.argv[1:] = options[2:]
297-
parser = CodeCounterArgsParser()
298-
args = parser.args
299-
self.assertTrue('config' in args)
300-
config.invoke(args['config'])
298+
299+
args = CodeCounterArgs()
300+
self.assertTrue(args.has_config_args())
301+
config.invoke(args.config())
301302

302303
suffix = 'TEST_SUFFIX'
303304
comment = 'not TEST_COMMENT'
@@ -322,10 +323,10 @@ def test_Config_add4(self, mock_input):
322323
'--comment-add=TEST_COMMENT',
323324
'--ignore-add=TEST_IGNORE']
324325
sys.argv[1:] = options[2:]
325-
parser = CodeCounterArgsParser()
326-
args = parser.args
327-
self.assertTrue('config' in args)
328-
config.invoke(args['config'])
326+
327+
args = CodeCounterArgs()
328+
self.assertTrue(args.has_config_args())
329+
config.invoke(args.config())
329330

330331
suffix = 'TEST_SUFFIX'
331332
comment = 'TEST_COMMENT'

0 commit comments

Comments
 (0)