Example #1
0
  def __new__(cls, clsname, parents, attrs):
    if clsname == "Document":
      return type.__new__(cls, clsname, parents, attrs)

    client = getProperty("client", attrs, parents)
    if client is None:
      return type.__new__(cls, clsname, parents, attrs)

    meta = {}
    uniques = []
    references_col_classes = []
    references = []

    for name in attrs.keys():
      if isinstance(attrs[name], BaseProperty):
        meta[name] = prop = attrs.pop(name)
        refcls = getattr(prop, "reference_class", False)
        prop.name = name
        if refcls and not issubclass(refcls, Document):
          raise TypeError("ReferenceProperties for Document must be another Document!")

        colname = getattr(prop, "collection_name", False)
        if colname:
          if colname in prop.reference_class._meta:
            raise RiakkitError("%s already in %s!" % (colname, prop.reference_class))
          references_col_classes.append((colname, prop.reference_class, name))
          references.append(name)
        elif prop.unique: # Unique is not allowed with anything that has backref
          prop.unique_bucket = client.bucket(getUniqueListGivenBucketName(attrs["bucket_name"], name))
          uniques.append(name)

    all_parents = reversed(walkParents(parents))
    for p_cls in all_parents:
      meta.update(p_cls._meta)
      uniques.extend(p_cls._uniques)

    attrs["_meta"] = meta
    attrs["_uniques"] = uniques
    attrs["instances"] = WeakValueDictionary()
    attrs["_references"] = references

    new_class = type.__new__(cls, clsname, parents, attrs)

    bucket_name = attrs.get("bucket_name", None)
    if bucket_name is not None:
      if bucket_name in _document_classes:
        raise RiakkitError("Bucket name of %s already exists in the registry!"
                              % new_class.bucket_name)
      else:
        _document_classes[bucket_name] = new_class

      new_class.bucket = client.bucket(bucket_name)

    for colname, rcls, back_name in references_col_classes:
      rcls._meta[colname] = MultiReferenceProperty(reference_class=new_class)
      rcls._meta[colname].name = colname
      rcls._meta[colname].is_reference_back = back_name
      rcls._references.append(colname)

    return new_class
Example #2
0
    def __new__(cls, clsname, parents, attrs):
        if clsname == "Document":
            return type.__new__(cls, clsname, parents, attrs)

        client = getProperty("client", attrs, parents)
        if client is None:
            return type.__new__(cls, clsname, parents, attrs)

        meta = {}
        uniques = []
        references_col_classes = []
        references = []

        for name in attrs.keys():
            if isinstance(attrs[name], BaseProperty):
                meta[name] = prop = attrs.pop(name)
                refcls = getattr(prop, "reference_class", False)
                prop.name = name
                if refcls and not issubclass(refcls, Document):
                    raise TypeError(
                        "ReferenceProperties for Document must be another Document!"
                    )

                colname = getattr(prop, "collection_name", False)
                if colname:
                    if colname in prop.reference_class._meta:
                        raise RiakkitError("%s already in %s!" %
                                           (colname, prop.reference_class))
                    references_col_classes.append(
                        (colname, prop.reference_class, name))
                    references.append(name)
                elif prop.unique:  # Unique is not allowed with anything that has backref
                    prop.unique_bucket = client.bucket(
                        getUniqueListGivenBucketName(attrs["bucket_name"],
                                                     name))
                    uniques.append(name)

        all_parents = reversed(walkParents(parents))
        for p_cls in all_parents:
            meta.update(p_cls._meta)
            uniques.extend(p_cls._uniques)

        attrs["_meta"] = meta
        attrs["_uniques"] = uniques

        # I know why you're here. It took you 1938402 years to finally get here and
        # you want to know what .instances does. Before you vencture onto the next
        # line of code, I would like to take this opportunity to say few words:
        # Am I proud of what I wrote? No. In fact, I'm ashamed of it. I admit, this
        # is a piece of shit. HOWEVER, in my defense, this got it to work, even
        # though it caused a shittonne of other problems. I remember very clearly
        # the time I came up with this "clever" solution. It fixed all my issues.

        # The following line made me so annoyed with this library that a new one is
        # written. You may not trust me anymore after the next line... in fact, I
        # don't even trust myself... but riakkit-ng is probably going to be better.

        attrs["instances"] = WeakValueDictionary()
        attrs["_references"] = references

        new_class = type.__new__(cls, clsname, parents, attrs)

        bucket_name = attrs.get("bucket_name", None)

        new_class.buckets = {}

        if bucket_name is not None:
            if isinstance(bucket_name, basestring):
                new_class.bucket_name = bucket_name = [bucket_name]

            for bn in bucket_name:
                if bn in _document_classes:
                    raise RiakkitError(
                        "Bucket name of %s already exists in the registry!" %
                        bn)
                else:
                    _document_classes[bn] = new_class

                new_class.buckets[bn] = client.bucket(bn)

            if len(new_class.buckets) == 1:
                new_class.bucket = new_class.buckets.values()[0]
            else:
                new_class.bucket = new_class.buckets[bucket_name[0]]

        for colname, rcls, back_name in references_col_classes:
            rcls._meta[colname] = MultiReferenceProperty(
                reference_class=new_class)
            rcls._meta[colname].name = colname
            rcls._meta[colname].is_reference_back = back_name
            rcls._references.append(colname)

        return new_class
