1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
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
25 def theme_parser_setup(subparser
):
26 theme_subparsers
= subparser
.add_subparsers(
28 help=u
'Theme sub-commands')
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')
36 theme_subparsers
.add_parser(
39 u
"Link the currently installed theme's assets "
40 u
"to the served theme asset directory"))
47 def link_assets(theme
, link_dir
, printer
=simple_printer
):
49 Returns a list of string of text telling the user what we did
50 which should be printable.
52 link_dir
= link_dir
.rstrip(os
.path
.sep
)
53 link_parent_dir
= os
.path
.dirname(link_dir
)
58 printer(_("Cannot link theme... no theme set\n"))
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
):
70 if theme
.get('assets_dir') is None:
71 printer(_("No asset directory for this theme\n"))
72 if _maybe_unlink_link_dir():
74 _("However, old link directory symlink found; removed.\n"))
77 _maybe_unlink_link_dir()
79 # make the link directory parent dirs if necessary
80 if not os
.path
.lexists(link_parent_dir
):
81 os
.makedirs(link_parent_dir
)
84 theme
['assets_dir'].rstrip(os
.path
.sep
),
86 printer("Linked the theme's asset directory:\n %s\nto:\n %s\n" % (
87 theme
['assets_dir'], link_dir
))
90 def install_theme(install_dir
, themefile
):
98 def assetlink_command(args
):
100 Link the asset directory of the currently installed theme
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'])
107 def install_command(args
):
109 Handle the 'install this theme' subcommand
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
)
117 'assetlink': assetlink_command
,
118 'install': install_command
}
122 SUBCOMMANDS
[args
.subcommand
](args
)