]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/db.py
Open file as an SQLite DB
[m_librarian.git] / m_librarian / db.py
index 7e5744f243e64eabebe14afadd66f768a08e2430..9a48caa8fdade56f6ca38f86f393ad410ef98b5d 100755 (executable)
@@ -1,16 +1,16 @@
 #! /usr/bin/env python
 
-__all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
-           'AuthorBook', 'BookGenre', 'open_db', 'init_db',
-           'insert_name', 'insert_author', 'update_counters',
-           ]
-
 import os
 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
     ForeignKey, DateCol, DatabaseIndex, RelatedJoin, \
     connectionForURI, sqlhub, SQLObjectNotFound, dberrors
 from .config import get_config
 
+__all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
+           'AuthorBook', 'BookGenre', 'open_db', 'init_db',
+           'insert_name', 'insert_author', 'update_counters',
+           ]
+
 
 class Author(SQLObject):
     surname = UnicodeCol(notNull=True)
@@ -142,11 +142,21 @@ def open_db(db_uri=None):
         except:
             db_uri = find_sqlite_dburi()
 
+    if db_uri.startswith(os.sep) or db_uri.startswith(os.altsep) \
+            or db_uri.startswith(os.pardir + os.sep) \
+            or db_uri.startswith(os.pardir + os.altsep):
+        if db_uri.startswith(os.pardir + os.sep) \
+        or db_uri.startswith(os.pardir + os.altsep):
+            db_uri = os.path.abspath(db_uri)
+        db_uri = 'sqlite://' + db_uri.replace(os.sep, '/')
+
     sqlhub.processConnection = connection = connectionForURI(db_uri)
 
     if connection.dbName == 'sqlite':
         def lower(s):
-            return s.lower()
+            if isinstance(s, basestring):
+                return s.lower()
+            return s
 
         sqlite = connection.module