def __new__(mcs, name, bases, attributes): for attr_name, attr in list(attributes.items()): if isinstance(attr, Property): if attr.key is None: attr.key = attr_name elif isinstance(attr, Label): if attr.name is None: attr.name = Words(attr_name).camel(upper_first=True) elif isinstance(attr, Related): if attr.relationship_type is None: attr.relationship_type = Words(attr_name).upper("_") attributes.setdefault("__primarylabel__", name) primary_key = attributes.get("__primarykey__") if primary_key is None: for base in bases: if primary_key is None and hasattr(base, "__primarykey__"): primary_key = getattr(base, "__primarykey__") break else: primary_key = "__id__" attributes["__primarykey__"] = primary_key return super(GraphObjectType, mcs).__new__(mcs, name, bases, attributes)
def __new__(mcs, name, bases, attributes): for attr_name, attr in list(attributes.items()): if isinstance(attr, Property): if attr.key is None: attr.key = attr_name if attr.__doc__ is attr.__class__.__doc__: attr.__doc__ = repr(attr) elif isinstance(attr, Label): if attr.name is None: attr.name = Words(attr_name).camel(upper_first=True) if attr.__doc__ is attr.__class__.__doc__: attr.__doc__ = repr(attr) elif isinstance(attr, Related): if attr.relationship_type is None: attr.relationship_type = Words(attr_name).upper("_") if attr.__doc__ is attr.__class__.__doc__: def related_repr(obj): try: args = ":class:`%s`" % obj.related_class.__qualname__ except AttributeError: args = ":class:`.%s`" % obj.related_class if obj.relationship_type is not None: args += ", relationship_type=%r" % obj.relationship_type return "%s(%s)" % (obj.__class__.__name__, args) attr.__doc__ = related_repr(attr) attributes.setdefault("__primarylabel__", name) primary_key = attributes.get("__primarykey__") if primary_key is None: for base in bases: if primary_key is None and hasattr(base, "__primarykey__"): primary_key = getattr(base, "__primarykey__") break else: primary_key = "__id__" attributes["__primarykey__"] = primary_key return super(ModelType, mcs).__new__(mcs, name, bases, attributes)
def hydrate(cls, data): from english.casing import Words code = data["code"] message = data["message"] _, classification, category, title = code.split(".") if classification == "ClientError": try: error_cls = ClientError.get_mapped_class(code) except KeyError: error_cls = ClientError message = "%s: %s" % (Words(title).camel(upper_first=True), message) elif classification == "DatabaseError": error_cls = DatabaseError elif classification == "TransientError": error_cls = TransientError else: error_cls = cls inst = error_cls(message) inst.classification = classification inst.category = category inst.title = title inst.code = code inst.message = message return inst
def _clean_key(cls, key): from english.casing import Words return Words(key).snake()