@ -15,13 +15,13 @@ else:
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					class  Couch : 
 
					 
					 
					 
					class  Couch : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						""" 
 
					 
					 
					 
						""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						This  class  is  a  wrapper  for  read / write  against  couchdb .  The  class  captures  common  operations  for  read / write . 
 
					 
					 
					 
						This  class  is  a  wrapper  for  read / write  against  couchdb .  The  class  captures  common  operations  for  read / write . 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							@param 	url 		host  &  port  reference 
 
					 
					 
					 
							@param 	url 		host  &  port  reference  default  http : / / localhost : 5984  
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							@param 	doc 		user  id  involved 
 
					 
					 
					 
							@param 	doc 		user  id  involved 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							@param 	dbname 		database  name  ( target ) 
 
					 
					 
					 
							@param 	dbname 		database  name  ( target ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						""" 
 
					 
					 
					 
						""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						def  __init__ ( self , * * args ) : 
 
					 
					 
					 
						def  __init__ ( self , * * args ) : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							url  		=  args [ ' url ' ] 
 
					 
					 
					 
							url  		=  args [ ' url ' ]  if  ' url '  in  args  else  ' http://localhost:5984 '  
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							self . u id 	=  args [ ' doc ' ] 
 
					 
					 
					 
							self . _ id 	=  args [ ' doc ' ] 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							dbname 		=  args [ ' dbname ' ] 
 
					 
					 
					 
							dbname 		=  args [ ' dbname ' ] 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							if  ' username '  not  in  args  and  ' password '  not  in  args  : 
 
					 
					 
					 
							if  ' username '  not  in  args  and  ' password '  not  in  args  : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								self . server  	=  cloudant . CouchDB ( None , None , url = url ) 
 
					 
					 
					 
								self . server  	=  cloudant . CouchDB ( None , None , url = url ) 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -34,9 +34,9 @@ class Couch:
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								# 
 
					 
					 
					 
								# 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								# @TODO Check if the database exists ... 
 
					 
					 
					 
								# @TODO Check if the database exists ... 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								# 
 
					 
					 
					 
								# 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								doc  =  cloudant . document . Document ( self . dbase , self . uid)  #self.dbase.get(self.u  id)
 
					 
					 
					 
								doc  =  cloudant . document . Document ( self . dbase , self . _id)  #self.dbase.get(self._  id)
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
								if  not  doc . exists ( ) : 
 
					 
					 
					 
								if  not  doc . exists ( ) : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
									doc  =  self . dbase . create_document ( { " _id " : self . u id} ) 
 
					 
					 
					 
									doc  =  self . dbase . create_document ( { " _id " : self . _ id} ) 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
									doc . save ( ) 
 
					 
					 
					 
									doc . save ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							else : 
 
					 
					 
					 
							else : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								self . dbase  =  None 
 
					 
					 
					 
								self . dbase  =  None 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -51,8 +51,8 @@ class Couch:
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# At this point we are sure that the server is connected 
 
					 
					 
					 
							# At this point we are sure that the server is connected 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# We are also sure that the database actually exists 
 
					 
					 
					 
							# We are also sure that the database actually exists 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# 
 
					 
					 
					 
							# 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							doc  =  cloudant . document . Document ( self . dbase , self . u id) 
 
					 
					 
					 
							doc  =  cloudant . document . Document ( self . dbase , self . _ id) 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							# q = self.dbase.all_docs(key=self. u id)['rows'] 
 
					 
					 
					 
							# q = self.dbase.all_docs(key=self. _ id)['rows'] 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							# if not q : 
 
					 
					 
					 
							# if not q : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							if  not  doc . exists ( ) : 
 
					 
					 
					 
							if  not  doc . exists ( ) : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								return  False 
 
					 
					 
					 
								return  False 
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -107,7 +107,7 @@ class CouchReader(Couch,Reader):
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						# 	# We insure the document of the given user has the requested attachment. 
 
					 
					 
					 
						# 	# We insure the document of the given user has the requested attachment. 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						# 	#  
 
					 
					 
					 
						# 	#  
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
							
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						# 	doc = self.dbase.get(self. u id)
 
					 
					 
					 
						# 	doc = self.dbase.get(self. _ id)
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
							
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						# 	if '_attachments' in doc: 
 
					 
					 
					 
						# 	if '_attachments' in doc: 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						# 		r = self.filename in doc['_attachments'].keys() 
 
					 
					 
					 
						# 		r = self.filename in doc['_attachments'].keys() 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -120,8 +120,8 @@ class CouchReader(Couch,Reader):
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# 
 
					 
					 
					 
							# 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# @TODO Need to get this working ... 
 
					 
					 
					 
							# @TODO Need to get this working ... 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# 
 
					 
					 
					 
							# 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . u id) 
 
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . _ id) 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							# content = self.dbase.fetch_attachment(self. u id,self.filename).split('\n') ;
 
					 
					 
					 
							# content = self.dbase.fetch_attachment(self. _ id,self.filename).split('\n') ;
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							content  =  self . get_attachment ( self . filename ) 
 
					 
					 
					 
							content  =  self . get_attachment ( self . filename ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							for  row  in  content : 
 
					 
					 
					 
							for  row  in  content : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								yield  row 
 
					 
					 
					 
								yield  row 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -132,9 +132,9 @@ class CouchReader(Couch,Reader):
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							else : 
 
					 
					 
					 
							else : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								return  self . basic_read ( ) 
 
					 
					 
					 
								return  self . basic_read ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						def  basic_read ( self ) : 
 
					 
					 
					 
						def  basic_read ( self ) : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . u id) 
 
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . _ id) 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
							
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# document = self.dbase.get(self. u id)
 
					 
					 
					 
							# document = self.dbase.get(self. _ id)
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							if  document . exists ( )  : 			
 
					 
					 
					 
							if  document . exists ( )  : 			
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								document . fetch ( ) 
 
					 
					 
					 
								document . fetch ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								document  =  dict ( document ) 
 
					 
					 
					 
								document  =  dict ( document ) 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -157,32 +157,62 @@ class CouchWriter(Couch,Writer):
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							""" 
 
					 
					 
					 
							""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							Couch . __init__ ( self , * * args ) 
 
					 
					 
					 
							Couch . __init__ ( self , * * args ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
						def  set  ( self , info ) : 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						def  write ( self , * * params ) : 
 
					 
					 
					 
							document   =  cloudand . document . Document ( self . dbase , self . _id ) 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							if  document . exists ( )  : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								keys  =  list ( set ( document . keys ( ) )  -  set ( [ ' _id ' , ' _rev ' , ' _attachments ' ] ) ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								for  id  in  keys  : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									document . field_set ( document , id , None ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								for  id  in  args  : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									value  =  args [ id ] 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									document . field_set ( document , id , value ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								document . save ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								pass 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							else : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								_document  =  dict ( { " _id " : self . _id } , * * args ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								document . create_document ( _document ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						def  write ( self , info ) : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							""" 
 
					 
					 
					 
							""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								write  a  given  attribute  to  a  document  database 
 
					 
					 
					 
								write  a  given  attribute  to  a  document  database 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								@param 	label 	scope  of  the  row  repair | broken | fixed | stats 
 
					 
					 
					 
								@info 	object  to  be  written  to  the  to  an  attribute .  this  
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								@param 	row 	row  to  be  written 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							""" 
 
					 
					 
					 
							""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
							
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# document = self.dbase.get(self.uid) 
 
					 
					 
					 
							# document = self.dbase.get(self. _ id)
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . uid )  #.get(self.uid) 
 
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . _id)  #.get(self._  id)
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							if  document . exists ( )  is  False  : 
 
					 
					 
					 
							if  document . exists ( )  is  False  : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								document  =  self . dbase . create_document ( { " _id " : self . uid } ) 
 
					 
					 
					 
								document  =  self . dbase . create_document ( { " _id " : self . _id } ) 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							label  =  params [ ' label ' ] 
 
					 
					 
					 
							# label = params['label'] 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							row 	=  params [ ' row ' ] 
 
					 
					 
					 
							# row	= params['row'] 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							if  label  not  in  document  : 
 
					 
					 
					 
							# if label not in document : 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								document [ label ]  =  [ ] 
 
					 
					 
					 
							# 	document[label] = [] 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							document [ label ] . append ( row ) 
 
					 
					 
					 
							# document[label].append(row) 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							for  key  in  info  : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								if  key  in  document  and  type ( document [ key ] )  ==  list  : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									document [ key ]  + =  info [ key ] 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								else : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									document [ key ]  =  info [ key ] 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							document . save ( ) 
 
					 
					 
					 
							document . save ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# self.dbase.bulk_docs([document]) 
 
					 
					 
					 
							# self.dbase.bulk_docs([document]) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# self.dbase.save_doc(document) 
 
					 
					 
					 
							# self.dbase.save_doc(document) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
								
 
					 
					 
					 
						
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
						def  upload ( self , * * args ) : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							: param 	name 	name  of  the  file  to  be  uploaded 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							: param 	data 	content  of  the  file  ( binary  or  text ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							: param 	content_type 	( default ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							mimetype  =  args [ ' content_type ' ]  if  ' content_type '  in  args  else  ' text/plain ' 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . uid ) 		
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							document . put_attachment ( self . dbase , args [ ' filename ' ] , mimetype , args [ ' content ' ] ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							document . save ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						def  archive ( self , params = None ) : 
 
					 
					 
					 
						def  archive ( self , params = None ) : 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							""" 
 
					 
					 
					 
							""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							This  function  will  archive  the  document  onto  itself .  		
 
					 
					 
					 
							This  function  will  archive  the  document  onto  itself .  		
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							""" 
 
					 
					 
					 
							""" 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# document = self.dbase.all_docs(self.uid,include_docs=True) 
 
					 
					 
					 
							# document = self.dbase.all_docs(self. _ id,include_docs=True)
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . filename ) 
 
					 
					 
					 
							document  =  cloudant . document . Document ( self . dbase , self . filename ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							document . fetch ( ) 
 
					 
					 
					 
							document . fetch ( ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							content  =  { } 
 
					 
					 
					 
							content  =  { } 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -196,8 +226,9 @@ class CouchWriter(Couch,Writer):
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# document= _doc 
 
					 
					 
					 
							# document= _doc 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							now  =  str ( datetime . today ( ) ) 
 
					 
					 
					 
							now  =  str ( datetime . today ( ) ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
							
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							name  =  ' - ' . join ( [ document [ ' _id ' ]  ,  now , ' .json ' ] ) 			
 
					 
					 
					 
							name  =  ' - ' . join ( [ document [ ' _id ' ]  ,  now , ' .json ' ] ) 	
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							self . upload ( filename = name , data = content , content_type = ' application/json ' ) 		
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# self.dbase.bulk_docs([document]) 
 
					 
					 
					 
							# self.dbase.bulk_docs([document]) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							# self.dbase.put_attachment(document,content,name,'application/json') 
 
					 
					 
					 
							# self.dbase.put_attachment(document,content,name,'application/json') 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							document . put_attachment ( self . dbase , name , ' application/json ' , content  )
 
					 
					 
					 
							# document.put_attachment(self.dbase,name,'application/json',content  )
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							document . save (  )
 
					 
					 
					 
							# document.save(  )