geek.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import re
  7. def compat(code: str) -> str:
  8. """
  9. Reformats modules, built for GeekTG to work with Hikka
  10. :param code: code to reformat
  11. :return: reformatted code
  12. :rtype: str
  13. :example:
  14. ```python
  15. code = '''
  16. from ..inline import GeekInlineQuery, rand
  17. from ..inline import rand, InlineQueryResultArticle
  18. from ..inline import InlineQueryResultArticle, rand
  19. from ..inline import rand, InlineQueryResultArticle, InputTextMessageContent
  20. '''
  21. print(compat(code))
  22. ```
  23. """
  24. return "\n".join([
  25. re.sub(
  26. r"^( *)from \.\.inline import (.+)$",
  27. r"\1from ..inline.types import \2",
  28. re.sub(
  29. r"^( *)from \.\.inline import rand[^,]*$",
  30. "\1from ..utils import rand",
  31. re.sub(
  32. r"^( *)from \.\.inline import rand, ?(.+)$",
  33. r"\1from ..inline.types import \2\n\1from ..utils import rand",
  34. re.sub(
  35. r"^( *)from \.\.inline import (.+), ?rand[^,]*$",
  36. r"\1from ..inline.types import \2\n\1from ..utils import"
  37. r" rand",
  38. re.sub(
  39. r"^( *)from \.\.inline import (.+), ?rand, ?(.+)$",
  40. r"\1from ..inline.types import \2, \3\n\1from ..utils"
  41. r" import rand",
  42. line.replace("GeekInlineQuery", "InlineQuery").replace(
  43. "self.inline._bot",
  44. "self.inline.bot",
  45. ),
  46. flags=re.M,
  47. ),
  48. flags=re.M,
  49. ),
  50. flags=re.M,
  51. ),
  52. flags=re.M,
  53. ),
  54. flags=re.M,
  55. )
  56. for line in code.splitlines()
  57. ])