Repository navigation
Expand file tree
/
Copy pathdefaults+
More file actions
executable file
·136 lines (108 loc) · 3.29 KB
/
Copy pathdefaults+
File metadata and controls
executable file
·136 lines (108 loc) · 3.29 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
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
#!/usr/bin/env python
# Copyright (c) 2014 by Craig Buchek.
# Copyright (c) 2014 by BoochTek, LLC.
# Licensed under the MIT License.
# Description:
# This program is like the Mac OS X `defaults` program, but is able to manipulate nested dicts.
from __future__ import print_function
import sys
import CoreFoundation
KEY_PATH_SEPARATOR = "."
command = sys.argv[1]
domain = sys.argv[2]
key_path = sys.argv[3]
if len(sys.argv) >= 5:
value = sys.argv[4]
def main():
if command == "read":
try:
prefs = Preferences(domain)
print(prefs.read(KeyPath(key_path)))
except:
print("Couldn't get the value %s for %s" % (key_path, domain), file=sys.stderr)
exit(2)
elif command == "write":
prefs = Preferences(domain)
try:
parsed_value = int(value)
except ValueError:
parsed_value = value
prefs.write(KeyPath(key_path), parsed_value)
elif command == "copy":
prefs = Preferences(domain)
prefs.copy(KeyPath(key_path), KeyPath(value))
else:
print("Don't know how to %s" % command)
exit(1)
class Preferences(object):
def __init__(self, domain):
self.domain = domain
def read(self, key_path):
top = self.top_dict(key_path)
return top[key_path.rest()]
def write(self, key_path, value):
top = self.top_dict(key_path)
top[key_path.rest()] = value
CoreFoundation.CFPreferencesSetAppValue(key_path.first(), top, self.domain)
self.sync()
def copy(self, source_key_path, destination_key_path):
self.write(destination_key_path, self.read(source_key_path))
def top_dict(self, key_path):
return NestedDict(mutable_copy_of_dict(CoreFoundation.CFPreferencesCopyAppValue(key_path.first(), self.domain)))
def sync(self):
CoreFoundation.CFPreferencesAppSynchronize(self.domain)
class KeyPath(object):
def __init__(self, key_path_str):
self.key_path_str = key_path_str
def keys(self):
return self.key_path_str.split(KEY_PATH_SEPARATOR)
def first(self):
return self.keys()[0]
def last(self):
return self.keys()[-1]
def rest(self):
return self.keys()[1:]
# Adapted from http://stackoverflow.com/questions/15077973/how-can-i-access-a-deeply-nested-dictionary-using-tuples
class NestedDict(dict):
"""
Nested dictionary of arbitrary depth.
Allows data access via a list or tuple of keys, specifying the path to an element.
Also makes mutable copies of dicts before setting.
"""
def __getitem__(self, keys):
# If `keys` is a collection (other than string), convert it to a tuple.
if isinstance(keys, (list, tuple)):
keys = tuple(keys)
node = self
for key in keys:
node = node.get(key)
return node
else:
try:
return dict.__getitem__(self, keys)
except KeyError:
raise KeyError(keys)
def __setitem__(self, keys, value):
# If `keys` is a collection (other than string).
if isinstance(keys, (list, tuple)):
try:
node = self
for key in keys[:-1]:
try:
node[key] = mutable_copy_of_dict(node[key])
node = node[key]
except KeyError:
node[key] = type(self)()
node = node[key]
node[keys[-1]] = value
except TypeError:
print("TypeError")
else:
dict.__setitem__(self, keys, value)
def mutable_copy_of_dict(dict):
"""
Return a mutable copy of a CFDictionary.
"""
return CoreFoundation.CFDictionaryCreateMutableCopy(CoreFoundation.kCFAllocatorDefault, 0, dict)
if __name__ == "__main__":
main()