| 132 | return self.get_editor() |
| 133 | |
| 134 | def spawn_editor(self, data: T) -> T: |
| 135 | text = isinstance(data, str) |
| 136 | fd, name = tempfile.mkstemp("", "mitmproxy", text=text) |
| 137 | with_hexeditor = isinstance(data, bytes) and strutils.is_mostly_bin(data) |
| 138 | with open(fd, "w" if text else "wb") as f: |
| 139 | f.write(data) |
| 140 | if with_hexeditor: |
| 141 | c = self.get_hex_editor() |
| 142 | else: |
| 143 | c = self.get_editor() |
| 144 | cmd = shlex.split(c) |
| 145 | cmd.append(name) |
| 146 | with self.uistopped(): |
| 147 | try: |
| 148 | subprocess.call(cmd) |
| 149 | except Exception: |
| 150 | signals.status_message.send(message="Can't start editor: %s" % c) |
| 151 | else: |
| 152 | with open(name, "r" if text else "rb") as f: |
| 153 | data = f.read() |
| 154 | os.unlink(name) |
| 155 | return data |
| 156 | |
| 157 | def spawn_external_viewer(self, data, contenttype): |
| 158 | if contenttype: |