Skip to content

Commit

Permalink
fix bug in python3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-black-tea committed Nov 10, 2023
1 parent b89cb54 commit e142790
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/linktools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from rich import get_console
from rich.tree import Tree

from linktools.cli.argparse import add_subparsers
from ._environ import environ
from .cli import BaseCommand, walk_commands
from .decorator import cached_property
Expand Down Expand Up @@ -91,13 +92,13 @@ def command_infos(self) -> Dict[CategoryInfo, List[CommandInfo]]:
return command_infos

def init_arguments(self, parser: ArgumentParser) -> None:
catalog_parsers = parser.add_subparsers(metavar="CATALOG", required=True)
catalog_parsers = add_subparsers(parser, metavar="CATALOG", required=True)
for category_info, command_infos in self.command_infos.items():
catalog_parser = catalog_parsers.add_parser(
category_info.name,
help=category_info.description
)
command_parsers = catalog_parser.add_subparsers(metavar="COMMAND", required=True)
command_parsers = add_subparsers(catalog_parser, metavar="COMMAND", required=True)
for command_info in command_infos:
command_parser = command_info.command.create_argument_parser(
command_info.name,
Expand Down
2 changes: 2 additions & 0 deletions src/linktools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
/_==__==========__==_ooo__ooo=_/' /___________,"
"""

from . import argparse

from .command import \
LogCommandMixin, \
BaseCommand, CommandError, \
Expand Down
6 changes: 6 additions & 0 deletions src/linktools/cli/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ def wrapper(o):
raise ValueError("value not in range %s-%s" % (min, max))

return wrapper


def add_subparsers(parser: argparse.ArgumentParser, **kwargs):
if sys.version_info < (3, 7):
kwargs.pop("required", None)
return parser.add_subparsers(**kwargs)
4 changes: 2 additions & 2 deletions src/linktools/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import rich
from rich import get_console

from .argparse import BooleanOptionalAction
from .argparse import BooleanOptionalAction, add_subparsers
from .._environ import BaseEnviron, environ
from .._logging import LogHandler
from ..decorator import cached_property
Expand Down Expand Up @@ -275,7 +275,7 @@ def add_subcommands(self: "BaseCommand", parser: ArgumentParser = None, target:
parser = parser or self._argument_parser
target = target or self

subparsers = parser.add_subparsers(metavar="COMMAND", help="Command Help", required=True)
subparsers = add_subparsers(parser, metavar="COMMAND", help="Command Help", required=True)
for _, command_info in SubCommandMixin._find_command_infos(target):
command_actions = []
command_func = getattr(target, command_info.func.__name__)
Expand Down
4 changes: 2 additions & 2 deletions src/linktools/cli/commands/common/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from linktools import ToolError
from linktools.cli import BaseCommand
from linktools.cli.argparse import KeyValueAction
from linktools.cli.argparse import KeyValueAction, add_subparsers
from linktools.utils import DownloadError


Expand Down Expand Up @@ -61,7 +61,7 @@ def init_arguments(self, parser: ArgumentParser) -> None:
help="execute tools as a daemon")

tool_names = sorted([tool.name for tool in iter(self.environ.tools)])
subparsers = parser.add_subparsers(metavar="TOOL", help=f"{{{','.join(tool_names)}}}", required=True)
subparsers = add_subparsers(parser, metavar="TOOL", help=f"{{{','.join(tool_names)}}}", required=True)
for tool_name in tool_names:
tool_parser = subparsers.add_parser(tool_name, prefix_chars=chr(0))
tool_parser.add_argument("tool_args", metavar="args", nargs="...")
Expand Down
18 changes: 10 additions & 8 deletions src/linktools/utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import random
import re
import socket
import sys
import threading
import time
import typing
Expand Down Expand Up @@ -406,20 +407,21 @@ def get_path(root_path: str, *paths: [str], create: bool = False, create_parent:
return target_path


@typing.overload
def read_file(path: str) -> bytes: ...
if sys.version_info >= (3, 8):
@typing.overload
def read_file(path: str) -> bytes: ...


@typing.overload
def read_file(path: str, binary: typing.Literal[True]) -> bytes: ...
@typing.overload
def read_file(path: str, binary: typing.Literal[True]) -> bytes: ...


@typing.overload
def read_file(path: str, binary: typing.Literal[False]) -> str: ...
@typing.overload
def read_file(path: str, binary: typing.Literal[False]) -> str: ...


@typing.overload
def read_file(path: str, binary: bool) -> Union[str, bytes]: ...
@typing.overload
def read_file(path: str, binary: bool) -> Union[str, bytes]: ...


def read_file(path: str, binary: bool = True) -> Union[str, bytes]:
Expand Down

0 comments on commit e142790

Please sign in to comment.