def attach_file(self, filename, text=None, tabular=False, syntax='auto', fileinfo=False):
attachment = Attachment()
if tabular:
syntax = None
(mime, _) = mimetypes.guess_type(filename)
attachment.title = os.path.basename(filename)
if text is None:
with open(filename, 'rUb') as f:
text = f.read().decode('utf-8')
if tabular:
csvfile = StringIO(text.strip())
if tabular == 'sniff':
dialect = csv.Sniffer().sniff(text)
else:
dialect = tabular
text = md_table(csv.reader(csvfile, dialect))
elif syntax == 'auto':
syntax = detect_syntax(attachment.title, mime)
if syntax is not None:
text = md_code(text, syntax)
attachment.text = text
if fileinfo:
statinfo = os.stat(filename)
attachment.add_field('Size', sizeof_fmt(statinfo.st_size), True)
attachment.add_field('Mime', mime, True)
self.attachments.append(attachment)
return attachment
评论列表
文章目录