summaryrefslogtreecommitdiff
path: root/misc/pylib/robofab/tools/rfPrefs.py
blob: 440984d0d3e36b206f204c537408960ed092bf5b (plain)
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
"""A simple module for dealing with preferences that are used by scripts. Based almost entirely on MacPrefs.

To save some preferences:
myPrefs = RFPrefs(drive/directory/directory/myPrefs.plist)
myPrefs.myString = 'xyz'
myPrefs.myInteger = 1234
myPrefs.myList = ['a', 'b', 'c']
myPrefs.myDict = {'a':1, 'b':2}
myPrefs.save()

To retrieve some preferences:
myPrefs = RFPrefs(drive/directory/directory/myPrefs.plist)
myString = myPrefs.myString
myInteger = myPrefs.myInteger
myList = myPrefs.myList
myDict = myPrefs.myDict

When using this module within FontLab, it is not necessary to
provide the RFPrefs class with a path. If a path is not given,
it will look for a file in FontLab/RoboFab Data/RFPrefs.plist.
If that file does not exist, it will make it.
"""

from robofab import RoboFabError
from robofab.plistlib import Plist
from cStringIO import StringIO
import os

class _PrefObject:
	
	def __init__(self, dict=None):
		if not dict:
			self._prefs = {}
		else:
			self._prefs = dict
	
	def __len__(self):
		return len(self._prefs)
	
	def __delattr__(self, attr):
		if self._prefs.has_key(attr):
			del self._prefs[attr]
		else:
			raise AttributeError, 'delete non-existing instance attribute'
	
	def __getattr__(self, attr):
		if attr == '__members__':
			keys = self._prefs.keys()
			keys.sort()
			return keys
		try:
			return self._prefs[attr]
		except KeyError:
			raise AttributeError, attr

	def __setattr__(self, attr, value):
		if attr[0] != '_':
			self._prefs[attr] = value
		else:
			self.__dict__[attr] = value
			
	def asDict(self):
		return self._prefs

class RFPrefs(_PrefObject):
	
	"""The main preferences object to call"""
	
	def __init__(self, path=None):
		from robofab.world import world
		self.__path = path
		self._prefs = {}
		if world.inFontLab:
			#we don't have a path, but we know where we can put it
			if not path:
				from robofab.tools.toolsFL import makeDataFolder
				settingsPath = makeDataFolder()
				path = os.path.join(settingsPath, 'RFPrefs.plist')
				self.__path = path
				self._makePrefsFile()
			#we do have a path, make sure it exists and load it
			else:
				self._makePrefsFile()
		else:
			#no path, raise error
			if not path:
				raise RoboFabError, "no preferences path defined"
			#we do have a path, make sure it exists and load it
			else:
				self._makePrefsFile()
		self._prefs = Plist.fromFile(path)
			
	def _makePrefsFile(self):
		if not os.path.exists(self.__path):
			self.save()
	
	def __getattr__(self, attr):
		if attr[0] == '__members__':
			keys = self._prefs.keys()
			keys.sort()
			return keys
		try:
			return self._prefs[attr]
		except KeyError:
			raise AttributeError, attr
			#if attr[0] != '_':
			#	self._prefs[attr] = _PrefObject()
			#	return self._prefs[attr]
			#else:
			#	raise AttributeError, attr
			
	def save(self):
		"""save the plist file"""
		f = StringIO()
		pl = Plist()
		for i in self._prefs.keys():
			pl[i] = self._prefs[i]
		pl.write(f)
		data = f.getvalue()
		f = open(self.__path, 'wb')
		f.write(data)
		f.close()