diff --git a/muranorepository/api/utils.py b/muranorepository/api/utils.py index b75325f..77e241f 100644 --- a/muranorepository/api/utils.py +++ b/muranorepository/api/utils.py @@ -1,14 +1,16 @@ + import os import shutil import re import tempfile import datetime -import yaml + from flask import jsonify, abort from flask import make_response from werkzeug import secure_filename from muranorepository.utils.parser import ManifestParser +from muranorepository.utils.parser import serialize from muranorepository.utils.archiver import Archiver from muranorepository.consts import DATA_TYPES, MANIFEST from muranorepository.consts import CLIENTS_DICT @@ -217,7 +219,7 @@ def create_service(data): service_id + '-manifest.yaml') try: with open(path_to_manifest, 'w') as service_manifest: - service_manifest.write(yaml.dump(data, default_flow_style=False)) + service_manifest.write(serialize(data)) except Exception as e: log.exception(e) if os.path.exists(path_to_manifest): diff --git a/muranorepository/api/v1.py b/muranorepository/api/v1.py index aaba184..ce239e6 100644 --- a/muranorepository/api/v1.py +++ b/muranorepository/api/v1.py @@ -29,21 +29,6 @@ v1_api = Blueprint('v1', __name__) CONF = cfg.CONF -def convert(input): - """ - Convert unicode to regular strings - """ - if isinstance(input, dict): - return dict([(convert(key), convert(value)) - for key, value in input.iteritems()]) - elif isinstance(input, list): - return [convert(element) for element in input] - elif isinstance(input, unicode): - return input.encode('utf-8') - else: - return input - - @v1_api.route('/client/') def get_archive_data(client_type): if client_type not in CLIENTS_DICT.keys(): @@ -235,7 +220,7 @@ def reset_caches(): @v1_api.route('/admin/services/create', methods=['PUT']) def create_service(): try: - service_data = convert(json.loads(request.data)) + service_data = json.loads(request.data) except: return make_response('Unable to load json data', 500) resp = api.create_service(service_data) @@ -248,7 +233,7 @@ def update_service(service_name): api.check_service_name(service_name) parser = ManifestParser() try: - service_data = convert(json.loads(request.data)) + service_data = json.loads(request.data) except: return make_response('Unable to load json data', 500) result = parser.update_service(service_name, service_data) diff --git a/muranorepository/utils/parser.py b/muranorepository/utils/parser.py index b997e5d..20ae338 100644 --- a/muranorepository/utils/parser.py +++ b/muranorepository/utils/parser.py @@ -14,6 +14,7 @@ import os import yaml +import sys from oslo.config import cfg import logging as log from muranorepository.manifest import Manifest @@ -21,6 +22,29 @@ from muranorepository.consts import DATA_TYPES, MANIFEST CONF = cfg.CONF +def serialize(data): + def convert(data): + """ + Convert unicode to regular strings. + + Needed in python 2.x to handle differences in str/unicode processing. + In python 3.x this can be done much more easily: + yaml.dump(data, allow_unicode=True, encoding='utf-8') call would + convert all unicode to utf-8 encoded byte-strings. + """ + if isinstance(data, dict): + return dict([(convert(key), convert(value)) + for key, value in data.iteritems()]) + elif isinstance(data, list): + return [convert(element) for element in input] + elif isinstance(data, unicode): + return data.encode('utf-8') + else: + return data + + return yaml.dump(convert(data), default_flow_style=False) + + class ManifestParser(object): def __init__(self, manifest_directory=None): if not manifest_directory: @@ -120,6 +144,5 @@ class ManifestParser(object): service_manifest_data[key] = data[key] with open(path_to_manifest, 'w') as manifest_file: - manifest_file.write(yaml.dump(service_manifest_data, - default_flow_style=False)) + manifest_file.write(serialize(service_manifest_data)) return True