Example #1
0
    def patch(self, path, data):
        """Patches a list of changes at a path. If the path/id combo doesn't
    exist, the path is created.

    Example:
      model = {'abc': { 'def': { 'foo': 'bar' } } }
      patch('abc/def', {'hij': {'hello': 'world'}, 'klm': 'mno'})
      model == {'abc':
        { 'def': { 'foo': 'bar', hij': { 'hello': 'world' }, 'klm': 'mno' } } }

    Arguments:
      path: The path at which to apply the patches
      data: The values to insert. A dict of ids(or sub paths) to values
    """
        for key, value in data.iteritems():
            mutation_data_obj = path_utils.follow_path(
                self.data_tree,
                path_utils.join_paths(path, path_utils.drop_last(key)),
                create_missing=True)
            if mutation_data_obj is not None:
                mutation_data_obj[path_utils.last(key)] = value
            mutation_paths_obj = path_utils.follow_path(
                self.path_tree,
                path_utils.join_paths(path, path_utils.drop_last(key)),
                create_missing=True)
            if mutation_paths_obj is not None:
                mutation_paths_obj[path_utils.last(key)] = 'patch'
            if self.additional is None:
                continue
            local_data_obj = path_utils.follow_path(
                self.additional,
                path_utils.join_paths(path, path_utils.drop_last(key)),
                create_missing=True)
            if local_data_obj is not None:
                local_data_obj[path_utils.last(key)] = value
Example #2
0
    def get(self, path, id, local_instance=True):
        """Get data from the model. Getting data from the local instance and the
    remove instance is the same with some exceptions:
    - Remote fetches don't have mutations from an unclosed transaction,
      local fetches do.

    Arguments:
      path: The path to get
      id: The id of the value to get at the path
      local_instance: If true, gets the data from the local copy,
          if false, gets the data from the remote copy.
    """
        if not local_instance:
            print "getting accessor_mutex"
            accessor_mutex.acquire()
            data = self.firebase.get(path, id)
            print "releasing accessor_mutex"
            accessor_mutex.release()
            return data
        full_path = path_utils.join_paths(path, id)
        obj = path_utils.follow_path(self.instance,
                                     path_utils.drop_last(full_path))
        if obj is None:
            return None
        return obj.get(path_utils.last(full_path))