-
-
Save pslobo/012d1fae3ce6ce062ff6 to your computer and use it in GitHub Desktop.
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
from xml.etree.ElementTree import Element, SubElement, tostring | |
from os import path | |
from Foundation import ( | |
CFPreferencesAppSynchronize, | |
CFPreferencesCopyAppValue, | |
CFPreferencesCopyKeyList, | |
CFPreferencesSetValue, | |
kCFPreferencesAnyUser, | |
kCFPreferencesCurrentHost | |
) | |
HOME = path.expanduser('~') | |
PLIST_PATH = HOME + "/Library/Preferences/com.apple.sidebarlists.plist" | |
class toXML(): | |
def __init__(self): | |
self.toxml = Element('items') | |
def __repr__(self): | |
return tostring(self.toxml) | |
def add_item(self, title, subtitle="", arg="", valid="YES", autocomplete="", icon="icon.png"): | |
item = SubElement(self.toxml, 'item', { | |
'uid': str(len(self.toxml)), | |
'arg': arg, | |
'valid': valid, | |
'autocomplete': autocomplete | |
}) | |
_title = SubElement(item, 'title') | |
_title.text = title | |
_subtitle = SubElement(item, 'subtitle') | |
_subtitle.text = subtitle | |
_icon = SubElement(item, 'icon') | |
_icon.text = icon | |
def display(self): | |
print self | |
class Preferences(object): | |
"""Class which directly reads/writes Apple CF preferences.""" | |
def __init__(self, bundle_id, user=kCFPreferencesAnyUser): | |
"""Init. | |
Args: | |
bundle_id: str, like 'ManagedInstalls' | |
""" | |
if bundle_id.endswith('.plist'): | |
bundle_id = bundle_id[:-6] | |
self.bundle_id = bundle_id | |
self.user = user | |
def __iter__(self): | |
keys = CFPreferencesCopyKeyList( | |
self.bundle_id, self.user, kCFPreferencesCurrentHost) | |
if keys is not None: | |
for i in keys: | |
yield i | |
def __contains__(self, pref_name): | |
pref_value = CFPreferencesCopyAppValue(pref_name, self.bundle_id) | |
return pref_value is not None | |
def __getitem__(self, pref_name): | |
return CFPreferencesCopyAppValue(pref_name, self.bundle_id) | |
def __setitem__(self, pref_name, pref_value): | |
CFPreferencesSetValue( | |
pref_name, pref_value, self.bundle_id, self.user, | |
kCFPreferencesCurrentHost) | |
CFPreferencesAppSynchronize(self.bundle_id) | |
def __delitem__(self, pref_name): | |
self.__setitem__(pref_name, None) | |
def __repr__(self): | |
return '<%s %s>' % (self.__class__.__name__, self.bundle_id) | |
def get(self, pref_name, default=None): | |
if pref_name not in self: | |
return default | |
else: | |
return self.__getitem__(pref_name) | |
def set(self, pref_name, pref_value): | |
self.__setitem__(pref_name, pref_value) | |
class ServerFavoritesPrefs(Preferences): | |
def __init__(self): | |
self.bundle_id = PLIST_PATH | |
class VNCFavorites(object): | |
def __init__(self): | |
self.prefs = ServerFavoritesPrefs() | |
self._servers = None | |
@property | |
def servers(self): | |
if not self._servers: | |
self._servers = self._get_servers() | |
return self._servers | |
def _get_servers(self): | |
return [ | |
dict(name=server['Name'], url=server['URL']) | |
for server in self.prefs.get('favoriteservers')['CustomListItems'] | |
] | |
if __name__ == '__main__': | |
v = VNCFavorites() | |
toXML = toXML() | |
for server in v.servers: | |
toXML.add_item( | |
title=server['name'], | |
subtitle="URL {url}".format(**server), | |
arg=server['url'] | |
) | |
print toXML |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment