Refactor convert() function to serialize().
Take advantage of PyYAML in python3. Change-Id: Id208c825857d0a12862923448dd31147806bdb2b
This commit is contained in:
parent
5201e84c43
commit
7edd454623
@ -1,14 +1,16 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
import datetime
|
import datetime
|
||||||
import yaml
|
|
||||||
from flask import jsonify, abort
|
from flask import jsonify, abort
|
||||||
from flask import make_response
|
from flask import make_response
|
||||||
from werkzeug import secure_filename
|
from werkzeug import secure_filename
|
||||||
|
|
||||||
from muranorepository.utils.parser import ManifestParser
|
from muranorepository.utils.parser import ManifestParser
|
||||||
|
from muranorepository.utils.parser import serialize
|
||||||
from muranorepository.utils.archiver import Archiver
|
from muranorepository.utils.archiver import Archiver
|
||||||
from muranorepository.consts import DATA_TYPES, MANIFEST
|
from muranorepository.consts import DATA_TYPES, MANIFEST
|
||||||
from muranorepository.consts import CLIENTS_DICT
|
from muranorepository.consts import CLIENTS_DICT
|
||||||
@ -217,7 +219,7 @@ def create_service(data):
|
|||||||
service_id + '-manifest.yaml')
|
service_id + '-manifest.yaml')
|
||||||
try:
|
try:
|
||||||
with open(path_to_manifest, 'w') as service_manifest:
|
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:
|
except Exception as e:
|
||||||
log.exception(e)
|
log.exception(e)
|
||||||
if os.path.exists(path_to_manifest):
|
if os.path.exists(path_to_manifest):
|
||||||
|
@ -29,21 +29,6 @@ v1_api = Blueprint('v1', __name__)
|
|||||||
CONF = cfg.CONF
|
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/<path:client_type>')
|
@v1_api.route('/client/<path:client_type>')
|
||||||
def get_archive_data(client_type):
|
def get_archive_data(client_type):
|
||||||
if client_type not in CLIENTS_DICT.keys():
|
if client_type not in CLIENTS_DICT.keys():
|
||||||
@ -235,7 +220,7 @@ def reset_caches():
|
|||||||
@v1_api.route('/admin/services/create', methods=['PUT'])
|
@v1_api.route('/admin/services/create', methods=['PUT'])
|
||||||
def create_service():
|
def create_service():
|
||||||
try:
|
try:
|
||||||
service_data = convert(json.loads(request.data))
|
service_data = json.loads(request.data)
|
||||||
except:
|
except:
|
||||||
return make_response('Unable to load json data', 500)
|
return make_response('Unable to load json data', 500)
|
||||||
resp = api.create_service(service_data)
|
resp = api.create_service(service_data)
|
||||||
@ -248,7 +233,7 @@ def update_service(service_name):
|
|||||||
api.check_service_name(service_name)
|
api.check_service_name(service_name)
|
||||||
parser = ManifestParser()
|
parser = ManifestParser()
|
||||||
try:
|
try:
|
||||||
service_data = convert(json.loads(request.data))
|
service_data = json.loads(request.data)
|
||||||
except:
|
except:
|
||||||
return make_response('Unable to load json data', 500)
|
return make_response('Unable to load json data', 500)
|
||||||
result = parser.update_service(service_name, service_data)
|
result = parser.update_service(service_name, service_data)
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
|
import sys
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
import logging as log
|
import logging as log
|
||||||
from muranorepository.manifest import Manifest
|
from muranorepository.manifest import Manifest
|
||||||
@ -21,6 +22,29 @@ from muranorepository.consts import DATA_TYPES, MANIFEST
|
|||||||
CONF = cfg.CONF
|
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):
|
class ManifestParser(object):
|
||||||
def __init__(self, manifest_directory=None):
|
def __init__(self, manifest_directory=None):
|
||||||
if not manifest_directory:
|
if not manifest_directory:
|
||||||
@ -120,6 +144,5 @@ class ManifestParser(object):
|
|||||||
service_manifest_data[key] = data[key]
|
service_manifest_data[key] = data[key]
|
||||||
|
|
||||||
with open(path_to_manifest, 'w') as manifest_file:
|
with open(path_to_manifest, 'w') as manifest_file:
|
||||||
manifest_file.write(yaml.dump(service_manifest_data,
|
manifest_file.write(serialize(service_manifest_data))
|
||||||
default_flow_style=False))
|
|
||||||
return True
|
return True
|
||||||
|
Loading…
Reference in New Issue
Block a user