Example #3
0
  def __new__(cls, clsname, parents, attrs):
    if clsname == "Document":
      return type.__new__(cls, clsname, parents, attrs)

    client = getProperty("client", attrs, parents)
    if client is None:
      return type.__new__(cls, clsname, parents, attrs)

    meta = {}
    uniques = []
    references_col_classes = []
    references = []

    for name in attrs.keys():
      if isinstance(attrs[name], BaseProperty):
        meta[name] = prop = attrs.pop(name)
        refcls = getattr(prop, "reference_class", False)
        prop.name = name
        if refcls and not issubclass(refcls, Document):
          raise TypeError("ReferenceProperties for Document must be another Document!")

        colname = getattr(prop, "collection_name", False)
        if colname:
          if colname in prop.reference_class._meta:
            raise RiakkitError("%s already in %s!" % (colname, prop.reference_class))
          references_col_classes.append((colname, prop.reference_class, name))
          references.append(name)
        elif prop.unique: # Unique is not allowed with anything that has backref
          prop.unique_bucket = client.bucket(getUniqueListGivenBucketName(attrs["bucket_name"], name))
          uniques.append(name)

    all_parents = reversed(walkParents(parents))
    for p_cls in all_parents:
      meta.update(p_cls._meta)
      uniques.extend(p_cls._uniques)

    attrs["_meta"] = meta
    attrs["_uniques"] = uniques

    # I know why you're here. It took you 1938402 years to finally get here and
    # you want to know what .instances does. Before you vencture onto the next
    # line of code, I would like to take this opportunity to say few words:
    # Am I proud of what I wrote? No. In fact, I'm ashamed of it. I admit, this
    # is a piece of shit. HOWEVER, in my defense, this got it to work, even
    # though it caused a shittonne of other problems. I remember very clearly
    # the time I came up with this "clever" solution. It fixed all my issues.

    # The following line made me so annoyed with this library that a new one is
    # written. You may not trust me anymore after the next line... in fact, I
    # don't even trust myself... but riakkit-ng is probably going to be better.

    attrs["instances"] = WeakValueDictionary()
    attrs["_references"] = references

    new_class = type.__new__(cls, clsname, parents, attrs)

    bucket_name = attrs.get("bucket_name", None)

    new_class.buckets = {}

    if bucket_name is not None:
      if isinstance(bucket_name, basestring):
        new_class.bucket_name = bucket_name = [bucket_name]

      for bn in bucket_name:
        if bn in _document_classes:
          raise RiakkitError("Bucket name of %s already exists in the registry!"
                                % bn)
        else:
          _document_classes[bn] = new_class

        new_class.buckets[bn] = client.bucket(bn)

      if len(new_class.buckets) == 1:
        new_class.bucket = new_class.buckets.values()[0]
      else:
        new_class.bucket = new_class.buckets[bucket_name[0]]

    for colname, rcls, back_name in references_col_classes:
      rcls._meta[colname] = MultiReferenceProperty(reference_class=new_class)
      rcls._meta[colname].name = colname
      rcls._meta[colname].is_reference_back = back_name
      rcls._references.append(colname)

    return new_class