diff --git a/jsondb/__init__.py b/jsondb/__init__.py index 9d5a48e..561daa4 100644 --- a/jsondb/__init__.py +++ b/jsondb/__init__.py @@ -1,6 +1,6 @@ from .db import Database -__version__ = "0.1.2" -__maintainer__ = "Gunther Cox" -__email__ = "gunthercx@gmail.com" +__version__ = '0.1.3' +__maintainer__ = 'Gunther Cox' +__email__ = 'gunthercx@gmail.com' diff --git a/jsondb/compat.py b/jsondb/compat.py index 955fe28..a49b087 100644 --- a/jsondb/compat.py +++ b/jsondb/compat.py @@ -1,10 +1,10 @@ +import codecs import sys +import io -''' -Use the faster cjson library if it is available -''' try: + # Use the faster cjson library if it is available import cjson as json json_encode = json.encode @@ -18,17 +18,19 @@ def encode(value): - return json_encode(value) + value = json_encode(value, ensure_ascii=False) + if sys.version < '3': + return unicode(value) + return value def decode(value): - return json_decode(value) + return json_decode(value, encoding='utf-8') if sys.version < '3': # Python 2 and 3 unicode string compatability - import codecs def u(x): return codecs.unicode_escape_decode(x)[0] @@ -42,3 +44,23 @@ def u(x): # Dictionary iteration compatibility def iteritems(dictionary): return dictionary.items() + + +def open_file_for_reading(*args, **kwargs): + if sys.version < '3': + kwargs['mode'] = 'rb+' + else: + kwargs['encoding'] = 'utf-8' + kwargs['mode'] = 'r+' + + return io.open(*args, **kwargs) + + +def open_file_for_writing(*args, **kwargs): + if sys.version < '3': + kwargs['mode'] = 'w+' + else: + kwargs['encoding'] = 'utf-8' + kwargs['mode'] = 'w+' + + return io.open(*args, **kwargs) diff --git a/jsondb/file_writer.py b/jsondb/file_writer.py index 7bf724a..c1d06fb 100644 --- a/jsondb/file_writer.py +++ b/jsondb/file_writer.py @@ -1,4 +1,4 @@ -from .compat import decode, encode +from .compat import decode, encode, open_file_for_reading, open_file_for_writing def read_data(file_path): @@ -9,7 +9,7 @@ def read_data(file_path): if not is_valid(file_path): write_data(file_path, {}) - db = open(file_path, "r+") + db = open_file_for_reading(file_path) content = db.read() obj = decode(content) @@ -22,7 +22,7 @@ def write_data(path, obj): """ Writes to a file and returns the updated file content. """ - with open(path, "w+") as db: + with open_file_for_writing(path) as db: db.write(encode(obj)) return obj