geek.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. [
  26. re.sub(
  27. r"^( *)from \.\.inline import (.+)$",
  28. r"\1from ..inline.types import \2",
  29. re.sub(
  30. r"^( *)from \.\.inline import rand[^,]*$",
  31. "\1from ..utils import rand",
  32. re.sub(
  33. r"^( *)from \.\.inline import rand, ?(.+)$",
  34. r"\1from ..inline.types import \2\n\1from ..utils import rand",
  35. re.sub(
  36. r"^( *)from \.\.inline import (.+), ?rand[^,]*$",
  37. r"\1from ..inline.types import \2\n\1from ..utils import"
  38. r" rand",
  39. re.sub(
  40. r"^( *)from \.\.inline import (.+), ?rand, ?(.+)$",
  41. r"\1from ..inline.types import \2, \3\n\1from ..utils"
  42. r" import rand",
  43. line.replace("GeekInlineQuery", "InlineQuery").replace(
  44. "self.inline._bot",
  45. "self.inline.bot",
  46. ),
  47. flags=re.M,
  48. ),
  49. flags=re.M,
  50. ),
  51. flags=re.M,
  52. ),
  53. flags=re.M,
  54. ),
  55. flags=re.M,
  56. )
  57. for line in code.splitlines()
  58. ]
  59. )