dal-ast-js 0.0.1-dev → 0.0.3-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,88 +0,0 @@
1
- import ast
2
- from pathlib import Path
3
- from getSynthesizedNode import getSynthesizedNode
4
- import shutil
5
-
6
- class Synthesizer:
7
-
8
- def __init__(self, dalAst):
9
- self.dalAst = dalAst
10
- self.pythonAst = ast.Module(
11
- body=[],
12
- type_ignores=[]
13
- )
14
-
15
- def run(self):
16
- '''
17
- Run the synthesizer
18
- '''
19
- # Process each node in the DAL ast.
20
- for node in self.dalAst["body"]:
21
- self.processTree(node, self.pythonAst, 0)
22
-
23
- importNode = ast.parse("from LoggingHelper import semanticLogger").body[0]
24
- self.pythonAst.body.insert(0, importNode)
25
-
26
- self.writeToOutputFolder()
27
-
28
-
29
- def writeToOutputFolder(self):
30
- '''
31
- Writes the synthesized output to output folder and
32
- also adds the logging helper.
33
-
34
- If the output folder has files, clear it.
35
- '''
36
- outputFolder = Path(__file__).parent / "output"
37
-
38
- # Make folder if it doesn't exist
39
- outputFolder.mkdir(parents=True, exist_ok=True)
40
-
41
- # Clear the output folder (assuming it existed)
42
- for item in outputFolder.iterdir():
43
- if item.is_dir():
44
- shutil.rmtree(item)
45
- else:
46
- item.unlink()
47
-
48
- # Write the synthesized output
49
- outputFile = Path(__file__).parent / "output" / "synthesized.py"
50
- with open(outputFile,"w+") as f:
51
- f.write(ast.unparse(self.pythonAst))
52
-
53
- # Copyt logging helper
54
- src = Path(__file__).parent / "output_helpers" / "LoggingHelper.py"
55
- dst = Path(__file__).parent / "output" / "LoggingHelper.py"
56
- shutil.copy(src, dst)
57
-
58
-
59
- def processTree(self, dalAstNode, pythonAstNode, indent):
60
- '''
61
- Process the tree node. If there is a body, process
62
- each node in the body.
63
-
64
- Writes the synthesized ast node to the ast tree.
65
- '''
66
- # self.printTree(indent, dalAstNode["type"])
67
- astNodeBody = getSynthesizedNode(dalAstNode)
68
-
69
- if astNodeBody is None:
70
- if dalAstNode['type'] == "cmd":
71
- type = dalAstNode['type'] + "," + dalAstNode['command']
72
- else:
73
- type = dalAstNode['type']
74
- print(f"Unable to synthesize node of type {type}")
75
- else:
76
- ast.fix_missing_locations(astNodeBody)
77
- pythonAstNode.body.append(astNodeBody)
78
-
79
- if "body" in dalAstNode:
80
- for node in dalAstNode["body"]:
81
- self.processTree(node, astNodeBody, indent + 1)
82
-
83
- def printTree(self, indent, value):
84
- '''
85
- Prints Tree with indentation for inspection.
86
- '''
87
- spaces = (indent * 4) * " "
88
- print(f"{spaces}{value}")