-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_location.py
executable file
·74 lines (68 loc) · 2.68 KB
/
sync_location.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 python3
import json
import os
import platform
import sys
from xml.dom import minidom
# Read the syncthing config to locate the folders.
# Only folders synced on the current device will be available as globals.
folders = {}
config_file = "~/.config/syncthing/config.xml"
if platform.system().lower() == "darwin":
config_file = "~/Library/Application Support/Syncthing/config.xml"
if platform.system().lower() == "windows":
config_file = "~/AppData/Local/Syncthing/config.xml"
config_file = os.path.expanduser(config_file)
if os.path.isfile(config_file):
config_xml = minidom.parse(config_file)
for configured_folder in config_xml.getElementsByTagName("folder"):
folder = configured_folder.attributes["label"].value
if not folder:
folder = configured_folder.attributes["id"].value
if not folder:
continue
folders[folder] = configured_folder.attributes["path"].value
if not folders[folder].endswith("/"):
folders[folder] += "/"
globals()[folder] = folders[folder]
# The download folder is a special entry located at the first of these:
# - A syncthing folder named 'downloads', path is returned as is
# - Mounted disk in reverse-alphabetical order (so HDD2 before HDD etc.)
# - Main user folder: ~/Downloads/
# For most setups, this will usually just return the user Downloads folder,
# or the syncthing folder downloads, if it exists, which is returned as is.
disks = []
if os.path.isdir("/mnt/"):
disks = [f"/mnt/{f}" for f in os.listdir("/mnt/")]
disks = sorted([d for d in disks if os.path.isdir(d)], reverse=True)
disks.append(os.path.expanduser("~"))
for disk in disks:
if "downloads" not in folders and os.path.isdir(disk):
downloads = os.path.join(disk, "Downloads/")
try:
os.makedirs(downloads, exist_ok=True)
except PermissionError:
continue
folders["downloads"] = downloads
globals()["downloads"] = downloads
globals()["all"] = folders
# This short section provides basic command line usability, for example:
# `sync_location default` will print the location of the default sync folder.
def main():
if len(sys.argv) == 1:
print(json.dumps(folders, indent=4))
sys.exit(0)
elif len(sys.argv) == 2:
if sys.argv[1] == "--folders":
print("\n".join(folders.keys()))
sys.exit(0)
elif sys.argv[1] in folders:
print(folders[sys.argv[1]])
sys.exit(0)
elif len(sys.argv) == 3:
if sys.argv[1] in folders:
print(os.path.join(folders[sys.argv[1]], sys.argv[2]))
sys.exit(0)
sys.exit(1)
if __name__ == "__main__":
main()