-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathConfig.hs
491 lines (477 loc) · 16.3 KB
/
Config.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
-- | Internal module used for configuration purposes. You don't likely
-- have to use it.
module Config
( SourceData(..)
, fetchData
, fetchDataSingle
, mapSource
, populateLocales
) where
import Control.Monad (filterM)
import Control.Monad.Catch
import Control.Monad.IO.Class
import qualified Data.ByteString as BS
import qualified Data.HashMap.Strict as HM
import Data.Monoid ((<>))
import Data.Text (Text, pack, unpack)
import Data.Vector (Vector)
import Data.Yaml
import Faker
import Faker.Internal.Types (CacheFileKey(..), SourceData(..))
import Language.Haskell.TH (Name)
import Paths_fakedata (getDataFileName)
import System.Directory (doesFileExist, listDirectory)
import System.FilePath ((<.>), (</>), takeExtension, takeFileName)
localesDirectory :: FilePath
localesDirectory = "faker/lib/locales"
localesEnDirectory :: FilePath
localesEnDirectory = "faker/lib/locales/en"
localesCustomEnDirectory :: FilePath
localesCustomEnDirectory = "customFakeSource/en"
isLocaleFile :: FilePath -> IO Bool
isLocaleFile fname = do
exist <- doesFileExist fname
pure (exist && (takeExtension fname == ".yml"))
listLocaleFiles :: FilePath -> IO [FilePath]
listLocaleFiles fname = do
files <- listDirectory fname
filterM isLocaleFile files
populateLocales :: IO [Text]
populateLocales = do
files <- listLocaleFiles localesDirectory
let files' = map (pack . takeFileName) files
pure files'
sourceFile :: SourceData -> FilePath
sourceFile Address = "address"
sourceFile Name = "name"
sourceFile Ancient = "ancient"
sourceFile Adjective = "adjective"
sourceFile Animal = "animal"
sourceFile App = "app"
sourceFile Appliance = "appliance"
sourceFile ATHF = "aqua_teen_hunger_force"
sourceFile Artist = "artist"
sourceFile BTTF = "back_to_the_future"
sourceFile Bank = "bank"
sourceFile Beer = "beer"
sourceFile BoJackHorseman = "bojack_horseman"
sourceFile Book = "book"
sourceFile BossaNova = "bossa_nova"
sourceFile BreakingBad = "breaking_bad"
sourceFile Buffy = "buffy"
sourceFile Business = "business"
sourceFile Cannabis = "cannabis"
sourceFile BigBangTheory = "big_bang_theory"
sourceFile Cat = "cat"
sourceFile ChuckNorris = "chuck_norris"
sourceFile Code = "code"
sourceFile Coffee = "coffee"
sourceFile Coin = "coin"
sourceFile Color = "color"
sourceFile Horse = "horse"
sourceFile Commerce = "commerce"
sourceFile Community = "community"
sourceFile Compass = "compass"
sourceFile Company = "company"
sourceFile Construction = "construction"
sourceFile Cosmere = "cosmere"
sourceFile CryptoCoin = "crypto_coin"
sourceFile CultureSeries = "culture_series"
sourceFile Currency = "currency"
sourceFile DcComics = "dc_comics"
sourceFile Demographic = "demographic"
sourceFile Dessert = "dessert"
sourceFile Device = "device"
sourceFile Dog = "dog"
sourceFile Dota = "dota"
sourceFile DrWho = "dr_who"
sourceFile DragonBall = "dragon_ball"
sourceFile DumbAndDumber = "dumb_and_dumber"
sourceFile Dune = "dune"
sourceFile Educator = "educator"
sourceFile ElderScrolls = "elder_scrolls"
sourceFile ElectricalComponents = "electrical_components"
sourceFile Esport = "esport"
sourceFile Fallout = "fallout"
sourceFile FamilyGuy = "family_guy"
sourceFile File = "file"
sourceFile Finance = "finance"
sourceFile Food = "food"
sourceFile Football = "football"
sourceFile FreshPrinceOfBelAir = "fresh_prince_of_bel_air"
sourceFile Friends = "friends"
sourceFile FunnyName = "funny_name"
sourceFile JackHandey = "jack_handey"
sourceFile GameOfThrones = "game_of_thrones"
sourceFile Gender = "gender"
sourceFile GhostBusters = "ghostbusters"
sourceFile GratefulDead = "grateful_dead"
sourceFile GreekPhilosophers = "greek_philosophers"
sourceFile Hacker = "hacker"
sourceFile HalfLife = "half_life"
sourceFile HarryPotter = "harry_potter"
sourceFile Heroes = "heroes"
sourceFile HeroesOfTheStorm = "heroes_of_the_storm"
sourceFile HeyArnold = "hey_arnold"
sourceFile Hipster = "hipster"
sourceFile HitchhikersGuideToTheGalaxy = "hitchhikers_guide_to_the_galaxy"
sourceFile Hobbit = "hobbit"
sourceFile House = "house"
sourceFile HowIMetYourMother = "how_i_met_your_mother"
sourceFile IdNumber = "id_number"
sourceFile IndustrySegments = "industry_segments"
sourceFile Internet = "internet"
sourceFile Invoice = "invoice"
sourceFile Job = "job"
sourceFile Kpop = "kpop"
sourceFile LeagueOfLegends = "league_of_legends"
sourceFile Lebowski = "lebowski"
sourceFile LordOfTheRings = "lord_of_the_rings"
sourceFile Lorem = "lorem"
sourceFile LoveCraft = "lovecraft"
sourceFile Markdown = "markdown"
sourceFile Marketing = "marketing"
sourceFile Measurement = "measurement"
sourceFile MichaelScott = "michael_scott"
sourceFile Military = "military"
sourceFile Movie = "movie"
sourceFile Music = "music"
sourceFile Myst = "myst"
sourceFile Nation = "nation"
sourceFile NatoPhoneticAlphabet = "nato_phonetic_alphabet"
sourceFile NewGirl = "new_girl"
sourceFile OnePiece = "one_piece"
sourceFile OverWatch = "overwatch"
sourceFile ParksAndRec = "parks_and_rec"
sourceFile Phish = "phish"
sourceFile PhoneNumber = "phone_number"
sourceFile Pokemon = "pokemon"
sourceFile PrincessBride = "princess_bride"
sourceFile ProgrammingLanguage = "programming_language"
sourceFile Quote = "quote"
sourceFile Relationship = "relationship"
sourceFile Restaurant = "restaurant"
sourceFile RickAndMorty = "rick_and_morty"
sourceFile RockBand = "rock_band"
sourceFile Rupaul = "rupaul"
sourceFile Science = "science"
sourceFile Seinfeld = "seinfeld"
sourceFile Shakespeare = "shakespeare"
sourceFile SiliconValley = "silicon_valley"
sourceFile Simpsons = "simpsons"
sourceFile SlackEmoji = "slack_emoji"
sourceFile SonicTheHedgehog = "sonic_the_hedgehog"
sourceFile Source = "source"
sourceFile SouthPark = "south_park"
sourceFile Space = "space"
sourceFile StarTrek = "star_trek"
sourceFile StarWars = "star_wars"
sourceFile StarGate = "stargate"
sourceFile StrangerThings = "stranger_thing"
sourceFile Stripe = "stripe"
sourceFile Subscription = "subscription"
sourceFile SuperSmashBros = "super_smash_bros"
sourceFile SuperHero = "superhero"
sourceFile SwordArtOnline = "sword_art_online"
sourceFile Team = "team"
sourceFile TheExpanse = "the_expanse"
sourceFile TheItCrowd = "the_it_crowd"
sourceFile TheThickOfIt = "the_thick_of_it"
sourceFile TwinPeaks = "twin_peaks"
sourceFile UmphreysMcgee = "umphreys_mcgee"
sourceFile University = "university"
sourceFile VForVendetta = "v_for_vendetta"
sourceFile Vehicle = "vehicle"
sourceFile VentureBros = "venture_bros"
sourceFile Verbs = "verbs"
sourceFile Witcher = "witcher"
sourceFile WorldCup = "world_cup"
sourceFile WorldOfWarcraft = "world_of_warcraft"
sourceFile Yoda = "yoda"
sourceFile Zelda = "zelda"
sourceFile Basketball = "basketball"
sourceFile Opera = "opera"
sourceFile Blood = "blood"
sourceFile Chiquito = "chiquito"
sourceFile Computer = "computer"
sourceFile Control = "control"
sourceFile Departed = "departed"
sourceFile DnD = "dnd"
sourceFile PearlJam = "pearl_jam"
sourceFile Rajnikanth = "rajnikanth"
sourceFile Show = "show"
sourceFile WarhammerFantasy = "warhammer_fantasy"
sourceFile Suits = "suits"
sourceFile Barcode = "barcode"
sourceFile DrivingLicense = "driving_license"
sourceFile Drone = "drone"
sourceFile Futurama = "futurama"
sourceFile Minecraft = "minecraft"
sourceFile Prince = "prince"
sourceFile Rush = "rush"
sourceFile StreetFighter = "street_fighter"
sourceFile StudioGhibli = "studio_ghibli"
sourceFile Bird = "bird"
sourceFile Camera = "camera"
sourceFile ClashOfClans = "clash_of_clan"
sourceFile Conan = "conan"
sourceFile Doraemon = "doraemon"
sourceFile FinalSpace = "final_space"
sourceFile HowToTrainYourDragon = "how_to_train_your_dragon"
sourceFile Mountain = "mountain"
sourceFile Naruto = "naruto"
sourceFile Room = "room"
sourceFile SuperMario = "super_mario"
sourceFile Tea = "tea"
sourceFile Tolkien = "tolkien"
sourceFile Touhou = "touhou"
sourceFile Volleyball = "volleyball"
mapSource :: Text -> Name
mapSource "studioGhibli" = 'StudioGhibli
mapSource "streetFighter" = 'StreetFighter
mapSource "prince" = 'Prince
mapSource "minecraft" = 'Minecraft
mapSource "futurama" = 'Futurama
mapSource "rush" = 'Rush
mapSource "drone" = 'Drone
mapSource "drivingLicense" = 'DrivingLicense
mapSource "barcode" = 'Barcode
mapSource "pearl_jam" = 'PearlJam
mapSource "dnd" = 'DnD
mapSource "chiquito" = 'Chiquito
mapSource "departed" = 'Departed
mapSource "computer" = 'Computer
mapSource "control" = 'Control
mapSource "horse" = 'Horse
mapSource "address" = 'Address
mapSource "basketball" = 'Basketball
mapSource "name" = 'Name
mapSource "blood" = 'Blood
mapSource "ancient" = 'Ancient
mapSource "animal" = 'Animal
mapSource "app" = 'App
mapSource "appliance" = 'Appliance
mapSource "aquaTeenHungerForce" = 'ATHF
mapSource "artist" = 'Artist
mapSource "backToTheFuture" = 'BTTF
mapSource "bank" = 'Bank
mapSource "beer" = 'Beer
mapSource "bojackHourseman" = 'BoJackHorseman
mapSource "book" = 'Book
mapSource "bossaNova" = 'BossaNova
mapSource "breakingBad" = 'BreakingBad
mapSource "bigBangTheory" = 'BigBangTheory
mapSource "buffy" = 'Buffy
mapSource "business" = 'Business
mapSource "cannabis" = 'Cannabis
mapSource "cat" = 'Cat
mapSource "chuckNorris" = 'ChuckNorris
mapSource "code" = 'Code
mapSource "coffee" = 'Coffee
mapSource "coin" = 'Coin
mapSource "color" = 'Color
mapSource "commerce" = 'Commerce
mapSource "community" = 'Community
mapSource "company" = 'Company
mapSource "construction" = 'Construction
mapSource "cosmere" = 'Cosmere
mapSource "cryptoCoin" = 'CryptoCoin
mapSource "cultureSeries" = 'CultureSeries
mapSource "currency" = 'Currency
mapSource "dcComics" = 'DcComics
mapSource "demographic" = 'Demographic
mapSource "dessert" = 'Dessert
mapSource "device" = 'Device
mapSource "dog" = 'Dog
mapSource "dota" = 'Dota
mapSource "drWho" = 'DrWho
mapSource "dragonBall" = 'DragonBall
mapSource "dumbAndDumber" = 'DumbAndDumber
mapSource "dune" = 'Dune
mapSource "educator" = 'Educator
mapSource "compass" = 'Compass
mapSource "elderScrolls" = 'ElderScrolls
mapSource "electricalComponents" = 'ElectricalComponents
mapSource "esport" = 'Esport
mapSource "fallout" = 'Fallout
mapSource "familyGuy" = 'FamilyGuy
mapSource "file" = 'File
mapSource "finance" = 'Finance
mapSource "food" = 'Food
mapSource "football" = 'Football
mapSource "freshPrinceOfBelAir" = 'FreshPrinceOfBelAir
mapSource "friends" = 'Friends
mapSource "funnyName" = 'FunnyName
mapSource "gameOfThrones" = 'GameOfThrones
mapSource "gender" = 'Gender
mapSource "ghostbusters" = 'GhostBusters
mapSource "gratefulDead" = 'GratefulDead
mapSource "greekPhilosophers" = 'GreekPhilosophers
mapSource "hacker" = 'Hacker
mapSource "halfLife" = 'HalfLife
mapSource "harryPotter" = 'HarryPotter
mapSource "heroes" = 'Heroes
mapSource "heroesOfTheStorm" = 'HeroesOfTheStorm
mapSource "heyArnold" = 'HeyArnold
mapSource "hipster" = 'Hipster
mapSource "hitchhikersGuideToTheGalaxy" = 'HitchhikersGuideToTheGalaxy
mapSource "hobbit" = 'Hobbit
mapSource "house" = 'House
mapSource "howIMetYourMother" = 'HowIMetYourMother
mapSource "idNumber" = 'IdNumber
mapSource "industrySegments" = 'IndustrySegments
mapSource "internet" = 'Internet
mapSource "invoice" = 'Invoice
mapSource "job" = 'Job
mapSource "kpop" = 'Kpop
mapSource "leagueOfLegends" = 'LeagueOfLegends
mapSource "lebowski" = 'Lebowski
mapSource "lordOfTheRings" = 'LordOfTheRings
mapSource "lorem" = 'Lorem
mapSource "lovecraft" = 'LoveCraft
mapSource "markdown" = 'Markdown
mapSource "marketing" = 'Marketing
mapSource "michaelScott" = 'MichaelScott
mapSource "military" = 'Military
mapSource "movie" = 'Movie
mapSource "music" = 'Music
mapSource "myst" = 'Myst
mapSource "nation" = 'Nation
mapSource "natoPhoneticAlphabet" = 'NatoPhoneticAlphabet
mapSource "newGirl" = 'NewGirl
mapSource "onePiece" = 'OnePiece
mapSource "overwatch" = 'OverWatch
mapSource "parksAndRec" = 'ParksAndRec
mapSource "phish" = 'Phish
mapSource "phoneNumber" = 'PhoneNumber
mapSource "pokemon" = 'Pokemon
mapSource "princessBride" = 'PrincessBride
mapSource "programmingLauguage" = 'ProgrammingLanguage
mapSource "quote" = 'Quote
mapSource "relationship" = 'Relationship
mapSource "restaurant" = 'Restaurant
mapSource "rickAndMorty" = 'RickAndMorty
mapSource "rockBand" = 'RockBand
mapSource "rupaul" = 'Rupaul
mapSource "science" = 'Science
mapSource "seinfeld" = 'Seinfeld
mapSource "shakespeare" = 'Shakespeare
mapSource "siliconValley" = 'SiliconValley
mapSource "simpsons" = 'Simpsons
mapSource "slackEmoji" = 'SlackEmoji
mapSource "sonicTheHedgehog" = 'SonicTheHedgehog
mapSource "source" = 'Source
mapSource "southPark" = 'SouthPark
mapSource "space" = 'Space
mapSource "starTrek" = 'StarTrek
mapSource "starWars" = 'StarWars
mapSource "stargate" = 'StarGate
mapSource "strangerThings" = 'StrangerThings
mapSource "stripe" = 'Stripe
mapSource "subscription" = 'Subscription
mapSource "superSmashBros" = 'SuperSmashBros
mapSource "superhero" = 'SuperHero
mapSource "swordArtOnline" = 'SwordArtOnline
mapSource "team" = 'Team
mapSource "theExpanse" = 'TheExpanse
mapSource "theItCrowd" = 'TheItCrowd
mapSource "theThickOfIt" = 'TheThickOfIt
mapSource "twinPeaks" = 'TwinPeaks
mapSource "umphreysMcgee" = 'UmphreysMcgee
mapSource "university" = 'University
mapSource "vForVendetta" = 'VForVendetta
mapSource "vehicle" = 'Vehicle
mapSource "ventureBros" = 'VentureBros
mapSource "verbs" = 'Verbs
mapSource "witcher" = 'Witcher
mapSource "worldCup" = 'WorldCup
mapSource "worldOfWarcraft" = 'WorldOfWarcraft
mapSource "yoda" = 'Yoda
mapSource "zelda" = 'Zelda
mapSource "measurement" = 'Measurement
mapSource "opera" = 'Opera
mapSource "rajnikanth" = 'Rajnikanth
mapSource "show" = 'Show
mapSource "suits" = 'Suits
mapSource "warhammerFantasy" = 'WarhammerFantasy
mapSource "adjective" = 'Adjective
mapSource "bird" = 'Bird
mapSource "camera" = 'Camera
mapSource "clashOfClans" = 'ClashOfClans
mapSource "conan" = 'Conan
mapSource "doraemon" = 'Doraemon
mapSource "finalSpace" = 'FinalSpace
mapSource "howToTrainYourDragon" = 'HowToTrainYourDragon
mapSource "jackHandey" = 'JackHandey
mapSource "mountain" = 'Mountain
mapSource "naruto" = 'Naruto
mapSource "room" = 'Room
mapSource "superMario" = 'SuperMario
mapSource "tea" = 'Tea
mapSource "tolkien" = 'Tolkien
mapSource "touhou" = 'Touhou
mapSource "volleyball" = 'Volleyball
mapSource item = error $ "mapSource: Invalid argument passed " <> (show item)
guessSourceFile :: SourceData -> Text -> FilePath
guessSourceFile sdata sysloc =
case sysloc of
"en" ->
case sdata of
Finance -> localesCustomEnDirectory </> (sourceFile sdata) <.> "yml"
sdata' -> localesEnDirectory </> (sourceFile sdata') <.> "yml"
"ja" -> localesDirectory </> "ja" </> (sourceFile sdata) <> ".yml"
"fr" -> localesDirectory </> "fr" </> (sourceFile sdata) <> ".yml"
oth -> localesDirectory </> (unpack oth <> ".yml")
getSourceFile :: (MonadThrow m, MonadIO m) => FilePath -> m FilePath
getSourceFile fname = do
fname' <- liftIO $ getDataFileName fname
exist <- liftIO $ doesFileExist fname'
if exist
then pure fname'
else throwM $ InvalidLocale fname'
fetchData ::
(MonadThrow m, MonadIO m)
=> FakerSettings
-> SourceData
-> (FakerSettings -> Value -> Parser a)
-> m a
fetchData settings sdata parser = do
let locale = getLocale settings
fname = guessSourceFile sdata locale
ckey = CacheFileKey {cfkSource = sdata, cfkLocale = locale}
cache <- liftIO $ getCacheFile settings
case (HM.lookup ckey cache) of
Nothing -> do
afile <- getSourceFile fname
bs <- liftIO $ BS.readFile afile
yaml <- decodeThrow bs
let nhash = HM.insert ckey yaml cache
liftIO $ setCacheFile nhash settings
either (throwM . ParseError) pure (parseEither (parser settings) yaml)
Just yaml ->
either (throwM . ParseError) pure (parseEither (parser settings) yaml)
fetchDataSingle ::
(MonadThrow m, MonadIO m)
=> FakerSettings
-> SourceData
-> (FakerSettings -> Value -> Parser Text)
-> m (Vector Text)
fetchDataSingle settings sdata parser = do
let locale = getLocale settings
fname = guessSourceFile sdata locale
ckey = CacheFileKey {cfkSource = sdata, cfkLocale = locale}
cache <- liftIO $ getCacheFile settings
case (HM.lookup ckey cache) of
Nothing -> do
afile <- getSourceFile fname
bs <- liftIO $ BS.readFile afile
yaml <- decodeThrow bs
let nhash = HM.insert ckey yaml cache
liftIO $ setCacheFile nhash settings
pure <$>
either (throwM . ParseError) pure (parseEither (parser settings) yaml)
Just yaml ->
pure <$>
either (throwM . ParseError) pure (parseEither (parser settings) yaml)