mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Add Bot API parser and code-generator
This commit is contained in:
parent
5e9d4e55d9
commit
af2573dbee
15 changed files with 3242 additions and 1 deletions
90
generator/structures.py
Normal file
90
generator/structures.py
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import typing
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from generator.normalizers import normalize_type, get_returning
|
||||
|
||||
|
||||
@dataclass
|
||||
class Annotation:
|
||||
name: str
|
||||
type: str
|
||||
description: str
|
||||
required: bool = True
|
||||
|
||||
@property
|
||||
def python_name(self):
|
||||
if self.name == "from":
|
||||
return "from_user"
|
||||
return self.name
|
||||
|
||||
@property
|
||||
def python_type(self) -> str:
|
||||
return normalize_type(self.type, self.required)
|
||||
|
||||
@property
|
||||
def python_argument(self):
|
||||
result = f"{self.python_name}: {self.python_type}"
|
||||
|
||||
value = "" if self.required else "None"
|
||||
if self.name == "from":
|
||||
value = f"pydantic.Schema({value or '...'}, alias=\"from\")"
|
||||
|
||||
if value:
|
||||
result += f" = {value}"
|
||||
return result
|
||||
|
||||
|
||||
@dataclass
|
||||
class Entity:
|
||||
name: str
|
||||
anchor: str
|
||||
description: str = None
|
||||
annotations: typing.List[Annotation] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def is_method(self) -> bool:
|
||||
return self.name[0].islower()
|
||||
|
||||
@property
|
||||
def is_type(self) -> bool:
|
||||
return not self.is_method
|
||||
|
||||
@property
|
||||
def python_name(self) -> str:
|
||||
return self.name
|
||||
|
||||
def _get_returning(self):
|
||||
if self.is_type:
|
||||
return self.name, ""
|
||||
|
||||
return get_returning(self.description)
|
||||
|
||||
@property
|
||||
def returning(self):
|
||||
return self._get_returning()[1]
|
||||
|
||||
@property
|
||||
def returning_type(self):
|
||||
return self._get_returning()[0]
|
||||
|
||||
@property
|
||||
def python_returning_type(self):
|
||||
return normalize_type(self.returning_type)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Group:
|
||||
title: str
|
||||
anchor: str
|
||||
description: str = None
|
||||
childs: typing.List[Entity] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def has_methods(self):
|
||||
return any(entity.is_method for entity in self.childs)
|
||||
|
||||
@property
|
||||
def has_types(self):
|
||||
return any(entity.is_method for entity in self.childs)
|
||||
Loading…
Add table
Add a link
Reference in a new issue