Skip to content

Commit 3e636af

Browse files
authored
Merge pull request #7 from InnoFang/dev
❇️ 🎨 βœ… πŸ› refactor unit test cases, fix some bugs, and add a new feature that support remove some values of config
2 parents c8ba6d3 + dcebe0a commit 3e636af

File tree

11 files changed

+299
-204
lines changed

11 files changed

+299
-204
lines changed

β€Ž.github/workflows/code-counter-CI.ymlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ jobs:
1717
with:
1818
python-version: ${{ matrix.python-version }}
1919
- name: Test with pytest
20-
run: python -m unittest discover -s test -p "test.py"
20+
run: python -m unittest discover -s tests -p "test_*.py"

β€ŽMakefileβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
.PHONY:
2+
test:
3+
python -m unittest discover -s tests -p "test_*.py"
4+
15
setup:
26
python setup.py install
37

4-
5-
.PHONY:
68
check:
79
python setup.py check
810

@@ -19,4 +21,4 @@ uninstall:
1921
echo y | pip uninstall code-counter
2022

2123
clean:
22-
rm -rf build code_counter.* dist
24+
rm -rf build code_counter.* dist

β€Žcode_counter/__init__.pyβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.0-alpha"
1+
__version__ = "1.0.0"
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
{
22
"suffix": [
3-
"c",
3+
"java",
4+
"swift",
5+
"dart",
6+
"rb",
47
"cc",
5-
"clj",
6-
"cpp",
7-
"cs",
8+
"go",
9+
"php",
10+
"sh",
11+
"lua",
12+
"m",
813
"cu",
14+
"c",
15+
"vb",
16+
"clj",
17+
"js",
918
"cuh",
10-
"dart",
11-
"go",
12-
"h",
1319
"hpp",
14-
"java",
15-
"jl",
16-
"js",
20+
"R",
21+
"cs",
1722
"kt",
18-
"lisp",
19-
"lua",
20-
"pde",
21-
"m",
22-
"php",
23+
"ts",
24+
"scala",
25+
"rust",
2326
"py",
24-
"R",
25-
"rb",
27+
"cpp",
2628
"rs",
27-
"rust",
28-
"sh",
29-
"scala",
30-
"swift",
31-
"ts",
32-
"vb"
29+
"h",
30+
"jl",
31+
"pde",
32+
"lisp"
3333
],
3434
"comment": [
35-
"#",
3635
"//",
37-
"/*",
36+
":",
3837
"*",
3938
"*/",
40-
":",
4139
";",
42-
"\"\"\"\""
40+
"#",
41+
"\"\"\"\"",
42+
"/*"
4343
],
4444
"ignore": [
45-
"out",
4645
"venv",
47-
".git",
48-
".idea",
49-
"build",
46+
"dist",
5047
"target",
48+
"build",
49+
".idea",
5150
"node_modules",
5251
".vscode",
53-
"dist"
52+
".git",
53+
"out"
5454
]
5555
}

β€Žcode_counter/conf/config.pyβ€Ž

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,78 @@
55
import pkg_resources
66

77

8+
class SetEncoder(json.JSONEncoder):
9+
def default(self, obj):
10+
if isinstance(obj, set):
11+
return list(obj)
12+
return json.JSONEncoder.default(self, obj)
13+
14+
815
class Config:
916

1017
def __init__(self):
1118
conf = self.__load()
1219

13-
self.suffix = conf['suffix']
14-
self.comment = conf['comment']
15-
self.ignore = conf['ignore']
20+
self.suffix = set(conf['suffix'])
21+
self.comment = set(conf['comment'])
22+
self.ignore = set(conf['ignore'])
1623

1724
def invoke(self, args):
1825
if args.restore:
1926
self.restore()
2027
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)
2328
if any([args.suffix_reset, args.comment_reset, args.ignore_reset]):
2429
self.__reset_config(args.suffix_reset, args.comment_reset, args.ignore_reset)
30+
if any([args.suffix_add, args.comment_add, args.ignore_add]):
31+
self.__append_config(args.suffix_add, args.comment_add, args.ignore_add)
32+
if any([args.suffix_del, args.comment_del, args.ignore_del]):
33+
self.__remove_config(args.suffix_del, args.comment_del, args.ignore_del)
2534
if args.show_list:
2635
self.show()
2736

2837
def show(self):
29-
print(json.dumps(self.__dict__, indent=4))
38+
print(json.dumps(self.__dict__, indent=4, cls=SetEncoder))
3039

3140
def __confirm(self, tips):
3241
check = input(tips)
3342
return check.strip().lower() == 'y'
3443

