File: trb_syntax_common.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 1,523 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Common information so that all traceback generating scripts
   create files in the same formatter.

"""
import os
import sys
from contextlib import redirect_stderr

this_dir = os.path.dirname(__file__)
sys.path.append(os.path.join(this_dir, ".."))


import friendly_traceback
from syntax_errors_descriptions import descriptions

# prevent known warnings from being printed out and inserted in
# the documentation
friendly_traceback.enable_warnings(testing=True)

def write(text):
    sys.stderr.write(text + "\n")

nb = 0

def make_title(text, formatter="pre"):
    global nb
    nb += 1
    if formatter == "pre":
        write("\n" + f"({nb}) " + text)
        write("-" * len(f"({nb}) " + text) + "\n")
        write(".. code-block:: none\n")
    elif formatter == "markdown_docs":
        write("\n---\n")
        write("## " + f"({nb}) " + text)
    else:
        print("Unsupported formatter: ", formatter)
        sys.exit()


cur_dir = os.getcwd()
sys.path.append(os.path.join(cur_dir, "syntax"))


def create_tracebacks(target, intro_text, formatter="pre"):
    with open(target, "w", encoding="utf8") as out, redirect_stderr(out):
        write(intro_text)

        for name in descriptions:
            title = descriptions[name]["title"]
            make_title(title, formatter=formatter)
            try:
                __import__(name)
            except Exception:  # noqa
                friendly_traceback.explain_traceback()


print("    Number of cases in trb_syntax_common.py: ", len(descriptions))