#!/usr/bin/env python
# This generates the hgvs.parser.Parser Python code from the OMeta grammar file
# We generate it offline then keep the statically generated file to reduce startup time
# @see https://github.com/biocommons/hgvs/issues/661

import hashlib
import os
import sys

import parsley
from ometa.grammar import OMeta
from ometa.builder import writePython

if __name__ == "__main__":
    script_path = os.path.realpath(__file__)
    script_dir = os.path.dirname(script_path)
    hgvs_base_dir = os.path.dirname(script_dir)
    grammar_file = os.path.join(hgvs_base_dir, "src/hgvs/_data/hgvs.pymeta")
    generated_code_dir = os.path.join(hgvs_base_dir, "src/hgvs/generated")

    grammar_hash = hashlib.md5(open(grammar_file, 'rb').read()).hexdigest()
    prefix_length = len(hgvs_base_dir) + 1  # extra to also remove slash

    header_template = """# --------------------------------------------------
# THIS IS A GENERATED FILE. DO NOT MODIFY.
# Changes will be overwritten by the generation script.
#    Generated by: {generate_script}
#    Grammar file: {grammar_file}
#    Grammar hash: {grammar_hash}
# Parsley version: {parsley_version}
#  Python version: {python_version}
# --------------------------------------------------
"""
    header = header_template.format(generate_script=script_path[prefix_length:],
                                    grammar_file=grammar_file[prefix_length:],
                                    grammar_hash=grammar_hash,
                                    parsley_version=parsley.__version__,
                                    python_version=sys.version)

    g = OMeta(open(grammar_file).read(), name="Grammar")
    tree = g.parseGrammar("Grammar")
    source = writePython(tree, "Grammar")

    # Create a package for the new file for Python2 compatability
    if not os.path.exists(generated_code_dir):
        os.mkdir(generated_code_dir)
        package_filename = os.path.join(generated_code_dir, "__init__.py")
        with open(package_filename, "w") as _:
            pass

    source_filename = os.path.join(generated_code_dir, "hgvs_grammar.py")
    with open(source_filename, "w") as source_file:
        source_file.write(header + "\n")
        source_file.write(source)