44+
def __reset_config(self, suffix_reset, comment_reset, ignore_reset):
45+
if suffix_reset:
46+
if self.__confirm("'suffix' will be replaced with {} . (y/n) ".format(suffix_reset)):
47+
self.suffix = set(suffix_reset)
48+
if comment_reset:
49+
if self.__confirm("'comment' will be replaced with {} . (y/n) ".format(comment_reset)):
50+
self.comment = set(comment_reset)
51+
if ignore_reset:
52+
if self.__confirm("'ignore' will be replaced with {} . (y/n) ".format(ignore_reset)):
53+
self.ignore = set(ignore_reset)
54+
55+
self.__update()
56+
3557
def __append_config(self, suffix_add, comment_add, ignore_add):
3658
if suffix_add:
37-
if self.__confirm("'suffix' will be appended with {} (y/n)".format(suffix_add)):
38-
self.suffix.extend(suffix_add)
59+
if self.__confirm("'suffix' will be appended with {} . (y/n) ".format(suffix_add)):
60+
self.suffix.update(suffix_add)
3961
if comment_add:
40-
if self.__confirm("'comment' will be appended with {} (y/n)".format(comment_add)):
41-
self.comment.extend(comment_add)
62+
if self.__confirm("'comment' will be appended with {} . (y/n) ".format(comment_add)):
63+
self.comment.update(comment_add)
4264
if ignore_add:
43-
if self.__confirm("'ignore' will be appended with {} (y/n)".format(ignore_add)):
44-
self.ignore.extend(ignore_add)
65+
if self.__confirm("'ignore' will be appended with {} . (y/n) ".format(ignore_add)):
66+
self.ignore.update(ignore_add)
4567

4668
self.__update()
4769

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
70+
def __remove_config(self, suffix_del, comment_del, ignore_del):
71+
if suffix_del:
72+
if self.__confirm("'suffix' will remove {} . (y/n) ".format(suffix_del)):
73+
self.suffix.difference_update(suffix_del)
74+
if comment_del:
75+
if self.__confirm("'comment' will remove {} . (y/n) ".format(comment_del)):
76+
self.comment.difference_update(comment_del)
77+
if ignore_del:
78+
if self.__confirm("'ignore' will remove {} . (y/n) ".format(ignore_del)):
79+
self.ignore.difference_update(ignore_del)
5880

5981
self.__update()
6082

@@ -67,14 +89,14 @@ def __load(self):
6789
def __update(self):
6890
filename = pkg_resources.resource_filename(__name__, 'config.json')
6991
with open(filename, 'w') as config:
70-
json.dump(self.__dict__, config, indent=4)
92+
json.dump(self.__dict__, config, indent=4, cls=SetEncoder)
7193

7294
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"]
95+
self.suffix = {"c", "cc", "clj", "cpp", "cs", "cu", "cuh", "dart", "go", "h", "hpp", "java", "jl", "js", "kt",
96+
"lisp", "lua", "pde", "m", "php", "py", "R", "rb", "rs", "rust", "sh", "scala", "swift", "ts",
97+
"vb"}
98+
self.comment = {"#", "//", "/*", "*", "*/", ":", ";", '""""'}
99+
self.ignore = {"out", "venv", ".git", ".idea", "build", "target", "node_modules", ".vscode", "dist"}
78100

79-
if self.__confirm('Default configuration will be restored (y/n)?'):
101+
if self.__confirm('The default configuration will be restored. (y/n) '):
80102
self.__update()

β€Žcode_counter/core/args.pyβ€Ž

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,19 @@ def __search(self):
6161
usage="cocnt search input_path [-h] [-v] [-g] "
6262
"[-o OUTPUT_PATH] [--suffix SUFFIX] [--comment COMMENT] [--ignore IGNORE]")
6363
parser.add_argument('input_path', type=split_args,
64-
help="counting the code lines according the given path(s)")
64+
help="counting the code lines according to the given path(s)")
6565
parser.add_argument('-v', '--verbose', dest="verbose", action='store_true',
66-
help="show verbose infomation")
66+
help="show verbose information")
6767
parser.add_argument('-g', '--graph', dest='graph', action='store_true',
6868
help="choose to whether to visualize the result")
6969
parser.add_argument('-o', '--output', dest='output_path',
70-
help="specify a output path if you want to store the result")
70+
help="specify an output path if you want to store the result")
7171
parser.add_argument('--suffix', dest='suffix', type=split_args,
72-
help="what code files do you want to count, this parameter is disposable")
72+
help="what code files do you want to count")
7373
parser.add_argument('--comment', dest='comment', type=split_args,
74-
help="the comment symbol, which can be judged whether the current line is a comment, "
75-
"this parameter is disposable")
74+
help="the comment symbol, which can be judged whether the current line is a comment")
7675
parser.add_argument('--ignore', dest='ignore', type=split_args,
77-
help="ignore some directories or files that you don't want to count, "
78-
"this parameter is disposable")
76+
help="ignore some directories or files that you don't want to count")
7977
return parser.parse_args(sys.argv[2:])
8078

