Save the program :param file: The name and path of the save file
(self, file, infile, tmpfile)
| 81 | print() |
| 82 | |
| 83 | def save(self, file, infile, tmpfile): |
| 84 | """Save the program |
| 85 | |
| 86 | :param file: The name and path of the save file |
| 87 | |
| 88 | """ |
| 89 | retCode = False |
| 90 | |
| 91 | ans = "Y" |
| 92 | if file in listdir(): |
| 93 | ans = input("Overwrite "+file+" (y/n): ").upper() |
| 94 | |
| 95 | if ans == "Y": |
| 96 | |
| 97 | if file+".pYb" in listdir(): |
| 98 | remove(file+".pYb") |
| 99 | |
| 100 | try: |
| 101 | with open(file+".pYb", 'w') as outfile: |
| 102 | |
| 103 | line_numbers = self.line_numbers() |
| 104 | |
| 105 | if file.split(".")[-1].upper() == "PGM": |
| 106 | filelen = 0 |
| 107 | for line_number in line_numbers: |
| 108 | statement = self.getprogram(line_number,infile,tmpfile) |
| 109 | if len(statement) > 1 and statement[0].lexeme == "DATA": |
| 110 | sign = -1 |
| 111 | else: |
| 112 | sign = 1 |
| 113 | fileLine = str(line_number)+","+str(sign*self.__program[line_number]) |
| 114 | outfile.write(fileLine+"\n") |
| 115 | filelen += (len(fileLine)+(0 if self.__imp == 'X' else 1)) |
| 116 | outfile.write("-999,-999\n") |
| 117 | filelen += (10 if self.__imp == 'X' else 11) |
| 118 | |
| 119 | for line_number in line_numbers: |
| 120 | fileLine = str(line_number) |
| 121 | |
| 122 | statement = self.getprogram(line_number,infile,tmpfile) |
| 123 | for token in statement: |
| 124 | # Add in quotes for strings |
| 125 | if token.category == Token.STRING: |
| 126 | fileLine += ' "' + token.lexeme + '"' |
| 127 | |
| 128 | else: |
| 129 | fileLine += " " + token.lexeme |
| 130 | outfile.write(fileLine+"\n") |
| 131 | |
| 132 | retCode = True |
| 133 | |
| 134 | except OSError: |
| 135 | print("Could not save to file") |
| 136 | |
| 137 | return retCode |
| 138 | |
| 139 | def load(self, file, tmpfile): |
| 140 | """Load the program |
no test coverage detected