-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp.py
152 lines (121 loc) · 4.38 KB
/
scp.py
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
from gi.repository import GObject, Gedit, Gdk, Gio, Gtk, PeasGtk, GLib
import subprocess
import os
import time
import re
SETTINGS_SCHEMA = "org.gnome.gedit.plugins.scp"
# UI manager snippet to add menu items to the View menu
ui_str = """
<ui>
<menubar name="MenuBar">
<menu name="ViewMenu" action="View">
<placeholder name="ViewOps_2">
<separator/>
<menuitem name="ScpUpload" action="ScpUploadAction"/>
<separator/>
</placeholder>
</menu>
</menubar>
</ui>
"""
class ScpAppPlugin(GObject.Object, Gedit.AppActivatable, PeasGtk.Configurable):
app = GObject.property(type=Gedit.App)
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
pass
def do_create_configure_widget(self):
widget = Gtk.Table(1,2,False)
label = Gtk.Label("Server:")
entry = Gtk.Entry()
settings = Gio.Settings.new(SETTINGS_SCHEMA)
server = settings.get_string('server')
entry.set_text(server)
entry.connect('key-press-event', self.on_key_press_event)
widget.attach(label, 0, 1, 0, 1)
widget.attach(entry, 1, 2, 0, 1)
widget.set_border_width(6)
return widget
def on_key_press_event(self, widget, event):
txt = widget.get_text()
settings = Gio.Settings.new(SETTINGS_SCHEMA)
settings.set_value('server', GLib.Variant("s", txt))
def do_deactivate(self):
pass
def do_update_state(self):
pass
class ScpWindowPlugin(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = "ScpPlugin"
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
icon = Gtk.Image.new_from_stock(Gtk.STOCK_YES, Gtk.IconSize.MENU)
sw = Gtk.ScrolledWindow()
self.textarea = Gtk.TextView()
panel = self.window.get_bottom_panel()
panel.add_item(self.textarea, "ScpBottomPanel", "Scp Upload", icon)
panel.activate_item(self.textarea)
self._insert_menu()
self._accel_group = Gtk.AccelGroup()
self.window.add_accel_group(self._accel_group)
if not isinstance('scp-upload', Gtk.Action):
action = self._action_group.get_action('scp-upload')
if not action:
return
self._accel_group.connect_group(Gdk.KEY_U, Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK, Gtk.ACCEL_LOCKED, self.upload)
self.window.set_data("Scp", handlers)
def upload(self, group):
tab = self.window.get_active_tab()
document = tab.get_document()
filename = document.get_uri_for_display()
panel = self.window.get_bottom_panel()
panel.activate_item(self.textarea)
visible = panel.get_property("visible")
panel.set_property("visible", True)
settings = Gio.Settings.new(SETTINGS_SCHEMA)
server = settings.get_string('server')
# get root
base = 'org.gnome.gedit.plugins.filebrowser'
settings = Gio.Settings.new(base)
root = settings.get_string('virtual-root')
if root is not None:
root = re.sub(r'^file://','', root)
dest = re.sub(root+'/', '', filename)
p = subprocess.Popen("scp " + filename +" "+server+'/'+dest, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
txt = "'%s' uploading " % dest
self.addtext(self.textarea, txt)
while p.poll() is None:
while Gtk.events_pending():
Gtk.main_iteration()
self.addtext(self.textarea,".")
time.sleep(0.1)
if p.returncode == 0:
self.addtext(self.textarea,"success\n")
else:
self.addtext(self.textarea,"Error: "+p.stderr.read()+"\n")
while Gtk.events_pending():
Gtk.main_iteration()
time.sleep(1)
panel.set_property("visible", visible)
def do_deactivate(self):
self._accel_group = None
self._action_group = None
def do_update_state(self):
pass
def _insert_menu(self):
# Get the GtkUIManager
manager = self.window.get_ui_manager()
# Create a new action group
self._action_group = Gtk.ActionGroup("GeditTextSizePluginActions")
self._action_group.add_actions([("ScpUploadAction", None, _("_Upload to SFTP"),
"<Ctrl><Alt>U", None,
self.upload)])
# Insert the action group
manager.insert_action_group(self._action_group)
# Merge the UI
self._ui_id = manager.add_ui_from_string(ui_str)
def addtext(self, TV,text):
buffer = TV.get_buffer()
iter = buffer.get_iter_at_mark(buffer.get_insert())
buffer.insert(iter,text) # use "\n" for newlines