Skip to content

Commit

Permalink
Allow to dump current config to file. Refs nephila#110
Browse files Browse the repository at this point in the history
  • Loading branch information
frost-nzcr4 committed Aug 11, 2015
1 parent cabfad0 commit 1c9aaf9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
10 changes: 10 additions & 0 deletions djangocms_installer/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def parse(args):
parser.add_argument('--config-file', dest='config_file', action='store',
default=None,
help='Configuration file for djangocms_installer')
parser.add_argument('--config-dump', dest='config_dump', action='store',
default=None,
help='Dump configuration file with current args')
parser.add_argument('--db', '-d', dest='db', action=DbAction,
default='sqlite://localhost/project.db',
help='Database configuration (in URL format)')
Expand Down Expand Up @@ -135,6 +138,10 @@ def parse(args):
"please choose a different one\n" % args.project_path)
sys.exit(4)

if args.config_dump and os.path.isfile(args.config_dump):
sys.stdout.write('Cannot dump because given configuration file "%s" is exists.\n' % args.config_dump)
sys.exit(8)

for item in data.CONFIGURABLE_OPTIONS:
action = parser._option_string_actions[item]
choices = default = ""
Expand Down Expand Up @@ -302,6 +309,9 @@ def parse(args):
setattr(args, 'urlconf_path',
os.path.join(args.project_directory, args.project_name, 'urls.py').strip())

if args.config_dump:
ini.dump_config_file(args.config_dump, args, parser)

return args


Expand Down
49 changes: 49 additions & 0 deletions djangocms_installer/config/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
except ImportError:
from ConfigParser import ConfigParser # Python 2.

from .data import CMS_VERSION_MATRIX, DJANGO_VERSION_MATRIX


SECTION = 'djangocms_installer'

Expand Down Expand Up @@ -44,6 +46,53 @@ def parse_config_file(parser, stdin_args):
return config_args


def dump_config_file(filename, args, parser=None):
"""Dump args to config file."""
config = ConfigParser()
config.add_section(SECTION)
if parser is None:
for attr in args:
config.set(SECTION, attr, args.attr)
else:
keys_empty_values_not_pass = (
'--extra-settings', '--languages', '--requirements', '--template', '--timezone')

#_positionals._option_string_actions
for action in parser._actions:
if action.dest in ('help', 'config_file', 'config_dump', 'project_name'):
continue

keyp = action.option_strings[0]
option_name = keyp.lstrip('-')
option_value = getattr(args, action.dest)
if any([i for i in keys_empty_values_not_pass if i in action.option_strings]):
if action.dest == 'timezone':
config.set(SECTION, option_name, option_value.zone)
elif action.dest == 'languages':
if len(option_value) == 1 and option_value[0] == 'en':
config.set(SECTION, option_name, '')
else:
config.set(SECTION, option_name, ','.join())
else:
config.set(SECTION, option_name, option_value if option_value else '')
elif action.choices == ('yes', 'no'):
config.set(SECTION, option_name, 'yes' if option_value else 'no')
elif action.dest == 'templates':
config.set(SECTION, option_name, option_value if option_value else 'no')
elif action.dest == 'cms_version':
version = 'stable' if option_value == CMS_VERSION_MATRIX['stable'] else option_value
config.set(SECTION, option_name, version)
elif action.dest == 'django_version':
version = 'stable' if option_value == DJANGO_VERSION_MATRIX['stable'] else option_value
config.set(SECTION, option_name, version)
elif action.const:
config.set(SECTION, option_name, 'true' if option_value else 'false')
else:
config.set(SECTION, option_name, str(option_value))
with open(filename, 'w') as fp:
config.write(fp)


def _convert_config_to_stdin(config, parser):
"""Convert config options to stdin args.
Expand Down

0 comments on commit 1c9aaf9

Please sign in to comment.