plugins/api: webob.Response -> werkzeug.Response
[mediagoblin.git] / mediagoblin / gmg_commands / theme.py
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 import os
18
19 from mediagoblin.init import setup_global_and_app_config
20 from mediagoblin.tools.theme import register_themes
21 from mediagoblin.tools.translate import pass_to_ugettext as _
22 from mediagoblin.tools.common import simple_printer
23
24
25 def theme_parser_setup(subparser):
26 theme_subparsers = subparser.add_subparsers(
27 dest=u"subcommand",
28 help=u'Theme sub-commands')
29
30 # Install command
31 install_parser = theme_subparsers.add_parser(
32 u'install', help=u'Install a theme to this mediagoblin instance')
33 install_parser.add_argument(
34 u'themefile', help=u'The theme archive to be installed')
35
36 theme_subparsers.add_parser(
37 u'assetlink',
38 help=(
39 u"Link the currently installed theme's assets "
40 u"to the served theme asset directory"))
41
42
43 ###########
44 # Utilities
45 ###########
46
47 def link_assets(theme, link_dir, printer=simple_printer):
48 """
49 Returns a list of string of text telling the user what we did
50 which should be printable.
51 """
52 link_dir = link_dir.rstrip(os.path.sep)
53 link_parent_dir = os.path.dirname(link_dir)
54
55 results = []
56
57 if theme is None:
58 printer(_("Cannot link theme... no theme set\n"))
59 return results
60
61 def _maybe_unlink_link_dir():
62 """unlink link directory if it exists"""
63 if os.path.lexists(link_dir) \
64 and os.path.islink(link_dir):
65 os.unlink(link_dir)
66 return True
67
68 return results
69
70 if theme.get('assets_dir') is None:
71 printer(_("No asset directory for this theme\n"))
72 if _maybe_unlink_link_dir():
73 printer(
74 _("However, old link directory symlink found; removed.\n"))
75 return results
76
77 _maybe_unlink_link_dir()
78
79 # make the link directory parent dirs if necessary
80 if not os.path.lexists(link_parent_dir):
81 os.makedirs(link_parent_dir)
82
83 os.symlink(
84 theme['assets_dir'].rstrip(os.path.sep),
85 link_dir)
86 printer("Linked the theme's asset directory:\n %s\nto:\n %s\n" % (
87 theme['assets_dir'], link_dir))
88
89
90 def install_theme(install_dir, themefile):
91 pass # TODO ;)
92
93
94 #############
95 # Subcommands
96 #############
97
98 def assetlink_command(args):
99 """
100 Link the asset directory of the currently installed theme
101 """
102 global_config, app_config = setup_global_and_app_config(args.conf_file)
103 theme_registry, current_theme = register_themes(app_config)
104 link_assets(current_theme, app_config['theme_linked_assets_dir'])
105
106
107 def install_command(args):
108 """
109 Handle the 'install this theme' subcommand
110 """
111 global_config, app_config = setup_global_and_app_config(args.conf_file)
112 install_dir = app_config['theme_install_dir']
113 install_theme(install_dir, args.themefile)
114
115
116 SUBCOMMANDS = {
117 'assetlink': assetlink_command,
118 'install': install_command}
119
120
121 def theme(args):
122 SUBCOMMANDS[args.subcommand](args)