themes_tool.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Support script for setup.py
  2. # - List available and valid themes by key (theme dir name)
  3. # - Compile theme resource
  4. # - Get list of all theme files
  5. import os
  6. import sys
  7. import argparse
  8. import pathlib
  9. # This makes it so that it should be executed from searx-qt source root:
  10. # python utils/themes_tool.py
  11. pwd = str(pathlib.Path().resolve())
  12. sys.path.insert(0, pwd)
  13. from searxqt.themes import Themes # noqa: E402
  14. THEMES_PATH = "./themes/"
  15. def findThemes(lookForCompiledResource=False):
  16. return Themes.findThemes(
  17. THEMES_PATH,
  18. lookForCompiledResource=lookForCompiledResource
  19. )
  20. def compileThemes():
  21. themes = findThemes(lookForCompiledResource=False)
  22. for theme in themes:
  23. cmd = 'utils/rcc.sh {root} {qrc}'.format(
  24. root=theme.path,
  25. qrc=theme.icons
  26. )
  27. status = os.WEXITSTATUS(os.system(cmd))
  28. if status == 0:
  29. print('Compiled resource for theme', theme.key)
  30. return True
  31. print('Failed to compile resource for theme', theme.key)
  32. return False
  33. def getTheme(themes, themeKey):
  34. """ Get single theme by key.
  35. @param themes: List with themes to pick from.
  36. @type themes: list
  37. @param themeKey: Key (dir name) of the theme to pick.
  38. @type themeKey: str
  39. """
  40. for theme in themes:
  41. if theme.key == themeKey:
  42. return theme
  43. return None
  44. def getThemes(themeKey=None):
  45. """ Pick one or all valid theme(s) depending on whether 'themeKey' is None
  46. or not.
  47. This function is used when the 'key' argument is set from cli.
  48. @param themeKey: Key (dir name) of the theme to pick.
  49. @type themeKey: None or str
  50. """
  51. themes = findThemes()
  52. if themeKey is not None:
  53. # Select single theme
  54. theme = getTheme(themes, themeKey)
  55. if not theme:
  56. print('Theme \'{0}\' not found.'.format(themeKey))
  57. return []
  58. themes = [theme]
  59. return themes
  60. def listThemes():
  61. """ Prints all valid available theme keys (dir names)
  62. """
  63. for theme in findThemes():
  64. print(theme.key)
  65. def compileResource(root, qrc):
  66. cmd = 'utils/rcc.sh {root} {qrc}'.format(
  67. root=root,
  68. qrc=qrc
  69. )
  70. status = os.WEXITSTATUS(os.system(cmd))
  71. if status == 0:
  72. print('Compiled resource for theme', root)
  73. return True
  74. print('Failed to compile resource for theme', root)
  75. return False
  76. def makeTheme(themeKey=None):
  77. """ Compile resource file for one or all themes.
  78. """
  79. themes = getThemes(themeKey)
  80. status = True
  81. for theme in themes:
  82. if not compileResource(theme.path, theme.icons):
  83. print(
  84. 'Failed to compile resource for theme \'{0}\''
  85. .format(theme.key)
  86. )
  87. status = False
  88. return status
  89. def printFilesForTheme(themeKey=None):
  90. """ Prints all theme files for a single theme (when 'themeKey' is not None
  91. else it will print all theme files for all themes. (Absolute paths)
  92. """
  93. themes = getThemes(themeKey)
  94. for theme in themes:
  95. if not theme:
  96. print('Theme \'{0}\' not found.'.format(themeKey))
  97. return False
  98. print(
  99. os.path.abspath(os.path.join(pwd, theme.iconsPath(compiled=True)))
  100. )
  101. print(os.path.abspath(os.path.join(pwd, theme.fullCssPath())))
  102. print(os.path.abspath(os.path.join(pwd, theme.failCssPath())))
  103. print(os.path.abspath(os.path.join(pwd, theme.resultsCssPath())))
  104. print(os.path.abspath(os.path.join(pwd, theme.path, 'manifest.json')))
  105. return True
  106. argsParser = argparse.ArgumentParser(description="searx-qt themes tool")
  107. subParsers = argsParser.add_subparsers(
  108. dest="cmd",
  109. help="Command",
  110. required=True
  111. )
  112. # list
  113. listParser = subParsers.add_parser("list", help="List themes")
  114. # make
  115. makeParser = subParsers.add_parser("make", help="Make themes")
  116. makeParser.add_argument(
  117. 'key',
  118. help='Theme key (theme dir name)',
  119. type=str,
  120. default='all',
  121. nargs='?'
  122. )
  123. # files
  124. filesParser = subParsers.add_parser("files", help="Get files for theme")
  125. filesParser.add_argument(
  126. 'key',
  127. help='Theme key (theme dir name)',
  128. type=str,
  129. default='all',
  130. nargs='?'
  131. )
  132. args = argsParser.parse_args()
  133. if args.cmd == 'list':
  134. listThemes()
  135. elif args.cmd == 'make':
  136. themeKey = None
  137. if args.key != 'all':
  138. themeKey = args.key
  139. if not makeTheme(themeKey):
  140. sys.exit(1)
  141. elif args.cmd == 'files':
  142. themeKey = None
  143. if args.key != 'all':
  144. themeKey = args.key
  145. if not printFilesForTheme(themeKey):
  146. sys.exit(1)
  147. sys.exit(0)