Skip to content

Commit

Permalink
Merge pull request #56 from jiro4989/feature/#15-file
Browse files Browse the repository at this point in the history
add file provider #15
  • Loading branch information
mergify[bot] authored Feb 10, 2020
2 parents 916feb1 + 0d069c2 commit 7f382f5
Show file tree
Hide file tree
Showing 12 changed files with 1,580 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ TODO list.

- [ ] base
- [o] faker.provider.address
- [ ] faker.provider.automotive
- [o] faker.provider.automotive
- [o] faker.provider.bank
- [ ] faker.provider.barcode
- [ ] faker.provider.color
- [o] faker.provider.company
- [ ] faker.provider.credit_card
- [o] faker.provider.currency
- [ ] faker.provider.date_time
- [ ] faker.provider.file
- [o] faker.provider.file
- [ ] faker.provider.geo
- [ ] faker.provider.internet
- [ ] faker.provider.isbn
Expand Down
6 changes: 3 additions & 3 deletions faker.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.10.0"
version = "0.11.0"
author = "jiro4989"
description = "faker is a Nim package that generates fake data for you."
license = "MIT"
Expand Down Expand Up @@ -30,7 +30,7 @@ proc addGeneratedText(lines: var seq[string]) =
proc readPublicProcs(file: string): seq[string] =
readFile(file)
.split("\n")
.filterIt(it.startsWith("proc") and "*(f: Faker)" in it)
.filterIt(it.startsWith("proc") and "*(f: Faker" in it)

proc readImplementedLocales(dir, provider: string): seq[string] =
for path in listFiles(dir):
Expand Down Expand Up @@ -61,7 +61,7 @@ proc genProviderIndexFile(provider: string) =
lines.add(&" let f = newFaker()")
let arg2 =
if 1 < args.split(",").len:
args.replace("f: Faker, ", "")
args.replace("f: Faker, ", "").replace("(", "").replace(")", "")
else:
""
lines.add(&" echo f.{procName}({arg2})")
Expand Down
4 changes: 2 additions & 2 deletions src/faker/provider.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# See 'faker.nimble'. #
# ----------------------------------------------- #

import provider/[address, automotive, bank, company, currency, job, person, phone_number, user_agent]
export address, automotive, bank, company, currency, job, person, phone_number, user_agent
import provider/[address, automotive, bank, company, currency, file, job, person, phone_number, user_agent, word]
export address, automotive, bank, company, currency, file, job, person, phone_number, user_agent, word
68 changes: 68 additions & 0 deletions src/faker/provider/file.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# ----------------------------------------------- #
# This module was generated by 'nimble genProvs'. #
# See 'faker.nimble'. #
# ----------------------------------------------- #

import ../base
import file/[file_en_US]
export base

proc fileExtension*(f: Faker, category=""): string =
## Generates random fileExtension.
runnableExamples:
let f = newFaker()
echo f.fileExtension(category="")

case f.locale
of "en_US": file_en_US.fileExtension(f, category="")
else: file_en_US.fileExtension(f, category="")

proc fileName*(f: Faker, category="", extension=""): string =
## Generates random fileName.
runnableExamples:
let f = newFaker()
echo f.fileName(category="", extension="")

case f.locale
of "en_US": file_en_US.fileName(f, category="", extension="")
else: file_en_US.fileName(f, category="", extension="")

proc filePath*(f: Faker, depth=1, category="", extension=""): string =
## Generates random filePath.
runnableExamples:
let f = newFaker()
echo f.filePath(depth=1, category="", extension="")

case f.locale
of "en_US": file_en_US.filePath(f, depth=1, category="", extension="")
else: file_en_US.filePath(f, depth=1, category="", extension="")

proc mimeType*(f: Faker, category=""): string =
## Generates random mimeType.
runnableExamples:
let f = newFaker()
echo f.mimeType(category="")

case f.locale
of "en_US": file_en_US.mimeType(f, category="")
else: file_en_US.mimeType(f, category="")

proc unixDevice*(f: Faker, prefix=""): string =
## Generates random unixDevice.
runnableExamples:
let f = newFaker()
echo f.unixDevice(prefix="")

case f.locale
of "en_US": file_en_US.unixDevice(f, prefix="")
else: file_en_US.unixDevice(f, prefix="")

proc unixPartition*(f: Faker, prefix=""): string =
## Generates random unixPartition.
runnableExamples:
let f = newFaker()
echo f.unixPartition(prefix="")

case f.locale
of "en_US": file_en_US.unixPartition(f, prefix="")
else: file_en_US.unixPartition(f, prefix="")
239 changes: 239 additions & 0 deletions src/faker/provider/file/file_en_US.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
from strformat import `&`
from sequtils import toSeq
import tables

include interfaces
import ../util
import ../word

