Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/faker/providers/file/__init__.py: 49%

49 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1import string 

2 

3from collections import OrderedDict 

4from typing import Dict, Optional 

5 

6from .. import BaseProvider, ElementsType 

7 

8 

9class Provider(BaseProvider): 

10 """Implement default file provider for Faker.""" 

11 

12 application_mime_types: ElementsType[str] = ( 

13 "application/atom+xml", # Atom feeds 

14 "application/ecmascript", 

15 # ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to 

16 # application/javascript but with stricter processing rules) 

17 "application/EDI-X12", # EDI X12 data; Defined in RFC 1767 

18 "application/EDIFACT", # EDI EDIFACT data; Defined in RFC 1767 

19 "application/json", # JavaScript Object Notation JSON; Defined in RFC 4627 

20 # ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to 

21 # application/ecmascript 

22 "application/javascript", 

23 # but with looser processing rules) It is not accepted in IE 8 

24 # or earlier - text/javascript is accepted but it is defined as obsolete in RFC 4329. 

25 # The "type" attribute of the <script> tag in HTML5 is optional and in practice 

26 # omitting the media type of JavaScript programs is the most interoperable 

27 # solution since all browsers have always assumed the correct 

28 # default even before HTML5. 

29 "application/octet-stream", 

30 # Arbitrary binary data.[6] Generally speaking this type identifies files that are not associated with 

31 # a specific application. Contrary to past assumptions by software packages such as Apache this is not 

32 # a type that should be applied to unknown files. In such a case, a server or application should not indicate 

33 # a content type, as it may be incorrect, but rather, should omit the type in order to allow the recipient 

34 # to guess the type.[7] 

35 "application/ogg", # Ogg, a multimedia bitstream container format; Defined in RFC 5334 

36 "application/pdf", # Portable Document Format, PDF has been in use for document exchange 

37 # on the Internet since 1993; Defined in RFC 3778 

38 "application/postscript", # PostScript; Defined in RFC 2046 

39 "application/rdf+xml", # Resource Description Framework; Defined by RFC 3870 

40 "application/rss+xml", # RSS feeds 

41 "application/soap+xml", # SOAP; Defined by RFC 3902 

42 # Web Open Font Format; (candidate recommendation; use application/x-font-woff 

43 "application/font-woff", 

44 # until standard is official) 

45 "application/xhtml+xml", # XHTML; Defined by RFC 3236 

46 "application/xml-dtd", # DTD files; Defined by RFC 3023 

47 "application/xop+xml", # XOP 

48 "application/zip", # ZIP archive files; Registered[8] 

49 "application/gzip", # Gzip, Defined in RFC 6713 

50 ) 

51 

52 audio_mime_types: ElementsType[str] = ( 

53 "audio/basic", # mulaw audio at 8 kHz, 1 channel; Defined in RFC 2046 

54 "audio/L24", # 24bit Linear PCM audio at 8-48 kHz, 1-N channels; Defined in RFC 3190 

55 "audio/mp4", # MP4 audio 

56 "audio/mpeg", # MP3 or other MPEG audio; Defined in RFC 3003 

57 "audio/ogg", # Ogg Vorbis, Speex, Flac and other audio; Defined in RFC 5334 

58 "audio/vorbis", # Vorbis encoded audio; Defined in RFC 5215 

59 # RealAudio; Documented in RealPlayer Help[9] 

60 "audio/vnd.rn-realaudio", 

61 "audio/vnd.wave", # WAV audio; Defined in RFC 2361 

62 "audio/webm", # WebM open media format 

63 ) 

64 

65 image_mime_types: ElementsType[str] = ( 

66 "image/gif", # GIF image; Defined in RFC 2045 and RFC 2046 

67 "image/jpeg", # JPEG JFIF image; Defined in RFC 2045 and RFC 2046 

68 "image/pjpeg", 

69 # JPEG JFIF image; Associated with Internet Explorer; Listed in ms775147(v=vs.85) - Progressive JPEG, 

70 # initiated before global browser support for progressive JPEGs (Microsoft and Firefox). 

71 # Portable Network Graphics; Registered,[10] Defined in RFC 2083 

72 "image/png", 

73 "image/svg+xml", # SVG vector image; Defined in SVG Tiny 1.2 Specification Appendix M 

74 # Tag Image File Format (only for Baseline TIFF); Defined in RFC 3302 

75 "image/tiff", 

76 "image/vnd.microsoft.icon", # ICO image; Registered[11] 

77 ) 

78 

79 message_mime_types: ElementsType[str] = ( 

80 "message/http", # Defined in RFC 2616 

81 "message/imdn+xml", # IMDN Instant Message Disposition Notification; Defined in RFC 5438 

82 "message/partial", # Email; Defined in RFC 2045 and RFC 2046 

83 # Email; EML files, MIME files, MHT files, MHTML files; Defined in RFC 

84 # 2045 and RFC 2046 

85 "message/rfc822", 

86 ) 

