-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(command): 重构命令路由 * feat(lua): 包装异步方法`self.event.reply` => `msg:echo` * feat(lua): 包装异步输入流方法`self.event.ask` => `msg:ask` * 'Refactored by Sourcery' (#83) Co-authored-by: Sourcery AI <> * feat(Token|Lexer): 添加`Token`与`Lexer`类 * refactor(Lexer): 词法分析器添加`advance`方法 * chore: lint code * refactor: sync gensokyo adapter * feat: Cli parser (#85) * feat(cli): 添加`Cli`类,解析命令行参数 * fix: 修复错误的`dest`与`action` * feat(cli): 实现`install_package` 与 `build_template` * feat(cli): 实现`-c|--config`指令配置镜像常量等 * feat(cli): 使用高效率的异步网络库`aiohttp` * fix(cli): `TYPE_CHECKING` with partially module `typing` * refactor!: examples, tests, src... rewrite in rust --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3eb819d
commit e6e9453
Showing
38 changed files
with
1,246 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,4 +108,5 @@ docs/.next/ | |
|
||
# example | ||
example/config.toml | ||
.pdm-python | ||
.pdm-python | ||
bottles.db |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
[bot] | ||
plugins = [] | ||
plugin_dirs = ["plugins"] | ||
rules = [] | ||
rule_dirs = ["rules"] | ||
adapters = [ | ||
"iamai.adapter.onebot11", | ||
# "iamai.adapter.gensokyo", | ||
# "iamai.adapter.apscheduler", | ||
# "iamai.adapter.dingtalk" | ||
] | ||
|
||
[bot.log] | ||
level = "INFO" | ||
verbose_exception = true | ||
|
||
[adapter.onebot11] | ||
adapter_type = "reverse-ws" | ||
host = "127.0.0.1" | ||
port = 8080 | ||
url = "/cqhttp/ws" | ||
show_raw = true | ||
|
||
[adapter.gensokyo] | ||
adapter_type = "reverse-ws" | ||
host = "127.0.0.1" | ||
port = 8081 | ||
url = "/gsk/ws" | ||
show_raw = true | ||
|
||
[adapter.dingtalk] | ||
adapter_type = "stream" | ||
host = "127.0.0.1" | ||
port = 15700 | ||
url = "/dingtalk" | ||
app_secret = "FnQU_a88xRpmcs3oPNXSgoQgm4TidGduVqKhLHR7_NgF6MLBUUbwYdE6MkOFWZFb" | ||
app_key = "dingo7xu5djthkxpoick" | ||
|
||
[adapter.apscheduler] | ||
scheduler_config = { "apscheduler.timezone" = "Asia/Shanghai" } | ||
|
||
[plugin.HydroRoll] | ||
uid = '' | ||
rules = [] | ||
rule_dirs = ["rules"] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
SET, GET, ALIAS = ("SET", "GET", "ALIAS") | ||
INTEGER = "INTEGER" | ||
EOF = "EOF" | ||
|
||
class Token(object): | ||
def __init__(self, type, value): | ||
self.type = type | ||
self.value = value | ||
|
||
def __str__(self): | ||
return f"Token({self.type}, {self.value}" | ||
|
||
def __repr__(self) -> str: | ||
return self.__str__() | ||
|
||
|
||
class Lexer(object): | ||
def __init__(self, text): | ||
self.text = text | ||
self.pos = 0 | ||
self.current_char = self.text[self.pos] | ||
|
||
def error(self): | ||
raise Exception("Invalid Character") | ||
|
||
def advance(self): | ||
"""Advance the `pos` pointer and set the `current_char` variable.""" | ||
self.pos += 1 | ||
if self.pos > len(self.text) - 1: | ||
self.current_char = None # Indicates end of input | ||
else: | ||
self.current_char = self.text[self.pos] | ||
|
||
def skip_whitespace(self): | ||
while self.current_char is not None and self.current_char.isspace(): | ||
self.advance() | ||
|
||
def integer(self): | ||
"""Return a (multidigit) integer consumed from the input.""" | ||
result = "" | ||
while self.current_char is not None and self.current_char.isdigit(): | ||
result += self.current_char | ||
self.advance() | ||
return int(result) | ||
|
||
def get_next_token(self): | ||
"""Lexical analyzer (also known as scanner or tokenizer) | ||
This method is responsible for breaking a sentence | ||
apart into tokens. One token at a time. | ||
""" | ||
while self.current_char is not None: | ||
if self.current_char.isspace(): | ||
self.skip_whitespace() | ||
continue | ||
|
||
if self.current_char.isdigit(): | ||
return Token(INTEGER, self.integer()) | ||
|
||
if self.current_char == "get": | ||
self.advance() | ||
return Token(GET, "get") | ||
|
||
if self.current_char == "set": | ||
self.advance() | ||
return Token(SET, "set") | ||
|
||
if self.current_char == "Alias": | ||
self.advance() | ||
return Token(ALIAS, "Alias") | ||
|
||
self.error() | ||
|
||
return Token(EOF, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .typing import CommandBase | ||
|
||
class AliasCommand(CommandBase): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .typing import CommandBase | ||
|
||
class GetCommand(CommandBase): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .typing import CommandBase | ||
|
||
class SetCommand(CommandBase): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from pydantic import BaseModel | ||
|
||
class CommandBase(BaseModel): | ||
... |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from cachetools import cached | ||
import time | ||
|
||
from iamai import Plugin | ||
from iamai.event import MessageEvent | ||
|
||
|
||
class CachedPlugin(Plugin): | ||
async def handle(self) -> None: | ||
# without cached | ||
def fib(n): | ||
return n if n < 2 else fib(n - 1) + fib(n - 2) | ||
|
||
s = time.time() | ||
await self.event.reply(f"{fib(36)}") | ||
await self.event.reply(f"Time Taken: {time.time() - s}") | ||
|
||
# Now using cached | ||
s = time.time() | ||
|
||
# Use this decorator to enable caching | ||
@cached(cache={}) | ||
def fib(n): | ||
return n if n < 2 else fib(n - 1) + fib(n - 2) | ||
|
||
await self.event.reply(f"{fib(36)}") | ||
await self.event.reply(f"Time Taken(cached): {time.time() - s}") | ||
|
||
async def rule(self) -> bool: | ||
return ( | ||
isinstance(self.event, MessageEvent) | ||
and self.event.get_plain_text() == ".cachetools" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Union | ||
from iamai import Plugin, Event, Depends | ||
from iamai.log import logger | ||
from .config import Config | ||
from iamai.event import MessageEvent | ||
from .database import Database | ||
from .permission import Permission | ||
from .workroutes import WorkRoutes | ||
from .inspector import Inspector | ||
|
||
|
||
class Bottles(Plugin, config=Config): | ||
database: Database = Depends() | ||
permission: Permission = Depends() | ||
workroutes: WorkRoutes = Depends() | ||
inspector: Inspector = Depends() | ||
|
||
def __init__(self): | ||
self.text = None | ||
self.prefix = None | ||
self.suffix = None | ||
|
||
async def handle(self) -> None: | ||
self.namespace = next( | ||
( | ||
key | ||
for key, value in self.config.command_list.items() | ||
if value == self.prefix | ||
), | ||
"", | ||
) | ||
if method := getattr(self.inspector, self.namespace, None): | ||
result = await method(self.suffix, self.config) | ||
if result: | ||
await self.event.reply(result) | ||
|
||
async def rule(self) -> bool: | ||
if not isinstance(self.event, MessageEvent): | ||
return False | ||
if not self.permission.is_admin(): | ||
return False | ||
self.text = self.event.get_plain_text() | ||
for prefix in list(self.config.command_list.values()): | ||
if self.text.startswith(prefix): | ||
self.prefix = prefix | ||
self.suffix = self.text[len(self.prefix) + 1 :] | ||
return True | ||
return False |
Oops, something went wrong.