forked from chr314/nautilus-copy-path
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnautilus_copy_path.py
153 lines (124 loc) · 5.37 KB
/
nautilus_copy_path.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
151
152
153
import os
import json
import shlex
from urllib.parse import urlparse, unquote
from translation import Translation
from gi.repository import Nautilus, GObject, Gtk, Gdk
from gi import require_version
require_version('Gtk', '3.0')
require_version('Nautilus', '3.0')
class NautilusCopyPath(Nautilus.MenuProvider, GObject.GObject, Nautilus.LocationWidgetProvider):
def __init__(self):
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
self.clipboard_primary = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
self.config = {
"items": {
"path": True,
"uri": True,
"name": True
},
"selections": {
"clipboard": True,
"primary": True
},
"shortcuts": {
"path": "<Ctrl><Shift>C",
"uri": "<Ctrl><Shift>U",
"name": "<Ctrl><Shift>D"
},
"language": "auto",
"separator": ", ",
"escape_value_items": False,
"escape_value": False,
"name_ignore_extension": False
}
with open(os.path.join(os.path.dirname(__file__), "config.json")) as json_file:
try:
self.config.update(json.load(json_file))
if self.config["language"]:
Translation.select_language(self.config["language"])
except:
pass
self.accel_group = Gtk.AccelGroup()
for key in self.config["shortcuts"]:
try:
keyval, modifier = Gtk.accelerator_parse(self.config["shortcuts"][key])
self.accel_group.connect(keyval, modifier, Gtk.AccelFlags.VISIBLE,
lambda *args, action=key: self._shortcuts_handler(action, *args))
except:
pass
self.window = None
def _shortcuts_handler(self, action, accel_group, acceleratable, keyval, modifier):
items = self._get_selection()
action_function = {'path': self._copy_paths, 'uri': self._copy_uris, 'name': self._copy_names}[action]
if len(items) > 0 and action_function:
action_function(None, items)
return True
def get_file_items(self, window, files):
return self._create_menu_items(files, "File")
def get_background_items(self, window, file):
return self._create_menu_items([file], "Background")
def get_widget(self, uri, window):
if self.window:
self.window.remove_accel_group(self.accel_group)
window.add_accel_group(self.accel_group)
self.window = window
return None
def _get_selection(self):
focus = self.window.get_focus()
items = []
if not isinstance(focus, Gtk.TreeView) and focus.get_parent().get_name() == 'NautilusListView':
return items
focus.get_selection().selected_foreach(lambda tree, path, iter, uris: uris.append(tree[iter][0]), items)
return items
def _create_menu_items(self, files, group):
plural = len(files) > 1
config_items = self.config["items"]
active_items = []
if config_items["path"]:
item_path = Nautilus.MenuItem(
name="NautilusCopyPath::CopyPath" + group,
label=Translation.t("copy_paths" if plural else "copy_path"),
)
item_path.connect("activate", self._copy_paths, files)
active_items.append(item_path)
if config_items["uri"]:
item_uri = Nautilus.MenuItem(
name="NautilusCopyPath::CopyUri" + group,
label=Translation.t("copy_uris" if plural else "copy_uri"),
)
item_uri.connect("activate", self._copy_uris, files)
active_items.append(item_uri)
if config_items["name"]:
item_name = Nautilus.MenuItem(
name="NautilusCopyPath::CopyName" + group,
label=Translation.t("copy_names" if plural else "copy_name"),
)
item_name.connect("activate", self._copy_names, files)
active_items.append(item_name)
return active_items
def _copy_paths(self, menu, files):
def _uri_to_path(file):
p = urlparse(file.get_activation_uri())
return os.path.abspath(os.path.join(p.netloc, unquote(p.path)))
self._copy_value(list(map(_uri_to_path, files)))
def _copy_uris(self, menu, files):
self._copy_value(list(map(lambda f: f.get_activation_uri(), files)))
def _copy_names(self, menu, files):
def _name(file):
path = unquote(os.path.basename(file.get_activation_uri()))
if self.config["name_ignore_extension"]:
path = os.path.splitext(path)[0]
return path
self._copy_value(list(map(_name, files)))
def _copy_value(self, value):
if len(value) > 0:
if self.config["escape_value_items"]:
value = list(map(lambda x: shlex.quote(x), value))
new_value = self.config["separator"].join(value)
if self.config["escape_value"]:
new_value = shlex.quote(new_value)
if self.config["selections"]["clipboard"]:
self.clipboard.set_text(new_value, -1)
if self.config["selections"]["primary"]:
self.clipboard_primary.set_text(new_value, -1)