def venqueue(trg_queue, item_f, args, user=None, group=None, mode=None):
    '''Enqueue the contents of a file, or file-like object, file-descriptor or
       the contents of a file at an address (e.g. '/my/file') queue with
       an argument list, venqueue is to enqueue what vprintf is to printf

       If entropy is passed in, failure on duplicates is raised to the caller,
       if entropy is not passed in, venqueue will increment entropy until it
       can create the queue item.
    '''
    # setup defaults
    trg_fd = name = None
    user = _c.FSQ_ITEM_USER if user is None else user
    group = _c.FSQ_ITEM_GROUP if group is None else group
    mode = _c.FSQ_ITEM_MODE if mode is None else mode
    now = fmt_time(datetime.datetime.now(), _c.FSQ_TIMEFMT, _c.FSQ_CHARSET)
    pid = coerce_unicode(os.getpid(), _c.FSQ_CHARSET)
    host = coerce_unicode(_HOSTNAME, _c.FSQ_CHARSET)
    tries = u'0'
    entropy = _mkentropy(pid, now, host)

    # open source file
    try:
        src_file = rationalize_file(item_f, _c.FSQ_CHARSET)
    except (OSError, IOError, ), e:
        raise FSQEnqueueError(e.errno, wrap_io_os_err(e))
    try:
        real_file = True if hasattr(src_file, 'fileno') else False
        # get low, so we can use some handy options; man 2 open
        try:
            item_name = construct(( now, entropy, pid, host,
                                    tries, ) + tuple(args))
            tmp_name = os.path.join(fsq_path.tmp(trg_queue), item_name)
            trg_fd = os.open(tmp_name, os.O_WRONLY|os.O_CREAT|os.O_EXCL, mode)
        except (OSError, IOError, ), e:
            if isinstance(e, FSQError):
                raise e
            raise FSQEnqueueError(e.errno, wrap_io_os_err(e))
        try:
            if user is not None or group is not None:
                # set user/group ownership for file; man 2 fchown
                os.fchown(trg_fd, *uid_gid(user, group, fd=trg_fd))
            with closing(os.fdopen(trg_fd, 'wb', 1)) as trg_file:
                # i/o time ... assume line-buffered
                while True:
                    if real_file:
                        reads, dis, card = select.select([src_file], [], [])
                        try:
                            msg = os.read(reads[0].fileno(), 2048)
                            if 0 == len(msg):
                                break
                        except (OSError, IOError, ), e:
                            if e.errno in (errno.EWOULDBLOCK, errno.EAGAIN,):
                                continue
                            raise e
                        trg_file.write(msg)
                    else:
                        line = src_file.readline()
                        if not line:
                            break
                        trg_file.write(line)

                # flush buffers, and force write to disk pre mv.
                trg_file.flush()
                os.fsync(trg_file.fileno())

                # hard-link into queue, unlink tmp, failure case here leaves
                # cruft in tmp, but no race condition into queue
                os.link(tmp_name, os.path.join(fsq_path.item(trg_queue,
                                                            item_name)))
                os.unlink(tmp_name)

                # return the queue item id (filename)
                return item_name
        except Exception, e:
            try:
                os.close(trg_fd)
            except (OSError, IOError, ), err:
                if err.errno != errno.EBADF:
                    raise FSQEnqueueError(err.errno, wrap_io_os_err(err))
            try:
                if tmp_name is not None:
                    os.unlink(tmp_name)
            except (OSError, __HOLE__, ), err:
                if err.errno != errno.ENOENT:
                   raise FSQEnqueueError(err.errno, wrap_io_os_err(err))
            try:
                if name is not None:
                    os.unlink(name)
            except OSError, err:
                if err.errno != errno.ENOENT:
                   raise FSQEnqueueError(err.errno, wrap_io_os_err(err))
            if (isinstance(e, OSError) or isinstance(e, IOError)) and\
                    not isinstance(e, FSQError):
                raise FSQEnqueueError(e.errno, wrap_io_os_err(e))
            raise e
    finally:
        src_file.close()
dataset/ETHPy150Open axialmarket/fsq/fsq/enqueue.py/venqueue