Skip to content

Commit

Permalink
Merge pull request #5 from gunthercox/chatterbot_352
Browse files Browse the repository at this point in the history
Support saving unescaped unicode characters to the database
  • Loading branch information
gunthercox authored Oct 30, 2016
2 parents 9bcca52 + e4c37c5 commit 087215b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
6 changes: 3 additions & 3 deletions jsondb/__init__.py
Original file line number Diff line number Diff line change
@@ -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'

34 changes: 28 additions & 6 deletions jsondb/compat.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]

Expand All @@ -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)
6 changes: 3 additions & 3 deletions jsondb/file_writer.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 087215b

Please sign in to comment.