-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakergen.py
executable file
·72 lines (55 loc) · 1.76 KB
/
fakergen.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import argparse
def printError(message):
if not _quiet:
print('\033[91m[ERROR]\033[0m ' + str(message))
exit(1)
def parseArgs():
parser = argparse.ArgumentParser()
parser.add_argument('template', help='template file')
parser.add_argument('-q', '--quiet', action='store_true', help='be quiet, no error message')
parser.add_argument('-s', '--seed', type=int, help='set seed value')
return parser.parse_args()
def fetchContent(templateFile):
try:
with open(templateFile) as f:
return f.read().splitlines()
except FileNotFoundError as err:
printError(err)
def generateData(templateContent):
from faker import Faker
fake = Faker()
if _seed:
fake.seed_instance(_seed)
try:
from customprovider import CustomProvider
fake.add_provider(CustomProvider)
except (ModuleNotFoundError, ImportError):
pass
pattern = r'\{\{\w+\([\w=,\s?#\'\":-]*\)\}\}'
for line in templateContent:
num = len(re.findall(pattern, line))
if num != 0:
newline = line
for i in range(1, num+1):
item = re.findall(pattern, newline)
try:
newline = newline.replace(item[0], str(eval('fake.' + re.sub(r'[\{\}]', '', item[0]))), 1)
except AttributeError as err:
printError(err)
print(newline)
else:
print(line)
def main():
args = parseArgs()
global _quiet
global _seed
_seed = args.seed or 0
_quiet = args.quiet or False
template = str(args.template)
content = fetchContent(template)
generateData(content)
if __name__ == '__main__':
main()