sqlite.py
Go to the documentation of this file.00001 import silme.io
00002 from silme.io.clients import IOClient, DBClient
00003 from silme.core import L10nPackage, L10nObject, EntityList, Object, Entity
00004
00005 import os
00006 from pysqlite2 import dbapi2 as sqlite
00007
00008 def register(Manager):
00009 Manager.register(SQLiteClient)
00010
00011 class SQLiteClient (DBClient):
00012 name = 'sqlite'
00013 desc = "SQLite reader/writer"
00014 type = IOClient.__name__
00015
00016 @classmethod
00017 def matches_path(cls, path):
00018 """
00019 tests if the ioclient should be used for this type of path
00020 Matches any sqlite:
00021 """
00022 return path.startswith('sqlite:')
00023
00024 @classmethod
00025 def get_entitylist(cls, path, source=False, code='default', parser=None):
00026
00027 entityList = EntityList()
00028 (path, table) = cls._explode_path(path)
00029 con = cls._connected()
00030 if not con:
00031 cls._connect(path)
00032
00033 cursor = cls.connection.cursor()
00034 cursor.execute('SELECT * FROM '+table)
00035 for row in cursor:
00036 entitylist.add_entity(Entity(row[0],row[1]))
00037 cursor.close()
00038 if not con:
00039 cls._close()
00040 return entitylist
00041
00042 @classmethod
00043 def get_l10npackage (cls, path, load_objects = True):
00044 l10npackage = L10nPackage()
00045 cls._connect(path)
00046 l10npackage.id = os.path.basename(path)
00047 l10npackage.objects['L10nTable'] = L10nObject(cls.build_entitylist(path, 'L10nTable'))
00048 cls._close()
00049 return l10npackage
00050
00051
00052
00053 @classmethod
00054
00055 def _explode_path(cls, path):
00056 return (path, 'l10n')
00057
00058 @classmethod
00059 def _connect(cls, path):
00060 cls.connection = sqlite.connect(path)
00061
00062 def _close(cls):
00063 if cls._connected():
00064 cls.connection.close()
00065 cls.connection = None
00066
00067 def _connected():
00068 return bool(cls.connection)