-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
130 lines (107 loc) · 3.52 KB
/
run.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
import os
import subprocess
from urllib.parse import unquote
import dbus
import dbus.mainloop.glib
from gi.repository import GLib
previous_locations = []
def gsconnectMount():
return os.path.join(os.environ["HOME"], ".gsconnectMounts")
def mount_port(id):
# dconf read /org/gnome/shell/extensions/gsconnect/device/DEVICE_ID/mount-port
return (
subprocess.check_output(
[
"dconf",
"read",
f"/org/gnome/shell/extensions/gsconnect/device/{id}/mount-port",
]
)
.decode("utf-8")
.strip()
)
def string_to_array(string):
# string to array of strings
string = string.strip()
string = string[1:-1]
string = string.split(",")
string = [x.strip() for x in string]
string = [x[1:-1] for x in string]
return string
def multi_paths(id):
# dconf read /org/gnome/shell/extensions/gsconnect/device/DEVICE_ID/multi-paths
return string_to_array(
subprocess.check_output(
[
"dconf",
"read",
f"/org/gnome/shell/extensions/gsconnect/device/{id}/multi-paths",
]
).decode("utf-8")
)
def path_names(id):
# dconf read /org/gnome/shell/extensions/gsconnect/device/DEVICE_ID/path-names
return string_to_array(
subprocess.check_output(
[
"dconf",
"read",
f"/org/gnome/shell/extensions/gsconnect/device/{id}/path-names",
]
).decode("utf-8")
)
def last_connection(id):
data = subprocess.check_output(
[
"dconf",
"read",
f"/org/gnome/shell/extensions/gsconnect/device/{id}/last-connection",
]
).decode("utf-8")[1:-1]
# 'lan://192.168.0.5:1716'
return data[6:].split(":")[0]
def handle_signal(*args, **kwargs):
global previous_locations
open_locations = []
for arg in args:
if isinstance(arg, dbus.Dictionary):
for key, value in arg.items():
if key == "OpenWindowsWithLocations":
for window, locations in value.items():
for location in locations:
open_locations.append(location)
for location in open_locations:
if location.startswith(f"file://{gsconnectMount()}"):
if location in previous_locations:
previous_locations.remove(location)
continue
handle_open_location(location)
previous_locations = open_locations
def handle_open_location(location):
if location == f"file://{gsconnectMount()}":
return
location = location[len(f"file://{gsconnectMount()}") + 1 :]
location = location.split("/")
if len(location) == 1:
return
id_of_device = location[0].split("___")[1]
location = unquote(location[-1])
mapping = dict(zip(path_names(id_of_device), multi_paths(id_of_device)))
mount_port_ = mount_port(id_of_device)
last_connection_ = last_connection(id_of_device)
os.system(
f"nautilus sftp://{last_connection_}:{mount_port_}{mapping[location]} > /dev/null 2>&1 &"
)
def main():
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_signal_receiver(
handle_signal,
dbus_interface="org.freedesktop.DBus.Properties",
signal_name="PropertiesChanged",
path="/org/freedesktop/FileManager1",
)
loop = GLib.MainLoop()
loop.run()
if __name__ == "__main__":
main()