Skip to content

Commit 97cd1cf

Browse files
authored
Merge pull request #3 from InnoFang/dev
♻️ ❇️ 🚧 💥 breaking change
2 parents 42b8910 + 2a21508 commit 97cd1cf

File tree

8 files changed

+319
-180
lines changed

8 files changed

+319
-180
lines changed

code_counter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.0'
1+
__version__ = "1.0.0-alpha"

code_counter/__main__.py

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,38 @@
11
#!/usr/bin/env python3
22
# coding:utf8
33

4-
import sys
54
import time
6-
import argparse
75
from code_counter.core.codecounter import CodeCounter
6+
from code_counter.core.argspaser import CodeCounterArgsParser
87
from code_counter.conf.config import Config
9-
from code_counter import __version__
108

11-
def args_parser():
12-
parser = argparse.ArgumentParser(prog="code-counter",
13-
description="A command-line interface (CLI) utility that can help you easily count code and display detailed results.")
149

15-
parser.add_argument('-V', '--version', action='version', version='%(prog)s {}'.format(__version__))
16-
17-
parser.add_argument('param', metavar='[path, CONFIG]',
18-
help="specify a file or directory path you want to count or use CONFIG placeholder to configure")
19-
parser.add_argument('-l', '--list', dest='use_list', action='store_true',
20-
help="the file contains a list of file path, which can make you search more than one file or directory")
21-
parser.add_argument('-v', '--verbose', dest="verbose", action='store_true',
22-
help="show verbose infomation")
23-
parser.add_argument('-g', '--graph', dest='graph', action='store_true',
24-
help="choose to whether to visualize the result")
25-
parser.add_argument('-o', '--output', dest='output_path', metavar='OUTPUT',
26-
help="specify a output path if you want to store the result")
27-
28-
# Configure arguments
29-
config_args_type = lambda args: list(args.split(','))
30-
31-
# suffix = parser.add_mutually_exclusive_group()
32-
parser.add_argument('--suffix', dest='suffix', type=config_args_type,
33-
help="what code files do you want to count, this parameter is disposable")
34-
parser.add_argument('--suffix-save', dest='suffix_save', type=config_args_type,
35-
help="override 'suffix' in config and count codes according to this value")
36-
parser.add_argument('--suffix-add', dest='suffix_add', type=config_args_type,
37-
help="append new value for 'suffix' in config and count codes according to this value")
38-
39-
# comment = parser.add_mutually_exclusive_group()
40-
parser.add_argument('--comment', dest='comment', type=config_args_type,
41-
help="the comment symbol, which can be judged whether the current line is a comment, this parameter is disposable")
42-
parser.add_argument('--comment-save', dest='comment_save', type=config_args_type,
43-
help="override 'comment' in config and count comment lines according to this value")
44-
parser.add_argument('--comment-add', dest='comment_add', type=config_args_type,
45-
help="append new value for 'comment' in config and count comment lines according to this value")
46-
47-
# ignore = parser.add_mutually_exclusive_group()
48-
parser.add_argument('--ignore', dest='ignore', type=config_args_type,
49-
help="ignore some directories or files that you don't want to count, this parameter is disposable")
50-
parser.add_argument('--ignore-save', dest='ignore_save', type=config_args_type,
51-
help="override 'ignore' in config and ignore some files or directory according to this value")
52-
parser.add_argument('--ignore-add', dest='ignore_add', type=config_args_type,
53-
help="append new value for 'ignore' in config and ignore some files or directory according to this value")
10+
def main():
11+
parser = CodeCounterArgsParser()
12+
args = parser.args
5413

55-
parser.add_argument('--restore', dest='restore', action='store_true',
56-
help="restore default config")
57-
58-
return parser.parse_args()
14+
config = Config()
15+
if 'config' in parser.args:
16+
config.invoke(args['config'])
17+
return
5918

60-
def main():
61-
args = args_parser()
62-
63-
config = Config(args)
19+
if 'search' not in parser.args:
20+
raise Exception('wrong subcommand, only `config` and `search` are supported!')
6421

65-
if args.param == 'CONFIG':
66-
config.show()
67-
sys.exit(0)
68-
6922
code_counter = CodeCounter(config)
7023

24+
search_args = args['search']
25+
code_counter.setSearchArgs(search_args)
26+
7127
time_start = time.time()
72-
code_counter.count(args.param, args.verbose, args.use_list, args.output_path)
28+
code_counter.search()
7329
time_end = time.time()
7430

75-
print('\n\tTotally cost {}s.'.format(time_end - time_start))
31+
print('\n\tTotally cost {} s.'.format(time_end - time_start))
7632

77-
if args.graph:
33+
if search_args.graph:
7834
code_counter.visualize()
7935

36+
8037
if __name__ == '__main__':
8138
main()
82-
83-

code_counter/conf/config.py

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,80 @@
11
#!/usr/bin/env python3
22
# coding:utf8
3-
import json
3+
import json
44
import sys
55
import pkg_resources
66