const
applicationMimeTypes = @[
"application/atom+xml", # Atom feeds
"application/ecmascript",
# ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to
# application/javascript but with stricter processing rules)
"application/EDI-X12", # EDI X12 data; Defined in RFC 1767
"application/EDIFACT", # EDI EDIFACT data; Defined in RFC 1767
"application/json", # JavaScript Object Notation JSON; Defined in RFC 4627
# ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to
# application/ecmascript
"application/javascript",
# but with looser processing rules) It is not accepted in IE 8
# or earlier - text/javascript is accepted but it is defined as obsolete in RFC 4329.
# The "type" attribute of the <script> tag in HTML5 is optional and in practice
# omitting the media type of JavaScript programs is the most interoperable
# solution since all browsers have always assumed the correct
# 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]
"application/ogg", # Ogg, a multimedia bitstream container format; Defined in RFC 5334
"application/pdf", # Portable Document Format, PDF has been in use for document exchange
# on the Internet since 1993; Defined in RFC 3778
"application/postscript", # PostScript; Defined in RFC 2046
"application/rdf+xml", # Resource Description Framework; Defined by RFC 3870
"application/rss+xml", # RSS feeds
"application/soap+xml", # SOAP; Defined by RFC 3902
# Web Open Font Format; (candidate recommendation; use application/x-font-woff
"application/font-woff",
# until standard is official)
"application/xhtml+xml", # XHTML; Defined by RFC 3236
"application/xml-dtd", # DTD files; Defined by RFC 3023
"application/xop+xml", # XOP
"application/zip", # ZIP archive files; Registered[8]
"application/gzip", # Gzip, Defined in RFC 6713
]

audioMimeTypes = @[
"audio/basic", # mulaw audio at 8 kHz, 1 channel; Defined in RFC 2046
"audio/L24", # 24bit Linear PCM audio at 8-48 kHz, 1-N channels; Defined in RFC 3190
"audio/mp4", # MP4 audio
"audio/mpeg", # MP3 or other MPEG audio; Defined in RFC 3003
"audio/ogg", # Ogg Vorbis, Speex, Flac and other audio; Defined in RFC 5334
"audio/vorbis", # Vorbis encoded audio; Defined in RFC 5215
# RealAudio; Documented in RealPlayer Help[9]
"audio/vnd.rn-realaudio",
"audio/vnd.wave", # WAV audio; Defined in RFC 2361
"audio/webm", # WebM open media format
]

imageMimeTypes = @[
"image/gif", # GIF image; Defined in RFC 2045 and RFC 2046
"image/jpeg", # JPEG JFIF image; Defined in RFC 2045 and RFC 2046
"image/pjpeg",
# 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).
# Portable Network Graphics; Registered,[10] Defined in RFC 2083
"image/png",
"image/svg+xml", # SVG vector image; Defined in SVG Tiny 1.2 Specification Appendix M
# Tag Image File Format (only for Baseline TIFF); Defined in RFC 3302
"image/tiff",
"image/vnd.microsoft.icon", # ICO image; Registered[11]
]

messageMimeTypes = @[
"message/http", # Defined in RFC 2616
"message/imdn+xml", # IMDN Instant Message Disposition Notification; Defined in RFC 5438
"message/partial", # Email; Defined in RFC 2045 and RFC 2046
# Email; EML files, MIME files, MHT files, MHTML files; Defined in RFC
# 2045 and RFC 2046
"message/rfc822",
]

modelMimeTypes = @[
"model/example", # Defined in RFC 4735
"model/iges", # IGS files, IGES files; Defined in RFC 2077
"model/mesh", # MSH files, MESH files; Defined in RFC 2077, SILO files
"model/vrml", # WRL files, VRML files; Defined in RFC 2077
# X3D ISO standard for representing 3D computer graphics, X3DB binary
# files
"model/x3d+binary",
"model/x3d+vrml", # X3D ISO standard for representing 3D computer graphics, X3DV VRML files
"model/x3d+xml", # X3D ISO standard for representing 3D computer graphics, X3D XML files
]

multipartMimeTypes = @[
"multipart/mixed", # MIME Email; Defined in RFC 2045 and RFC 2046
"multipart/alternative", # MIME Email; Defined in RFC 2045 and RFC 2046
# MIME Email; Defined in RFC 2387 and used by MHTML (HTML mail)
"multipart/related",
"multipart/form-data", # MIME Webform; Defined in RFC 2388
"multipart/signed", # Defined in RFC 1847
"multipart/encrypted", # Defined in RFC 1847
]

