extras.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. FORMAT_HTML = 'html'
  5. FORMAT_IMAGE = 'image'
  6. FORMAT_JSON = 'json'
  7. FORMAT_TEXT = 'text'
  8. FORMAT_URL = 'url'
  9. def extra(content, format, name=None, mime_type=None, extension=None):
  10. return {'name': name, 'format': format, 'content': content,
  11. 'mime_type': mime_type, 'extension': extension}
  12. def html(content):
  13. return extra(content, FORMAT_HTML)
  14. def image(content, name='Image', mime_type='image/png', extension='png'):
  15. return extra(content, FORMAT_IMAGE, name, mime_type, extension)
  16. def png(content, name='Image'):
  17. return image(content, name, mime_type='image/png', extension='png')
  18. def jpg(content, name='Image'):
  19. return image(content, name, mime_type='image/jpeg', extension='jpg')
  20. def svg(content, name='Image'):
  21. return image(content, name, mime_type='image/svg+xml', extension='svg')
  22. def json(content, name='JSON'):
  23. return extra(content, FORMAT_JSON, name, 'application/json', 'json')
  24. def text(content, name='Text'):
  25. return extra(content, FORMAT_TEXT, name, 'text/plain', 'txt')
  26. def url(content, name='URL'):
  27. return extra(content, FORMAT_URL, name)