Exemplo n.º 1
0
    def __init__(
        self,
        name,
        attributes,
        info=None,
        key=None,
        order_attribute=None,
        order=None,
    ):
        super(Level, self).__init__(name, ref=None, info=info)

        self.order = order
        self.attributes = attributes

        if key:
            self.key = self.get_attribute(key)
        elif len(self.attributes) >= 1:
            self.key = self.attributes[0]
        else:
            raise ModelError('Attribute list should not be empty')

        if order_attribute:
            try:
                self.order_attribute = self.get_attribute(order_attribute)
            except NoSuchAttributeError:
                raise NoSuchAttributeError(
                    'Unknown order attribute "{}" in level "{}"'.format(
                        order_attribute, self.name))
        else:
            self.order_attribute = self.attributes[0]
Exemplo n.º 2
0
 def get_cube(self, name):
     try:
         return self._cubes[name]
     except KeyError:
         raise NoSuchAttributeError(
             'Model "{}" has no cube "{}"'.format(self.name, name)
         )
Exemplo n.º 3
0
    def get_attributes(self, attributes, raise_on_error=True):
        """Returns a list of cube's attributes."""

        result = []
        for name in attributes:
            if isinstance(name, AttributeBase):
                if name not in result:
                    result.append(name)
                continue

            if not isinstance(name, compat.string_type):
                name = str(name)

            try:
                attr = self._all_attributes[name]
            except KeyError:
                if not raise_on_error:
                    continue
                raise NoSuchAttributeError(
                    'Unknown attribute "{}" in cube "{}"'
                    .format(name, self.name)
                )

            if attr not in result:
                result.append(attr)

        return result
Exemplo n.º 4
0
    def construct_column_for_attribute(self, attribute, columns):
        if isinstance(attribute, compat.string_type):
            attribute = self.cube.get_attributes([attribute])[0]

        names = [attribute.ref]

        try:
            column_object = self.get_column_object_by_attribute(attribute)
        except KeyError:
            raise NoSuchAttributeError(attribute)

        names.append(column_object.qualified_column)
        for name in names:
            try:
                column = columns[name]
                break
            except KeyError:
                continue
        else:
            raise ModelError('Unknown column "{}"'.format(name))

        if column_object.extract:
            column = sql.expression.extract(column_object.extract, column)

        if column_object.function:
            column = getattr(sql.expression.func, column_object.function)(column)

        label = attribute.ref if attribute.dimension.is_plain else attribute.base_name
        column = column.label(label)

        return column
Exemplo n.º 5
0
    def get_attribute(self, name):
        """Get attribute by `name`"""

        attrs = [attr for attr in self.attributes if attr.name == name]

        if attrs:
            return attrs[0]
        else:
            raise NoSuchAttributeError(name)
Exemplo n.º 6
0
    def get_attribute(self, name, raise_on_error=True):
        key = '{}.{}'.format(self.name, name)

        try:
            return self._attributes[key]
        except KeyError:
            if not raise_on_error:
                return None

            raise NoSuchAttributeError(
                'Unknown attribute "{}" in dimension "{}"'.format(
                    name, self.name), name)
Exemplo n.º 7
0
    def get_measure(self, name, raise_on_error=True):
        """Get measure object of `Measure` type.

        Raises `NoSuchAttributeError` when there is no such measure.
        """

        if isinstance(name, Measure):
            return name

        try:
            return self._measures[name]
        except KeyError:
            if not raise_on_error:
                return None
            raise NoSuchAttributeError(
                'Cube "{}" has no measure "{}"'.format(self.name, name)
            )
Exemplo n.º 8
0
    def get_column_by_attribute(self, attribute):
        """Return a column for `logical` reference. The returned column will
        have a label same as the column part of `logical`.
        """

        if isinstance(attribute, compat.string_type):
            attribute = self.cube.get_attributes([attribute])[0]

        logical = attribute.ref

        if logical in self._columns:
            return self._columns[logical]

        try:
            column_object = self.get_column_object_by_attribute(attribute)
        except KeyError:
            raise NoSuchAttributeError(logical)

        table_object = self.get_table_object(column_object.table_key)
        table = table_object.table

        try:
            column = table.columns[column_object.column]
        except KeyError:
            available = '", "'.join(str(c) for c in table.columns)
            raise ModelError(
                'Unknown column "{}" in table "{}" available columns: "{}"'
                .format(column_object.column, column_object.table, available)
            )

        if column_object.extract:
            column = sql.expression.extract(column_object.extract, column)

        if column_object.function:
            column = getattr(sql.expression.func, column_object.function)(column)

        column = column.label(column_object.column)

        self._columns[logical] = column
        return column