|
| 1 | +# @author RUFFENACH Timothée |
| 2 | +# Version 1.0 |
| 3 | +# Scrip to edit file |
| 4 | +# Help me for lab burpsuite resolve with zap |
| 5 | + |
| 6 | +from javax.swing import JFrame, JPanel, JComboBox, JOptionPane,JFileChooser,JOptionPane |
| 7 | +from java.io import File, FileWriter |
| 8 | +from java.awt.event import WindowAdapter, WindowEvent |
| 9 | +from java.awt import Toolkit |
| 10 | + |
| 11 | +class SelectionMenu(JFrame): |
| 12 | + def __init__(self): |
| 13 | + JFrame.__init__(self, "Selection Menu") |
| 14 | + self.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) |
| 15 | + self.setSize(500, 300) |
| 16 | + self.getContentPane().setLayout(None) |
| 17 | + |
| 18 | + # Create selection menu |
| 19 | + options = ["Inserting a character string alternately", "Duplicate data file"] |
| 20 | + |
| 21 | + # Create scrolling menu |
| 22 | + self.dropdown = JComboBox(options) |
| 23 | + self.dropdown.setBounds(50, 50, 200, 30) |
| 24 | + self.dropdown.addActionListener(self.selection_changee) |
| 25 | + self.getContentPane().add(self.dropdown) |
| 26 | + |
| 27 | + # center windows |
| 28 | + self.center_window() |
| 29 | + |
| 30 | + # Add event listener for closing the window |
| 31 | + self.addWindowListener(CustomWindowAdapter()) |
| 32 | + |
| 33 | + def selection_changee(self, event): |
| 34 | + selected_option = self.dropdown.getSelectedItem() |
| 35 | + |
| 36 | + if selected_option == "Inserting a character string alternately": |
| 37 | + self.dropdown.setPopupVisible(False) |
| 38 | + self.fonction_option1() |
| 39 | + elif selected_option == "Duplicate data file": |
| 40 | + self.dropdown.setPopupVisible(False) |
| 41 | + self.fonction_option2() |
| 42 | + |
| 43 | + # close scrolling menu |
| 44 | + self.dropdown.setPopupVisible(False) |
| 45 | + |
| 46 | + def fonction_option1(self): |
| 47 | + |
| 48 | + # get mandatory data |
| 49 | + alternate = self.getNumber(1,100, "How many alternate do you want between [1 to 100]") |
| 50 | + print("alternate number", alternate) |
| 51 | + string = self.getString() |
| 52 | + print("string", string) |
| 53 | + filePath = self.chooseFile() |
| 54 | + print("path", filePath) |
| 55 | + |
| 56 | + |
| 57 | + # get number line of file |
| 58 | + file = open(filePath, "r") |
| 59 | + nb_line = 0 |
| 60 | + for line in file: |
| 61 | + nb_line += 1 |
| 62 | + file.close() |
| 63 | + |
| 64 | + # the file can't have 0 line |
| 65 | + if nb_line == 0: |
| 66 | + JOptionPane.showMessageDialog(None, "Empty file", "Alerte", JOptionPane.WARNING_MESSAGE) |
| 67 | + return 0 |
| 68 | + |
| 69 | + # alternate must slower line of file |
| 70 | + if alternate > nb_line: |
| 71 | + JOptionPane.showMessageDialog(None, "The alternate line don't not more greatet when the line of file", "Alerte", JOptionPane.WARNING_MESSAGE) |
| 72 | + return 0 |
| 73 | + |
| 74 | + # read data of file |
| 75 | + file = open(filePath, "r") |
| 76 | + data = file.readlines() |
| 77 | + file.close() |
| 78 | + |
| 79 | + # add \n to data |
| 80 | + string += "\n" |
| 81 | + |
| 82 | + # make new data |
| 83 | + i = alternate |
| 84 | + while i < len(data): |
| 85 | + data.insert(i,string) |
| 86 | + i += alternate + 1 |
| 87 | + |
| 88 | + self.saveFile(data) |
| 89 | + |
| 90 | + def fonction_option2(self): |
| 91 | + |
| 92 | + copy = self.getNumber(1,100, "How many copy data file do you want [1 to 100]") |
| 93 | + filePath = self.chooseFile() |
| 94 | + print("path", filePath) |
| 95 | + # get number line of file |
| 96 | + |
| 97 | + file = open(filePath, "r") |
| 98 | + nb_line = 0 |
| 99 | + for line in file: |
| 100 | + nb_line += 1 |
| 101 | + file.close() |
| 102 | + |
| 103 | + # the file can't have 0 line |
| 104 | + if nb_line == 0: |
| 105 | + JOptionPane.showMessageDialog(None, "Empty file", "Alerte", JOptionPane.WARNING_MESSAGE) |
| 106 | + return 0 |
| 107 | + |
| 108 | + # read data of file |
| 109 | + file = open(filePath, "r") |
| 110 | + data = file.readlines() |
| 111 | + file.close() |
| 112 | + |
| 113 | + dataCopy=[] |
| 114 | + for i in range(copy): |
| 115 | + dataCopy = dataCopy + data |
| 116 | + |
| 117 | + self.saveFile(dataCopy) |
| 118 | + |
| 119 | + def saveFile(self,data): |
| 120 | + # create instance JFileChooser |
| 121 | + file_chooser = JFileChooser() |
| 122 | + |
| 123 | + # debug |
| 124 | + #for i in range(len(data)): |
| 125 | + # print(data[i]) |
| 126 | + |
| 127 | + # show dialog box |
| 128 | + result = file_chooser.showSaveDialog(None) |
| 129 | + |
| 130 | + if result == JFileChooser.APPROVE_OPTION: |
| 131 | + # get select file |
| 132 | + file = file_chooser.getSelectedFile() |
| 133 | + |
| 134 | + # get path |
| 135 | + path_file = file.getAbsolutePath() |
| 136 | + |
| 137 | + # write data |
| 138 | + with open(path_file, "w") as file: |
| 139 | + for i in range(len(data)): |
| 140 | + file.write(data[i]) |
| 141 | + |
| 142 | + print("File saved :", path_file) |
| 143 | + else: |
| 144 | + print("Save cancel.") |
| 145 | + |
| 146 | + def chooseFile(self): |
| 147 | + fileChooser = JFileChooser() |
| 148 | + fileChooser.setMultiSelectionEnabled(True) |
| 149 | + filePath = "" |
| 150 | + result = fileChooser.showOpenDialog(None) |
| 151 | + |
| 152 | + if result == JFileChooser.APPROVE_OPTION: |
| 153 | + selectedFiles = fileChooser.getSelectedFiles() |
| 154 | + for file in selectedFiles: |
| 155 | + filePath = file.getAbsolutePath() |
| 156 | + print('The path is :', filePath) |
| 157 | + |
| 158 | + return filePath |
| 159 | + |
| 160 | + |
| 161 | + def getNumber(self,min,max,asked): |
| 162 | + number = JOptionPane.showInputDialog(None, asked, "Input", JOptionPane.QUESTION_MESSAGE) |
| 163 | + |
| 164 | + if int(number) >= min and int(number) <= max: |
| 165 | + number = int(number) |
| 166 | + return number |
| 167 | + else: |
| 168 | + JOptionPane.showMessageDialog(None, "Choose number between " + min + " to " + max) |
| 169 | + self.chooseNumber() |
| 170 | + |
| 171 | + |
| 172 | + def getString(self): |
| 173 | + stringInput = JOptionPane.showInputDialog(None, "what is your string : ", "Input", JOptionPane.QUESTION_MESSAGE) |
| 174 | + |
| 175 | + return stringInput |
| 176 | + |
| 177 | + |
| 178 | + def center_window(self): |
| 179 | + screenSize = Toolkit.getDefaultToolkit().getScreenSize() |
| 180 | + screenWidth = screenSize.width |
| 181 | + screenHeight = screenSize.height |
| 182 | + windowWidth = self.getWidth() |
| 183 | + windowHeight = self.getHeight() |
| 184 | + |
| 185 | + # caclul to center windows |
| 186 | + posX = (screenWidth - windowWidth) // 2 |
| 187 | + posY = (screenHeight - windowHeight) // 2 |
| 188 | + |
| 189 | + self.setLocation(posX, posY) |
| 190 | + |
| 191 | +class CustomWindowAdapter(WindowAdapter): |
| 192 | + def windowClosing(self, event): |
| 193 | + confirm_closing() |
| 194 | + |
| 195 | +def confirm_closing(): |
| 196 | + reponse = JOptionPane.showConfirmDialog(None, "Do you want close script ?", "Confirmation", JOptionPane.YES_NO_OPTION) |
| 197 | + if reponse == JOptionPane.YES_OPTION: |
| 198 | + menu.dispose() # close windows en free up resouces |
| 199 | + else: |
| 200 | + pass # do nothing |
| 201 | + |
| 202 | +# menu |
| 203 | +menu = SelectionMenu() |
| 204 | +menu.setVisible(True) |
| 205 | + |
0 commit comments