schema_cache: Make handling of string compatible with python3

https://fedorahosted.org/freeipa/ticket/6559

Reviewed-By: Martin Basti <mbasti@redhat.com>
This commit is contained in:
David Kupka 2016-12-14 17:19:52 +01:00 committed by Martin Basti
parent 0ff12de338
commit 388ed93935

View File

@ -6,6 +6,7 @@ import collections
import contextlib import contextlib
import errno import errno
import fcntl import fcntl
import io
import json import json
import os import os
import sys import sys
@ -373,7 +374,7 @@ class Schema(object):
self._dict = {} self._dict = {}
self._namespaces = {} self._namespaces = {}
self._help = None self._help = None
self._file = six.StringIO() self._file = six.BytesIO()
for ns in self.namespaces: for ns in self.namespaces:
self._dict[ns] = {} self._dict[ns] = {}
@ -407,7 +408,7 @@ class Schema(object):
def _open(self, filename, mode): def _open(self, filename, mode):
path = os.path.join(self._DIR, filename) path = os.path.join(self._DIR, filename)
with open(path, mode) as f: with io.open(path, mode) as f:
if mode.startswith('r'): if mode.startswith('r'):
fcntl.flock(f, fcntl.LOCK_SH) fcntl.flock(f, fcntl.LOCK_SH)
else: else:
@ -454,7 +455,7 @@ class Schema(object):
def _read_schema(self, fingerprint): def _read_schema(self, fingerprint):
self._file.truncate(0) self._file.truncate(0)
with self._open(fingerprint, 'r') as f: with self._open(fingerprint, 'rb') as f:
self._file.write(f.read()) self._file.write(f.read())
with zipfile.ZipFile(self._file, 'r') as schema: with zipfile.ZipFile(self._file, 'r') as schema:
@ -504,21 +505,24 @@ class Schema(object):
ns = value ns = value
for member in ns: for member in ns:
path = '{}/{}'.format(key, member) path = '{}/{}'.format(key, member)
schema.writestr(path, json.dumps(ns[member])) schema.writestr(path,
json.dumps(ns[member]).encode('utf-8'))
else: else:
schema.writestr(key, json.dumps(value)) schema.writestr(key, json.dumps(value).encode('utf-8'))
schema.writestr('_help', schema.writestr(
json.dumps(self._generate_help(self._dict))) '_help',
json.dumps(self._generate_help(self._dict)).encode('utf-8')
)
self._file.seek(0) self._file.seek(0)
with self._open(fingerprint, 'w') as f: with self._open(fingerprint, 'wb') as f:
f.truncate(0) f.truncate(0)
f.write(self._file.read()) f.write(self._file.read())
def _read(self, path): def _read(self, path):
with zipfile.ZipFile(self._file, 'r') as zf: with zipfile.ZipFile(self._file, 'r') as zf:
return json.loads(zf.read(path)) return json.loads(zf.read(path).decode('utf-8'))
def read_namespace_member(self, namespace, member): def read_namespace_member(self, namespace, member):
value = self._dict[namespace][member] value = self._dict[namespace][member]