-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsftp_editor.py
More file actions
376 lines (321 loc) · 14.1 KB
/
Copy pathsftp_editor.py
File metadata and controls
376 lines (321 loc) · 14.1 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
sftp_editor.py — Editor file remoti SFTP per PCM (GTK3)
Modalità automatica:
- Se in Settings è configurato un editor GUI (mousepad, gedit, kate, code…):
scarica in file temp → apre l'editor esterno → al termine del processo
ricarica il temp e ricarica le modifiche sul server se il file è cambiato.
- Se l'editor configurato è un editor da terminale (nano, vim…) oppure non è
configurato: usa il widget interno GtkSource.View / GtkTextView.
"""
import hashlib
import io
import os
import subprocess
import tempfile
import threading
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib, Pango
try:
gi.require_version("GtkSource", "3.0")
from gi.repository import GtkSource
_GTKSOURCE = True
except (ValueError, ImportError):
_GTKSOURCE = False
from pcm_logging import get_logger as _get_log
# Editor che richiedono un terminale — non apribili come processo GUI standalone
_TERM_EDITORS = {
"nano", "vi", "vim", "nvim", "neovim", "emacs", "pico",
"micro", "mcedit", "joe", "jed", "ne", "mg", "ed",
}
# Mappa estensione → id linguaggio GtkSource
_EXT_LANG = {
".py": "python", ".sh": "sh", ".bash": "sh", ".zsh": "sh",
".js": "js", ".ts": "typescript", ".json": "json",
".yaml": "yaml", ".yml": "yaml", ".toml": "toml",
".xml": "xml", ".html": "html", ".css": "css",
".c": "c", ".h": "c", ".cpp": "cpp", ".hpp": "cpp",
".rs": "rust", ".go": "go", ".rb": "ruby",
".sql": "sql", ".md": "markdown",
".conf": "ini", ".ini": "ini", ".cfg": "ini",
".dockerfile": "dockerfile",
}
def _get_configured_editor() -> str:
"""
Ritorna il comando editor da Settings, vuoto se non configurato o da terminale.
Gestisce:
- editor binari nel PATH ("mousepad", "gedit", "NotePadPQ" …)
- script Python ("python3 /path/to/main.py")
- percorsi assoluti a script/eseguibili
Se il valore salvato è solo un nome (es. "NotePadPQ"), cerca nel PATH con
shutil.which; se non è trovato nel PATH viene comunque accettato come editor
GUI (l'utente sa cosa fa) purché non sia un editor da terminale.
"""
try:
import shutil as _shutil
import config_manager
ed = config_manager.load_settings().get("general", {}).get("default_editor", "").strip()
if not ed:
return ""
parts = ed.split()
# Caso "python3 /path/script.py"
if parts[0] in ("python3", "python") and len(parts) >= 2:
return ed
binary = os.path.basename(parts[0])
# Scarta editor da terminale (nano, vim, vi, …)
if binary in _TERM_EDITORS:
return ""
# Accetta se trovato nel PATH oppure se è un percorso assoluto valido
if _shutil.which(parts[0]) or os.path.isfile(parts[0]):
return ed
# Nome non nel PATH (es. "NotePadPQ" non installato come comando) —
# accettalo comunque come editor GUI: sarà subprocess a dare errore
# se davvero non esiste, con un messaggio chiaro all'utente.
if binary:
return ed
except Exception as e:
_get_log(__name__).debug("GUI editor detection failed: %s", e)
return ""
def _file_hash(path: str) -> bytes:
try:
with open(path, "rb") as f:
return hashlib.sha256(f.read()).digest()
except Exception:
return b""
# ---------------------------------------------------------------------------
# Helpers view interna
# ---------------------------------------------------------------------------
def _make_view() -> tuple:
"""Ritorna (view, buffer). Usa GtkSource se disponibile."""
if _GTKSOURCE:
buf = GtkSource.Buffer()
sm = GtkSource.StyleSchemeManager.get_default()
for sid in ("oblivion", "dark", "classic"):
scheme = sm.get_scheme(sid)
if scheme:
buf.set_style_scheme(scheme)
break
view = GtkSource.View.new_with_buffer(buf)
view.set_show_line_numbers(True)
view.set_auto_indent(True)
view.set_tab_width(4)
view.set_insert_spaces_instead_of_tabs(True)
view.set_highlight_current_line(True)
else:
buf = Gtk.TextBuffer()
view = Gtk.TextView.new_with_buffer(buf)
view.set_monospace(True)
view.set_wrap_mode(Gtk.WrapMode.NONE)
return view, buf
def _set_language(buf, remote_path: str):
if not _GTKSOURCE:
return
_, ext = os.path.splitext(remote_path.lower())
lang_id = _EXT_LANG.get(ext)
if lang_id:
lang = GtkSource.LanguageManager.get_default().get_language(lang_id)
if lang:
buf.set_language(lang)
# ---------------------------------------------------------------------------
# Widget principale
# ---------------------------------------------------------------------------
class SftpEditorWidget(Gtk.Box):
"""
Tab editor file remoto SFTP.
sftp — paramiko.SFTPClient già aperto
remote_path — percorso assoluto sul server
"""
def __init__(self, sftp, remote_path: str):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=0)
self._sftp = sftp
self._remote_path = remote_path
self._modified = False
self._tmp_path = None # solo in modalità editor esterno
self._ext_editor = _get_configured_editor()
self._build_ui()
self.connect("destroy", lambda w: self._cleanup_tmp())
threading.Thread(target=self._load, daemon=True).start()
# ------------------------------------------------------------------
# UI
# ------------------------------------------------------------------
def _build_ui(self):
tb = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
tb.set_margin_start(6); tb.set_margin_end(6)
tb.set_margin_top(4); tb.set_margin_bottom(4)
lbl = Gtk.Label(label=self._remote_path)
lbl.set_xalign(0.0)
lbl.set_hexpand(True)
lbl.set_ellipsize(Pango.EllipsizeMode.START)
lbl.set_selectable(True)
tb.pack_start(lbl, True, True, 0)
if self._ext_editor:
# Modalità editor esterno: bottone "Riapri" + info editor
editor_name = os.path.basename(self._ext_editor.split()[0])
lbl_ed = Gtk.Label()
lbl_ed.set_markup(f"<small>Editor: <b>{editor_name}</b></small>")
lbl_ed.set_margin_end(6)
tb.pack_start(lbl_ed, False, False, 0)
self._btn_reopen = Gtk.Button(label="↗ Riapri")
self._btn_reopen.set_tooltip_text(f"Riapre il file in {editor_name}")
self._btn_reopen.connect("clicked", self._on_reopen)
self._btn_reopen.set_sensitive(False)
tb.pack_start(self._btn_reopen, False, False, 0)
else:
# Modalità built-in: bottone "Salva"
self._btn_save = Gtk.Button(label="💾 Salva")
self._btn_save.connect("clicked", self._on_save)
self._btn_save.set_sensitive(False)
tb.pack_start(self._btn_save, False, False, 0)
self.pack_start(tb, False, False, 0)
self.pack_start(
Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), False, False, 0
)
if self._ext_editor:
# Area informativa (non editabile)
self._info_area = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
self._info_area.set_valign(Gtk.Align.CENTER)
self._info_area.set_halign(Gtk.Align.CENTER)
self._info_area.set_vexpand(True)
self._info_icon = Gtk.Label()
self._info_icon.set_markup('<span font="32">📝</span>')
self._info_lbl = Gtk.Label(label="Scaricamento in corso…")
self._info_lbl.set_justify(Gtk.Justification.CENTER)
self._info_area.pack_start(self._info_icon, False, False, 0)
self._info_area.pack_start(self._info_lbl, False, False, 0)
self.pack_start(self._info_area, True, True, 0)
else:
self._view, self._buf = _make_view()
_set_language(self._buf, self._remote_path)
self._buf.connect("changed", self._on_changed)
scroll = Gtk.ScrolledWindow()
scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
scroll.add(self._view)
self.pack_start(scroll, True, True, 0)
self._status = Gtk.Label(label="Caricamento…")
self._status.set_xalign(0.0)
self._status.set_margin_start(6)
self._status.set_margin_top(2); self._status.set_margin_bottom(2)
self.pack_start(self._status, False, False, 0)
def _on_changed(self, _buf):
self._modified = True
self._btn_save.set_sensitive(True)
# ------------------------------------------------------------------
# Download
# ------------------------------------------------------------------
def _load(self):
try:
if self._ext_editor:
self._load_to_temp()
else:
self._load_to_buf()
except Exception as e:
GLib.idle_add(self._set_status, f"✖ Errore caricamento: {e}")
def _load_to_buf(self):
buf_io = io.BytesIO()
self._sftp.getfo(self._remote_path, buf_io)
content = buf_io.getvalue().decode("utf-8", errors="replace")
GLib.idle_add(self._set_content_buf, content)
def _load_to_temp(self):
fname = os.path.basename(self._remote_path)
# Mantieni l'estensione originale così l'editor rileva il tipo
suffix = os.path.splitext(fname)[1] or ".txt"
fd, tmp = tempfile.mkstemp(suffix=suffix, prefix="pcm_sftp_")
os.close(fd)
self._sftp.get(self._remote_path, tmp)
self._tmp_path = tmp
GLib.idle_add(self._after_download)
def _after_download(self):
editor_name = os.path.basename(self._ext_editor.split()[0])
self._info_lbl.set_text(
f"File scaricato — si aprirà in {editor_name}.\n"
"Al salvataggio nell'editor, le modifiche verranno caricate sul server."
)
self._btn_reopen.set_sensitive(True)
self._set_status(f"✔ Pronto — {self._remote_path}")
self._open_ext_editor()
# ------------------------------------------------------------------
# Editor esterno
# ------------------------------------------------------------------
def _open_ext_editor(self):
if not self._tmp_path:
return
hash_before = _file_hash(self._tmp_path)
cmd = self._ext_editor.split() + [self._tmp_path]
try:
proc = subprocess.Popen(cmd)
except FileNotFoundError:
GLib.idle_add(self._set_status,
f"✖ Editor non trovato: {self._ext_editor}")
return
GLib.idle_add(self._set_status,
f"✎ {os.path.basename(self._ext_editor.split()[0])} aperto…")
def _watch():
proc.wait()
hash_after = _file_hash(self._tmp_path)
if hash_after and hash_after != hash_before:
GLib.idle_add(self._set_status, "⬆ Caricamento modifiche…")
self._upload_from_temp()
else:
GLib.idle_add(self._set_status, "✔ Nessuna modifica")
threading.Thread(target=_watch, daemon=True).start()
def _on_reopen(self, _btn=None):
if self._tmp_path and os.path.exists(self._tmp_path):
threading.Thread(target=self._open_ext_editor, daemon=True).start()
else:
threading.Thread(target=self._reload_and_open, daemon=True).start()
def _reload_and_open(self):
GLib.idle_add(self._set_status, "Riscaricamento…")
try:
self._load_to_temp()
except Exception as e:
GLib.idle_add(self._set_status, f"✖ {e}")
def _upload_from_temp(self):
try:
self._sftp.put(self._tmp_path, self._remote_path)
GLib.idle_add(self._set_status, f"✔ Salvato: {self._remote_path}")
except Exception as e:
GLib.idle_add(self._set_status, f"✖ Errore upload: {e}")
# ------------------------------------------------------------------
# Modalità built-in: salvataggio
# ------------------------------------------------------------------
def _set_content_buf(self, text: str):
self._buf.handler_block_by_func(self._on_changed)
self._buf.set_text(text)
self._buf.handler_unblock_by_func(self._on_changed)
self._modified = False
self._btn_save.set_sensitive(False)
self._set_status(f"✔ {self._remote_path}")
def _on_save(self, _btn=None):
if not self._sftp:
return
start, end = self._buf.get_bounds()
text = self._buf.get_text(start, end, False)
self._btn_save.set_sensitive(False)
self._set_status("Salvataggio…")
threading.Thread(target=self._save_remote, args=(text,), daemon=True).start()
def _save_remote(self, text: str):
try:
self._sftp.putfo(io.BytesIO(text.encode("utf-8")), self._remote_path)
GLib.idle_add(self._set_status, f"✔ Salvato: {self._remote_path}")
self._modified = False
except Exception as e:
GLib.idle_add(self._set_status, f"✖ Errore salvataggio: {e}")
GLib.idle_add(self._btn_save.set_sensitive, True)
# ------------------------------------------------------------------
# Cleanup
# ------------------------------------------------------------------
def _cleanup_tmp(self):
if self._tmp_path:
try:
os.unlink(self._tmp_path)
except Exception:
from pcm_logging import get_logger
get_logger(__name__).warning("Impossibile cancellare file temporaneo '%s'", self._tmp_path, exc_info=True)
self._tmp_path = None
def _set_status(self, msg: str):
self._status.set_text(msg)
def grab_focus(self):
if self._ext_editor:
self._info_area.grab_focus()
else:
self._view.grab_focus()