-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktop-alright.py
executable file
·74 lines (55 loc) · 1.92 KB
/
desktop-alright.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
#!/usr/bin/env python
import io
import os
import sys
from os.path import expanduser
from subprocess import call
from subprocess import check_output
def main():
if len(sys.argv) != 2:
print_usage()
sys.exit(1)
if sys.argv[1] == ACTION_SAVE:
save()
elif sys.argv[1] == ACTION_RESTORE:
restore()
else:
print_usage()
sys.exit(1)
def save():
desktop_dir = get_desktop_dir()
data = []
for entry in os.listdir(desktop_dir):
path = os.path.join(desktop_dir, entry)
info = check_output([CMD_GVFS_INFO, "-a", ATTR_ICON_POSITION, path]).splitlines()
nemo_icon_position = filter(lambda x: ATTR_ICON_POSITION in x, info)[0].strip()
pos = nemo_icon_position.split()[1]
data.append(pos + DAT_DELIMITER + entry)
with io.open(get_dat_path(), "w") as f:
f.write("\n".join(data))
call(["notify-send", "Icon positions saved"])
def restore():
desktop_dir = get_desktop_dir()
for line in io.open(get_dat_path()):
(pos, entry) = line.strip().split(DAT_DELIMITER)
path = os.path.join(desktop_dir, entry)
call([CMD_GVFS_SET_ATTRIBUTE, "-t", "string", path, ATTR_ICON_POSITION, pos])
call(["notify-send", "Icon positions will be restored after relogin"])
def get_home_dir():
return expanduser("~")
def get_desktop_dir():
return check_output([CMD_XDG_USER_DIR, "DESKTOP"]).decode("utf-8").rstrip("\r\n")
def get_dat_path():
return os.path.join(get_home_dir(), DAT_FILE)
def print_usage():
print("Usage:\n " + sys.argv[0] + " " + ACTION_SAVE + "\nor\n " + sys.argv[0] + " " + ACTION_RESTORE)
DAT_FILE = ".desktop_alright"
DAT_DELIMITER = ">"
ACTION_SAVE = "save"
ACTION_RESTORE = "restore"
ATTR_ICON_POSITION = "metadata::nemo-icon-position"
CMD_XDG_USER_DIR = "xdg-user-dir"
CMD_GVFS_INFO = "gvfs-info"
CMD_GVFS_SET_ATTRIBUTE = "gvfs-set-attribute"
if __name__ == '__main__':
main()