aboutsummaryrefslogtreecommitdiffstats
path: root/pyGHDL/cli/DOM.py
blob: 2feb6aecdc2b4afa62712a2b1b38bd59645774b0 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3

from sys import argv
from sys import exit as sysexit

from pathlib import Path

from pydecor import export

from pyGHDL import GHDLBaseException
from pyGHDL.libghdl import LibGHDLException
from pyGHDL.dom import NonStandard
from pyGHDL.dom.Common import DOMException
from pyGHDL.dom.formatting.prettyprint import PrettyPrint, PrettyPrintException

__all__ = []
__api__ = __all__


@export
class Application:
    _design: NonStandard.Design

    def __init__(self):
        self._design = NonStandard.Design()

    def addFile(self, filename: Path, library: str):
        document = NonStandard.Document(filename)
        self._design.Documents.append(document)

    def prettyPrint(self):
        PP = PrettyPrint()

        buffer = []

        buffer.append("Design:")
        for line in PP.formatDesign(self._design, 1):
            buffer.append(line)

        print("\n".join(buffer))


def handleException(ex):
    if isinstance(ex, PrettyPrintException):
        print("PP:", ex)
        return 5
    elif isinstance(ex, DOMException):
        print("DOM:", ex)
        return 4
    elif isinstance(ex, LibGHDLException):
        print("LIB:", ex)
        return 3
    elif isinstance(ex, GHDLBaseException):
        print("GHDL:", ex)
        return 2
    else:
        print(
            "Fatal: An unhandled exception has reached to the top-most exception handler."
        )
        return 1


def main(items=argv[1:]):
    _exitcode = 0

    if len(items) < 1:
        print("Please, provide the files to be analyzed as CLI arguments.")
        print("Using <testsuite/pyunit/SimpleEntity.vhdl> for demo purposes.\n")
        items = ["testsuite/pyunit/Current.vhdl"]

    for item in items:
        try:
            app = Application()
            app.addFile(Path(item), "default_lib")
            app.prettyPrint()
        except Exception as ex:
            _exitcode = handleException(ex)

    return _exitcode


if __name__ == "__main__":
    sysexit(main())