-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJackAnalyzer.py
53 lines (47 loc) · 2.07 KB
/
JackAnalyzer.py
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
from ParserXML import ParserXML
import sys
import os
class JackAnalyzer:
"""
Main analyzer class. Accepts a file or directory path as an input. Drives analysis process and writes to output file(s).
"""
def __init__(self, input, output_file_tag=""):
self.jack_files = self.get_files(input)
self.xml_files = []
self.output_file_tag = output_file_tag
self.parser = None
def analyze(self):
"""Reads Jack file(s), drives analysis process, and writes to XML file(s)."""
for jack_file in self.jack_files:
with open(jack_file, 'r', encoding='utf-8') as f:
raw_code = f.read()
self.parser = ParserXML(raw_code)
self.parser.compile()
xml_file = self.get_output_file(
jack_file, self.output_file_tag)
with open(xml_file, "w", encoding='utf-8') as f:
for line in self.parser.xml:
f.write(line)
self.xml_files.append(xml_file)
@staticmethod
def get_files(file_or_directory_path):
"""Returns a list of files, given either a relative file or directory path."""
jack_files = []
if os.path.isdir(file_or_directory_path):
directory = os.listdir(file_or_directory_path)
for f in directory:
if f.endswith(".jack"):
jack_file = os.path.join(file_or_directory_path, f)
jack_files.append(jack_file)
elif os.path.isfile(file_or_directory_path):
jack_files.append(file_or_directory_path)
else:
raise Exception(
"INVALID INPUT: '{}' is not a relative path to a file \
or directory."\
.format(file_or_directory_path))
return jack_files
@staticmethod
def get_output_file(jack_file, file_tag=""):
"""Returns the .xml file path that corresponds with a given Jack file. A file tag can be added (e.g. to distinguish from a compare file)."""
return jack_file[:-5] + file_tag + ".xml"