Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code-counter-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Test with pytest
run: python -m unittest discover -s test -p "test.py"
run: python -m unittest discover -s tests -p "test_*.py"
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY:
test:
python -m unittest discover -s tests -p "test_*.py"

setup:
python setup.py install


.PHONY:
check:
python setup.py check

Expand All @@ -19,4 +21,4 @@ uninstall:
echo y | pip uninstall code-counter

clean:
rm -rf build code_counter.* dist
rm -rf build code_counter.* dist
2 changes: 1 addition & 1 deletion code_counter/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.0-alpha"
__version__ = "1.0.0"
64 changes: 32 additions & 32 deletions code_counter/conf/config.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
{
"suffix": [
"c",
"java",
"swift",
"dart",
"rb",
"cc",
"clj",
"cpp",
"cs",
"go",
"php",
"sh",
"lua",
"m",
"cu",
"c",
"vb",
"clj",
"js",
"cuh",
"dart",
"go",
"h",
"hpp",
"java",
"jl",
"js",
"R",
"cs",
"kt",
"lisp",
"lua",
"pde",
"m",
"php",
"ts",
"scala",
"rust",
"py",
"R",
"rb",
"cpp",
"rs",
"rust",
"sh",
"scala",
"swift",
"ts",
"vb"
"h",
"jl",
"pde",
"lisp"
],
"comment": [
"#",
"//",
"/*",
":",
"*",
"*/",
":",
";",
"\"\"\"\""
"#",
"\"\"\"\"",
"/*"
],
"ignore": [
"out",
"venv",
".git",
".idea",
"build",
"dist",
"target",
"build",
".idea",
"node_modules",
".vscode",
"dist"
".git",
"out"
]
}
80 changes: 51 additions & 29 deletions code_counter/conf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,78 @@
import pkg_resources


class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)


class Config:

def __init__(self):
conf = self.__load()

self.suffix = conf['suffix']
self.comment = conf['comment']
self.ignore = conf['ignore']
self.suffix = set(conf['suffix'])
self.comment = set(conf['comment'])
self.ignore = set(conf['ignore'])

def invoke(self, args):
if args.restore:
self.restore()
else:
if any([args.suffix_add, args.comment_add, args.ignore_add]):
self.__append_config(args.suffix_add, args.comment_add, args.ignore_add)
if any([args.suffix_reset, args.comment_reset, args.ignore_reset]):
self.__reset_config(args.suffix_reset, args.comment_reset, args.ignore_reset)
if any([args.suffix_add, args.comment_add, args.ignore_add]):
self.__append_config(args.suffix_add, args.comment_add, args.ignore_add)
if any([args.suffix_del, args.comment_del, args.ignore_del]):
self.__remove_config(args.suffix_del, args.comment_del, args.ignore_del)
if args.show_list:
self.show()

def show(self):
print(json.dumps(self.__dict__, indent=4))
print(json.dumps(self.__dict__, indent=4, cls=SetEncoder))

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

def __reset_config(self, suffix_reset, comment_reset, ignore_reset):
if suffix_reset:
if self.__confirm("'suffix' will be replaced with {} . (y/n) ".format(suffix_reset)):
self.suffix = set(suffix_reset)
if comment_reset:
if self.__confirm("'comment' will be replaced with {} . (y/n) ".format(comment_reset)):
self.comment = set(comment_reset)
if ignore_reset:
if self.__confirm("'ignore' will be replaced with {} . (y/n) ".format(ignore_reset)):
self.ignore = set(ignore_reset)

self.__update()

def __append_config(self, suffix_add, comment_add, ignore_add):
if suffix_add:
if self.__confirm("'suffix' will be appended with {} (y/n)".format(suffix_add)):
self.suffix.extend(suffix_add)
if self.__confirm("'suffix' will be appended with {} . (y/n) ".format(suffix_add)):
self.suffix.update(suffix_add)
if comment_add:
if self.__confirm("'comment' will be appended with {} (y/n)".format(comment_add)):
self.comment.extend(comment_add)
if self.__confirm("'comment' will be appended with {} . (y/n) ".format(comment_add)):
self.comment.update(comment_add)
if ignore_add:
if self.__confirm("'ignore' will be appended with {} (y/n)".format(ignore_add)):
self.ignore.extend(ignore_add)
if self.__confirm("'ignore' will be appended with {} . (y/n) ".format(ignore_add)):
self.ignore.update(ignore_add)

self.__update()

def __reset_config(self, suffix_reset, comment_reset, ignore_reset):
if suffix_reset:
if self.__confirm("'suffix' will be replaced with {} (y/n)".format(suffix_reset)):
self.suffix = suffix_reset
if comment_reset:
if self.__confirm("'comment' will be replaced with {} (y/n)".format(comment_reset)):
self.comment = comment_reset
if ignore_reset:
if self.__confirm("'ignore' will be replaced with {} (y/n)".format(ignore_reset)):
self.ignore = ignore_reset
def __remove_config(self, suffix_del, comment_del, ignore_del):
if suffix_del:
if self.__confirm("'suffix' will remove {} . (y/n) ".format(suffix_del)):
self.suffix.difference_update(suffix_del)
if comment_del:
if self.__confirm("'comment' will remove {} . (y/n) ".format(comment_del)):
self.comment.difference_update(comment_del)
if ignore_del:
if self.__confirm("'ignore' will remove {} . (y/n) ".format(ignore_del)):
self.ignore.difference_update(ignore_del)

