| 1 |
"""Common information so that all traceback generating scripts |
| 2 |
create files in the same formatter. |
| 3 |
|
| 4 |
""" |
| 5 |
import os |
| 6 |
import sys |
| 7 |
from contextlib import redirect_stderr |
| 8 |
|
| 9 |
this_dir = os.path.dirname(__file__) |
| 10 |
sys.path.append(os.path.join(this_dir, "..")) |
| 11 |
|
| 12 |
|
| 13 |
import friendly_traceback |
| 14 |
from syntax_errors_descriptions import descriptions |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
friendly_traceback.enable_warnings(testing=True) |
| 19 |
|
| 20 |
def write(text): |
| 21 |
sys.stderr.write(text + "\n") |
| 22 |
|
| 23 |
nb = 0 |
| 24 |
|
| 25 |
def make_title(text, formatter="pre"): |
| 26 |
global nb |
| 27 |
nb += 1 |
| 28 |
if formatter == "pre": |
| 29 |
write("\n" + f"({nb}) " + text) |
| 30 |
write("-" * len(f"({nb}) " + text) + "\n") |
| 31 |
write(".. code-block:: none\n") |
| 32 |
elif formatter == "markdown_docs": |
| 33 |
write("\n---\n") |
| 34 |
write("## " + f"({nb}) " + text) |
| 35 |
else: |
| 36 |
print("Unsupported formatter: ", formatter) |
| 37 |
sys.exit() |
| 38 |
|
| 39 |
|
| 40 |
cur_dir = os.getcwd() |
| 41 |
sys.path.append(os.path.join(cur_dir, "syntax")) |
| 42 |
|
| 43 |
|
| 44 |
def create_tracebacks(target, intro_text, formatter="pre"): |
| 45 |
with open(target, "w", encoding="utf8") as out, redirect_stderr(out): |
| 46 |
write(intro_text) |
| 47 |
|
| 48 |
for name in descriptions: |
| 49 |
title = descriptions[name]["title"] |
| 50 |
make_title(title, formatter=formatter) |
| 51 |
try: |
| 52 |
__import__(name) |
| 53 |
except Exception: |
| 54 |
friendly_traceback.explain_traceback() |
| 55 |
|
| 56 |
|
| 57 |
print(" Number of cases in trb_syntax_common.py: ", len(descriptions)) |