def to_attachment()

in pygenie/adapter/genie_3.py [0:0]


def to_attachment(att):
    if is_str(att) and os.path.isfile(att):
        return (os.path.basename(att), open(att, 'rb'))
    elif is_str(att) and os.path.isdir(att):
        _files = list()
        for local_file in [os.path.join(att, d) for d in os.listdir(att)]:
            if os.path.isfile(local_file) and \
                    os.path.getsize(local_file) > 0 and \
                    not os.path.basename(local_file).startswith('.'):
                _files.append(
                    (os.path.basename(local_file), open(local_file, 'rb'))
                )
        return _files
    elif isinstance(att, dict):
        try:
            return (att['name'], att['data'])
        except KeyError:
            raise GenieAttachmentError("in-line attachment is missing required keys ('name', 'data') ({})".format(att))
    raise GenieAttachmentError("cannot handle attachment '{}'".format(att))