87 

88 model_mime_types: ElementsType[str] = ( 

89 "model/example", # Defined in RFC 4735 

90 "model/iges", # IGS files, IGES files; Defined in RFC 2077 

91 "model/mesh", # MSH files, MESH files; Defined in RFC 2077, SILO files 

92 "model/vrml", # WRL files, VRML files; Defined in RFC 2077 

93 # X3D ISO standard for representing 3D computer graphics, X3DB binary 

94 # files 

95 "model/x3d+binary", 

96 "model/x3d+vrml", # X3D ISO standard for representing 3D computer graphics, X3DV VRML files 

97 "model/x3d+xml", # X3D ISO standard for representing 3D computer graphics, X3D XML files 

98 ) 

99 

100 multipart_mime_types: ElementsType[str] = ( 

101 "multipart/mixed", # MIME Email; Defined in RFC 2045 and RFC 2046 

102 "multipart/alternative", # MIME Email; Defined in RFC 2045 and RFC 2046 

103 # MIME Email; Defined in RFC 2387 and used by MHTML (HTML mail) 

104 "multipart/related", 

105 "multipart/form-data", # MIME Webform; Defined in RFC 2388 

106 "multipart/signed", # Defined in RFC 1847 

107 "multipart/encrypted", # Defined in RFC 1847 

108 ) 

109 

110 text_mime_types: ElementsType[str] = ( 

111 "text/cmd", # commands; subtype resident in Gecko browsers like Firefox 3.5 

112 "text/css", # Cascading Style Sheets; Defined in RFC 2318 

113 "text/csv", # Comma-separated values; Defined in RFC 4180 

114 "text/html", # HTML; Defined in RFC 2854 

115 "text/javascript", 

116 # (Obsolete): JavaScript; Defined in and obsoleted by RFC 4329 in order to discourage its usage in favor of 

117 # application/javascript. However, text/javascript is allowed in HTML 4 and 5 and, unlike 

118 # application/javascript, has cross-browser support. The "type" attribute of the <script> tag in HTML5 is 

119 # optional and there is no need to use it at all since all browsers have always assumed the correct default 

120 # (even in HTML 4 where it was required by the specification). 

121 "text/plain", # Textual data; Defined in RFC 2046 and RFC 3676 

122 "text/vcard", # vCard (contact information); Defined in RFC 6350 

123 "text/xml", # Extensible Markup Language; Defined in RFC 3023 

124 ) 

125 

126 video_mime_types: ElementsType[str] = ( 

127 "video/mpeg", # MPEG-1 video with multiplexed audio; Defined in RFC 2045 and RFC 2046 

128 "video/mp4", # MP4 video; Defined in RFC 4337 

129 # Ogg Theora or other video (with audio); Defined in RFC 5334 

130 "video/ogg", 

131 "video/quicktime", # QuickTime video; Registered[12] 

132 "video/webm", # WebM Matroska-based open media format 

133 "video/x-matroska", # Matroska open media format 

134 "video/x-ms-wmv", # Windows Media Video; Documented in Microsoft KB 288102 

135 "video/x-flv", # Flash video (FLV files) 

136 ) 

137 

138 mime_types: Dict[str, ElementsType[str]] = OrderedDict( 

139 ( 

140 ("application", application_mime_types), 

141 ("audio", audio_mime_types), 

142 ("image", image_mime_types), 

143 ("message", message_mime_types), 

144 ("model", model_mime_types), 

145 ("multipart", multipart_mime_types), 

146 ("text", text_mime_types), 

147 ("video", video_mime_types), 

148 ) 

149 ) 

150 

151 audio_file_extensions: ElementsType[str] = ( 

152 "flac", 

153 "mp3", 

154 "wav", 

155 ) 

156 

157 image_file_extensions: ElementsType[str] = ( 

158 "bmp", 

159 "gif", 

160 "jpeg", 

161 "jpg", 

162 "png", 

163 "tiff", 

164 ) 

165 

166 text_file_extensions: ElementsType[str] = ( 

167 "css", 

168 "csv", 

169 "html", 

170 "js", 

171 "json", 

172 "txt", 

173 ) 

174 

175 video_file_extensions: ElementsType[str] = ( 

176 "mp4", 

177 "avi", 

178 "mov", 

179 "webm", 

180 ) 

181 

