-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
darwin: split preferences to cf and sc
- Loading branch information
Showing
4 changed files
with
144 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import typing | ||
|
||
from rpcclient.exceptions import RpcClientException | ||
|
||
kCFPreferencesCurrentUser = 'kCFPreferencesCurrentUser' | ||
kCFPreferencesAnyUser = 'kCFPreferencesAnyUser' | ||
kCFPreferencesCurrentHost = 'kCFPreferencesCurrentHost' | ||
kCFPreferencesAnyHost = 'kCFPreferencesAnyHost' | ||
|
||
|
||
class CFPreferences: | ||
""" | ||
API to the CFPreferences* functions - preferences managed by cfprefsd. | ||
https://developer.apple.com/documentation/corefoundation/preferences_utilities?language=objc | ||
""" | ||
|
||
def __init__(self, client): | ||
""" | ||
:param rpcclient.darwin.client.DarwinClient client: | ||
""" | ||
self._client = client | ||
|
||
def copy_key_list(self, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[typing.List[str]]: | ||
application_id = self._client.cf(application_id) | ||
username = self._client.cf(username) | ||
hostname = self._client.cf(hostname) | ||
return self._client.symbols.CFPreferencesCopyKeyList(application_id, username, hostname).py | ||
|
||
def copy_value(self, key: str, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[str]: | ||
key = self._client.cf(key) | ||
application_id = self._client.cf(application_id) | ||
username = self._client.cf(username) | ||
hostname = self._client.cf(hostname) | ||
return self._client.symbols.CFPreferencesCopyValue(key, application_id, username, hostname).py | ||
|
||
def copy_all_values(self, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[typing.Mapping]: | ||
result = {} | ||
key_list = self.copy_key_list(application_id, username, hostname) | ||
if not key_list: | ||
raise RpcClientException(f'failed to get key list for: {application_id}/{username}/{hostname}') | ||
for k in key_list: | ||
result[k] = self.copy_value(k, application_id, username, hostname) | ||
return result | ||
|
||
def set_value(self, key: str, value: str, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost): | ||
key = self._client.cf(key) | ||
value = self._client.cf(value) | ||
application_id = self._client.cf(application_id) | ||
username = self._client.cf(username) | ||
hostname = self._client.cf(hostname) | ||
self._client.symbols.CFPreferencesSetValue(key, value, application_id, username, hostname) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,13 @@ | ||
import typing | ||
|
||
kCFPreferencesCurrentUser = 'kCFPreferencesCurrentUser' | ||
kCFPreferencesAnyUser = 'kCFPreferencesAnyUser' | ||
kCFPreferencesCurrentHost = 'kCFPreferencesCurrentHost' | ||
kCFPreferencesAnyHost = 'kCFPreferencesAnyHost' | ||
from rpcclient.darwin.cfpreferences import CFPreferences | ||
from rpcclient.darwin.scpreferences import SCPreferences | ||
|
||
|
||
class Preferences: | ||
""" Preferences utils """ | ||
|
||
def __init__(self, client): | ||
""" | ||
:param rpcclient.darwin.client.DarwinClient client: | ||
""" | ||
self._client = client | ||
|
||
def copy_key_list(self, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[typing.List[str]]: | ||
application_id = self._client.cf(application_id) | ||
username = self._client.cf(username) | ||
hostname = self._client.cf(hostname) | ||
return self._client.symbols.CFPreferencesCopyKeyList(application_id, username, hostname).py | ||
|
||
def copy_value(self, key: str, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[str]: | ||
key = self._client.cf(key) | ||
application_id = self._client.cf(application_id) | ||
username = self._client.cf(username) | ||
hostname = self._client.cf(hostname) | ||
return self._client.symbols.CFPreferencesCopyValue(key, application_id, username, hostname).py | ||
|
||
def copy_all_values(self, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[typing.Mapping]: | ||
result = {} | ||
for k in self.copy_key_list(application_id, username, hostname): | ||
result[k] = self.copy_value(k, application_id, username, hostname) | ||
return result | ||
|
||
def set_value(self, key: str, value: str, application_id: str, username: str = kCFPreferencesCurrentUser, | ||
hostname: str = kCFPreferencesCurrentHost): | ||
key = self._client.cf(key) | ||
value = self._client.cf(value) | ||
application_id = self._client.cf(application_id) | ||
username = self._client.cf(username) | ||
hostname = self._client.cf(hostname) | ||
self._client.symbols.CFPreferencesSetValue(key, value, application_id, username, hostname) | ||
self.cf = CFPreferences(client) | ||
self.sc = SCPreferences(client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import typing | ||
|
||
from rpcclient.exceptions import RpcClientException | ||
|
||
|
||
class SCPreferencesObject: | ||
def __init__(self, client, ref): | ||
self._client = client | ||
self._ref = ref | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
try: | ||
yield | ||
finally: | ||
self.release() | ||
|
||
@property | ||
def keys(self) -> typing.List[str]: | ||
return self._client.symbols.SCPreferencesCopyKeyList(self._ref).py | ||
|
||
def set(self, key: str, value): | ||
if not self._client.symbols.SCPreferencesSetValue(self._ref, self._client.cf(key), self._client.cf(value)): | ||
raise RpcClientException(f'SCPreferencesSetValue failed to set: {key}') | ||
self._commit() | ||
|
||
def remove(self, key: str): | ||
if not self._client.symbols.SCPreferencesRemoveValue(self._ref, self._client.cf(key)): | ||
raise RpcClientException(f'SCPreferencesRemoveValue failed to remove: {key}') | ||
self._commit() | ||
|
||
def get(self, key: str): | ||
return self._client.symbols.SCPreferencesGetValue(self._ref, self._client.cf(key)).py | ||
|
||
def to_dict(self) -> typing.Mapping: | ||
result = {} | ||
for k in self.keys: | ||
result[k] = self.get(k) | ||
return result | ||
|
||
def release(self): | ||
self._client.CFRelase(self._ref) | ||
|
||
def _commit(self): | ||
if not self._client.symbols.SCPreferencesCommitChanges(self._ref): | ||
raise RpcClientException('SCPreferencesCommitChanges failed') | ||
self._client.symbols.SCPreferencesSynchronize(self._ref) | ||
|
||
|
||
class SCPreferences: | ||
""" | ||
API to the SCPreferences* functions - preferences managed by SystemConfiguration framework. | ||
https://developer.apple.com/documentation/systemconfiguration/scpreferences?language=objc | ||
""" | ||
|
||
def __init__(self, client): | ||
""" | ||
:param rpcclient.darwin.client.DarwinClient client: | ||
""" | ||
self._client = client | ||
|
||
def get_preferences_object(self, preferences_id: str) -> SCPreferencesObject: | ||
ref = self._client.symbols.SCPreferencesCreate(0, self._client.cf('rpcserver'), self._client.cf(preferences_id)) | ||
if not ref: | ||
raise RpcClientException(f'SCPreferencesCreate failed for: {preferences_id}') | ||
return SCPreferencesObject(self._client, ref) | ||
|
||
def get_key_list(self, preferences_id: str) -> typing.List[str]: | ||
with self.get_preferences_object(preferences_id) as o: | ||
return o.keys | ||
|
||
def get_dict(self, preferences_id: str): | ||
with self.get_preferences_object(preferences_id) as o: | ||
return o.to_dict() |