Roman Tolkachyov avatar Roman Tolkachyov committed 8369444

reduce now works

Comments (0)

Files changed (5)

EasyCouch.egg-info/SOURCES.txt

 easycouch/couch.py
 easycouch/document.py
 easycouch/properties.py
+easycouch/utils.py
 easycouch/view.py

easycouch/couch.py

 from properties import BaseProperty
 from document import Document, InlineDocument
 from view import ViewDefenition, EasyRow
+from utils import LazyDocument
 
 class DocumentMapper(object):
 	def __init__(self, couch, cls_ref):
 	
 	def struct_to_obj(self, value):
 		""" Преобразовать структуру (dict, list) couch в структуру объекта
-		Так же выявляет в структурах inline-документы.
+		Так же выявляет в структурах inline-документы и ссылки.
 		"""
 		if isinstance(value, dict) and value.has_key('doctype'):
 			doctype = value['doctype']
 			value = self.couch.get_mapper(doctype).to_obj(value)
+		elif isinstance(value, dict) and value.has_key('_id'):
+			print "make lazy", value
+			value = LazyDocument(self.couch, value['_id'])
+			print "get lazy", value()
 		elif isinstance(value, dict):
 			for key in value:
 				value[key] = self.struct_to_obj(value[key])
 	
 	def struct_to_json(self, value):
 		""" Преобразует структуру из объектов python в простую json структуру"""
-		if isinstance(value, InlineDocument):
+		if isinstance(value, Document):
+			value = {'_id': value._id}
+		elif isinstance(value, LazyDocument):
+			value = {'_id': value.obj_id}
+			print "found lazy"
+		elif isinstance(value, InlineDocument):
 			value = self.couch.get_mapper(value.doctype or value.__class__.__name__).to_json(value)
 		elif isinstance(value, list):
 			i = 0
 		reduce_func = None
 		map_func = None
 		if fname[-7:] == "_reduce":
-			reduce_func = sources
+			lines[0] = "def fun(key, values, rereduce):\n"
+			reduce_func = "".join(lines)
 			fname = fname[:-7]
 		elif fname[-4:] == "_map":
 			map_func = sources

easycouch/document.py

 import uuid
 import logging
 from properties import BaseProperty
+from utils import LazyDocument
 
 class MutationStore(object):
 	""" Объект, задачей которого является отдельное хранение свойств класса и виртуальных свойств
 	
 	def __getattr__(self, name):
 		if self._state_stored and self._data.has_key(name):
+			if isinstance(self._data[name], LazyDocument):
+				self._data[name] = self._data[name]()
+				print "found lazy replaced with ", self._data[name]
 			return self._data[name]
 		else:
 			return None

easycouch/utils.py

+#coding=utf-8
+class LazyDocument(object):
+	""" "ленивый" документ
+	Подставляется на место конструкций {'_id': '...'} и при обращении возвращает реальный
+	объект с этим _id. Это позволит в дальнейшем сделать join'ы
+	"""
+	def __init__(self, couch, obj_id):
+		self.couch = couch
+		self.obj_id = obj_id
+	
+	def __call__(self):
+		print "Lazy opened", self.obj_id
+		return self.couch.get(self.obj_id)
 
 setup(
     name='EasyCouch',
-    version='0.1dev',
+    version='0.2dev',
     packages=['easycouch',],
     license='Creative Commons Attribution-Noncommercial-Share Alike license',
     long_description=open('README').read(),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.