Beispiel #1
0
def save(grammar, tryfilename, override=False, clean=True):
    """
    Tries to write out grammar contents to new file <tryfilename>

    :param override:
        If <override> is true, it will write to <tryfilename> without question
        If <override> is false, it will search for the first available filename
        using first_available_filename

    :param clean: Determines whether the marked up grammar body
    or the cleaned up grammar body is saved

    :return: If successful, actual file name used
    """
    if override:
        usefilename = tryfilename
    else:
        usefilename = saveutils.first_available_filename(tryfilename)

    try:
        f = open(usefilename, 'w')
        if clean:
            f.write(gramparse.clean_body(grammar))
        else:
            f.write(grammar.body)
        f.close()
    except IOError as e:
        print "Error saving grammar '{0}' to {1}".format(
            grammar.name, usefilename)
        raise e

    return usefilename
def run_test():
    numexemplars = 10
    testgrammarfile = "flofree.cfdg"
    testdir = "test-save-exemplars"
    savefilename = "{0}/flofree.png".format(testdir)
    if os.path.isdir(testdir):
        shutil.rmtree(testdir)
    print "Creating directory {0}...".format(testdir)
    os.mkdir(testdir)

    # Create grammar
    print "Creating grammar from file '{0}'...".format(testgrammarfile)
    g = gramparse.grammar_from_file(testgrammarfile)

    # Generate <numexemplars> exemplars
    print "Generating {0} exemplars...".format(numexemplars)
    exemplars = exgen.generate_exemplars(g, numexemplars, debug=True)
    assert len(exemplars) == numexemplars, \
        "Expected {0} exemplars to be generated, " \
        "instead have {1}".format(numexemplars, len(exemplars))

    # Saving exemplars to separate files
    print "Exemplars successfully generated. Saving exemplars to files..."
    for i, ex in list(enumerate(exemplars)):
        savefilename = saveutils.first_available_filename(savefilename)
        print "Saving exemplar {0} of {1} to {2}".format(i+1, numexemplars, savefilename)
        exsave.save_image(ex, savefilename)

    print "All exemplars successfully saved!"
Beispiel #3
0
def save_image(exemplar, tryfilename, override=False):
    if override:
        usefilename = tryfilename
    else:
        usefilename = saveutils.first_available_filename(tryfilename)

    try:
        exemplar.image.save(usefilename)
    except IOError as e:
        print "IOError saving image to {0}".format(usefilename)
        raise e
    except Exception as e:
        print "Some non-io error occurred saving image to {0}:" \
              " perhaps image arg is incorrect".format(usefilename)
        raise e

    return usefilename
def run_test():
    testgrammarfile = "flofree.cfdg"
    testdir = "test-save-exemplar"
    newfilename = "{0}/flofree.png".format(testdir)
    if not os.path.isdir(testdir):
        print "Creating directory {0}...".format(testdir)
        os.mkdir(testdir)

    # Create grammar
    print "Creating grammar from file '{0}'...".format(testgrammarfile)
    g = gramparse.grammar_from_file(testgrammarfile)

    print "Running CFDG with grammar body..."
    exemplar = exgen.generate_exemplar(g)

    print "Saving exemplar image to '{0}'...".format(
        saveutils.first_available_filename(newfilename))
    exemplar.save_image(newfilename)

    print "Exemplar successfully saved!"
def run_test():
    global g
    global v

    filename = "clouds.cfdg"
    dirname = "test-save-variant"
    newfilename = "{0}/test-save-variant.cfdg".format(dirname)
    if not os.path.isdir(dirname):
        print "Creating directory {0}...".format(dirname)
        os.mkdir(dirname)

    print "Reading grammar from file: " + filename + "..."
    g = gramparse.grammar_from_file(filename)

    print "Creating variant from grammar..."
    v = gramgen.generate_variant(g, 1, 5)

    print "Saving variant to file: " + \
          saveutils.first_available_filename(newfilename)
    gramsave.save(v, newfilename)

    print "Variant successfully saved!"