Files
@ a8b9f2d68e7d
Branch filter:
Location: kallithea/kallithea/lib/paster_commands/make_config.py
a8b9f2d68e7d
3.8 KiB
text/x-python
make-config: allow configuration of any ini value
Custom values can now be specified like:
gearbox make-config my.ini host=8.8.8.8 '[handler_console]' formatter=color_formatter '[app:main]' i18n.lang=zu
Custom values can now be specified like:
gearbox make-config my.ini host=8.8.8.8 '[handler_console]' formatter=color_formatter '[app:main]' i18n.lang=zu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | # -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.lib.paster_commands.make_config
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make-config gearbox command for Kallithea
:license: GPLv3, see LICENSE.md for more details.
"""
import os
import sys
import uuid
import argparse
from collections import defaultdict
import mako.exceptions
TMPL = 'template.ini.mako'
here = os.path.dirname(os.path.abspath(__file__))
from kallithea.lib.paster_commands.common import BasePasterCommand
from kallithea.lib import inifile
class Command(BasePasterCommand):
"""Kallithea: Create a new config file
make-config is part of a two-phase installation process (the
second phase is setup-app). make-config creates a bare configuration
file (possibly filling in defaults from the extra
variables you give).
The first key=value arguments are used to customize the Mako variables that
are shown with --show-defaults. The following settings will be
patched/inserted in the [app:main] section ... until another section name
is specified and change where the following values go.
"""
takes_config_file = False # at least not an existing one ...
def take_action(self, args):
_run(args)
def get_parser(self, prog_name):
parser = super(Command, self).get_parser(prog_name)
parser.add_argument('config_file', nargs='?',
help='application config file to write')
parser.add_argument('custom', nargs=argparse.REMAINDER,
help='custom values for the config file')
parser.add_argument('--show-defaults', action='store_true',
help="Show the default values that can be overridden")
return parser
def _run(args):
if args.config_file is None:
if not args.show_defaults:
raise ValueError("Missing argument: config_file")
else:
if args.show_defaults:
raise ValueError("Can't specify both config_file and --show_defaults")
mako_variable_values = {}
ini_settings = defaultdict(dict)
section_name = None
for parameter in args.custom:
parts = parameter.split('=', 1)
if len(parts) == 1 and parameter.startswith('[') and parameter.endswith(']'):
section_name = parameter
elif len(parts) == 2:
key, value = parts
if section_name is None and key in inifile.default_variables:
mako_variable_values[key] = value
else:
if section_name is None:
section_name = '[app:main]'
ini_settings[section_name][key] = value
else:
raise ValueError("Invalid name=value parameter %r" % parameter)
if args.show_defaults:
for key, value in inifile.default_variables.items():
value = mako_variable_values.get(key, value)
print '%s=%s' % (key, value)
sys.exit(0)
# use default that cannot be replaced
mako_variable_values.update({
'uuid': lambda: uuid.uuid4().hex,
})
try:
config_file = os.path.abspath(args.config_file)
inifile.create(config_file, mako_variable_values, ini_settings)
print 'Wrote new config file in %s' % config_file
except Exception:
print mako.exceptions.text_error_template().render()
|