Skip to content

Commit 5b17eeb

Browse files
committed
🐛 fix the problem of adding duplicate data
1 parent 74ea270 commit 5b17eeb

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

code_counter/conf/config.json

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
{
22
"suffix": [
3-
"c",
4-
"cc",
5-
"clj",
6-
"cpp",
7-
"cs",
8-
"cu",
9-
"cuh",
10-
"dart",
11-
"go",
12-
"h",
13-
"hpp",
14-
"java",
15-
"jl",
16-
"js",
17-
"kt",
183
"lisp",
19-
"lua",
20-
"pde",
21-
"m",
22-
"php",
234
"py",
24-
"R",
5+
"clj",
6+
"vb",
7+
"hpp",
8+
"ts",
9+
"cc",
2510
"rb",
11+
"dart",
12+
"pde",
13+
"cu",
14+
"sh",
15+
"swift",
16+
"cs",
17+
"js",
2618
"rs",
19+
"php",
20+
"c",
2721
"rust",
28-
"sh",
22+
"lua",
23+
"jl",
24+
"java",
25+
"cuh",
26+
"R",
27+
"go",
2928
"scala",
30-
"swift",
31-
"ts",
32-
"vb"
29+
"m",
30+
"cpp",
31+
"h",
32+
"kt"
3333
],
3434
"comment": [
35-
"#",
36-
"//",
37-
"/*",
3835
"*",
39-
"*/",
4036
":",
37+
"*/",
38+
"\"\"\"\"",
4139
";",
42-
"\"\"\"\""
40+
"/*",
41+
"//",
42+
"#"
4343
],
4444
"ignore": [
45-
"out",
46-
"venv",
4745
".git",
48-
".idea",
49-
"build",
5046
"target",
51-
"node_modules",
47+
"dist",
48+
"build",
5249
".vscode",
53-
"dist"
50+
"node_modules",
51+
"out",
52+
".idea",
53+
"venv"
5454
]
5555
}

code_counter/conf/config.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
import json
55
import pkg_resources
66

7+
class SetEncoder(json.JSONEncoder):
8+
def default(self, obj):
9+
if isinstance(obj, set):
10+
return list(obj)
11+
return json.JSONEncoder.default(self, obj)
712

813
class Config:
914

1015
def __init__(self):
1116
conf = self.__load()
1217

13-
self.suffix = conf['suffix']
14-
self.comment = conf['comment']
15-
self.ignore = conf['ignore']
18+
self.suffix = set(conf['suffix'])
19+
self.comment = set(conf['comment'])
20+
self.ignore = set(conf['ignore'])
1621

1722
def invoke(self, args):
1823
if args.restore:
@@ -26,7 +31,7 @@ def invoke(self, args):
2631
self.show()
2732

2833
def show(self):
29-
print(json.dumps(self.__dict__, indent=4))
34+
print(json.dumps(self.__dict__, indent=4, cls=SetEncoder))
3035

3136
def __confirm(self, tips):
3237
check = input(tips)
@@ -35,26 +40,26 @@ def __confirm(self, tips):
3540
def __append_config(self, suffix_add, comment_add, ignore_add):
3641
if suffix_add:
3742
if self.__confirm("'suffix' will be appended with {} (y/n)".format(suffix_add)):
38-
self.suffix.extend(suffix_add)
43+
self.suffix.update(suffix_add)
3944
if comment_add:
4045
if self.__confirm("'comment' will be appended with {} (y/n)".format(comment_add)):
41-
self.comment.extend(comment_add)
46+
self.comment.update(comment_add)
4247
if ignore_add:
4348
if self.__confirm("'ignore' will be appended with {} (y/n)".format(ignore_add)):
44-
self.ignore.extend(ignore_add)
49+
self.ignore.update(ignore_add)
4550

4651
self.__update()
4752

4853
def __reset_config(self, suffix_reset, comment_reset, ignore_reset):
4954
if suffix_reset:
5055
if self.__confirm("'suffix' will be replaced with {} (y/n)".format(suffix_reset)):
51-
self.suffix = suffix_reset
56+
self.suffix = set(suffix_reset)
5257
if comment_reset:
5358
if self.__confirm("'comment' will be replaced with {} (y/n)".format(comment_reset)):
54-
self.comment = comment_reset
59+
self.comment = set(comment_reset)
5560
if ignore_reset:
5661
if self.__confirm("'ignore' will be replaced with {} (y/n)".format(ignore_reset)):
57-
self.ignore = ignore_reset
62+
self.ignore = set(ignore_reset)
5863

5964
self.__update()
6065

@@ -67,14 +72,14 @@ def __load(self):
6772
def __update(self):
6873
filename = pkg_resources.resource_filename(__name__, 'config.json')
6974
with open(filename, 'w') as config:
70-
json.dump(self.__dict__, config, indent=4)
75+
json.dump(self.__dict__, config, indent=4, cls=SetEncoder)
7176

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

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

0 commit comments

Comments
 (0)