__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # coding=utf-8
  2. from __future__ import unicode_literals
  3. import string
  4. from collections import OrderedDict
  5. from .. import BaseProvider
  6. class Provider(BaseProvider):
  7. application_mime_types = (
  8. "application/atom+xml", # Atom feeds
  9. "application/ecmascript",
  10. # ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to
  11. # application/javascript but with stricter processing rules)
  12. "application/EDI-X12", # EDI X12 data; Defined in RFC 1767
  13. "application/EDIFACT", # EDI EDIFACT data; Defined in RFC 1767
  14. "application/json", # JavaScript Object Notation JSON; Defined in RFC 4627
  15. # ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to
  16. # application/ecmascript
  17. "application/javascript",
  18. # but with looser processing rules) It is not accepted in IE 8
  19. # or earlier - text/javascript is accepted but it is defined as obsolete in RFC 4329.
  20. # The "type" attribute of the <script> tag in HTML5 is optional and in practice
  21. # omitting the media type of JavaScript programs is the most interoperable
  22. # solution since all browsers have always assumed the correct
  23. # default even before HTML5. "application/octet-stream", # Arbitrary binary data.[6] Generally speaking this type identifies files that are not associated with a specific application. Contrary to past assumptions by software packages such as Apache this is not a type that should be applied to unknown files. In such a case, a server or application should not indicate a content type, as it may be incorrect, but rather, should omit the type in order to allow the recipient to guess the type.[7]
  24. "application/ogg", # Ogg, a multimedia bitstream container format; Defined in RFC 5334
  25. "application/pdf", # Portable Document Format, PDF has been in use for document exchange
  26. # on the Internet since 1993; Defined in RFC 3778
  27. "application/postscript", # PostScript; Defined in RFC 2046
  28. "application/rdf+xml", # Resource Description Framework; Defined by RFC 3870
  29. "application/rss+xml", # RSS feeds
  30. "application/soap+xml", # SOAP; Defined by RFC 3902
  31. # Web Open Font Format; (candidate recommendation; use application/x-font-woff
  32. "application/font-woff",
  33. # until standard is official)
  34. "application/xhtml+xml", # XHTML; Defined by RFC 3236
  35. "application/xml-dtd", # DTD files; Defined by RFC 3023
  36. "application/xop+xml", # XOP
  37. "application/zip", # ZIP archive files; Registered[8]
  38. "application/gzip", # Gzip, Defined in RFC 6713
  39. )
  40. audio_mime_types = (
  41. "audio/basic", # mulaw audio at 8 kHz, 1 channel; Defined in RFC 2046
  42. "audio/L24", # 24bit Linear PCM audio at 8-48 kHz, 1-N channels; Defined in RFC 3190
  43. "audio/mp4", # MP4 audio
  44. "audio/mpeg", # MP3 or other MPEG audio; Defined in RFC 3003
  45. "audio/ogg", # Ogg Vorbis, Speex, Flac and other audio; Defined in RFC 5334
  46. "audio/vorbis", # Vorbis encoded audio; Defined in RFC 5215
  47. # RealAudio; Documented in RealPlayer Help[9]
  48. "audio/vnd.rn-realaudio",
  49. "audio/vnd.wave", # WAV audio; Defined in RFC 2361
  50. "audio/webm", # WebM open media format
  51. )
  52. image_mime_types = (
  53. "image/gif", # GIF image; Defined in RFC 2045 and RFC 2046
  54. "image/jpeg", # JPEG JFIF image; Defined in RFC 2045 and RFC 2046
  55. "image/pjpeg",
  56. # JPEG JFIF image; Associated with Internet Explorer; Listed in ms775147(v=vs.85) - Progressive JPEG, initiated before global browser support for progressive JPEGs (Microsoft and Firefox).
  57. # Portable Network Graphics; Registered,[10] Defined in RFC 2083
  58. "image/png",
  59. "image/svg+xml", # SVG vector image; Defined in SVG Tiny 1.2 Specification Appendix M
  60. # Tag Image File Format (only for Baseline TIFF); Defined in RFC 3302
  61. "image/tiff",
  62. "image/vnd.microsoft.icon", # ICO image; Registered[11]
  63. )
  64. message_mime_types = (
  65. "message/http", # Defined in RFC 2616
  66. "message/imdn+xml", # IMDN Instant Message Disposition Notification; Defined in RFC 5438
  67. "message/partial", # Email; Defined in RFC 2045 and RFC 2046
  68. # Email; EML files, MIME files, MHT files, MHTML files; Defined in RFC
  69. # 2045 and RFC 2046
  70. "message/rfc822",
  71. )
  72. model_mime_types = (
  73. "model/example", # Defined in RFC 4735
  74. "model/iges", # IGS files, IGES files; Defined in RFC 2077
  75. "model/mesh", # MSH files, MESH files; Defined in RFC 2077, SILO files
  76. "model/vrml", # WRL files, VRML files; Defined in RFC 2077
  77. # X3D ISO standard for representing 3D computer graphics, X3DB binary
  78. # files
  79. "model/x3d+binary",
  80. "model/x3d+vrml", # X3D ISO standard for representing 3D computer graphics, X3DV VRML files
  81. "model/x3d+xml", # X3D ISO standard for representing 3D computer graphics, X3D XML files
  82. )
  83. multipart_mime_types = (
  84. "multipart/mixed", # MIME Email; Defined in RFC 2045 and RFC 2046
  85. "multipart/alternative", # MIME Email; Defined in RFC 2045 and RFC 2046
  86. # MIME Email; Defined in RFC 2387 and used by MHTML (HTML mail)
  87. "multipart/related",
  88. "multipart/form-data", # MIME Webform; Defined in RFC 2388
  89. "multipart/signed", # Defined in RFC 1847
  90. "multipart/encrypted", # Defined in RFC 1847
  91. )
  92. text_mime_types = (
  93. "text/cmd", # commands; subtype resident in Gecko browsers like Firefox 3.5
  94. "text/css", # Cascading Style Sheets; Defined in RFC 2318
  95. "text/csv", # Comma-separated values; Defined in RFC 4180
  96. "text/html", # HTML; Defined in RFC 2854
  97. "text/javascript",
  98. # (Obsolete): JavaScript; Defined in and obsoleted by RFC 4329 in order to discourage its usage in favor of application/javascript. However, text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support. The "type" attribute of the <script> tag in HTML5 is optional and there is no need to use it at all since all browsers have always assumed the correct default (even in HTML 4 where it was required by the specification).
  99. "text/plain", # Textual data; Defined in RFC 2046 and RFC 3676
  100. "text/vcard", # vCard (contact information); Defined in RFC 6350
  101. "text/xml", # Extensible Markup Language; Defined in RFC 3023
  102. )
  103. video_mime_types = (
  104. "video/mpeg", # MPEG-1 video with multiplexed audio; Defined in RFC 2045 and RFC 2046
  105. "video/mp4", # MP4 video; Defined in RFC 4337
  106. # Ogg Theora or other video (with audio); Defined in RFC 5334
  107. "video/ogg",
  108. "video/quicktime", # QuickTime video; Registered[12]
  109. "video/webm", # WebM Matroska-based open media format
  110. "video/x-matroska", # Matroska open media format
  111. "video/x-ms-wmv", # Windows Media Video; Documented in Microsoft KB 288102
  112. "video/x-flv", # Flash video (FLV files)
  113. )
  114. mime_types = OrderedDict((
  115. ('application', application_mime_types),
  116. ('audio', audio_mime_types),
  117. ('image', image_mime_types),
  118. ('message', message_mime_types),
  119. ('model', model_mime_types),
  120. ('multipart', multipart_mime_types),
  121. ('text', text_mime_types),
  122. ('video', video_mime_types),
  123. ))
  124. audio_file_extensions = (
  125. "flac",
  126. "mp3",
  127. "wav",
  128. )
  129. image_file_extensions = (
  130. "bmp",
  131. "gif",
  132. "jpeg",
  133. "jpg",
  134. "png",
  135. "tiff",
  136. )
  137. text_file_extensions = (
  138. "css",
  139. "csv",
  140. "html",
  141. "js",
  142. "json",
  143. "txt",
  144. )
  145. video_file_extensions = (
  146. "mp4",
  147. "avi",
  148. "mov",
  149. "webm",
  150. )
  151. office_file_extensions = (
  152. "doc", # legacy MS Word
  153. "docx", # MS Word
  154. "xls", # legacy MS Excel
  155. "xlsx", # MS Excel
  156. "ppt", # legacy MS PowerPoint
  157. "pptx", # MS PowerPoint
  158. "odt", # LibreOffice document
  159. "ods", # LibreOffice spreadsheet
  160. "odp", # LibreOffice presentation
  161. "pages", # Apple Pages
  162. "numbers", # Apple Numbers
  163. "key", # Apple Keynote
  164. "pdf", # Portable Document Format
  165. )
  166. file_extensions = OrderedDict((
  167. ("audio", audio_file_extensions),
  168. ("image", image_file_extensions),
  169. ("office", office_file_extensions),
  170. ("text", text_file_extensions),
  171. ("video", video_file_extensions),
  172. ))
  173. unix_device_prefixes = ('sd', 'vd', 'xvd',)
  174. def mime_type(self, category=None):
  175. """
  176. :param category: application|audio|image|message|model|multipart|text|video
  177. """
  178. category = category if category else self.random_element(
  179. list(self.mime_types.keys()))
  180. return self.random_element(self.mime_types[category])
  181. def file_name(self, category=None, extension=None):
  182. """
  183. :param category: audio|image|office|text|video
  184. :param extension: file extension
  185. """
  186. extension = extension if extension else self.file_extension(category)
  187. filename = self.generator.word()
  188. return '{0}.{1}'.format(filename, extension)
  189. def file_extension(self, category=None):
  190. """
  191. :param category: audio|image|office|text|video
  192. """
  193. category = category if category else self.random_element(
  194. list(self.file_extensions.keys()))
  195. return self.random_element(self.file_extensions[category])
  196. def file_path(self, depth=1, category=None, extension=None):
  197. """
  198. :param category: audio|image|office|text|video
  199. :param extension: file extension
  200. :param depth: depth of the file (depth >= 0)
  201. """
  202. file = self.file_name(category, extension)
  203. path = "/{0}".format(file)
  204. for _ in range(0, depth):
  205. path = "/{0}{1}".format(self.generator.word(), path)
  206. return path
  207. def unix_device(self, prefix=None):
  208. """
  209. :param prefix: sd|vd|xvd
  210. """
  211. prefix = prefix or self.random_element(self.unix_device_prefixes)
  212. suffix = self.random_element(string.ascii_lowercase)
  213. path = '/dev/%s%s' % (prefix, suffix)
  214. return path
  215. def unix_partition(self, prefix=None):
  216. """
  217. :param prefix: sd|vd|xvd
  218. """
  219. path = self.unix_device(prefix=prefix)
  220. path += str(self.random_digit())
  221. return path