182 office_file_extensions: ElementsType[str] = ( 

183 "doc", # legacy MS Word 

184 "docx", # MS Word 

185 "xls", # legacy MS Excel 

186 "xlsx", # MS Excel 

187 "ppt", # legacy MS PowerPoint 

188 "pptx", # MS PowerPoint 

189 "odt", # LibreOffice document 

190 "ods", # LibreOffice spreadsheet 

191 "odp", # LibreOffice presentation 

192 "pages", # Apple Pages 

193 "numbers", # Apple Numbers 

194 "key", # Apple Keynote 

195 "pdf", # Portable Document Format 

196 ) 

197 

198 file_extensions: Dict[str, ElementsType[str]] = OrderedDict( 

199 ( 

200 ("audio", audio_file_extensions), 

201 ("image", image_file_extensions), 

202 ("office", office_file_extensions), 

203 ("text", text_file_extensions), 

204 ("video", video_file_extensions), 

205 ) 

206 ) 

207 unix_device_prefixes: ElementsType[str] = ("sd", "vd", "xvd") 

208 

209 def mime_type(self, category: Optional[str] = None) -> str: 

210 """Generate a mime type under the specified ``category``. 

211 

212 If ``category`` is ``None``, a random category will be used. The list of 

213 valid categories include ``'application'``, ``'audio'``, ``'image'``, 

214 ``'message'``, ``'model'``, ``'multipart'``, ``'text'``, and 

215 ``'video'``. 

216 

217 :sample: 

218 :sample: category='application' 

219 """ 

220 category = category if category else self.random_element(list(self.mime_types.keys())) 

221 return self.random_element(self.mime_types[category]) 

222 

223 def file_name(self, category: Optional[str] = None, extension: Optional[str] = None) -> str: 

224 """Generate a random file name with extension. 

225 

226 If ``extension`` is ``None``, a random extension will be created under 

227 the hood using |file_extension| with the specified ``category``. If a 

228 value for ``extension`` is provided, the value will be used instead, 

229 and ``category`` will be ignored. The actual name part itself is 

230 generated using |word|. 

231 

232 :sample: size=10 

233 :sample: category='audio' 

234 :sample: extension='abcdef' 

235 :sample: category='audio', extension='abcdef' 

236 """ 

237 if extension is None: 

238 extension = self.file_extension(category) 

239 filename: str = self.generator.word() 

240 return f"{filename}.{extension}" 

241 

242 def file_extension(self, category: Optional[str] = None) -> str: 

243 """Generate a file extension under the specified ``category``. 

244 

245 If ``category`` is ``None``, a random category will be used. The list of 

246 valid categories include: ``'audio'``, ``'image'``, ``'office'``, 

247 ``'text'``, and ``'video'``. 

248 

249 :sample: 

250 :sample: category='image' 

251 """ 

252 if category is None: 

253 category = self.random_element(list(self.file_extensions.keys())) 

254 return self.random_element(self.file_extensions[category]) 

255 

256 def file_path( 

257 self, 

258 depth: int = 1, 

259 category: Optional[str] = None, 

260 extension: Optional[str] = None, 

261 absolute: Optional[bool] = True, 

262 ) -> str: 

263 """Generate an pathname to a file. 

264 

265 This method uses |file_name| under the hood to generate the file name 

266 itself, and ``depth`` controls the depth of the directory path, and 

267 |word| is used under the hood to generate the different directory names. 

268 

269 If ``absolute`` is ``True`` (default), the generated path starts with 

270 ``/`` and is absolute. Otherwise, the generated path is relative. 

271 

272 :sample: size=10 

273 :sample: depth=3 

274 :sample: depth=5, category='video' 

275 :sample: depth=5, category='video', extension='abcdef' 

276 """ 

277 file: str = self.file_name(category, extension) 

278 path: str = f"/{file}" 

279 for _ in range(0, depth): 

280 path = f"/{self.generator.word()}{path}" 

281 return path if absolute else path[1:] 

282 

283 def unix_device(self, prefix: Optional[str] = None) -> str: 

284 """Generate a Unix device file name. 

285 

286 If ``prefix`` is ``None``, a random prefix will be used. The list of 

287 valid prefixes include: ``'sd'``, ``'vd'``, and ``'xvd'``. 

288 

289 :sample: 

290 :sample: prefix='mmcblk' 

291 """ 

292 if prefix is None: 

293 prefix = self.random_element(self.unix_device_prefixes) 

294 suffix: str = self.random_element(string.ascii_lowercase) 

295 path = "/dev/%s%s" % (prefix, suffix) 

296 return path 

297 

298 def unix_partition(self, prefix: Optional[str] = None) -> str: 

299 """Generate a Unix partition name. 

300 

301 This method uses |unix_device| under the hood to create a device file 

302 name with the specified ``prefix``. 

303 

304 :sample: 

305 :sample: prefix='mmcblk' 

306 """ 

307 path: str = self.unix_device(prefix=prefix) 

308 path += str(self.random_digit()) 

309 return path