-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneDrivePath.py
More file actions
99 lines (75 loc) · 3.09 KB
/
Copy pathOneDrivePath.py
File metadata and controls
99 lines (75 loc) · 3.09 KB
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
import os
import sys
from pathlib import Path
# Determine local OneDrive path, accounting for multiple OneDrive accounts
# (e.g. personal + one or more business/school accounts)
def _normalize(s):
return "".join(ch for ch in s.lower() if ch.isalnum())
def _win_accounts():
# Registry has one subkey per signed-in account, so it's the only way to
# see every account (env vars only expose one personal + one business).
# Matched by folder name, same as macOS/Linux.
accounts = []
try:
import winreg
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\OneDrive\Accounts")
for i in range(winreg.QueryInfoKey(key)[0]):
with winreg.OpenKey(key, winreg.EnumKey(key, i)) as sub:
try:
folder = Path(winreg.QueryValueEx(sub, "UserFolder")[0])
except OSError:
continue
accounts.append((folder.name, folder))
except OSError:
pass
if not accounts:
for var in ("OneDriveConsumer", "OneDriveCommercial", "OneDrive"):
val = os.getenv(var)
if val:
accounts.append((Path(val).name, Path(val)))
return accounts
def _mac_accounts():
accounts = []
cloud_storage = Path.home() / "Library" / "CloudStorage"
if cloud_storage.exists():
for folder in sorted(cloud_storage.glob("OneDrive-*")):
accounts.append((folder.name, folder))
legacy = Path.home() / "OneDrive"
if legacy.exists():
accounts.append((legacy.name, legacy))
return accounts
def _linux_accounts():
# No official Linux client; unofficial clients (e.g. abraunegg/onedrive)
# default to syncing a single account into ~/OneDrive
folder = Path.home() / "OneDrive"
return [(folder.name, folder)] if folder.exists() else []
_default_account = None
def _resolve(account):
if sys.platform == "win32":
accounts = _win_accounts()
elif sys.platform == "darwin":
accounts = _mac_accounts()
else:
accounts = _linux_accounts()
accounts = [(label, path) for label, path in accounts if path.exists()]
if account is not None:
needle = _normalize(account)
accounts = [a for a in accounts if needle in _normalize(a[0])]
if not accounts:
raise FileNotFoundError(f"No OneDrive account matching '{account}' found")
elif not accounts:
raise FileNotFoundError("OneDrive folder not found")
return accounts[0][1]
def setAccount(account: str) -> None:
# Validate the account exists now, so later onedrivePath() calls don't
# surprise you with a lookup failure
global _default_account
_resolve(account)
_default_account = account
def onedrivePath(name: str = None, account: str = None) -> str:
local_onedrive_path = _resolve(account if account is not None else _default_account)
# Append the relative path to the local OneDrive path
if name is not None:
local_onedrive_path /= name.lstrip("/\\")
return str(local_onedrive_path)
# Now you can use local relative paths within this directory