Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix translate cube #462

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions cubes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,31 @@ def __repr__(self):

return "{%s}" % ", ".join(items)

def assert_instance(obj, class_, label):
"""Raises ArgumentError when `obj` is not instance of `cls`"""
if not isinstance(obj, class_):
def assert_instance(obj, cls, label):
"""Raises ArgumentError when `obj` is not instance of `cls`.

* `cls` - can either be a type or a tuple of type (see also `isinstance`).
* `label` - the variable name on which the test is performed.
"""
if not isinstance(obj, cls):
if isinstance(cls, type):
class_name = cls.__name__
else:
class_name = ' or '.join(c.__name__ for c in cls)
raise ModelInconsistencyError("%s should be sublcass of %s, "
"provided: %s" % (label,
class_.__name__,
class_name,
type(obj).__name__))


def assert_all_instances(list_, class_, label="object"):
"""Raises ArgumentError when objects in `list_` are not instances of
`cls`"""
def assert_all_instances(list_, cls, label="object"):
"""Raises ArgumentError when objects in `list_` are not instances of `cls`.

* `cls` - can either be a type or a tuple of type (see also `isinstance`).
* `label` - the variable name on which the test is performed.
"""
for obj in list_ or []:
assert_instance(obj, class_, label="object")
assert_instance(obj, cls, label=label)


class MissingPackageError(Exception):
Expand Down
39 changes: 21 additions & 18 deletions cubes/metadata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,30 @@ def to_dict(self, create_label=None, **options):

return out

def localized(self, context):
"""Returns a copy of the cube translated with `translation`"""

acopy = self.__class__.__new__(self.__class__)
acopy.__dict__ = self.__dict__.copy()

d = acopy.__dict__
def clone(self, **attrs):
"""Clone the model with modifications of his attributes.

for attr in self.localizable_attributes:
d[attr] = context.get(attr, getattr(self, attr))

for attr in self.localizable_lists:
list_copy = []
* `attrs` - new values for each of his attributes.
"""
d = self.to_dict()
d.update(**attrs)
return type(self)(**d)

if hasattr(acopy, attr):
for obj in getattr(acopy, attr):
obj_context = context.object_localization(attr, obj.name)
list_copy.append(obj.localized(obj_context))
setattr(acopy, attr, list_copy)
def localized(self, context):
"""Returns a copy of the cube translated with `translation`"""

return acopy
changed_attr = {
attr: context.get(attr, getattr(self, attr))
for attr in self.localizable_attributes
}
changed_attr.update({
attr: [
obj.localized(context.object_localization(attr, obj.name))
for obj in getattr(self, attr)
]
for attr in self.localizable_lists
})
return self.clone(**changed_attr)


def object_dict(objects, by_ref=False, error_message=None, error_dict=None):
Expand Down
7 changes: 7 additions & 0 deletions cubes/metadata/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
IMPLICIT_AGGREGATE_LABELS.update(aggregate_calculator_labels())


def _replace_all_dict_by_instance(sequence, cls):
return [cls(**o) if isinstance(o, dict) else o for o in sequence]


class Cube(ModelObject):
"""Logical representation of a cube.

Expand Down Expand Up @@ -249,6 +253,7 @@ def __init__(self, name, dimensions=None, measures=None, aggregates=None,
# Measures

measures = measures or []
measures = _replace_all_dict_by_instance(measures, Measure)
assert_all_instances(measures, Measure, "measure")
self._measures = object_dict(measures,
error_message="Duplicate measure {key} "
Expand All @@ -258,6 +263,7 @@ def __init__(self, name, dimensions=None, measures=None, aggregates=None,
# Aggregates
#
aggregates = aggregates or []
measures = _replace_all_dict_by_instance(measures, MeasureAggregate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall be aggregates = _replace...?

assert_all_instances(aggregates, MeasureAggregate, "aggregate")

self._aggregates = object_dict(aggregates,
Expand All @@ -267,6 +273,7 @@ def __init__(self, name, dimensions=None, measures=None, aggregates=None,

# We don't need to access details by name
details = details or []
measures = _replace_all_dict_by_instance(measures, Attribute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall be details = _replace...?

assert_all_instances(details, Attribute, "detail")
self.details = details

Expand Down