Encode a single argument for the file-system

def encode(arg, delimiter=None, encodeseq=None, encoded=tuple()):
    '''Encode a single argument for the file-system'''
    arg = coerce_unicode(arg, _c.FSQ_CHARSET)
    new_arg = sep = u''
    delimiter, encodeseq = delimiter_encodeseq(
        _c.FSQ_DELIMITER if delimiter is None else delimiter,
        _c.FSQ_ENCODE if encodeseq is None else encodeseq,
        _c.FSQ_CHARSET)

    # validate encoded tuple
    for enc in encoded:
        enc = coerce_unicode(enc, _c.FSQ_CHARSET)
        try:
            enc = enc.encode('ascii')
        except UnicodeEncodeError:
            raise FSQEncodeError(errno.EINVAL, u'invalid encoded value: {0}'\
                                 u' non-ascii'.format(enc))
    # char-wise encode walk
    for seq in arg:
        if seq == delimiter or seq == encodeseq or seq in _ENCODED + encoded:
            h_val = hex(ord(seq))
            # front-pad with zeroes
            if 3 == len(h_val):
                h_val = sep.join([h_val[:2], u'0', h_val[2:]])
            if 4 != len(h_val):
                raise FSQEncodeError(errno.EINVAL, u'invalid hex ({0}) for'\
                                     ' encode-target: {1}'.format(h_val, seq))
            seq = sep.join([encodeseq, h_val[2:]])

        new_arg = sep.join([new_arg, seq])

    return new_arg