7+
78
class Config:
89

9-
def __init__(self, args):
10-
if args.restore:
11-
self.__restore()
12-
return
13-
14-
conf = self.__load()
15-
self.suffix = args.suffix if args.suffix else conf['suffix']
16-
self.comment = args.comment if args.comment else conf['comment']
17-
self.ignore = args.ignore if args.ignore else conf['ignore']
10+
def __init__(self):
11+
conf = self.__load()
12+
13+
self.suffix = conf['suffix']
14+
self.comment = conf['comment']
15+
self.ignore = conf['ignore']
16+
17+
def invoke(self, args):
18+
if args.restore:
19+
self.__restore()
20+
else:
21+
if any([args.suffix_add, args.comment_add, args.ignore_add]):
22+
self.__append_config(args.suffix_add, args.comment_add, args.ignore_add)
23+
if any([args.suffix_reset, args.comment_reset, args.ignore_reset]):
24+
self.__reset_config(args.suffix_reset, args.comment_reset, args.ignore_reset)
25+
if args.list:
26+
self.show()
27+
28+
def show(self):
29+
print(json.dumps(self.__dict__, indent=4))
30+
31+
def __confirm(self, tips):
32+
check = input(tips)
33+
return check.strip().lower() == 'y'
34+
35+
def __append_config(self, suffix_add, comment_add, ignore_add):
36+
if suffix_add:
37+
if self.__confirm("'suffix' will be appended with {} (y/n)".format(suffix_add)):
38+
self.suffix.extend(suffix_add)
39+
if comment_add:
40+
if self.__confirm("'comment' will be appended with {} (y/n)".format(comment_add)):
41+
self.comment.extend(comment_add)
42+
if ignore_add:
43+
if self.__confirm("'ignore' will be appended with {} (y/n)".format(ignore_add)):
44+
self.ignore.extend(ignore_add)
45+
46+
self.__update()
1847

19-
if any([args.suffix_add, args.comment_add, args.ignore_add]):
20-
self.__append_config(args.suffix_add, args.comment_add, args.ignore_add)
48+
def __reset_config(self, suffix_reset, comment_reset, ignore_reset):
49+
if suffix_reset:
50+
if self.__confirm("'suffix' will be replaced with {} (y/n)".format(suffix_reset)):
51+
self.suffix = suffix_reset
52+
if comment_reset:
53+
if self.__confirm("'comment' will be replaced with {} (y/n)".format(comment_reset)):
54+
self.comment = comment_reset
55+
if ignore_reset:
56+
if self.__confirm("'ignore' will be replaced with {} (y/n)".format(ignore_reset)):
57+
self.ignore = ignore_reset
2158

22-
if any([args.suffix_save, args.comment_save, args.ignore_save]):
23-
self.__save_config(args.suffix_save, args.comment_save, args.ignore_save)
24-
25-
def show(self):
26-
print(json.dumps(self.__dict__, indent=4))
27-
28-
def __confirm(self,tips):
29-
check = input(tips)
30-
return check.strip().lower() == 'y'
59+
self.__update()
3160

32-
def __append_config(self, suffix_add, comment_add, ignore_add):
33-
if suffix_add:
34-
if self.__confirm("'suffix' will be appended with {} (y/n)".format(suffix_add)):
35-
self.suffix.extend(suffix_add)
36-
if comment_add:
37-
if self.__confirm("'comment' will be appended with {} (y/n)".format(comment_add)):
38-
self.comment.extend(comment_add)
39-
if ignore_add:
40-
if self.__confirm("'ignore' will be appended with {} (y/n)".format(ignore_add)):
41-
self.ignore.extend(ignore_add)
61+
def __load(self):
62+
filename = pkg_resources.resource_filename(__name__, 'config.json')
63+
with open(filename, 'r') as config:
64+
conf = json.load(config)
65+
return conf
4266

43-
self.__update()
44-
45-
def __save_config(self, suffix_save, comment_save, ignore_save):
46-
if suffix_save:
47-
if self.__confirm("'suffix' will be replaced with {} (y/n)".format(suffix_save)):
48-
self.suffix = suffix_save
49-
if comment_save:
50-
if self.__confirm("'comment' will be replaced with {} (y/n)".format(comment_save)):
51-
self.comment = comment_save
52-
if ignore_save:
53-
if self.__confirm("'ignore' will be replaced with {} (y/n)".format(ignore_save)):
54-
self.ignore = ignore_save
67+
def __update(self):
68+
filename = pkg_resources.resource_filename(__name__, 'config.json')
69+
with open(filename, 'w') as config:
70+
json.dump(self.__dict__, config, indent=4)
5571