self.__update()

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

def restore(self):
self.suffix = ["c", "cc", "clj", "cpp", "cs", "cu", "cuh", "dart", "go", "h",
"hpp", "java", "jl", "js", "kt", "lisp", "lua", "pde", "m", "php",
"py", "R", "rb", "rs", "rust", "sh", "scala", "swift", "ts", "vb"]
self.comment = ["#", "//", "/*", "*", "*/", ":", ";", '""""']
self.ignore = ["out", "venv", ".git", ".idea", "build", "target", "node_modules", ".vscode", "dist"]
self.suffix = {"c", "cc", "clj", "cpp", "cs", "cu", "cuh", "dart", "go", "h", "hpp", "java", "jl", "js", "kt",
"lisp", "lua", "pde", "m", "php", "py", "R", "rb", "rs", "rust", "sh", "scala", "swift", "ts",
"vb"}
self.comment = {"#", "//", "/*", "*", "*/", ":", ";", '""""'}
self.ignore = {"out", "venv", ".git", ".idea", "build", "target", "node_modules", ".vscode", "dist"}

if self.__confirm('Default configuration will be restored (y/n)?'):
if self.__confirm('The default configuration will be restored. (y/n) '):
self.__update()
39 changes: 22 additions & 17 deletions code_counter/core/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,19 @@ def __search(self):
usage="cocnt search input_path [-h] [-v] [-g] "
"[-o OUTPUT_PATH] [--suffix SUFFIX] [--comment COMMENT] [--ignore IGNORE]")
parser.add_argument('input_path', type=split_args,
help="counting the code lines according the given path(s)")
help="counting the code lines according to the given path(s)")
parser.add_argument('-v', '--verbose', dest="verbose", action='store_true',
help="show verbose infomation")
help="show verbose information")
parser.add_argument('-g', '--graph', dest='graph', action='store_true',
help="choose to whether to visualize the result")
parser.add_argument('-o', '--output', dest='output_path',
help="specify a output path if you want to store the result")
help="specify an output path if you want to store the result")
parser.add_argument('--suffix', dest='suffix', type=split_args,
help="what code files do you want to count, this parameter is disposable")
help="what code files do you want to count")
parser.add_argument('--comment', dest='comment', type=split_args,
help="the comment symbol, which can be judged whether the current line is a comment, "
"this parameter is disposable")
help="the comment symbol, which can be judged whether the current line is a comment")
parser.add_argument('--ignore', dest='ignore', type=split_args,
help="ignore some directories or files that you don't want to count, "
"this parameter is disposable")
help="ignore some directories or files that you don't want to count")
return parser.parse_args(sys.argv[2:])

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

parser.add_argument('--comment-reset', dest='comment_reset', type=split_args,
help="override 'comment' in config and count comment lines according to this value")
help="reset the 'comment' in the config and count comment lines according to this value")
parser.add_argument('--comment-add', dest='comment_add', type=split_args,
help="append new value for 'comment' in config "
help="append new value for the 'comment' in the config "
"and count comment lines according to this value")
parser.add_argument('--comment-del', dest='comment_del', type=split_args,
help="delete some values of the 'comment' in the config")

parser.add_argument('--ignore-reset', dest='ignore_reset', type=split_args,
help="override 'ignore' in config "
"and ignore some files or directory according to this value")
help="reset the 'ignore' in the config "
"and ignore some files or directories according to this value")
parser.add_argument('--ignore-add', dest='ignore_add', type=split_args,
help="append new value for 'ignore' in config "
"and ignore some files or directory according to this value")
help="append new value for the 'ignore' in the config "
"and ignore some files or directories according to this value")
parser.add_argument('--ignore-del', dest='ignore_del', type=split_args,
help="delete some values of the 'ignore' in the config")

parser.add_argument('--restore', dest='restore', action='store_true',
help="restore default config")
Expand Down
42 changes: 22 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@

import os
from setuptools import setup, find_packages

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


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


setup(name='code-counter',
version=__import__('code_counter').__version__,
author="Inno Fang",
author_email="innofang@yeah.net",
url='https://github.com/innofang/code-counter', # homepage
project_urls={
'Documentation': 'https://github.com/InnoFang/code-counter/blob/master/README.md',
'Source': 'https://github.com/InnoFang/code-counter',
'Bug Reports': 'https://github.com/InnoFang/code-counter/issues',
'Documentation': 'https://github.com/InnoFang/code-counter/blob/master/README.md',
'Source': 'https://github.com/InnoFang/code-counter',
'Bug Reports': 'https://github.com/InnoFang/code-counter/issues',
},
description=short_desc,
long_description=read_readme('README.md'),
packages=find_packages(),
include_package_data=True,
long_description_content_type="text/markdown",
license='Apache License',
install_requires = ["matplotlib", "numpy"],
python_requires='>=3.5',
install_requires=["matplotlib", "numpy"],
python_requires='>=3.6',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Environment :: Console',
'Topic :: Utilities',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Environment :: Console',
'Topic :: Utilities',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
],
entry_points={
'console_scripts': [
'cocnt = code_counter.__main__:main'
]
},
keywords='code count line file counter',
)
)
Empty file removed test/.gitkeep
Empty file.
Loading