Anonymous avatar Anonymous committed 6d12644

improvements for repub usage

Comments (0)

Files changed (6)

 import datetime
 import json
+from pkg_resources import iter_entry_points
 
 import sqlalchemy as sql
 
 from moai.utils import check_type
 
-class Database(object):
+def get_database(uri):
+    prefix = uri.split(':')[0]
+    for entry_point in iter_entry_points(group='moai.database', name=prefix):
+        dbclass = entry_point.load()
+        return dbclass(uri)
+    else:
+        raise ValueError('No such database registered: %s' % prefix)
+
+
+class SQLDatabase(object):
     """Sql implementation of a database backend
     This implements the :ref:`IDatabase` interface, look there for
     more documentation.
     def oai_query(self,
                   offset=0,
                   batch_size=20,
-                  needed_sets=[],
-                  disallowed_sets=[],
-                  allowed_sets=[],
+                  needed_sets=None,
+                  disallowed_sets=None,
+                  allowed_sets=None,
                   from_date=None,
                   until_date=None,
                   identifier=None):
 
+        needed_sets = needed_sets or []
+        disallowed_sets = disallowed_sets or []
+        allowed_sets = allowed_sets or []
         if batch_size < 0:
             batch_size = 0
 
         # make sure until date is set, and not in future
-        if until_date == None or until_date > datetime.datetime.now():
-            until_date = datetime.datetime.now()
+        if until_date == None or until_date > datetime.datetime.utcnow():
+            until_date = datetime.datetime.utcnow()
 
 
         query = self._records.select(

moai/metadata/mods.py

 
             dai_list = []
             for contributor in contributor_data:
-                unique_id = data['record']['id'] + '_' + contributor.get(
+                unique_id = data['id'] + '_' + contributor.get(
                     'id', contributor['name'][0])
                 if unique_id[0].isdigit():
                     unique_id = '_'+unique_id
 
     def _createHeader(self, record):
         deleted = record['deleted']
-        for deleted_set in self.config.sets_deleted:
-            if deleted_set in record['sets']:
+        for setspec in record['sets']:
+            if setspec in self.config.sets_deleted:
                 deleted = True
                 break
         return oaipmh.common.Header(record['id'],
     def _listQuery(self, set=None, from_=None, until=None, 
                    cursor=0, batch_size=10, identifier=None):
             
-        now = datetime.now()
+        now = datetime.utcnow()
         if until != None and until > now:
             # until should never be in the future
             until = now
         if self.config.delay:
             # subtract delay from until_ param, if present
             if until is None:
-                until = datetime.now()
+                until = datetime.utcnow()
             until = until.timetuple()
             ut = time.mktime(until)-self.filter_data.delay
             until = datetime.fromtimestamp(ut)
             
         needed_sets = self.config.sets_needed
         if not set is None:
-            needed_sets.append(set)
+            needed_sets.add(set)
         allowed_sets = self.config.sets_allowed
         disallowed_sets = self.config.sets_disallowed    
         
                  sets_deleted = None,
                  filter_sets = None,
                  delay = 0,
-                 base_asset_path=None):
+                 base_asset_path=None,
+                 ):
         
         self.name = repository_name
         self.url = base_url
         self.metadata_prefixes = metadata_prefixes or ['oai_dc']
         self.batch_size = batch_size
         self.content_type = content_type
-        self.sets_needed = sets_needed or []
-        self.sets_allowed = sets_allowed or []
-        self.sets_disallowed = sets_disallowed or []
-        self.sets_deleted = sets_deleted or []
-        self.filter_sets = filter_sets or []
+        self.sets_needed = set(sets_needed or [])
+        self.sets_allowed = set(sets_allowed or [])
+        self.sets_disallowed = set(sets_disallowed or [])
+        self.sets_deleted = set(sets_deleted or [])
+        self.filter_sets = set(filter_sets or [])
         self.delay = delay
         self.base_asset_path = base_asset_path or tempfile.gettempdir()
 from webob import Request, Response
 
 from moai.server import Server, FeedConfig
-from moai.database import Database
+from moai.database import get_database
 
 class WSGIRequest(object):
     """This is a request object that can be used in a WSGI environment.
         response.content_type = mimetype
         response.status = code
         response.body = msg
-        raise response
+        return response
 
 
 class MOAIWSGIApp(object):
                 name,
                 url,
                 admin_email,
-                content,
-                provider,
                 database,
                 formats,
                 **kwargs):
     # WSGI APP Factory
     formats = formats.split()
     admin_email = admin_email.split()
-
-    database = Database(database)
+    sets_deleted = kwargs.get('deleted_sets') or []
+    if sets_deleted:
+        sets_deleted = sets_deleted.split()
+    sets_disallowed = kwargs.get('disallowed_sets', '') or []
+    if sets_disallowed:
+        sets_disallowed = sets_disallowed.split()
+    sets_allowed = kwargs.get('allowed_sets', '') or []
+    if sets_allowed:
+        sets_allowed = sets_allowed.split()
+    sets_needed = kwargs.get('needed_sets', '') or []
+    if sets_needed:
+        sets_needed = sets_needed.split()
+    database = get_database(database)
     feedconfig = FeedConfig(name,
                             url,
                             admin_emails=admin_email,
-                            metadata_prefixes=formats)
+                            metadata_prefixes=formats,
+                            sets_deleted=sets_deleted,
+                            sets_disallowed=sets_disallowed,
+                            sets_allowed=sets_allowed,
+                            sets_needed=sets_needed)
     server = Server(url, database, feedconfig)
     return MOAIWSGIApp(server)
 
     'moai.content':[
         'moai_example=moai.example:ExampleContent'
      ],
+    'moai.database':[
+        'sqlite=moai.database:SQLDatabase',
+        'mysql=moai.database:SQLDatabase',
+        'postgres=moai.database:SQLDatabase',
+        'oracle=moai.database:SQLDatabase'],
     'moai.provider':[
         'file=moai.provider.file:FileBasedContentProvider',
         'list=moai.provider.list:ListBasedContentProvider',
     'webob',
     'pasteScript',
     'sqlalchemy',
-    'simplejson'
     ],
     test_suite='moai.test.suite'
 )
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.