Package couchdbkit :: Module loaders
[hide private]
[frames] | no frames]

Source Code for Module couchdbkit.loaders

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of couchdbkit released under the MIT license.  
  4  # See the NOTICE for more information. 
  5   
  6  """ 
  7  Loaders are a simple way to manage design docs in your Python application.  
  8  Loaders are compatible with couchapp script (http://github.com/couchapp/couchapp). 
  9  So it means that you can simply use couchdbkit as replacement for your python 
 10  applications with advantages of couchdbkit client. Compatibility with couchapp means that 
 11  you can also use macros to include javascript code or design doc members in your views, 
 12  shows & lists. 
 13   
 14  Loaders are FileSystemDocsLoader and FileSystemDocLoader. The first 
 15  one takes a directory and retrieve all design docs before sending them to 
 16  CouchDB. Second allow you to send only one design doc. 
 17   
 18  This module is here for compatibility reason and will be removed in 0.6. 
 19  It's replaced by couchdbkit.designer module and push* functions. 
 20  """ 
 21  from __future__ import with_statement 
 22   
 23  from .designer import document, push, pushapps, pushdocs 
 24   
25 -class BaseDocsLoader(object):
26 """Baseclass for all doc loaders. """ 27
28 - def get_docs(self):
29 raise NotImplementedError
30
31 - def sync(self, dbs, atomic=True, **kwargs):
32 raise NotImplementedError
33
34 -class FileSystemDocsLoader(BaseDocsLoader):
35 """ Load docs from the filesystem. This loader can find docs 36 in folders on the filesystem and is the preferred way to load them. 37 38 The loader takes the path for design docs as a string or if multiple 39 locations are wanted a list of them which is then looked up in the 40 given order: 41 42 >>> loader = FileSystemDocsLoader('/path/to/templates') 43 >>> loader = FileSystemDocsLoader(['/path/to/templates', '/other/path']) 44 45 You could also do the same to loads docs. 46 """ 47
48 - def __init__(self, designpath, docpath=None):
49 if isinstance(designpath, basestring): 50 self.designpaths = [designpath] 51 else: 52 self.designpaths = designpath 53 54 docpath = docpath or [] 55 if isinstance(docpath, basestring): 56 docpath = [docpath] 57 self.docpaths = docpath
58 59
60 - def get_docs(self):
61 docs = [] 62 for path in self.docpaths: 63 ret = pushdocs(path, [], export=True) 64 docs.extend(ret['docs']) 65 66 for path in self.designpaths: 67 ret = pushapps(path, [], export=True) 68 docs.extend(ret['docs']) 69 return docs
70 71
72 - def sync(self, dbs, atomic=True, **kwargs):
73 for path in self.docpaths: 74 pushdocs(path, dbs, atomic=atomic) 75 76 for path in self.designpaths: 77 pushapps(path, dbs, atomic=atomic)
78
79 -class FileSystemDocLoader(BaseDocsLoader):
80 """ Load only one design doc from a path on the filesystem. 81 82 >>> loader = FileSystemDocLoader("/path/to/designdocfolder", "nameodesigndoc") 83 """ 84
85 - def __init__(self, designpath, name, design_name=None):
86 self.designpath = designpath 87 self.name = name 88 if not design_name.startswith("_design"): 89 design_name = "_design/%s" % design_name 90 self.design_name = design_name
91
92 - def get_docs(self):
93 return document(self.design_path, create=False, 94 docid=self.design_name)
95
96 - def sync(self, dbs, atomic=True, **kwargs):
97 push(self.design_path, dbs, atomic=atomic, 98 docid=self.design_name)
99