Difficult-Rocket/SRtool/command/api.py

98 lines
2.6 KiB
Python
Raw Normal View History

2022-02-07 22:14:51 +08:00
# -------------------------------
# Difficult Rocket
2022-06-27 16:51:14 +08:00
# Copyright © 2021-2022 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
from typing import Union, Optional, Type, Tuple, List
2022-02-07 22:14:51 +08:00
2022-05-11 11:11:39 +08:00
# DR
2022-06-29 13:45:25 +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
2022-02-07 22:14:51 +08:00
self.value_dict = {}
self.value_list = []
2022-05-11 11:11:39 +08:00
self.command_tree = {}
tree_list = text.split(' ')
2022-05-25 09:16:38 +08:00
self.tree_node = tree_list
@staticmethod
def parse_text(raw_text: str) -> str:
q_mark_iter = re.finditer('\\"', raw_text)
for q_mark in q_mark_iter:
...
2022-05-25 09:16:38 +08:00
@staticmethod
def parse_command(raw_command: Union[str, "CommandText"]) -> Tuple[List[str], Union[CommandParseError, type(True)]]:
spilt_list = re.split(r'', raw_command)
done_list = [re.sub(r'\\"', '"', raw_text) for raw_text in spilt_list]
return done_list, True # 完事了
2022-02-07 22:14:51 +08:00
2022-02-07 23:03:42 +08:00
def find(self, text: str) -> Union[str, bool]:
2022-02-07 22:14:51 +08:00
finding = re.match(text, self.text)
if finding:
return finding.group()
else:
return False
def match(self, text: str) -> bool:
finding = re.match(text, self.text)
if finding: # 如果找到了
try:
next_find = self.text[finding.span()[1]]
# 这里try因为可能匹配到的是字符串末尾
except IndexError:
next_find = ' '
# 直接过滤掉
if next_find == ' ':
self.text = self.text[finding.span()[1] + 1:]
return True
# 将匹配到的字符串,和最后一个匹配字符后面的字符删除(相当暴力的操作)
return False
else:
return False
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)