Exemplo n.º 1
0
class DBHandleBase(object):
    '''
    Handle for the *volatile* database, which serves the purpose of caching
    the inspected data. This database can be destroyed or corrupted, so it should
    be simply re-created from scratch.
    '''

    def __init__(self, path):
        '''
        Constructor.
        '''
        self._path = path
        self.init_queries = list()
        self._db = CsvDB(self._path)

    def open(self, new=False):
        '''
        Init the database, if required.
        '''
        self._db.new() if new else self._db.open()  # pylint: disable=W0106
        self._run_init_queries()

    def _run_init_queries(self):
        '''
        Initialization queries
        '''
        for obj in (Package, PackageCfgFile, PayloadFile, IgnoredDir, AllowedDir):
            self._db.create_table_from_object(obj())

    def purge(self):
        '''
        Purge whole database.
        '''
        for table_name in self._db.list_tables():
            self._db.flush(table_name)

        self._run_init_queries()

    def flush(self, table):
        '''
        Flush the table.
        '''
        self._db.flush(table)

    def close(self):
        '''
        Close the database connection.
        '''
        self._db.close()

    def __getattr__(self, item):
        '''
        Proxy methods from the Database instance.

        :param item:
        :return:
        '''
        return getattr(self._db, item)
Exemplo n.º 2
0
 def test_open(self):
     '''
     Test opening the database.
     :return:
     '''
     csvdb = CsvDB('/foobar')
     csvdb.open()
     assert csvdb.list_tables() == ['test_db']
     assert csvdb.is_closed() is False
Exemplo n.º 3
0
 def test_open(self):
     """
     Test opening the database.
     :return:
     """
     with patch("os.listdir", MagicMock(return_value=["test_db"])), patch(
             "gzip.open", mock_open("foo:int,bar:str")):
         csvdb = CsvDB("/foobar")
         csvdb.open()
         assert list(csvdb.list_tables()) == ["test_db"]
         assert csvdb.is_closed() is False