pbf.utils.CQCode
使用示例
cq_code = CQCode("[CQ:face,id=54][CQ:image,url=url_test,arg=arg_test]")
print(cq_code.getArr())
输出如下
[{'type': 'face', 'data': {'id': '54'}}, {'type': 'image', 'data': {'url': 'url_test', 'arg': 'arg_test'}}]
1""" 2# 使用示例 3```python 4cq_code = CQCode("[CQ:face,id=54][CQ:image,url=url_test,arg=arg_test]") 5print(cq_code.getArr()) 6``` 7输出如下 8``` 9[{'type': 'face', 'data': {'id': '54'}}, {'type': 'image', 'data': {'url': 'url_test', 'arg': 'arg_test'}}] 10``` 11""" 12 13 14try: 15 from ..statement import Statement 16except ImportError: # debug 17 from pbf.statement import Statement 18 19class CQCode: 20 content: str = None 21 22 def __init__(self, content: str) -> None: 23 """ 24 初始化CQCode对象。 25 :param content: str CQCode内容 26 """ 27 if content is None: 28 raise ValueError("Content cannot be None!") 29 self.content = content 30 31 def getArr(self) -> list: 32 """ 33 获取CQCode完整数组。 34 :return: List[dict] 35 """ 36 arr: list = [] 37 38 left: list = self.content.split("[") 39 del left[0] 40 for i in left: 41 try: 42 right: list = i.split("]")[0].split(",") 43 cqDict: dict = {"type": "cqcode", "data": {}} 44 for j in right: 45 if j == right[0]: 46 cqDict["type"] = j.split(":")[1] 47 else: 48 tmp_str = j.split("=") 49 cqDict["data"][tmp_str[0]] = tmp_str[1] 50 arr.append(cqDict) 51 except Exception as exc: 52 raise ValueError("Not a valid CQCode!") from exc 53 54 return arr 55 56 def get(self, key: str, index: int = None, type: str = None) -> list: 57 """ 58 获取CQCode数组中的数据。 59 :param key: str 键 60 :param index: int (可选)第几个CQCode,不传则从所有cqcode中寻找 61 :param type: str (可选)CQCode类型,不传则从所有cqcode中寻找 62 :return: list 63 """ 64 arr: list = [] 65 CQArr: list = self.getArr() 66 67 if index is not None: 68 if len(CQArr) > index: 69 return [CQArr[index].get("data").get(key)] 70 else: 71 # raise IndexError("Index out of range!") 72 return [] 73 74 elif type is not None: 75 for i in CQArr: 76 if i.get("type") == type: 77 arr.append(i.get("data").get(key)) 78 79 else: 80 for i in CQArr: 81 if i.get("data").get(key) is not None: 82 arr.append(i.get("data").get(key)) 83 84 return arr 85 86 def toStatement(self): 87 """ 88 转换为Statement对象。 89 :return: Statement 90 """ 91 ret_list: list = [] 92 for item in self.getArr(): 93 ret_list.append(Statement('cqcode').set(item)) 94 return ret_list 95 96 97if __name__ == "__main__": 98 cq_code = CQCode("[CQ:face,id=54][CQ:image,url=url_test,arg=arg_test]") 99 print(cq_code.getArr())
class
CQCode:
20class CQCode: 21 content: str = None 22 23 def __init__(self, content: str) -> None: 24 """ 25 初始化CQCode对象。 26 :param content: str CQCode内容 27 """ 28 if content is None: 29 raise ValueError("Content cannot be None!") 30 self.content = content 31 32 def getArr(self) -> list: 33 """ 34 获取CQCode完整数组。 35 :return: List[dict] 36 """ 37 arr: list = [] 38 39 left: list = self.content.split("[") 40 del left[0] 41 for i in left: 42 try: 43 right: list = i.split("]")[0].split(",") 44 cqDict: dict = {"type": "cqcode", "data": {}} 45 for j in right: 46 if j == right[0]: 47 cqDict["type"] = j.split(":")[1] 48 else: 49 tmp_str = j.split("=") 50 cqDict["data"][tmp_str[0]] = tmp_str[1] 51 arr.append(cqDict) 52 except Exception as exc: 53 raise ValueError("Not a valid CQCode!") from exc 54 55 return arr 56 57 def get(self, key: str, index: int = None, type: str = None) -> list: 58 """ 59 获取CQCode数组中的数据。 60 :param key: str 键 61 :param index: int (可选)第几个CQCode,不传则从所有cqcode中寻找 62 :param type: str (可选)CQCode类型,不传则从所有cqcode中寻找 63 :return: list 64 """ 65 arr: list = [] 66 CQArr: list = self.getArr() 67 68 if index is not None: 69 if len(CQArr) > index: 70 return [CQArr[index].get("data").get(key)] 71 else: 72 # raise IndexError("Index out of range!") 73 return [] 74 75 elif type is not None: 76 for i in CQArr: 77 if i.get("type") == type: 78 arr.append(i.get("data").get(key)) 79 80 else: 81 for i in CQArr: 82 if i.get("data").get(key) is not None: 83 arr.append(i.get("data").get(key)) 84 85 return arr 86 87 def toStatement(self): 88 """ 89 转换为Statement对象。 90 :return: Statement 91 """ 92 ret_list: list = [] 93 for item in self.getArr(): 94 ret_list.append(Statement('cqcode').set(item)) 95 return ret_list
CQCode(content: str)
23 def __init__(self, content: str) -> None: 24 """ 25 初始化CQCode对象。 26 :param content: str CQCode内容 27 """ 28 if content is None: 29 raise ValueError("Content cannot be None!") 30 self.content = content
初始化CQCode对象。
Parameters
- content: str CQCode内容
def
getArr(self) -> list:
32 def getArr(self) -> list: 33 """ 34 获取CQCode完整数组。 35 :return: List[dict] 36 """ 37 arr: list = [] 38 39 left: list = self.content.split("[") 40 del left[0] 41 for i in left: 42 try: 43 right: list = i.split("]")[0].split(",") 44 cqDict: dict = {"type": "cqcode", "data": {}} 45 for j in right: 46 if j == right[0]: 47 cqDict["type"] = j.split(":")[1] 48 else: 49 tmp_str = j.split("=") 50 cqDict["data"][tmp_str[0]] = tmp_str[1] 51 arr.append(cqDict) 52 except Exception as exc: 53 raise ValueError("Not a valid CQCode!") from exc 54 55 return arr
获取CQCode完整数组。
Returns
List[dict]
def
get(self, key: str, index: int = None, type: str = None) -> list:
57 def get(self, key: str, index: int = None, type: str = None) -> list: 58 """ 59 获取CQCode数组中的数据。 60 :param key: str 键 61 :param index: int (可选)第几个CQCode,不传则从所有cqcode中寻找 62 :param type: str (可选)CQCode类型,不传则从所有cqcode中寻找 63 :return: list 64 """ 65 arr: list = [] 66 CQArr: list = self.getArr() 67 68 if index is not None: 69 if len(CQArr) > index: 70 return [CQArr[index].get("data").get(key)] 71 else: 72 # raise IndexError("Index out of range!") 73 return [] 74 75 elif type is not None: 76 for i in CQArr: 77 if i.get("type") == type: 78 arr.append(i.get("data").get(key)) 79 80 else: 81 for i in CQArr: 82 if i.get("data").get(key) is not None: 83 arr.append(i.get("data").get(key)) 84 85 return arr
获取CQCode数组中的数据。
Parameters
- key: str 键
- index: int (可选)第几个CQCode,不传则从所有cqcode中寻找
- type: str (可选)CQCode类型,不传则从所有cqcode中寻找
Returns
list