forked from biocommons/hgvs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
biocommons#661 - Improve Parser startup time by storing generated Pyt…
…hon code
- Loading branch information
Showing
5 changed files
with
3,138 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/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) |
Empty file.
Oops, something went wrong.