Esempio n. 1
0
  def __init__(self, id, connection=None):
    # May be called multiple times due to __new__-based cache
    if 'data' in self.__dict__: return

    # Initialise basic values
    self._data = {}
    self.connection = ensureConnection(connection)
    self._type = self.__class__.__name__.lower()

    # Pull out the id, and gather known values
    if type(id) is dict:
      self.gather(id)
    else:
      if isXid(id):
        (rid, oid) = decodeXid(id)
        self.gather({'id': oid, 'xid': id, 'account': {'id': rid}})
      else: self.gather({'id': id})

    self.fetched = False
Esempio n. 2
0
  def __new__(cls, id, connection=None):
    connection = ensureConnection(connection)

    if type(id) is dict:
      if 'xid' in id: vid = decodeXid(id['xid'])[1]
      elif 'code' in id: vid = id['code']
      else: vid = id['id']
    else: vid = id

    obj = connection.cache(cls.__name__, vid)

    if obj:
      # If we were passed data, gather it
      if type(id) is dict:
        obj.gather(id)
    else:
      # Create a new object from supplied data
      obj = super(Base, cls).__new__(cls)
      connection.cache(cls.__name__, vid, obj)

    return obj