8179
def __config(self):
@@ -87,24 +85,31 @@ def __config(self):
8785
"[--comment-add COMMENT_ADD] [--ignore-reset IGNORE_RESET] "
8886
"[--ignore-add IGNORE_ADD] [--restore] ")
8987
parser.add_argument('--list', dest='show_list', action='store_true',
90-
help="list all variables set in config file, along with their values")
88+
help="list all variables set in the config file, along with their values")
9189
parser.add_argument('--suffix-reset', dest='suffix_reset', type=split_args,
92-
help="override 'suffix' in config and count codes according to this value")
90+
help="reset the 'suffix' in the config and count code lines according to this value")
9391
parser.add_argument('--suffix-add', dest='suffix_add', type=split_args,
94-
help="append new value for 'suffix' in config and count codes according to this value")
92+
help="append new value for the 'suffix' in the config "
93+
"and count code lines according to this value")
94+
parser.add_argument('--suffix-del', dest='suffix_del', type=split_args,
95+
help="delete some values of the 'suffix' in the config")
9596

9697
parser.add_argument('--comment-reset', dest='comment_reset', type=split_args,
97-
help="override 'comment' in config and count comment lines according to this value")
98+
help="reset the 'comment' in the config and count comment lines according to this value")
9899
parser.add_argument('--comment-add', dest='comment_add', type=split_args,
99-
help="append new value for 'comment' in config "
100+
help="append new value for the 'comment' in the config "
100101
"and count comment lines according to this value")
102+
parser.add_argument('--comment-del', dest='comment_del', type=split_args,
103+
help="delete some values of the 'comment' in the config")
101104

102105
parser.add_argument('--ignore-reset', dest='ignore_reset', type=split_args,
103-
help="override 'ignore' in config "
104-
"and ignore some files or directory according to this value")
106+
help="reset the 'ignore' in the config "
107+
"and ignore some files or directories according to this value")
105108
parser.add_argument('--ignore-add', dest='ignore_add', type=split_args,
106-
help="append new value for 'ignore' in config "
107-
"and ignore some files or directory according to this value")
109+
help="append new value for the 'ignore' in the config "
110+
"and ignore some files or directories according to this value")
111+
parser.add_argument('--ignore-del', dest='ignore_del', type=split_args,
112+
help="delete some values of the 'ignore' in the config")
108113

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

β€Žsetup.pyβ€Ž

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
1-
21
import os
32
from setuptools import setup, find_packages
43

5-
short_desc = "A command-line interface (CLI) utility that can help you easily count code and display detailed results."
4+
short_desc = "A command-line interface (CLI) utility " \
5+
"that can help you easily count code lines and display detailed results."
6+
67

78
def read_readme(file_name):
89
with open(os.path.join(os.path.dirname(__file__), file_name), encoding='utf-8') as f:
910
return f.read()
1011

12+
1113
setup(name='code-counter',
1214
version=__import__('code_counter').__version__,
1315
author="Inno Fang",
1416
author_email="[email protected]",
1517
url='https://github.com/innofang/code-counter', # homepage
1618
project_urls={
17-
'Documentation': 'https://github.com/InnoFang/code-counter/blob/master/README.md',
18-
'Source': 'https://github.com/InnoFang/code-counter',
19-
'Bug Reports': 'https://github.com/InnoFang/code-counter/issues',
19+
'Documentation': 'https://github.com/InnoFang/code-counter/blob/master/README.md',
20+
'Source': 'https://github.com/InnoFang/code-counter',
21+
'Bug Reports': 'https://github.com/InnoFang/code-counter/issues',
2022
},
2123
description=short_desc,
2224
long_description=read_readme('README.md'),
2325
packages=find_packages(),
2426
include_package_data=True,
2527
long_description_content_type="text/markdown",
2628
license='Apache License',
27-
install_requires = ["matplotlib", "numpy"],
28-
python_requires='>=3.5',
29+
install_requires=["matplotlib", "numpy"],
30+
python_requires='>=3.6',
2931
classifiers=[
30-
'Development Status :: 4 - Beta',
31-
'Intended Audience :: Developers',
32-
'License :: OSI Approved :: Apache Software License',
33-
'Environment :: Console',
34-
'Topic :: Utilities',
35-
'Programming Language :: Python :: 3',
36-
'Programming Language :: Python :: 3.5',
37-
'Programming Language :: Python :: 3.6',
38-
'Programming Language :: Python :: 3.7',
39-
'Programming Language :: Python :: 3.8',
40-
'Programming Language :: Python :: 3.9',
41-
],
32+
'Development Status :: 4 - Beta',
33+
'Intended Audience :: Developers',
34+
'License :: OSI Approved :: Apache Software License',
35+
'Environment :: Console',
36+
'Topic :: Utilities',
37+
'Programming Language :: Python :: 3',
38+
'Programming Language :: Python :: 3.6',
39+
'Programming Language :: Python :: 3.7',
40+
'Programming Language :: Python :: 3.8',
41+
'Programming Language :: Python :: 3.9',
42+
'Programming Language :: Python :: 3.10',
43+
],
4244
entry_points={
4345
'console_scripts': [
4446
'cocnt = code_counter.__main__:main'
4547
]
4648
},
4749
keywords='code count line file counter',
48-
)
50+
)

β€Žtest/.gitkeepβ€Ž

Whitespace-only changes.

0 commit comments

Comments
Β (0)