Example #1
0
    def drop(self, tables=None, quiet=True):
        if tables is True:
            tables = self.db_tables
        elif isinstance(tables, basestring):
            tables = str2list(tables)
        elif self.config.get('table'):
            # drop bound table, if available
            tables = [self.config.get('table')]
        else:
            raise RuntimeError("table to drop must be defined!")
        _tables = []
        for table in tables:
            self.config['db_schema'] = None
            _table = self.get_table(table, except_=False, reflect=True)
            if _table is not None:
                _tables.append(_table)

        if _tables:
            logger.warn("Permanently dropping %s" % tables)
            [t.drop() for t in _tables]
            # clear out existing 'cached' metadata
            self._Base = None
        else:
            logger.warn("No tables found to drop, got %s" % _tables)
        return
Example #2
0
    def drop(self, tables=None, quiet=True):
        if tables is True:
            tables = self.db_tables
        elif isinstance(tables, basestring):
            tables = str2list(tables)
        elif self.config.get('table'):
            # drop bound table, if available
            tables = [self.config.get('table')]
        else:
            raise RuntimeError("table to drop must be defined!")
        _tables = []
        for table in tables:
            self.config['db_schema'] = None
            _table = self.get_table(table, except_=False, reflect=True)
            if _table is not None:
                _tables.append(_table)

        if _tables:
            logger.warn("Permanently dropping %s" % tables)
            [t.drop() for t in _tables]
            # clear out existing 'cached' metadata
            self._Base = None
        else:
            logger.warn("No tables found to drop, got %s" % _tables)
        return
Example #3
0
    def deptree(self, field, oids, date=None, level=None, table=None):
        '''
        Dependency tree builder. Recursively fetchs objects that
        are children of the initial set of parent object ids provided.

        :param field: Field that contains the 'parent of' data
        :param oids: Object oids to build depedency tree for
        :param date: date (metrique date range) that should be queried.
                    If date==None then the most recent versions of the
                    objects will be queried.
        :param level: limit depth of recursion
        '''
        table = self.get_table(table)
        fringe = str2list(oids)
        checked = set(fringe)
        loop_k = 0
        while len(fringe) > 0:
            if level and loop_k == abs(level):
                break
            query = '_oid in %s' % list(fringe)
            docs = self.find(table=table,
                             query=query,
                             fields=[field],
                             date=date,
                             raw=True)
            fringe = {
                oid
                for doc in docs for oid in (doc[field] or [])
                if oid not in checked
            }
            checked |= fringe
            loop_k += 1
        return sorted(checked)
Example #4
0
    def get_changed_oids(self, last_update=None):
        '''
        Returns a list of object ids of those objects that have changed since
        `mtime`. This method expects that the changed objects can be
        determined based on the `delta_mtime` property of the cube which
        specifies the field name that carries the time of the last change.

        This method is expected to be overriden in the cube if it is not
        possible to use a single field to determine the time of the change and
        if another approach of determining the oids is available. In such
        cubes the `delta_mtime` property is expected to be set to `True`.

        If `delta_mtime` evaluates to False then this method is not expected
        to be used.

        :param mtime: datetime string used as 'change since date'
        '''
        mtime_columns = self.lconfig.get('delta_mtime', [])
        if not (mtime_columns and last_update):
            return []
        mtime_columns = str2list(mtime_columns)
        where = []
        for _column in mtime_columns:
            _sql = "%s >= %s" % (_column, last_update)
            where.append(_sql)
        return self.sql_get_oids(where)
Example #5
0
    def deptree(self, field, oids, date=None, level=None, table=None):
        '''
        Dependency tree builder. Recursively fetchs objects that
        are children of the initial set of parent object ids provided.

        :param field: Field that contains the 'parent of' data
        :param oids: Object oids to build depedency tree for
        :param date: date (metrique date range) that should be queried.
                    If date==None then the most recent versions of the
                    objects will be queried.
        :param level: limit depth of recursion
        '''
        table = self.get_table(table)
        fringe = str2list(oids)
        checked = set(fringe)
        loop_k = 0
        while len(fringe) > 0:
            if level and loop_k == abs(level):
                break
            query = '_oid in %s' % list(fringe)
            docs = self.find(table=table, query=query, fields=[field],
                             date=date, raw=True)
            fringe = {oid for doc in docs for oid in (doc[field] or [])
                      if oid not in checked}
            checked |= fringe
            loop_k += 1
        return sorted(checked)
Example #6
0
    def get_changed_oids(self, last_update=None):
        '''
        Returns a list of object ids of those objects that have changed since
        `mtime`. This method expects that the changed objects can be
        determined based on the `delta_mtime` property of the cube which
        specifies the field name that carries the time of the last change.

        This method is expected to be overriden in the cube if it is not
        possible to use a single field to determine the time of the change and
        if another approach of determining the oids is available. In such
        cubes the `delta_mtime` property is expected to be set to `True`.

        If `delta_mtime` evaluates to False then this method is not expected
        to be used.

        :param mtime: datetime string used as 'change since date'
        '''
        mtime_columns = self.lconfig.get('delta_mtime', [])
        if not (mtime_columns and last_update):
            return []
        mtime_columns = str2list(mtime_columns)
        where = []
        for _column in mtime_columns:
            _sql = "%s >= %s" % (_column, last_update)
            where.append(_sql)
        return self.sql_get_oids(where)
Example #7
0
 def columns(self, table=None, columns=None, reflect=False):
     table = self.get_table(table)
     columns = sorted(str2list(columns))
     if not columns:
         columns = sorted(c.name for c in table.columns)
     if reflect:
         columns = [c for c in table.columns if c.name in columns]
     return columns
Example #8
0
 def columns(self, table=None, columns=None, reflect=False):
     table = self.get_table(table)
     columns = sorted(str2list(columns))
     if not columns:
         columns = sorted(c.name for c in table.columns)
     if reflect:
         columns = [c for c in table.columns if c.name in columns]
     return columns
Example #9
0
def test_str2list():
    from metrique.utils import str2list

    assert str2list(None) == []

    a_str = 'a, b,     c,    d , e'
    a_lst = ['a', 'b', 'c', 'd', 'e']
    assert str2list(a_str) == a_lst
    assert str2list(a_lst) == a_lst

    b_str = '1, 2,     3,    4 , 5'
    b_lst = [1., 2., 3., 4., 5.]
    assert str2list(b_str, map_=float) == b_lst

    for i in [{}, 1, 1.0]:
        # try some non-string input values
        try:
            str2list(i)
        except TypeError:
            pass
        else:
            assert False, "Managed to call str2list on non-string: %s" % i
Example #10
0
def test_str2list():
    from metrique.utils import str2list

    assert str2list(None) == []

    a_str = 'a, b,     c,    d , e'
    a_lst = ['a', 'b', 'c', 'd', 'e']
    assert str2list(a_str) == a_lst
    assert str2list(a_lst) == a_lst

    b_str = '1, 2,     3,    4 , 5'
    b_lst = [1., 2., 3., 4., 5.]
    assert str2list(b_str, map_=float) == b_lst

    for i in [{}, 1, 1.0]:
        # try some non-string input values
        try:
            str2list(i)
        except TypeError:
            pass
        else:
            assert False, "Managed to call str2list on non-string: %s" % i