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  import base64 
 24  import copy 
 25  import httplib 
 26  import mimetypes 
 27  import os 
 28  import socket 
 29  import sys 
 30   
 31  from .designer import push, pushapps, pushdocs 
 32   
33 -class BaseDocsLoader(object):
34 """Baseclass for all doc loaders. """ 35
36 - def get_docs(self):
37 raise NotImplementedError
38
39 - def sync(self, dbs, atomic=True, **kwargs):
40 raise NotImplementedError
41
42 -class FileSystemDocsLoader(BaseDocsLoader):
43 """ Load docs from the filesystem. This loader can find docs 44 in folders on the filesystem and is the preferred way to load them. 45 46 The loader takes the path for design docs as a string or if multiple 47 locations are wanted a list of them which is then looked up in the 48 given order: 49 50 >>> loader = FileSystemDocsLoader('/path/to/templates') 51 >>> loader = FileSystemDocsLoader(['/path/to/templates', '/other/path']) 52 53 You could also do the same to loads docs. 54 """ 55
56 - def __init__(self, designpath, docpath=None):
57 paths = [] 58 if isinstance(designpath, basestring): 59 self.designpaths = [designpath] 60 else: 61 self.designpaths = designpath 62 63 docpath = docpath or [] 64 if isinstance(docpath, basestring): 65 docpath = [docpath] 66 self.docpaths = docpath
67 68
69 - def get_docs(self):
70 docs = [] 71 for path in self.docpaths: 72 ret = pushdocs(path, [], export=True) 73 docs.extend(ret['docs']) 74 75 for path in self.designpaths: 76 ret = pushapps(path, [], export=True) 77 docs.extend(ret['docs']) 78 return docs
79 80
81 - def sync(self, dbs, atomic=True, **kwargs):
82 for path in self.docpaths: 83 ret = pushdocs(path, dbs, atomic=atomic) 84 docs.extend(ret['docs']) 85 86 for path in self.designpaths: 87 pushapps(path, dbs, atomic=atomic)
88
89 -class FileSystemDocLoader(BaseDocsLoader):
90 """ Load only one design doc from a path on the filesystem. 91 92 >>> loader = FileSystemDocLoader("/path/to/designdocfolder", "nameodesigndoc") 93 """ 94
95 - def __init__(self, designpath, name, design_name=None):
96 self.designpath = designpath 97 self.name = name 98 if not design_name.startswith("_design"): 99 design_name = "_design/%s" % design_name 100 self.design_name = design_name
101
102 - def get_docs(self):
103 return document(self.design_path, create=False, 104 docid=self.design_name)
105
106 - def sync(self, dbs, atomic=True, **kwargs):
107 push(self.design_path, dbs, atomic=atomic, 108 docid=self.design_name)
109