-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (58 loc) · 2.02 KB
/
main.py
File metadata and controls
67 lines (58 loc) · 2.02 KB
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
from sys import argv, exit
import os
from interpreters.helpers import displayObject
from interpreter_index import interpreters
debug = False
def getFileExt(myFile):
mySplit = myFile.split('.')
for i in mySplit:
pass
return i
if len(argv) == 3:
script, myFile, myOutput = argv
if os.path.exists(myFile) is False:
print("ERROR: File does not exist, please double-check the file path")
exit()
myType = getFileExt(myFile)
if debug:
print("Extension: " + myType)
reader = ''
for item in interpreters:
for extension in item[0]:
if extension == myType.lower():
reader = item[2]()
break
if reader == '':
print("ERROR: Could not find an interpreter compatible with " + myType + " file extension")
exit()
writer = ''
for item in interpreters:
if item[1].lower() == myOutput.lower():
writer = item[2]()
destType = item[0][0]
break
if writer == '':
print("ERROR: Could not find an interpreter for " + myOutput)
exit()
session = reader.read(myFile, debug)
if debug:
displayObject(session)
destinationFile = myFile.replace(myType, destType)
# Add a function to check if the file already exists, and if it does
# if the user wants to overwrite the file, or cancel the conversion
if os.path.exists(destinationFile):
answerReceived = False
while answerReceived == False:
myAnswer = input("File " + destinationFile + " already exists, would you like to overwrite it? [y/N]")
if myAnswer.lower() == 'n' or myAnswer.lower() == 'y':
answerReceived = True
if myAnswer.lower() == 'n':
print('Conversion stopped')
exit()
output = writer.write(session, debug)
myOutputFile = open(destinationFile, mode='w')
myOutputFile.write(output)
myOutputFile.close()
print("Conversion complete")
else:
print("ERROR: Wrong number of arguments")