Package couchdbkit :: Package ext :: Package django :: Module loading
[hide private]
[frames] | no frames]

Source Code for Module couchdbkit.ext.django.loading

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (c) 2008-2009 Benoit Chesneau <benoitc@e-engura.com>  
  4  # 
  5  # Permission to use, copy, modify, and distribute this software for any 
  6  # purpose with or without fee is hereby granted, provided that the above 
  7  # copyright notice and this permission notice appear in all copies. 
  8  # 
  9  # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
 10  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
 11  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
 12  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 13  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 14  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
 15  # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 16   
 17  """ 
 18  Maintain registry of documents used in your django project 
 19  and manage db sessions  
 20  """ 
 21   
 22  import sys 
 23  import os 
 24   
 25  import urllib 
 26  import urlparse 
 27   
 28  from couchdbkit import Server, contain, ResourceConflict 
 29  from couchdbkit import push 
 30  from couchdbkit.resource import CouchdbResource, PreconditionFailed 
 31  from django.conf import settings 
 32  from django.db.models import signals, get_app 
 33  from django.core.exceptions import ImproperlyConfigured 
 34  from django.utils.datastructures import SortedDict 
 35  from restkit import BasicAuth 
 36   
 37  COUCHDB_DATABASES = getattr(settings, "COUCHDB_DATABASES", []) 
 38  COUCHDB_TIMEOUT = getattr(settings, "COUCHDB_TIMEOUT", 300) 
 39   
40 -class CouchdbkitHandler(object):
41 """ The couchdbkit handler for django """ 42 43 # share state between instances 44 __shared_state__ = dict( 45 _databases = {}, 46 app_schema = SortedDict() 47 ) 48
49 - def __init__(self, databases):
50 """ initialize couchdbkit handler with COUCHDB_DATABASES 51 settings """ 52 53 self.__dict__ = self.__shared_state__ 54 55 # create databases sessions 56 for app_name, uri in databases: 57 58 try: 59 if isinstance(uri, tuple): 60 # case when you want to specify server uri 61 # and database name specifically. usefull 62 # when you proxy couchdb on some path 63 server_uri, dbname = uri 64 else: 65 server_uri, dbname = uri.rsplit("/", 1) 66 except ValueError: 67 raise ValueError("couchdb uri [%s:%s] invalid" % ( 68 app_name, uri)) 69 70 71 res = CouchdbResource(server_uri, timeout=COUCHDB_TIMEOUT) 72 73 server = Server(server_uri, resource_instance=res) 74 app_label = app_name.split('.')[-1] 75 self._databases[app_label] = (server, dbname)
76
77 - def sync(self, app, verbosity=2):
78 """ used to sync views of all applications and eventually create 79 database. 80 """ 81 app_name = app.__name__.rsplit('.', 1)[0] 82 app_label = app_name.split('.')[-1] 83 if app_label in self._databases: 84 if verbosity >=1: 85 print "sync `%s` in CouchDB" % app_name 86 db = self.get_db(app_label) 87 88 app_path = os.path.abspath(os.path.join(sys.modules[app.__name__].__file__, "..")) 89 design_path = "%s/%s" % (app_path, "_design") 90 if not os.path.isdir(design_path): 91 if settings.DEBUG: 92 print >>sys.stderr, "%s don't exists, no ddoc synchronized" % design_path 93 return 94 95 push(os.path.join(app_path, "_design"), db, force=True, 96 docid="_design/%s" % app_label)
97
98 - def get_db(self, app_label, register=False):
99 """ retrieve db session for a django application """ 100 if register: 101 return 102 103 db = self._databases[app_label] 104 if isinstance(db, tuple): 105 server, dbname = db 106 db = server.get_or_create_db(dbname) 107 self._databases[app_label] = db 108 return db
109
110 - def register_schema(self, app_label, *schema):
111 """ register a Document object""" 112 for s in schema: 113 schema_name = schema[0].__name__.lower() 114 schema_dict = self.app_schema.setdefault(app_label, SortedDict()) 115 if schema_name in schema_dict: 116 fname1 = os.path.abspath(sys.modules[s.__module__].__file__) 117 fname2 = os.path.abspath(sys.modules[schema_dict[schema_name].__module__].__file__) 118 if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]: 119 continue 120 schema_dict[schema_name] = s
121
122 - def get_schema(self, app_label, schema_name):
123 """ retriev Document object from its name and app name """ 124 return self.app_schema.get(app_label, SortedDict()).get(schema_name.lower())
125 126 couchdbkit_handler = CouchdbkitHandler(COUCHDB_DATABASES) 127 register_schema = couchdbkit_handler.register_schema 128 get_schema = couchdbkit_handler.get_schema 129 get_db = couchdbkit_handler.get_db 130