themes.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.cli.themes
  4. ~~~~~~~~~~~~~~~~~~
  5. This module contains all theme commands.
  6. :copyright: (c) 2016 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import sys
  10. import os
  11. import shutil
  12. import click
  13. from flask import current_app
  14. from flask_themes2 import get_themes_list, get_theme
  15. from flaskbb.cli.main import flaskbb
  16. from flaskbb.cli.utils import check_cookiecutter, validate_theme
  17. from flaskbb.utils.settings import flaskbb_config
  18. @flaskbb.group()
  19. def themes():
  20. """Themes command sub group."""
  21. pass
  22. @themes.command("list")
  23. def list_themes():
  24. """Lists all installed themes."""
  25. click.secho("[+] Listing all installed themes...", fg="cyan")
  26. active_theme = get_theme(flaskbb_config['DEFAULT_THEME'])
  27. available_themes = set(get_themes_list()) - set([active_theme])
  28. click.secho("[+] Active Theme:", fg="blue", bold=True)
  29. click.secho(" - {} (version {})".format(
  30. active_theme.name, active_theme.version), bold=True
  31. )
  32. click.secho("[+] Available Themes:", fg="yellow", bold=True)
  33. for theme in available_themes:
  34. click.secho(" - {} (version {})".format(
  35. theme.name, theme.version), bold=True
  36. )
  37. @themes.command("new")
  38. @click.argument("theme_identifier", callback=check_cookiecutter)
  39. @click.option("--template", "-t", type=click.STRING,
  40. default="https://github.com/sh4nks/cookiecutter-flaskbb-theme",
  41. help="Path to a cookiecutter template or to a valid git repo.")
  42. def new_theme(theme_identifier, template):
  43. """Creates a new theme based on the cookiecutter theme
  44. template. Defaults to this template:
  45. https://github.com/sh4nks/cookiecutter-flaskbb-theme.
  46. It will either accept a valid path on the filesystem
  47. or a URL to a Git repository which contains the cookiecutter template.
  48. """
  49. from cookiecutter.main import cookiecutter
  50. out_dir = os.path.join(current_app.root_path, "themes")
  51. click.secho("[+] Creating new theme {}".format(theme_identifier),
  52. fg="cyan")
  53. cookiecutter(template, output_dir=out_dir)
  54. click.secho("[+] Done. Created in {}".format(out_dir),
  55. fg="green", bold=True)
  56. @themes.command("remove")
  57. @click.argument("theme_identifier")
  58. @click.option("--force", "-f", default=False, is_flag=True,
  59. help="Removes the theme without asking for confirmation.")
  60. def remove_theme(theme_identifier, force):
  61. """Removes a theme from the filesystem."""
  62. validate_theme(theme_identifier)
  63. if not force and not \
  64. click.confirm(click.style("Are you sure?", fg="magenta")):
  65. sys.exit(0)
  66. theme = get_theme(theme_identifier)
  67. click.secho("[+] Removing theme from filesystem...", fg="cyan")
  68. shutil.rmtree(theme.path, ignore_errors=False, onerror=None)