Skip to content

Commit

Permalink
fix pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
zoumingzhe committed Apr 19, 2024
1 parent 4faf991 commit 2af87fa
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 229 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/pylint.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f test/requirements.txt ]; then pip install -r test/requirements.txt; fi
make build install
- name: Analysing the code with pylint
run: |
pylint $(git ls-files xarg/*.py test/*.py example/*.py)
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
18 changes: 18 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[MASTER]
disable=
C0103, # invalid-name
C0114, # missing-module-docstring
C0115, # missing-class-docstring
C0116, # missing-function-docstring
W0212, #
W0238, #
W0611, #
W0613, #
W0621, #
W0622, #
W0718, #
R0903, # too-few-public-methods
R0904, # too-many-public-methods
R0913, # too-many-arguments
R0914, # too-many-locals
R0915, # too-many-statements
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ uninstall:
pip3 uninstall -y xarg-python


test:
pip3 install --upgrade flake8 pytest
prepare-test:
pip3 install --upgrade pylint flake8 pytest

pylint:
pylint $$(git ls-files xarg/*.py test/*.py example/*.py)

flake8:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

pytest:
pytest

test: prepare-test pylint flake8 pytest
43 changes: 0 additions & 43 deletions example/example.py

This file was deleted.

26 changes: 10 additions & 16 deletions example/example_actuator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@


@add_command('key', help="item key")
def cmd_key(argp: argp):
# print("key", argp)
def cmd_key(_arg: argp):
pass


@add_command('value', help="item value")
def cmd_val(argp: argp):
# print("value", argp)
def cmd_val(_arg: argp):
pass


@add_command('get', help="get item")
def cmd_get(argp: argp):
# print("get", argp)
def cmd_get(_arg: argp):
pass


Expand All @@ -35,8 +32,7 @@ def run_get(args) -> int:


@add_command('set', help="set item")
def cmd_set(argp: argp):
# print("set", argp)
def cmd_set(_arg: argp):
pass


Expand All @@ -47,8 +43,7 @@ def run_set(args) -> int:


@add_command('del', help="delete item")
def cmd_del(argp: argp):
# print("del", argp)
def cmd_del(_arg: argp):
pass


Expand All @@ -59,12 +54,10 @@ def run_del(args) -> int:


@add_command('example')
def cmd(argp: argp):
# print("example", argp)
argp.add_opt_on('--debug')
argp.add_opt_on('-t', '--test')
argp.add_opt_off('-s', '--show')
pass
def cmd(_arg: argp):
_arg.add_opt_on('--debug')
_arg.add_opt_on('-t', '--test')
_arg.add_opt_off('-s', '--show')


@run_command(cmd, cmd_get, cmd_set, cmd_del)
Expand All @@ -75,6 +68,7 @@ def run(args) -> int:

def main(argv: Optional[List[str]] = None) -> int:
return commands().run(
argv=argv,
prog="xarg-example",
description="Simple command-line tool based on argparse.")

Expand Down
2 changes: 1 addition & 1 deletion test/TestLog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from xarg import cmds

if __name__ == "__main__":
cmds.initiate_logging(level="info")
cmds.initiate_logger(logger=cmds.logger)
end = time() + 100
while time() < end:
cmds.logger.info(int(time() / 3))
48 changes: 30 additions & 18 deletions xarg/actuator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class add_command:
>>> @add_command("example")\n
>>> def cmd(_arg: argp):\n
>>> argp.add_opt_on("-t", "--test")\n
>>> _arg.add_opt_on("-t", "--test")\n
'''

def __init__(self, name: str, **kwargs):
Expand Down Expand Up @@ -60,13 +60,16 @@ def __init__(self, name: str, **kwargs):
self.__options: Dict[str, Any] = kwargs
self.__bind: Optional[run_command] = None
self.__subs: Optional[Tuple[add_command, ...]] = None
self.__func: Optional[Callable[[argp], None]] = None

def __call__(self, cmd_func: Callable[[argp], None]):
self.__func: Callable[[argp], None] = cmd_func
self.__func = cmd_func
return self

@property
def func(self) -> Callable[[argp], None]:
if self.__func is None:
raise ValueError("No function")
return self.__func

@property
Expand Down Expand Up @@ -166,13 +169,16 @@ def __init__(self, cmd_bind: add_command, *sub_cmds: add_command,
self.__bind: add_command = cmd_bind
self.__prep: Optional["pre_command"] = None
self.__done: Optional["end_command"] = None
self.__func: Optional[Callable[["commands"], int]] = None

def __call__(self, run_func: Callable[["commands"], int]):
self.__func: Callable[["commands"], int] = run_func
self.__func = run_func
return self

@property
def func(self) -> Callable[["commands"], int]:
if self.__func is None:
raise ValueError("No function")
return self.__func

@property
Expand Down Expand Up @@ -228,13 +234,16 @@ def __init__(self, run_bind: run_command):
assert isinstance(run_bind, run_command)
run_bind.prep = self
self.__main: run_command = run_bind
self.__func: Optional[Callable[["commands"], int]] = None

def __call__(self, run_func: Callable[["commands"], int]):
self.__func: Callable[["commands"], int] = run_func
self.__func = run_func
return self

@property
def func(self) -> Callable[["commands"], int]:
if self.__func is None:
raise ValueError("No function")
return self.__func

@property
Expand Down Expand Up @@ -268,13 +277,16 @@ def __init__(self, run_bind: run_command):
assert isinstance(run_bind, run_command)
run_bind.done = self
self.__main: run_command = run_bind
self.__func: Optional[Callable[["commands"], int]] = None

def __call__(self, run_func: Callable[["commands"], int]):
self.__func: Callable[["commands"], int] = run_func
self.__func = run_func
return self

@property
def func(self) -> Callable[["commands"], int]:
if self.__func is None:
raise ValueError("No function")
return self.__func

@property
Expand Down Expand Up @@ -302,7 +314,7 @@ class commands(log):
>>> @add_command("example")\n
>>> def cmd(_arg: argp):\n
>>> argp.add_opt_on("-t", "--test")\n
>>> _arg.add_opt_on("-t", "--test")\n
>>> @run_command(cmd, cmd_get, cmd_set)\n
>>> def run(cmds: commands) -> int:\n
Expand Down Expand Up @@ -400,28 +412,28 @@ def stderr(self, context: Any):
sys.stderr.write(f"{context}\n")
sys.stderr.flush()

def __add_optional_version(self, argp: argp):
def __add_optional_version(self, _arg: argp):
version = self.version
if not isinstance(version, str):
return

options = argp.filter_optional_name("-v", "--version")
options = _arg.filter_optional_name("-v", "--version")
if len(options) > 0:
argp.add_argument(*options, action="version",
_arg.add_argument(*options, action="version",
version=f"%(prog)s {version.strip()}")

def __add_inner_parser_tail(self, argp: argp):
def __add_inner_parser_tail(self, _arg: argp):

def filter_optional_name(*name: str) -> Optional[str]:
options = argp.filter_optional_name(*name)
options = _arg.filter_optional_name(*name)
if len(options) > 0:
for i in name:
if i in options:
return i
return None

def add_optional_level():
group = argp.argument_group(self.LOGGER_ARGUMENT_GROUP)
group = _arg.argument_group(self.LOGGER_ARGUMENT_GROUP)
group_level = group.add_mutually_exclusive_group()

option_level = filter_optional_name("--level", "--log-level")
Expand Down Expand Up @@ -458,7 +470,7 @@ def add_optional_stream():
if not isinstance(option, str):
return

group = argp.argument_group(self.LOGGER_ARGUMENT_GROUP)
group = _arg.argument_group(self.LOGGER_ARGUMENT_GROUP)
group.add_argument(option,
type=str,
nargs=1,
Expand All @@ -478,7 +490,7 @@ def add_optional_format():
" %(funcName)s %(filename)s:%(lineno)s"\
" %(message)s"

group = argp.argument_group(self.LOGGER_ARGUMENT_GROUP)
group = _arg.argument_group(self.LOGGER_ARGUMENT_GROUP)
group.add_argument(option,
type=str,
nargs="?",
Expand All @@ -489,7 +501,7 @@ def add_optional_format():
help="Logger output format.")

def add_optional_console():
group = argp.argument_group(self.LOGGER_ARGUMENT_GROUP)
group = _arg.argument_group(self.LOGGER_ARGUMENT_GROUP)
group_std = group.add_mutually_exclusive_group()

option = filter_optional_name("--stdout", "--log-stdout")
Expand Down Expand Up @@ -568,7 +580,7 @@ def parse(self, root: Optional[add_command] = None,
root = self.root
assert isinstance(root, add_command)

_map: Dict[add_command, argp] = dict()
_map: Dict[add_command, argp] = {}
_arg = argp(argv=argv, **kwargs)
self.__prog = _arg.prog
self.__add_optional_version(_arg)
Expand Down Expand Up @@ -668,14 +680,14 @@ def run(self,

kwargs.pop("prog", None) # Please do not specify prog
args = self.parse(root, argv, **kwargs)
self.logger.debug(f"{args}")
self.logger.debug("%s", args)

try:
version = self.version
if isinstance(version, str):
# Output version for the debug level. Internal log
# items are debug level only, except for errors.
self.logger.debug(f"version: {version}")
self.logger.debug("version: %s", version)

ret = self.__pre(args, root)
if ret != 0 and ret is not None:
Expand Down
Loading

0 comments on commit 2af87fa

Please sign in to comment.