Fetching contributors…
Cannot retrieve contributors at this time
executable file 97 lines (81 sloc) 2.97 KB
#!/usr/bin/env python
# encoding: utf-8
This script will generate N instances of the specified grammar.
import argparse
import glob
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import gramfuzz
def generate(grammar=None, num=1, output=sys.stdout, max_recursion=10, seed=None):
"""Load and generate ``num`` number of top-level rules from the specified grammar.
:param list grammar: The grammar file to load and generate data from
:param int num: The number of times to generate data
:param output: The output destination (an open, writable stream-type object. default=``sys.stdout``)
:param int max_recursion: The maximum reference-recursion when generating data (default=``10``)
:param int seed: The seed to initialize the PRNG with. If None, will not initialize it.
if seed is not None:
fuzzer = gramfuzz.GramFuzzer()
cat_group = os.path.basename(grammar).replace(".py", "")
results = fuzzer.gen(cat_group=cat_group, num=num, max_recursion=max_recursion)
for res in results:
def main(argv):
parser = argparse.ArgumentParser(
grammar_choices = [
os.path.basename(x).replace(".py", "") \
for x in glob.glob(os.path.join(os.path.dirname(__file__), "grams", "*.py"))
parser.add_argument("-g", "--grammar",
help = "The grammar to load. One of: {}".format(
metavar = "GRAMMAR",
choices = grammar_choices,
default = None,
required = True,
parser.add_argument("-n", "--number",
metavar = "N",
help = "The number of times to generate top-level nodes from the specified grammar(s) (default=1)",
type = int,
default = 1,
parser.add_argument("-s", "--seed",
metavar = "RAND_SEED",
help = "The random seed to initialize the PRNG with (default=None)",
type = int,
default = None,
metavar = "R",
help = "The maximum reference recursion depth allowed (default=10)",
type = int,
default = 10,
parser.add_argument("-o", "--output",
metavar = "OUTPUT",
help = "The output file to output the generated data to (default=stdout)",
default = sys.stdout,
required = False,
type = argparse.FileType("wb"),
args = parser.parse_args(argv)
grammar = os.path.join(os.path.dirname(__file__), "grams", args.grammar + ".py"),
num = args.number,
output = args.output,
max_recursion = args.max_recursion,
seed = args.seed,
if __name__ == "__main__":