Permalink
Browse files

Cleaned up the url/name params. Breaks old code. Sorry.

  • Loading branch information...
1 parent e3c96a6 commit ed3a6ea650bf4a89a057cc7f59de8f91c9b2f704 Eli Stevens committed Jul 18, 2011
Showing with 28 additions and 33 deletions.
  1. +27 −32 couchable/core.py
  2. +1 −1 couchable/testing/test_couchable.py
View
@@ -59,30 +59,6 @@
#import couchdb.mapping
"""
-import couchable
-import couchdb
-server = couchdb.Server()
-try:
- cdb = server['pykour']
-except:
- cdb = server.create('pykour')
-
-class C(couchable.Couchable):
- def __init__(self, **kwargs):
- self.__dict__.update(kwargs)
-
-c=C(foo=1,bar=2,_baz=3)
-d=C(_c=c)
-e=C(c=c, d=d, couchable_foo={'couchable_bar':9})
-couchable.store(e, cdb)
-print 'c', c._id
-print 'd', d._id
-print 'e', e._id
-i=e._id
-del c
-del d
-del e
-e=couchable.load(i, cdb)
"""
def importstr(module_str, from_=None):
@@ -197,7 +173,7 @@ class CouchableDb(object):
_obj_by_id_cache = weakref.WeakValueDictionary()
_cls2srcMd5sum_dict = {}
- def __init__(self, name=None, url='http://localhost:5984/', db=None):
+ def __init__(self, url=None, name=None, db=None):
"""
Creates a CouchableDb wrapper around a couchdb.Database object. If
the database does not yet exist, it will be created.
@@ -209,17 +185,33 @@ def __init__(self, name=None, url='http://localhost:5984/', db=None):
@type db: couchdb.Database
@param db: An instance of couchdb.Database that has already been instantiated. Overrides the name and url params.
"""
+
+ if db is not None:
+ server_url, name = db.resource.url.rstrip('/').rsplit('/', 1)
+ elif name is None:
+ if '/' in url:
+ server_url, name = url.rstrip('/').rsplit('/', 1)
+ else:
+ name = url
+ server_url = 'http://localhost:5984/'
+ else:
+ server_url = url
+ url = server_url + '/' + name
# This dance is odd due to the semantics of how WVD works.
- cache_key = (url, name)
+ cache_key = url
self._obj_by_id = self._obj_by_id_cache.get(cache_key, weakref.WeakValueDictionary())
- self._obj_by_id_cache[(url, name)] = self._obj_by_id
+ self._obj_by_id_cache[cache_key] = self._obj_by_id
- self.name = name
self.url = url
+ self.server_url = server_url
+ self.name = name
+
+ #print self.url, self.server_url, self.name
+
if db is None:
- self.server = couchdb.Server(self.url)
+ self.server = couchdb.Server(self.server_url)
try:
db = self.server[self.name]
@@ -418,7 +410,7 @@ def store(self, what, skip=None):
#print 'msg', msg
#print 'data', str(data.getvalue())
- print 'hitting bulk docs:', [x for x in [str(bulk_tup[1].get('_id', None)) for bulk_tup in bulk_list] if 'CoordinateSystem' not in x]
+ #print 'hitting bulk docs:', [x for x in [str(bulk_tup[1].get('_id', None)) for bulk_tup in bulk_list] if 'CoordinateSystem' not in x]
ret_list = self.db.update([bulk_tup[1] for bulk_tup in bulk_list])
#print ret_list
@@ -1041,9 +1033,12 @@ def _unpack(self, parent_doc, doc, loaded_dict, inst=None):
type_str, data = data.split(':', 1)
if method_str == 'append':
if type_str == 'unicode':
- return unicode(data, 'utf8')
+ if isinstance(data, unicode):
+ return data
+ else:
+ return unicode(data, 'utf8')
if type_str == 'str':
- return data
+ return str(data)
elif method_str == 'repr':
if type_str in __builtins__:
@@ -146,7 +146,7 @@ def setUp(self):
'int': 1,
'float': 2.0,
'str': 'sss',
- 'unicode': u'uuu',
+ 'unicode': u'uuu\u2603',
'set': set(['a', 'b']),
'frozenset': frozenset(['a', 'b']),
'list': [1, 2.0, 's', u'u'],

0 comments on commit ed3a6ea

Please sign in to comment.