integrations.standard_io.api
integrations/standard_io/api.py
1""" 2integrations/standard_io/api.py 3""" 4 5 6import textwrap 7from pathlib import Path 8from typing import TYPE_CHECKING 9 10import pandas as pd 11 12from integrations.base.interface import APIInterface 13from libs.types import StyleOptions 14from libs.utils import formatter 15 16if TYPE_CHECKING: 17 from integrations.base.interface import MessageParserProtocol 18 19 20class AdapterAPI(APIInterface): 21 """インターフェースAPI操作クラス""" 22 23 def _text_formatter(self, text: str) -> str: 24 """テキスト整形 25 26 Args: 27 text (str): 対象テキスト 28 29 Returns: 30 str: 整形済みテキスト 31 """ 32 33 ret: str = "" 34 for line in text.splitlines(): 35 line = line.replace("<@>", "") 36 line = textwrap.dedent(line) 37 if line: 38 ret += f"{line}\n" 39 return ret.strip() 40 41 def post(self, m: "MessageParserProtocol"): 42 """メッセージ出力 43 44 Args: 45 m (MessageParserProtocol): メッセージデータ 46 """ 47 48 # 見出し 49 if m.post.headline: 50 title, text = next(iter(m.post.headline.items())) 51 if text: 52 print("=" * 80) 53 if not title.isnumeric() and title: 54 print(f"【{title}】") 55 print(textwrap.dedent(text).rstrip()) 56 print("=" * 80) 57 58 # 本文 59 for data in m.post.message: 60 for title, msg in data.items(): 61 style = msg.get("options", StyleOptions()) 62 63 if style.key_title and title: 64 print(f"【{title}】") 65 66 match msg.get("data"): 67 case x if isinstance(x, str): 68 print(self._text_formatter(x)) 69 case x if isinstance(x, pd.DataFrame): 70 disp = x.to_markdown( 71 index=style.show_index, 72 tablefmt="simple_outline", 73 floatfmt=formatter.floatfmt_adjust(x, index=style.show_index), 74 ).replace(" nan ", "-----") 75 if title == "座席データ": 76 disp = disp.replace("0.00", "-.--") 77 print(disp) 78 case x if isinstance(x, Path): 79 print(f"{title}: {x.absolute()}") 80 case _: 81 pass 82 83 print("")
21class AdapterAPI(APIInterface): 22 """インターフェースAPI操作クラス""" 23 24 def _text_formatter(self, text: str) -> str: 25 """テキスト整形 26 27 Args: 28 text (str): 対象テキスト 29 30 Returns: 31 str: 整形済みテキスト 32 """ 33 34 ret: str = "" 35 for line in text.splitlines(): 36 line = line.replace("<@>", "") 37 line = textwrap.dedent(line) 38 if line: 39 ret += f"{line}\n" 40 return ret.strip() 41 42 def post(self, m: "MessageParserProtocol"): 43 """メッセージ出力 44 45 Args: 46 m (MessageParserProtocol): メッセージデータ 47 """ 48 49 # 見出し 50 if m.post.headline: 51 title, text = next(iter(m.post.headline.items())) 52 if text: 53 print("=" * 80) 54 if not title.isnumeric() and title: 55 print(f"【{title}】") 56 print(textwrap.dedent(text).rstrip()) 57 print("=" * 80) 58 59 # 本文 60 for data in m.post.message: 61 for title, msg in data.items(): 62 style = msg.get("options", StyleOptions()) 63 64 if style.key_title and title: 65 print(f"【{title}】") 66 67 match msg.get("data"): 68 case x if isinstance(x, str): 69 print(self._text_formatter(x)) 70 case x if isinstance(x, pd.DataFrame): 71 disp = x.to_markdown( 72 index=style.show_index, 73 tablefmt="simple_outline", 74 floatfmt=formatter.floatfmt_adjust(x, index=style.show_index), 75 ).replace(" nan ", "-----") 76 if title == "座席データ": 77 disp = disp.replace("0.00", "-.--") 78 print(disp) 79 case x if isinstance(x, Path): 80 print(f"{title}: {x.absolute()}") 81 case _: 82 pass 83 84 print("")
インターフェースAPI操作クラス
42 def post(self, m: "MessageParserProtocol"): 43 """メッセージ出力 44 45 Args: 46 m (MessageParserProtocol): メッセージデータ 47 """ 48 49 # 見出し 50 if m.post.headline: 51 title, text = next(iter(m.post.headline.items())) 52 if text: 53 print("=" * 80) 54 if not title.isnumeric() and title: 55 print(f"【{title}】") 56 print(textwrap.dedent(text).rstrip()) 57 print("=" * 80) 58 59 # 本文 60 for data in m.post.message: 61 for title, msg in data.items(): 62 style = msg.get("options", StyleOptions()) 63 64 if style.key_title and title: 65 print(f"【{title}】") 66 67 match msg.get("data"): 68 case x if isinstance(x, str): 69 print(self._text_formatter(x)) 70 case x if isinstance(x, pd.DataFrame): 71 disp = x.to_markdown( 72 index=style.show_index, 73 tablefmt="simple_outline", 74 floatfmt=formatter.floatfmt_adjust(x, index=style.show_index), 75 ).replace(" nan ", "-----") 76 if title == "座席データ": 77 disp = disp.replace("0.00", "-.--") 78 print(disp) 79 case x if isinstance(x, Path): 80 print(f"{title}: {x.absolute()}") 81 case _: 82 pass 83 84 print("")
メッセージ出力
Arguments:
- m (MessageParserProtocol): メッセージデータ