Difficult-Rocket/Difficult_Rocket/command/api.py

88 lines
2.3 KiB
Python
Raw Normal View History

2022-02-07 22:14:51 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2022-02-07 22:14:51 +08:00
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
# system function
import re
2023-04-05 16:00:38 +08:00
from typing import Union, Optional
2022-02-07 22:14:51 +08:00
2022-05-11 11:11:39 +08:00
# DR
2023-02-18 19:19:15 +08:00
# from Difficult_Rocket.exception.command import *
2022-05-11 11:11:39 +08:00
search_re = re.compile(r'(?<!\\)"')
2022-02-07 22:14:51 +08:00
class CommandText:
"""
CommandLine返回的字符可以用来搜索
"""
def __init__(self, text: str):
2022-05-11 11:11:39 +08:00
self.plain_command = text
2022-02-07 22:14:51 +08:00
self.text = text
2022-05-11 11:11:39 +08:00
self.error = False
2023-04-05 16:00:38 +08:00
self.value_list = []
2022-02-07 22:14:51 +08:00
self.value_dict = {}
2022-05-11 11:11:39 +08:00
2023-01-21 22:50:18 +08:00
def counter(self, start: Optional[int] = 0) -> int:
assert isinstance(start, int)
i = start
while True:
yield i
if self.error:
break
i += 1
2022-02-07 22:14:51 +08:00
2022-02-07 23:03:42 +08:00
def find(self, text: str) -> Union[str, bool]:
2023-01-27 21:09:37 +08:00
return finding.group() if (finding := re.match(text, self.text)) else False
2022-02-07 22:14:51 +08:00
2023-01-22 09:17:01 +08:00
def re_match(self, text: str) -> bool:
2023-01-27 21:09:37 +08:00
if finding := re.match(text, self.text):
2022-02-07 22:14:51 +08:00
try:
next_find = self.text[finding.span()[1]]
# 这里try因为可能匹配到的是字符串末尾
2023-01-22 09:17:01 +08:00
# 20230122 我现在也不知道为啥这么写了
# 果然使用正则表达式就是让一个问题变成两个问题
2022-02-07 22:14:51 +08:00
except IndexError:
self.text = self.text[finding.span()[1] + 1:]
return True
2023-01-22 09:17:01 +08:00
if next_find == ' ':
return True
2023-01-27 21:09:37 +08:00
# 将匹配到的字符串,和最后一个匹配字符后面的字符删除(相当暴力的操作)
return False
2022-02-07 22:14:51 +08:00
2023-01-21 22:50:18 +08:00
def int_value(self, name: Optional[str]):
...
2022-02-07 22:14:51 +08:00
def value(self,
name: str = None,
split: str = ' ',
middle: list = ('\'', '\"')):
pass
2022-05-11 11:11:39 +08:00
def get_all(self, value_name: str):
self.value_list.append(self.text)
if value_name:
self.value_dict[value_name] = self.text
self.text = ''
return self.value_list[-1]
def get_value(self):
pass
2022-02-07 22:14:51 +08:00
def __str__(self):
return str(self.text)
def __int__(self):
return int(self.text)