/
TigrReader.py
76 lines (63 loc) · 2.81 KB
/
TigrReader.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from TIGr import AbstractSourceReader
from CommandFactory import CommandFactory
from RegexParser import RegexParser
from TurtleDrawer import TurtleDrawer
from TigrExceptionHandler import TigrExceptionHandler
import sys
class TigrReader(AbstractSourceReader):
def __init__(self, parser, exception_handler, optional_file_name=None, optional_source=None):
super().__init__(parser, optional_file_name)
self.exception_handler = exception_handler
if optional_source:
self.source = optional_source
def go(self):
if not self.source:
if not self.file_name:
self.file_name = input("Please enter a file name: ")
try:
self.source = open(self.file_name).readlines()
except (IOError, FileNotFoundError) as e:
self.exception_handler.display_and_exit(e, "Error loading source code from file")
self.parser.parse(self.source)
if __name__ == "__main__":
# check for file name arguments
import argparse
import time
'''
example usage within a terminal:
> python TigrReader.py -f commands.txt
> type commands.txt | python TigrReader.py
> python TigrReader.py
> Enter your commands. Ctrl + Z to exit
> If no commands are entered, you will be prompted for a file name.
'''
arg_parser = argparse.ArgumentParser(description="Extract filename if present")
arg_parser.add_argument("-f", "--file", help="Name of the file", default=None)
args = arg_parser.parse_args()
the_exception_handler = TigrExceptionHandler("TIGr encountered an error and had to exit")
if args.file:
# file name provided - read input from file
TigrReader(RegexParser(CommandFactory(TurtleDrawer()),
the_exception_handler),
the_exception_handler,
optional_file_name=args.file).go()
else:
# read from stdin
# TODO examine if this section of the code can be incorporated within the class structure
source = None
if sys.stdin.isatty():
# read from input at prompt
print("Enter your commands. Ctrl + Z to exit or finish.")
# only on windows, if this was portable we should add the linux interrupt command x3
print("If no commands are entered, you will be prompted for a file name.")
source = sys.stdin.readlines()
else:
# read from piped input
source = sys.stdin.readlines()
reader = TigrReader(RegexParser(CommandFactory(TurtleDrawer()),
the_exception_handler),
the_exception_handler,
optional_source=source)
reader.go()
print(*reader.parser.output_log)
time.sleep(10)