theme.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. """
  18. import pkg_resources
  19. import os
  20. from configobj import ConfigObj
  21. BUILTIN_THEME_DIR = pkg_resources.resource_filename('mediagoblin', 'themes')
  22. def themedata_for_theme_dir(name, theme_dir):
  23. """
  24. Given a theme directory, extract important theme information.
  25. """
  26. # open config
  27. config = ConfigObj(os.path.join(theme_dir, 'theme.ini')).get('theme', {})
  28. templates_dir = os.path.join(theme_dir, 'templates')
  29. if not os.path.exists(templates_dir):
  30. templates_dir = None
  31. assets_dir = os.path.join(theme_dir, 'assets')
  32. if not os.path.exists(assets_dir):
  33. assets_dir = None
  34. themedata = {
  35. 'name': config.get('name', name),
  36. 'description': config.get('description'),
  37. 'licensing': config.get('licensing'),
  38. 'dir': theme_dir,
  39. 'templates_dir': templates_dir,
  40. 'assets_dir': assets_dir,
  41. 'config': config}
  42. return themedata
  43. def register_themes(app_config, builtin_dir=BUILTIN_THEME_DIR):
  44. """
  45. Register all themes relevant to this application.
  46. """
  47. registry = {}
  48. def _install_themes_in_dir(directory):
  49. for themedir in os.listdir(directory):
  50. abs_themedir = os.path.join(directory, themedir)
  51. if not os.path.isdir(abs_themedir):
  52. continue
  53. themedata = themedata_for_theme_dir(themedir, abs_themedir)
  54. registry[themedir] = themedata
  55. # Built-in themes
  56. if os.path.exists(builtin_dir):
  57. _install_themes_in_dir(builtin_dir)
  58. # Installed themes
  59. theme_install_dir = app_config.get('theme_install_dir')
  60. if theme_install_dir and os.path.exists(theme_install_dir):
  61. _install_themes_in_dir(theme_install_dir)
  62. current_theme_name = app_config.get('theme')
  63. if current_theme_name \
  64. and registry.has_key(current_theme_name):
  65. current_theme = registry[current_theme_name]
  66. else:
  67. current_theme = None
  68. return registry, current_theme