Skip to content

Commit

Permalink
py31, py32 support
Browse files Browse the repository at this point in the history
  • Loading branch information
whtsky committed Jul 14, 2013
1 parent dca6d66 commit 019c1bf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,7 @@ language: python
python:
- 2.6
- 2.7
- 3.1
- 3.2
- 3.3
- pypy
Expand Down
10 changes: 3 additions & 7 deletions werobot/session/filestorage.py
@@ -1,13 +1,11 @@
import json

try:
import anydbm as dbm
assert dbm
except ImportError:
import dbm

from . import SessionStorage
from werobot.utils import py3k
from werobot.utils import json_loads, json_dumps


class FileStorage(SessionStorage):
Expand All @@ -16,12 +14,10 @@ def __init__(self, filename='werobot_session'):

def get(self, id):
session_json = self.db.get(id, "{}")
if py3k:
session_json = session_json.decode()
return json.loads(session_json)
return json_loads(session_json)

def set(self, id, value):
self.db[id] = json.dumps(value)
self.db[id] = json_dumps(value)

def delete(self, id):
del self.db[id]
10 changes: 3 additions & 7 deletions werobot/session/mongodbstorage.py
@@ -1,7 +1,5 @@
import json

from . import SessionStorage
from werobot.utils import py3k
from werobot.utils import json_loads, json_dumps


class MongoDBStorage(SessionStorage):
Expand All @@ -19,14 +17,12 @@ def get(self, id):
document = self._get_document(id)
if document:
session_json = document["session"]
if py3k:
session_json = session_json.decode()
return json.loads(session_json)
return json_loads(session_json)
return {}

def set(self, id, value):
document = self._get_document(id)
session = json.dumps(value)
session = json_dumps(value)
if document:
document["session"] = session
self.collection.save(document)
Expand Down
10 changes: 3 additions & 7 deletions werobot/session/redisstorage.py
@@ -1,7 +1,5 @@
import json

from . import SessionStorage
from werobot.utils import py3k
from werobot.utils import json_loads, json_dumps


class RedisStorage(SessionStorage):
Expand All @@ -17,13 +15,11 @@ def key_name(self, s):
def get(self, id):
id = self.key_name(id)
session_json = self.redis.get(id)
if py3k:
session_json = session_json.decode()
return json.loads(session_json)
return json_loads(session_json)

def set(self, id, value):
id = self.key_name(id)
self.redis.set(json.dumps(value))
self.redis.set(json_dumps(value))

def delete(self, id):
id = self.key_name(id)
Expand Down
10 changes: 10 additions & 0 deletions werobot/utils.py
Expand Up @@ -3,6 +3,7 @@
import random
import time
import logging
import json

try:
import curses
Expand Down Expand Up @@ -117,3 +118,12 @@ def format(self, record):
if record.exc_text:
formatted = formatted.rstrip() + "\n" + record.exc_text
return formatted.replace("\n", "\n ")


def json_loads(s):
s = to_unicode(s)
return json.loads(s)


def json_dumps(d):
return json.dumps(d)

0 comments on commit 019c1bf

Please sign in to comment.