textMimeTypes = @[
"text/cmd", # commands; subtype resident in Gecko browsers like Firefox 3.5
"text/css", # Cascading Style Sheets; Defined in RFC 2318
"text/csv", # Comma-separated values; Defined in RFC 4180
"text/html", # HTML; Defined in RFC 2854
"text/javascript",
# (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).
"text/plain", # Textual data; Defined in RFC 2046 and RFC 3676
"text/vcard", # vCard (contact information); Defined in RFC 6350
"text/xml", # Extensible Markup Language; Defined in RFC 3023
]

videoMimeTypes = @[
"video/mpeg", # MPEG-1 video with multiplexed audio; Defined in RFC 2045 and RFC 2046
"video/mp4", # MP4 video; Defined in RFC 4337
# Ogg Theora or other video (with audio); Defined in RFC 5334
"video/ogg",
"video/quicktime", # QuickTime video; Registered[12]
"video/webm", # WebM Matroska-based open media format
"video/x-matroska", # Matroska open media format
"video/x-ms-wmv", # Windows Media Video; Documented in Microsoft KB 288102
"video/x-flv", # Flash video (FLV files)
]

mimeTypes = {
"application": applicationMimeTypes,
"audio": audioMimeTypes,
"image": imageMimeTypes,
"message": messageMimeTypes,
"model": modelMimeTypes,
"multipart": multipartMimeTypes,
"text": textMimeTypes,
"video": videoMimeTypes,
}.toTable

audioFileExtensions = @[
"flac",
"mp3",
"wav",
]

imageFileExtensions = @[
"bmp",
"gif",
"jpeg",
"jpg",
"png",
"tiff",
]

textFileExtensions = @[
"css",
"csv",
"html",
"js",
"json",
"txt",
]

videoFileExtensions = @[
"mp4",
"avi",
"mov",
"webm",
]

officeFileExtensions = @[
"doc", # legacy MS Word
"docx", # MS Word
"xls", # legacy MS Excel
"xlsx", # MS Excel
"ppt", # legacy MS PowerPoint
"pptx", # MS PowerPoint
"odt", # LibreOffice document
"ods", # LibreOffice spreadsheet
"odp", # LibreOffice presentation
"pages", # Apple Pages
"numbers", # Apple Numbers
"key", # Apple Keynote
"pdf", # Portable Document Format
]

fileExtensions = {
"audio": audioFileExtensions,
"image": imageFileExtensions,
"office": officeFileExtensions,
"text": textFileExtensions,
"video": videoFileExtensions,
}.toTable

unixDevicePrefixes = @["sd", "vd", "xvd"]

proc filePath*(f: Faker, depth=1, category="", extension=""): string =
let filename = f.fileName(category=category, extension=extension)
result = &"/{filename}"
for _ in 0..<depth:
let word = f.word()
result = &"/{word}{result}"

proc fileName*(f: Faker, category="", extension=""): string =
let filename = f.word()
let extension =
if extension == "": f.fileExtension(category=category)
else: extension
result = &"{filename}.{extension}"

proc fileExtension*(f: Faker, category=""): string =
let category =
if category == "": f.rand.sample(toSeq(fileExtensions.keys))
else: category
result = f.rand.sample(fileExtensions[category])

proc mimeType*(f: Faker, category=""): string =
let category =
if category == "": f.rand.sample(toSeq(mimeTypes.keys))
else: category
result = f.rand.sample(mimeTypes[category])

proc unixDevice*(f: Faker, prefix=""): string =
let prefix =
if prefix == "": f.rand.sample(unixDevicePrefixes)
else: prefix
let suffix = f.rand.sample(asciiLowerCase)
result = &"/dev/{prefix}{suffix}"

proc unixPartition*(f: Faker, prefix=""): string =
let path = f.unixDevice(prefix=prefix)
result = path & $f.rand.rand(0..9)
16 changes: 16 additions & 0 deletions src/faker/provider/file/interfaces.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# interfaces はプロシージャの定義のみをまとめたモジュール。
# C言語でいうヘッダファイルのように使用する。
#
# このモジュールをincludeしたモジュールは、このモジュールに定義しているプロシー
# ジャを全て実装しないと、コンパイルが通らない。

import random
import ../../base
export base

proc fileExtension*(f: Faker, category=""): string
proc fileName*(f: Faker, category="", extension=""): string
proc filePath*(f: Faker, depth=1, category="", extension=""): string
proc mimeType*(f: Faker, category=""): string
proc unixDevice*(f: Faker, prefix=""): string
proc unixPartition*(f: Faker, prefix=""): string
3 changes: 2 additions & 1 deletion src/faker/provider/util.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ from strutils import join, align
from math import `^`

const
asciiUpperCase = {'A'..'Z'}
asciiUpperCase* = {'A'..'Z'}
asciiLowerCase* = {'a'..'z'}

proc formatNumbers*(r: var Rand, format: string): string =
## Replace '#' to random number.
Expand Down
Loading

0 comments on commit 7f382f5

Please sign in to comment.