Skip to content

Commit

Permalink
🎨 format
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Feb 26, 2024
1 parent f964e4c commit aa4cc6a
Show file tree
Hide file tree
Showing 28 changed files with 258 additions and 104 deletions.
5 changes: 3 additions & 2 deletions benchmark.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time
from arclet.alconna import Alconna, Args, ANY, command_manager, namespace
import cProfile
import pstats
import time

from arclet.alconna import ANY, Alconna, Args, command_manager, namespace


class Plain:
Expand Down
2 changes: 1 addition & 1 deletion commander/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from arclet.alconna import Alconna, command_manager, Args
from arclet.alconna import Alconna, Args, command_manager
from commander import Commands

command = Commands()
Expand Down
10 changes: 6 additions & 4 deletions commander/commander.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from arclet.alconna import Alconna, Arparma
from arclet.alconna.core import ArparmaExecutor
from tarina import is_awaitable
from dataclasses import dataclass, field
from typing import TypeVar, Tuple, Callable, Any, Optional
from typing import Any, Callable, Optional, Tuple, TypeVar
from weakref import WeakKeyDictionary

from tarina import is_awaitable

from arclet.alconna import Alconna, Arparma
from arclet.alconna.core import ArparmaExecutor

TCall = TypeVar("TCall", bound=Callable)


Expand Down
6 changes: 3 additions & 3 deletions entry_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from tests.analyser_test import *
from tests.args_test import *
from tests.base_test import *
from tests.util_test import *
from tests.core_test import *
from tests.components_test import *
from tests.config_test import *
from tests.analyser_test import *
from tests.core_test import *
from tests.util_test import *

if __name__ == '__main__':
import pytest
Expand Down
3 changes: 2 additions & 1 deletion exam1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from arclet.alconna import Alconna, Option, CommandMeta, Args, CompSession, Arg, OptionResult
from tarina import lang

from arclet.alconna import Alconna, Arg, Args, CommandMeta, CompSession, Option, OptionResult

lang.set("completion", "node", "")
lang.set("completion", "prompt_select", "")

Expand Down
2 changes: 1 addition & 1 deletion exam3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any, Callable, TypeVar
from typing_extensions import ParamSpec, Self
from typing import Callable, TypeVar, Any

T = TypeVar("T")
R = TypeVar("R")
Expand Down
9 changes: 5 additions & 4 deletions exam7.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from arclet.alconna import Args
from arclet.alconna.action import Action, store_true, store
from typing import Literal, overload, Optional
from typing_extensions import Self
from dataclasses import dataclass, field
from typing import Literal, Optional, overload
from typing_extensions import Self

from arclet.alconna import Args
from arclet.alconna.action import Action, store, store_true


@dataclass
Expand Down
6 changes: 4 additions & 2 deletions example/calculate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from arclet.alconna import Alconna, Arg
from nepattern import SwitchPattern, NUMBER
import operator

from nepattern import NUMBER, SwitchPattern

from arclet.alconna import Alconna, Arg

