Skip to content

Commit

Permalink
docs: update README and add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Mar 7, 2022
1 parent 1c2d65f commit 573070f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ On iOS:

```shell
git clone [email protected]:doronz88/rpc-project.git
cd src
./build_ios.sh
cd src/rpcserver
./build_darwin.sh
```

## Installing python client
Expand Down
7 changes: 7 additions & 0 deletions src/rpcclient/rpcclient/darwin/cfpreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, client):

def get_keys(self, application_id: str, username: str = kCFPreferencesCurrentUser,
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[typing.List[str]]:
""" wrapper for CFPreferencesCopyKeyList """
application_id = self._client.cf(application_id)
username = self._client.cf(username)
hostname = self._client.cf(hostname)
Expand All @@ -33,6 +34,7 @@ def get_keys(self, application_id: str, username: str = kCFPreferencesCurrentUse

def get_value(self, key: str, application_id: str, username: str = kCFPreferencesCurrentUser,
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[str]:
""" wrapper for CFPreferencesCopyValue """
key = self._client.cf(key)
application_id = self._client.cf(application_id)
username = self._client.cf(username)
Expand All @@ -41,6 +43,7 @@ def get_value(self, key: str, application_id: str, username: str = kCFPreference

def get_dict(self, application_id: str, username: str = kCFPreferencesCurrentUser,
hostname: str = kCFPreferencesCurrentHost) -> typing.Optional[typing.Mapping]:
""" get a dictionary representation of given preference """
result = {}
key_list = self.get_keys(application_id, username, hostname)
if not key_list:
Expand All @@ -51,24 +54,28 @@ def get_dict(self, application_id: str, username: str = kCFPreferencesCurrentUse

def set(self, key: str, value, application_id: str, username: str = kCFPreferencesCurrentUser,
hostname: str = kCFPreferencesCurrentHost):
""" wrapper for CFPreferencesSetValue """
self._client.symbols.CFPreferencesSetValue(self._client.cf(key), self._client.cf(value),
self._client.cf(application_id), self._client.cf(username),
self._client.cf(hostname))

def remove(self, key: str, application_id: str, username: str = kCFPreferencesCurrentUser,
hostname: str = kCFPreferencesCurrentHost):
""" remove a given key from a preference """
self._client.symbols.CFPreferencesSetValue(self._client.cf(key), 0,
self._client.cf(application_id), self._client.cf(username),
self._client.cf(hostname))

def set_dict(self, d: typing.Mapping, application_id: str, username: str = kCFPreferencesCurrentUser,
hostname: str = kCFPreferencesCurrentHost):
""" set entire preference dictionary (erase first if exists) """
with suppress(NoSuchPreferenceError):
self.clear(application_id, username, hostname)
self.update_dict(d, application_id, username, hostname)

def update_dict(self, d: typing.Mapping, application_id: str, username: str = kCFPreferencesCurrentUser,
hostname: str = kCFPreferencesCurrentHost):
""" update preference dictionary """
for k, v in d.items():
self.set(k, v, application_id, username, hostname)

Expand Down
2 changes: 2 additions & 0 deletions src/rpcclient/rpcclient/darwin/hid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@


class Hid:
""" Control HID devices and simulate events """

def __init__(self, client):
self._client = client

Expand Down
19 changes: 18 additions & 1 deletion src/rpcclient/rpcclient/darwin/scpreferences.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typing

from rpcclient.exceptions import RpcClientException
from rpcclient.allocated import Allocated
from rpcclient.exceptions import RpcClientException


class SCPreference(Allocated):
Expand All @@ -12,58 +12,72 @@ def __init__(self, client, ref):

@property
def keys(self) -> typing.List[str]:
""" wrapper for SCPreferencesCopyKeyList """
return self._client.symbols.SCPreferencesCopyKeyList(self._ref).py

def _set(self, key: str, value):
""" wrapper for SCPreferencesSetValue """
if not self._client.symbols.SCPreferencesSetValue(self._ref, self._client.cf(key), self._client.cf(value)):
raise RpcClientException(f'SCPreferencesSetValue failed to set: {key}')

def set(self, key: str, value):
""" set key:value and commit the change """
self._set(key, value)
self._commit()

def set_dict(self, d: typing.Mapping):
""" set the entire preference dictionary (clear if already exists) and commit the change """
self._clear()
self._update_dict(d)
self._commit()

def _update_dict(self, d: typing.Mapping):
""" update preference dictionary """
for k, v in d.items():
self._set(k, v)

def update_dict(self, d: typing.Mapping):
""" update preference dictionary and commit """
self._update_dict(d)
self._commit()

def _remove(self, key: str):
""" wrapper for SCPreferencesRemoveValue """
if not self._client.symbols.SCPreferencesRemoveValue(self._ref, self._client.cf(key)):
raise RpcClientException(f'SCPreferencesRemoveValue failed to remove: {key}')

def remove(self, key: str):
""" remove given key and commit """
self._remove(key)
self._commit()

def get(self, key: str):
""" wrapper for SCPreferencesGetValue """
return self._client.symbols.SCPreferencesGetValue(self._ref, self._client.cf(key)).py

def get_dict(self) -> typing.Mapping:
""" get a dictionary representation """
result = {}
for k in self.keys:
result[k] = self.get(k)
return result

def _clear(self):
""" clear dictionary """
for k in self.keys:
self._remove(k)

def clear(self):
""" clear dictionary and commit """
self._clear()
self._commit()

def _deallocate(self):
""" free the preference object """
self._client.symbols.CFRelease(self._ref)

def _commit(self):
""" commit all changes """
if not self._client.symbols.SCPreferencesCommitChanges(self._ref):
raise RpcClientException('SCPreferencesCommitChanges failed')
self._client.symbols.SCPreferencesSynchronize(self._ref)
Expand All @@ -82,15 +96,18 @@ def __init__(self, client):
self._client = client

def open(self, preferences_id: str) -> SCPreference:
""" get an SCPreference from a given preferences_id """
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 SCPreference(self._client, ref)

def get_keys(self, preferences_id: str) -> typing.List[str]:
""" get all keys from given preferences_id """
with self.open(preferences_id) as o:
return o.keys

def get_dict(self, preferences_id: str):
""" get dict from given preferences_id """
with self.open(preferences_id) as o:
return o.get_dict()

0 comments on commit 573070f

Please sign in to comment.