56-
self.__update()
57-
58-
def __load(self):
59-
filename = pkg_resources.resource_filename(__name__, 'config.json')
60-
with open(filename, 'r') as config:
61-
conf = json.load(config)
62-
return conf
63-
64-
def __update(self):
65-
filename = pkg_resources.resource_filename(__name__, 'config.json')
66-
with open(filename, 'w') as config:
67-
json.dump(self.__dict__, config, indent=4)
68-
69-
def __restore(self):
70-
self.suffix = ["c", "cc", "clj", "cpp", "cs", "cu", "cuh", "dart", "go", "h",
71-
"hpp", "java", "jl", "js", "kt", "lisp", "lua", "pde", "m", "php",
72-
"py", "R", "rb", "rs", "rust", "sh", "scala", "swift", "ts", "vb"]
73-
self.comment = ["#", "//", "/*", "*", "*/", ":", ";", '""""']
74-
self.ignore = ["out", "venv", ".git", ".idea", "build", "target", "node_modules", ".vscode", "dist"]
72+
def __restore(self):
73+
self.suffix = ["c", "cc", "clj", "cpp", "cs", "cu", "cuh", "dart", "go", "h",
74+
"hpp", "java", "jl", "js", "kt", "lisp", "lua", "pde", "m", "php",
75+
"py", "R", "rb", "rs", "rust", "sh", "scala", "swift", "ts", "vb"]
76+
self.comment = ["#", "//", "/*", "*", "*/", ":", ";", '""""']
77+
self.ignore = ["out", "venv", ".git", ".idea", "build", "target", "node_modules", ".vscode", "dist"]
7578

76-
if self.__confirm('Default configuration will be restored (y/n)?'):
77-
self.__update()
79+
if self.__confirm('Default configuration will be restored (y/n)?'):
80+
self.__update()

code_counter/core/argspaser.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
# coding:utf8
3+
4+
import sys
5+
import argparse
6+
from code_counter import __version__
7+
8+
9+
def config_args_type(args):
10+
return list(args.split(','))
11+
12+
13+
class CodeCounterArgsParser:
14+
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+
These are common Code-Counter commands used in various situations:
19+
search Search code in the given path(s)
20+
config Configure Code-Counter
21+
""")
22+
parser.add_argument('--version', action='version',
23+
version='%(prog)s {}'.format(__version__))
24+
parser.add_argument("command", help="Subcommand to run, `search` or `config`")
25+
args = parser.parse_args(sys.argv[1:2])
26+
if not hasattr(self, args.command):
27+
print("Unrecognized command")
28+
parser.print_help()
29+
self.args = {args.command: getattr(self, args.command)()}
30+
31+
def search(self):
32+
parser = argparse.ArgumentParser(
33+
description="Search code in the given path(s)")
34+
parser.add_argument('path',
35+
help="specify a file or directory path you want to count or use CONFIG placeholder to configure")
36+
parser.add_argument('-v', '--verbose', dest="verbose", action='store_true',
37+
help="show verbose infomation")
38+
parser.add_argument('-g', '--graph', dest='graph', action='store_true',
39+
help="choose to whether to visualize the result")
40+
parser.add_argument('-o', '--output', dest='output_path',
41+
help="specify a output path if you want to store the result")
42+
parser.add_argument('--suffix', dest='suffix', type=config_args_type,
43+
help="what code files do you want to count, this parameter is disposable")
44+
parser.add_argument('--comment', dest='comment', type=config_args_type,
45+
help="the comment symbol, which can be judged whether the current line is a comment, this parameter is disposable")
46+
parser.add_argument('--ignore', dest='ignore', type=config_args_type,
47+
help="ignore some directories or files that you don't want to count, this parameter is disposable")
48+
return parser.parse_args(sys.argv[2:])
49+
50+
def config(self):
51+
parser = argparse.ArgumentParser(prog="code-counter",
52+
description="A command-line interface (CLI) utility that can help you easily count code and display detailed results.")
53+
parser.add_argument('--list', dest='show_list', action='store_true',
54+
help="list all variables set in config file, along with their values")
55+
parser.add_argument('--suffix-reset', dest='suffix_reset', type=config_args_type,
56+
help="override 'suffix' in config and count codes according to this value")
57+
parser.add_argument('--suffix-add', dest='suffix_add', type=config_args_type,
58+
help="append new value for 'suffix' in config and count codes according to this value")
59+
60+
parser.add_argument('--comment-reset', dest='comment_reset', type=config_args_type,
61+
help="override 'comment' in config and count comment lines according to this value")
62+
parser.add_argument('--comment-add', dest='comment_add', type=config_args_type,
63+
help="append new value for 'comment' in config and count comment lines according to this value")
64+
65+
parser.add_argument('--ignore-reset', dest='ignore_reset', type=config_args_type,
66+
help="override 'ignore' in config and ignore some files or directory according to this value")
67+
parser.add_argument('--ignore-add', dest='ignore_add', type=config_args_type,
68+
help="append new value for 'ignore' in config and ignore some files or directory according to this value")
69+
70+
parser.add_argument('--restore', dest='restore', action='store_true',
71+
help="restore default config")
72+
73+
return parser.parse_args(sys.argv[2:])

0 commit comments

Comments
 (0)