alc = Alconna(
"calc",
Arg("a", NUMBER),
Expand Down
6 changes: 4 additions & 2 deletions example/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from arclet.alconna import Alconna, Args, Option, UnpackVar, KeyWordVar
from dataclasses import dataclass, asdict
from dataclasses import asdict, dataclass

from arclet.alconna import Alconna, Args, KeyWordVar, Option, UnpackVar


@dataclass
class User:
Expand Down
3 changes: 2 additions & 1 deletion example/exec_code.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import sys
from io import StringIO

from arclet.alconna import Alconna, Args, Field, Option, CommandMeta, MultiVar, Arparma
from nepattern import AnyString

from arclet.alconna import Alconna, Args, Arparma, CommandMeta, Field, MultiVar, Option

alc = Alconna(
"exec",
Args["code", MultiVar(AnyString), Field(completion=lambda: "print(1+1)")] / "\n",
Expand Down
3 changes: 2 additions & 1 deletion example/exec_sql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sqlite3 import connect
from typing import Optional
from arclet.alconna import Alconna, Arg, Option, KeyWordVar, MultiVar

from arclet.alconna import Alconna, Arg, KeyWordVar, MultiVar, Option

db = connect('example.db')

Expand Down
4 changes: 3 additions & 1 deletion example/img_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from arclet.alconna import Alconna, Args, namespace
from typing import Optional

from arclet.alconna import Alconna, Args, namespace


class Text:
type = "text"
text: str
Expand Down
5 changes: 3 additions & 2 deletions example/pip.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from arclet.alconna import Alconna, Option, count, Subcommand, Args, MultiVar, store_true
from nepattern import URL, create_local_patterns
from pathlib import Path
from typing import Tuple

from nepattern import URL, create_local_patterns

from arclet.alconna import Alconna, Args, MultiVar, Option, Subcommand, count, store_true

pip = Alconna(
"/pip",
Expand Down
200 changes: 149 additions & 51 deletions pdm.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ dev = [
"coverage~=7.0.1",
"pydeps~=1.11.0",
"fix-future-annotations>=0.4.0",
"isort>=5.13.2",
"black>=24.2.0",
]

[tool.pdm.scripts]
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
from .stub import SubcommandStub as SubcommandStub
from .typing import AllParam as AllParam
from .typing import CommandMeta as CommandMeta
from .typing import ContextVal as ContextVal
from .typing import KeyWordVar as KeyWordVar
from .typing import Kw as Kw
from .typing import MultiVar as MultiVar
from .typing import Nargs as Nargs
from .typing import UnpackVar as UnpackVar
from .typing import Up as Up
from .typing import ContextVal as ContextVal

__version__ = "1.7.44"

Expand Down
16 changes: 12 additions & 4 deletions src/arclet/alconna/_internal/_analyser.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
from __future__ import annotations

import re
from dataclasses import dataclass, field
from re import Match
from typing import TYPE_CHECKING, Any, Callable, Generic, Set
from typing_extensions import Self, TypeAlias

from tarina import lang, Empty
from tarina import Empty, lang

from ..action import Action
from ..args import Args
from ..arparma import Arparma
from ..base import Completion, Help, Option, Shortcut, Subcommand
from ..completion import comp_ctx
from ..exceptions import ArgumentMissing, FuzzyMatchSuccess, InvalidParam, ParamsUnmatched, PauseTriggered, SpecialOptionTriggered
from ..constraint import SHORTCUT_ARGS, SHORTCUT_REGEX_MATCH, SHORTCUT_REST, SHORTCUT_TRIGGER
from ..exceptions import (
ArgumentMissing,
FuzzyMatchSuccess,
InvalidParam,
ParamsUnmatched,
PauseTriggered,
SpecialOptionTriggered,
)
from ..manager import command_manager
from ..model import HeadResult, OptionResult, Sentence, SubcommandResult
from ..output import output_manager
from ..typing import TDC, InnerShortcutArgs
from ..constraint import SHORTCUT_TRIGGER, SHORTCUT_ARGS, SHORTCUT_REST, SHORTCUT_REGEX_MATCH
from ._handlers import (
_handle_shortcut_data,
_handle_shortcut_reg,
Expand Down Expand Up @@ -292,7 +300,7 @@ def shortcut(
return self.export(argv, True, exc)
argv.addon(short.args)
data = _handle_shortcut_data(argv, data)
if not data and argv.raw_data and any(isinstance(i, str) and "{%0}" in i for i in argv.raw_data):
if not data and argv.raw_data and any(isinstance(i, str) and bool(re.search(r"\{%(\d+)|\*(.*?)\}", i)) for i in argv.raw_data):
exc = ArgumentMissing(lang.require("analyser", "param_missing"))
if self.command.meta.raise_exception:
raise exc
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/_internal/_argv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from ..args import Arg
from ..base import Option, Subcommand
from ..config import Namespace, config
from ..constraint import ARGV_OVERRIDES
from ..exceptions import NullMessage
from ..typing import TDC
from ..constraint import ARGV_OVERRIDES


@dataclass(repr=True)
Expand Down
6 changes: 2 additions & 4 deletions src/arclet/alconna/_internal/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from typing import TYPE_CHECKING, Any, Iterable

from nepattern import ANY, AnyString, BasePattern, STRING
from nepattern import ANY, STRING, AnyString, BasePattern
from nepattern.util import TPattern
from tarina import Empty, lang, split_once

Expand All @@ -15,7 +15,7 @@
from ..exceptions import ArgumentMissing, FuzzyMatchSuccess, InvalidParam, PauseTriggered, SpecialOptionTriggered
from ..model import HeadResult, OptionResult, Sentence
from ..output import output_manager
from ..typing import KWBool, ShortcutRegWrapper, MultiKeyWordVar, MultiVar, ContextVal
from ..typing import ContextVal, KWBool, MultiKeyWordVar, MultiVar, ShortcutRegWrapper
from ._header import Double, Header
from ._util import escape, levenshtein, unescape

Expand Down Expand Up @@ -594,8 +594,6 @@ def _handle_multi_slot(argv: Argv, unit: str, data: list, index: int, current: i

def _handle_shortcut_data(argv: Argv, data: list):
data_len = len(data)
if not data_len:
return []
record = set()
offset = 0
for i, unit in enumerate(argv.raw_data.copy()):
Expand Down
17 changes: 10 additions & 7 deletions src/arclet/alconna/_internal/_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from inspect import isclass
from typing import Any, Callable

from nepattern import BasePattern, UnionPattern, all_patterns, parser, MatchMode
from nepattern import BasePattern, MatchMode, UnionPattern, all_patterns, parser
from nepattern.util import TPattern
from tarina import lang

Expand Down Expand Up @@ -105,7 +105,8 @@ def __repr__(self):
class Double:
"""用于匹配前缀和命令的组合"""

command: TPattern | BasePattern | str
command: TPattern | BasePattern
comp_pattern: TPattern | BasePattern

def __init__(
self,
Expand All @@ -127,17 +128,20 @@ def __init__(
self.prefix = set(texts) if texts else None
self.comp_pattern = prefixed(command)
self.cmd_type = 0
self.match = self.match0
elif not texts:
self.prefix = None
self.command = re.compile(command)
self.comp_pattern = re.compile(f"^{command}")
self.cmd_type = 1
self.match = self.match1
else:
prf = "|".join(re.escape(h) for h in texts)
self.prefix = re.compile(f"(?:{prf}){command}")
self.command = re.compile(command)
self.cmd_type = 2
self.comp_pattern = re.compile(f"^(?:{prf}){command}")
self.match = self.match2

def __repr__(self):
if self.cmd_type == 0:
Expand Down Expand Up @@ -182,11 +186,7 @@ def match1(self, pf: Any, cmd: Any, p_str: bool, c_str: bool, pbfn: Callable[...
pbfn(cmd[len(mat[0]):], replace=True)
return (pf, cmd), (pf, mat[0]), True, mat.groupdict()

def match(self, pf: Any, cmd: Any, p_str: bool, c_str: bool, pbfn: Callable[..., ...], comp: bool):
if not self.cmd_type:
return self.match0(pf, cmd, p_str, c_str, pbfn, comp)
if self.cmd_type == 1:
return self.match1(pf, cmd, p_str, c_str, pbfn, comp)
def match2(self, pf: Any, cmd: Any, p_str: bool, c_str: bool, pbfn: Callable[..., ...], comp: bool):
if not p_str and not c_str:
return
if p_str:
Expand All @@ -212,6 +212,9 @@ def match(self, pf: Any, cmd: Any, p_str: bool, c_str: bool, pbfn: Callable[...,
pbfn(cmd[len(mat[0]):], replace=True)
return (pf, cmd), (val._value, mat[0]), True, mat.groupdict()

def match(self, pf: Any, cmd: Any, p_str: bool, c_str: bool, pbfn: Callable[..., ...], comp: bool):
...


class Header:
"""命令头部的匹配表达式"""
Expand Down
4 changes: 2 additions & 2 deletions src/arclet/alconna/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from typing import Any, Callable, Generic, Iterable, List, Sequence, Type, TypeVar, Union, cast
from typing_extensions import Self, TypeAlias

from nepattern import BasePattern, MatchMode, RawStr, UnionPattern, parser, NONE, ANY, AntiPattern
from nepattern import ANY, NONE, AntiPattern, BasePattern, MatchMode, RawStr, UnionPattern, parser
from tarina import Empty, get_signature, lang

from .exceptions import InvalidArgs
from .typing import KeyWordVar, KWBool, MultiVar, UnpackVar, MultiKeyWordVar, AllParam
from .typing import AllParam, KeyWordVar, KWBool, MultiKeyWordVar, MultiVar, UnpackVar


def safe_dcls_kw(**kwargs):
Expand Down
4 changes: 2 additions & 2 deletions src/arclet/alconna/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from dataclasses import replace
from functools import reduce
from typing import Any, Iterable, Sequence, overload

from tarina import lang, Empty
from typing_extensions import Self

from tarina import Empty, lang

from .action import Action, store
from .args import Arg, Args
from .exceptions import InvalidArgs
Expand Down
3 changes: 2 additions & 1 deletion src/arclet/alconna/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Any, Callable, Generic, Literal, Sequence, TypeVar, cast, overload
from typing_extensions import Self
from weakref import WeakSet

from tarina import init_spec, lang

from ._internal._analyser import Analyser, TCompile
Expand All @@ -19,7 +20,7 @@
from .exceptions import ExecuteFailed, NullMessage
from .formatter import TextFormatter
from .manager import ShortcutArgs, command_manager
from .typing import TDC, CommandMeta, DataCollection, ShortcutRegWrapper, TPrefixes, InnerShortcutArgs
from .typing import TDC, CommandMeta, DataCollection, InnerShortcutArgs, ShortcutRegWrapper, TPrefixes

T_Duplication = TypeVar("T_Duplication", bound=Duplication)
T = TypeVar("T")
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .arparma import Arparma
from .config import Namespace, config
from .exceptions import ExceedMaxCount
from .typing import TDC, CommandMeta, DataCollection, ShortcutArgs, InnerShortcutArgs
from .typing import TDC, CommandMeta, DataCollection, InnerShortcutArgs, ShortcutArgs

if TYPE_CHECKING:
from ._internal._analyser import Analyser
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from .args import Args
from .base import Option, Subcommand
from .typing import AllParam
from .model import OptionResult, SubcommandResult
from .typing import AllParam

T = TypeVar("T")
T_Origin = TypeVar("T_Origin")
Expand Down
Loading

0 comments on commit aa4cc6a

Please sign in to comment.