-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpymonipsum.py
67 lines (48 loc) · 1.76 KB
/
pymonipsum.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
import pymongo
from pymongo import MongoClient
import Ipsum
import json
import sys
import argparse
def main(args):
parser = argparse.ArgumentParser(description=
"""Given a input json-schema.org compatible schema specification, create
one or more entries in a mongoDB database of random data"""
)
parser.add_argument('schemaFile', metavar='file',
help='json-schema.org schema file to use')
parser.add_argument('-c','--collection',
help='collection to import into')
parser.add_argument('-d','--db',
help="database to use")
parser.add_argument('--host', default="localhost",
help="hostname to connect to")
parser.add_argument('--port', type=int, default=27017,
help="port to connect to")
parser.add_argument('--count', default=1, type=int,
help='number of objects to create (default: 1)')
parser.add_argument('--drop',
action="store_true",
help="drop the collection first beofre importing into it")
rargs = parser.parse_args()
client = MongoClient(rargs.host, rargs.port)
db = client[rargs.db] # client["mydb"]
coll = db[rargs.collection]
if rargs.drop == True:
coll.drop() # start clean!
fname = rargs.schemaFile
count = rargs.count
fp = open(fname)
try:
schema = json.load(fp)
params = {
"mode": "raw"
}
q = Ipsum.Ipsum(params)
for i in xrange(count):
z = q.createItem(schema)
coll.insert(z)
except ValueError as e:
print "error generating data from file \"%s\": %s" % (fname, e)
# Std way to fire it up....
main(sys.argv)