def embed(template, d3json):
    # load metadata.json if present
    # d3json["args"] contains filenams passed in, with wildcards resolved
    if d3json["profile"]["metadatafile"]:
        metadata_file = d3json["profile"]["metadatafile"]
    else:
        metadata_file = os.path.join(os.path.dirname(d3json["profile"]["args"][0]), "metadata.json")
    try:
        with open(metadata_file) as json_data:
            metadata = json.load(json_data)
            json_data.close()
    except:
        #sys.exit("Cannot read metadata file " + metadata_file)
        metadata = {"title": d3json["profile"]["args"][0] 
                    + (" (+)" if 1 > len(d3json["profile"]["args"]) else "") }
    d3json["metadata"] = metadata
    # generate html by replacing token in template
    template_file = os.path.join(os.path.dirname(__file__), "templates", template)
    with open (template_file, "r") as template:
        output = template.read()
        output = output.replace("$TITLE$", metadata["title"])
        output = output.replace("$DATA$", json.dumps(d3json))
        print output
dataset/ETHPy150Open pbinkley/twarc-report/d3output.py/embed/swapped operands of `>`