From cf829da087aeb0af145754fa1d05c534cc40c421 Mon Sep 17 00:00:00 2001 From: Mingzhe Zou Date: Fri, 19 Apr 2024 11:13:29 +0800 Subject: [PATCH] fix pylint --- .github/workflows/pylint.yml | 2 +- .pylintrc | 11 ++ Makefile | 15 ++- example/example.py | 43 -------- example/example_actuator.py | 26 ++--- xarg/actuator.py | 44 +++++--- xarg/colorful.py | 190 +++++++++++++++++------------------ xarg/complete.py | 2 +- xarg/parser.py | 27 +++-- xarg/scanner.py | 19 ++-- xarg/sheet.py | 12 +-- 11 files changed, 194 insertions(+), 197 deletions(-) create mode 100644 .pylintrc delete mode 100644 example/example.py diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 4c9e49a..8b3c3a9 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..3d10be1 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,11 @@ +[MASTER] +disable= + C0103, # invalid-name + C0114, # missing-module-docstring + C0115, # missing-class-docstring + C0116, # missing-function-docstring + R0903, # too-few-public-methods + R0904, # too-many-public-methods + R0913, # too-many-arguments + R0914, # too-many-locals + R0915, # too-many-statements diff --git a/Makefile b/Makefile index 45c4c0f..6dbc844 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/example/example.py b/example/example.py deleted file mode 100644 index 711be58..0000000 --- a/example/example.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/python3 -# coding:utf-8 - -from xarg import __version__ as version -from xarg import xarg - - -def main(): - _arg = xarg("xarg", - description="hello the world", - epilog="This is the xarg project test command-line.") - _arg.add_argument('-v', - '--version', - action='version', - version=f'%(prog)s {version}') - _arg.add_subparsers(dest="sub", required=True) - _sub = _arg.add_parser("opt") - _sub.add_opt("-x") - _sub.add_opt("-arg") - _sub.add_opt("-o", "--opt") - _sub = _arg.add_parser("opt-on") - _sub.add_opt_on("--opt-on") - _sub = _arg.add_parser("opt-off") - _sub.add_opt_off("--opt-off") - _sub = _arg.add_parser("pos-default") - _sub.add_pos("pos") - _sub = _arg.add_parser("pos-1") - _sub.add_pos("pos_1", nargs=1) - _sub = _arg.add_parser("pos-2") - _sub.add_pos("pos_2", nargs=2) - _sub = _arg.add_parser("pos-0+") - _sub.add_pos("pos_0+", nargs=0) - _sub = _arg.add_parser("pos-1+") - _sub.add_pos("pos_1+", nargs=-1) - _sub = _arg.add_parser("pos-0-or-1") - _sub.add_pos("pos_0_or_1") - _sub = _arg.add_parser("sub_opt") - _sub.add_subparsers() - _sub2 = _sub.add_parser("opt-on") - _sub2.add_opt_on("--opt-on") - _sub2 = _sub.add_parser("opt-off") - _sub2.add_opt_off("--opt-off") - print(_arg.parse_args()) diff --git a/example/example_actuator.py b/example/example_actuator.py index 3959759..e8926f6 100644 --- a/example/example_actuator.py +++ b/example/example_actuator.py @@ -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 @@ -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 @@ -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 @@ -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) @@ -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.") diff --git a/xarg/actuator.py b/xarg/actuator.py index 95956bb..7db3b33 100644 --- a/xarg/actuator.py +++ b/xarg/actuator.py @@ -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): @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -400,20 +412,20 @@ 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: @@ -421,7 +433,7 @@ def filter_optional_name(*name: str) -> Optional[str]: 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") @@ -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, @@ -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="?", @@ -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") @@ -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) diff --git a/xarg/colorful.py b/xarg/colorful.py index f69b6da..a9f4561 100644 --- a/xarg/colorful.py +++ b/xarg/colorful.py @@ -48,7 +48,7 @@ class color(str): Reference: https://en.wikipedia.org/wiki/ANSI_escape_code ''' - def __init__(self, object: object): + def __init__(self, obj: object): self.__background: Optional[str] = None self.__foreground: Optional[str] = None self.__style: Set[str] = set() @@ -105,20 +105,20 @@ def add_style(self, value: StyleType) -> "color": return self @classmethod - def new_background(cls, object: object, value: str) -> "color": - colour: color = color(object) + def new_background(cls, obj: object, value: str) -> "color": + colour: color = color(obj) colour.background = value return colour @classmethod - def new_foreground(cls, object: object, value: str) -> "color": - colour: color = color(object) + def new_foreground(cls, obj: object, value: str) -> "color": + colour: color = color(obj) colour.foreground = value return colour @classmethod - def new_style(cls, object: object, value: Set[StyleType]) -> "color": - colour: color = color(object) + def new_style(cls, obj: object, value: Set[StyleType]) -> "color": + colour: color = color(obj) colour.style = value return colour @@ -127,177 +127,177 @@ def reset(cls) -> str: return Style.RESET_ALL @classmethod - def bold(cls, object: object) -> "color": - return color.new_style(object, {Style.BRIGHT}) + def bold(cls, obj: object) -> "color": + return color.new_style(obj, {Style.BRIGHT}) @classmethod - def dim(cls, object: object) -> "color": - return color.new_style(object, {Style.DIM}) + def dim(cls, obj: object) -> "color": + return color.new_style(obj, {Style.DIM}) @classmethod - def italic(cls, object: object) -> "color": - return color.new_style(object, {Style.ITALIC}) + def italic(cls, obj: object) -> "color": + return color.new_style(obj, {Style.ITALIC}) @classmethod - def underline(cls, object: object) -> "color": - return color.new_style(object, {Style.UNDERLINE}) + def underline(cls, obj: object) -> "color": + return color.new_style(obj, {Style.UNDERLINE}) @classmethod - def slow_blink(cls, object: object) -> "color": - return color.new_style(object, {Style.SLOWBLINK}) + def slow_blink(cls, obj: object) -> "color": + return color.new_style(obj, {Style.SLOWBLINK}) @classmethod - def rapid_blink(cls, object: object) -> "color": - return color.new_style(object, {Style.RAPIDBLINK}) + def rapid_blink(cls, obj: object) -> "color": + return color.new_style(obj, {Style.RAPIDBLINK}) @classmethod - def invert(cls, object: object) -> "color": - return color.new_style(object, {Style.INVERT}) + def invert(cls, obj: object) -> "color": + return color.new_style(obj, {Style.INVERT}) @classmethod - def hide(cls, object: object) -> "color": - return color.new_style(object, {Style.HIDE}) + def hide(cls, obj: object) -> "color": + return color.new_style(obj, {Style.HIDE}) @classmethod - def strikethrough(cls, object: object) -> "color": - return color.new_style(object, {Style.STRIKETHROUGH}) + def strikethrough(cls, obj: object) -> "color": + return color.new_style(obj, {Style.STRIKETHROUGH}) @classmethod - def doubly_underlined(cls, object: object) -> "color": - return color.new_style(object, {Style.DOUBLYUNDERLINED}) + def doubly_underlined(cls, obj: object) -> "color": + return color.new_style(obj, {Style.DOUBLYUNDERLINED}) @classmethod - def normal(cls, object: object) -> "color": - return color.new_style(object, {Style.NORMAL}) + def normal(cls, obj: object) -> "color": + return color.new_style(obj, {Style.NORMAL}) @classmethod - def reveal(cls, object: object) -> "color": - return color.new_style(object, {Style.REVEAL}) + def reveal(cls, obj: object) -> "color": + return color.new_style(obj, {Style.REVEAL}) @classmethod - def black(cls, object: object) -> "color": - return color.new_foreground(object, Fore.BLACK) + def black(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.BLACK) @classmethod - def red(cls, object: object) -> "color": - return color.new_foreground(object, Fore.RED) + def red(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.RED) @classmethod - def green(cls, object: object) -> "color": - return color.new_foreground(object, Fore.GREEN) + def green(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.GREEN) @classmethod - def yellow(cls, object: object) -> "color": - return color.new_foreground(object, Fore.YELLOW) + def yellow(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.YELLOW) @classmethod - def blue(cls, object: object) -> "color": - return color.new_foreground(object, Fore.BLUE) + def blue(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.BLUE) @classmethod - def magenta(cls, object: object) -> "color": - return color.new_foreground(object, Fore.MAGENTA) + def magenta(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.MAGENTA) @classmethod - def cyan(cls, object: object) -> "color": - return color.new_foreground(object, Fore.CYAN) + def cyan(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.CYAN) @classmethod - def white(cls, object: object) -> "color": - return color.new_foreground(object, Fore.WHITE) + def white(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.WHITE) @classmethod - def lightblack(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTBLACK_EX) + def lightblack(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTBLACK_EX) @classmethod - def lightred(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTRED_EX) + def lightred(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTRED_EX) @classmethod - def lightgreen(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTGREEN_EX) + def lightgreen(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTGREEN_EX) @classmethod - def lightyellow(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTYELLOW_EX) + def lightyellow(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTYELLOW_EX) @classmethod - def lightblue(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTBLUE_EX) + def lightblue(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTBLUE_EX) @classmethod - def lightmagenta(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTMAGENTA_EX) + def lightmagenta(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTMAGENTA_EX) @classmethod - def lightcyan(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTCYAN_EX) + def lightcyan(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTCYAN_EX) @classmethod - def lightwhite(cls, object: object) -> "color": - return color.new_foreground(object, Fore.LIGHTWHITE_EX) + def lightwhite(cls, obj: object) -> "color": + return color.new_foreground(obj, Fore.LIGHTWHITE_EX) @classmethod - def black_back(cls, object: object) -> "color": - return color.new_background(object, Back.BLACK) + def black_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.BLACK) @classmethod - def red_back(cls, object: object) -> "color": - return color.new_background(object, Back.RED) + def red_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.RED) @classmethod - def green_back(cls, object: object) -> "color": - return color.new_background(object, Back.GREEN) + def green_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.GREEN) @classmethod - def yellow_back(cls, object: object) -> "color": - return color.new_background(object, Back.YELLOW) + def yellow_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.YELLOW) @classmethod - def blue_back(cls, object: object) -> "color": - return color.new_background(object, Back.BLUE) + def blue_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.BLUE) @classmethod - def magenta_back(cls, object: object) -> "color": - return color.new_background(object, Back.MAGENTA) + def magenta_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.MAGENTA) @classmethod - def cyan_back(cls, object: object) -> "color": - return color.new_background(object, Back.CYAN) + def cyan_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.CYAN) @classmethod - def white_back(cls, object: object) -> "color": - return color.new_background(object, Back.WHITE) + def white_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.WHITE) @classmethod - def lightblack_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTBLACK_EX) + def lightblack_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTBLACK_EX) @classmethod - def lightred_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTRED_EX) + def lightred_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTRED_EX) @classmethod - def lightgreen_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTGREEN_EX) + def lightgreen_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTGREEN_EX) @classmethod - def lightyellow_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTYELLOW_EX) + def lightyellow_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTYELLOW_EX) @classmethod - def lightblue_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTBLUE_EX) + def lightblue_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTBLUE_EX) @classmethod - def lightmagenta_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTMAGENTA_EX) + def lightmagenta_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTMAGENTA_EX) @classmethod - def lightcyan_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTCYAN_EX) + def lightcyan_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTCYAN_EX) @classmethod - def lightwhite_back(cls, object: object) -> "color": - return color.new_background(object, Back.LIGHTWHITE_EX) + def lightwhite_back(cls, obj: object) -> "color": + return color.new_background(obj, Back.LIGHTWHITE_EX) diff --git a/xarg/complete.py b/xarg/complete.py index a93c5cc..9a0af30 100644 --- a/xarg/complete.py +++ b/xarg/complete.py @@ -91,7 +91,7 @@ class collections: def __init__(self): self.__cmds: Set[str] = set() - for _pkg in {"argcomplete", __project__}: + for _pkg in tuple({"argcomplete", __project__}): for _req in {i for i in self.get_package_info(_pkg).required_by}: config = ConfigParser() package_info = self.get_package_info(_req) diff --git a/xarg/parser.py b/xarg/parser.py index d91d5d4..f0f297f 100644 --- a/xarg/parser.py +++ b/xarg/parser.py @@ -91,21 +91,30 @@ def __init__(self, kwargs.setdefault("epilog", epilog) ArgumentParser.__init__(self, **kwargs) self.__argv: Optional[Sequence[str]] = argv - self.__help_option: Dict[str, _HelpAction] = dict() + self.__help_option: Dict[str, _HelpAction] = {} self.__prev_parser: argp = prev_parser or self - self.__next_parser: List[argp] = list() + self.__next_parser: List[argp] = [] if prev_parser is not None: - prev_parser.__next_parser.append(self) + prev_parser.next_parser.append(self) @property def argv(self) -> Optional[Sequence[str]]: - return self.root_parser.__argv + root = self.root_parser + return root.argv if root is not self else self.__argv + + @property + def prev_parser(self) -> "argp": + return self.__prev_parser + + @property + def next_parser(self) -> List["argp"]: + return self.__next_parser @property def root_parser(self) -> "argp": - root = self.__prev_parser - while root.__prev_parser != root: - root = root.__prev_parser + root = self.prev_parser + while root.prev_parser != root: + root = root.prev_parser return root def argument_group(self, @@ -216,12 +225,12 @@ def preparse_from_sys_argv(self) -> Namespace: def __dfs_enable_help_action(root: argp): root.__enable_help_action() - for _sub in root.__next_parser: + for _sub in root.next_parser: __dfs_enable_help_action(_sub) def __dfs_disable_help_action(root: argp): root.__disable_help_action() - for _sub in root.__next_parser: + for _sub in root.next_parser: __dfs_disable_help_action(_sub) try: diff --git a/xarg/scanner.py b/xarg/scanner.py index d9fa0a6..e4a154c 100644 --- a/xarg/scanner.py +++ b/xarg/scanner.py @@ -145,7 +145,7 @@ def sha256(self) -> str: return code def __init__(self): - self.__objdict: Dict[str, scanner.object] = dict() + self.__objdict: Dict[str, scanner.object] = {} self.__objects: Set[scanner.object] = set() self.__objsyms: Set[scanner.object] = set() self.__objregs: Set[scanner.object] = set() @@ -184,10 +184,13 @@ def add(self, obj: object): @classmethod def load(cls, paths: Sequence[str], - exclude: Sequence[str] = [], + exclude: Optional[Sequence[str]] = None, linkdir: bool = True, threads: int = THDNUM_DEFAULT, handler: Optional[Callable[[object], bool]] = None): + if exclude is None: + exclude = [] + assert isinstance(paths, Sequence) assert isinstance(exclude, Sequence) assert isinstance(linkdir, bool) @@ -224,7 +227,7 @@ def __init__(self): def task_scan_path(): scanned_dirs = set() name = current_thread().name - cmds.logger.debug(f"task thread[{name}] start") + cmds.logger.debug("task thread[%s] start", name) while not scan_stat.exit or not scan_stat.q_path.empty(): try: path = scan_stat.q_path.get(timeout=0.01) @@ -235,7 +238,7 @@ def task_scan_path(): assert isinstance(path, str) if path in scan_stat.filter or not os.path.exists(path): - cmds.logger.debug(f"scan filter {path}") + cmds.logger.debug("scan filter %s", path) scan_stat.q_path.task_done() continue @@ -257,21 +260,21 @@ def task_scan_path(): if result is True: scan_stat.q_task.put(object) scan_stat.q_path.task_done() - cmds.logger.debug(f"task thread[{name}] exit") + cmds.logger.debug("task thread[%s] exit", name) def task_scan(): name = current_thread().name - cmds.logger.debug(f"task thread[{name}] start") + cmds.logger.debug("task thread[%s] start", name) while not scan_stat.exit or not scan_stat.q_task.empty(): try: obj = scan_stat.q_task.get(timeout=0.01) except Empty: continue - cmds.logger.debug(f"scan {obj.path}") + cmds.logger.debug("scan %s", obj.path) scan_stat.scanner.add(obj=obj) scan_stat.q_task.task_done() - cmds.logger.debug(f"task thread[{name}] exit") + cmds.logger.debug("task thread[%s] exit", name) task_threads: List[Thread] = [] task_threads.append(Thread(target=task_scan, name="xarg-scan")) diff --git a/xarg/sheet.py b/xarg/sheet.py index 691f8dd..c114f29 100644 --- a/xarg/sheet.py +++ b/xarg/sheet.py @@ -107,10 +107,10 @@ class form(Generic[FKT, FVT]): """Custom table """ - def __init__(self, name: str, header: Iterable[FKT] = []): - self.__rows: List[row[FKT, FVT]] = list() + def __init__(self, name: str, header: Optional[Iterable[FKT]] = None): + self.__rows: List[row[FKT, FVT]] = [] self.__name: str = name - self.header = header + self.header = header if header is not None else [] def __len__(self) -> int: return len(self.__rows) @@ -301,9 +301,9 @@ def dump_sheet(self, table: form[Any, Any]): table.name, cell_overwrite_ok=True) widths: List[int] = [] values: Tuple[Tuple[Any, ...], ...] = table.dump() - for row_no in range(len(values)): - for col_no in range(len(values[row_no])): - value = str(values[row_no][col_no]) + for row_no, cells in enumerate(values): + for col_no, cell in enumerate(cells): + value = str(cell) sheet.write(row_no, col_no, value) width = wcswidth(value) if col_no >= len(widths):