Skip to content

Instantly share code, notes, and snippets.

@karlcow karlcow/convert-config.py Secret
Created Aug 25, 2017

Embed
What would you like to do?
convert secrets.py and environment.py into a JSON file.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Convert from the old config file to the new format."""
import fileinput
import json
config_keys = [
"OAUTH_TOKEN",
"ISSUES_REPO_URI",
"GITHUB_CLIENT_ID",
"GITHUB_CLIENT_SECRET",
"GITHUB_CALLBACK_URL",
"SECRET_KEY",
"HOOK_SECRET_KEY",
"UPLOADS_DEFAULT_DEST",
"UPLOADS_DEFAULT_URL",
"BACKUP_DEFAULT_DEST"]
config = {}
def grab_key(line):
"""Extract the keys from the config files."""
config_key, config_line = line.split('=')
config_value = config_line.strip()
if not config_value == "''":
if ' or ' in config_value:
config_value = config_value.split(' ')[2]
if ' + ' in config_value:
config_value = config_value.split(' ')[2]
config_value = config_value.strip("'")
else:
config_value = ''
if config_key.strip() == 'env.BACKUP_DEFAULT_DEST':
return None, None
else:
return config_key.strip(), config_value
for line in fileinput.input():
for key in config_keys:
if key in line:
config_key, config_value = grab_key(line.strip())
print(config_key, config_value)
if config_key in config and not config[config_key] == '':
continue
elif config_key is None:
continue
else:
if config_key == 'OAUTH_TOKEN':
config[config_key] = {"oauthkey": config_value}
else:
config[config_key] = config_value
print(json.dumps(config, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.