Merge lp:~mblayman/entertainer/new-old-config into lp:~rockstar/entertainer/new-indexer-that-doesnt-suck

Proposed by Matt Layman
Status: Merged
Merged at revision: not available
Proposed branch: lp:~mblayman/entertainer/new-old-config
Merge into: lp:~rockstar/entertainer/new-indexer-that-doesnt-suck
Diff against target: None lines
To merge this branch: bzr merge lp:~mblayman/entertainer/new-old-config
Reviewer Review Type Date Requested Status
Entertainer Release Team Pending
Review via email: mp+8158@code.launchpad.net

Commit message

Configuration now has simpler behavior.

To post a comment you must log in.
Revision history for this message
Matt Layman (mblayman) wrote :

Paul, here you go. A fresh new version of config that makes the indexer pass its tests.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'entertainerlib/configuration.py'
2--- entertainerlib/configuration.py 2009-06-30 04:16:45 +0000
3+++ entertainerlib/configuration.py 2009-07-02 20:58:37 +0000
4@@ -32,33 +32,19 @@
5 """
6
7 CFG_DIR = os.path.expanduser("~/.config/entertainer")
8- TEST_DIR = None
9-
10- # This dictionary keeps track of data values that are written to config
11- # files but need to be returned to a known state while testing.
12- _tainted = {}
13-
14- # This dictionary keeps track of data values that are tainted in memory
15- # instead of a config file
16- _tainted_in_memory = {}
17
18 _shared_state = {}
19
20 def __init__(self, test_dir=None):
21- """
22- Read configuration files and setup this object.
23- """
24 self.__dict__ = self._shared_state
25 MessageHandler.__init__(self)
26
27- if not self._shared_state:
28- self.TEST_DIR = test_dir
29-
30+ if not self._shared_state or test_dir is not None:
31 # Set in a production mode or a test mode
32- if self.TEST_DIR is None:
33+ if test_dir is None:
34 self.cfg_dir = self.CFG_DIR
35 else:
36- self.cfg_dir = self.TEST_DIR
37+ self.cfg_dir = test_dir
38
39 if not os.path.exists(os.path.expanduser(self.cfg_dir)):
40 self.create_cfg_dir()
41@@ -160,11 +146,8 @@
42 except IOError:
43 raise IOError("Couldn't read config file.")
44
45- def write_content_value(self, section, option, value, sanitize=False):
46+ def write_content_value(self, section, option, value):
47 """Write a new value to the content configuration file."""
48- if self.TEST_DIR and not sanitize:
49- self.taint('content', section, option)
50-
51 try:
52 self.content_config.set(section, option, value)
53 cfg_file = file(self.content_conf, 'w')
54@@ -174,11 +157,8 @@
55 except NoOptionError:
56 raise Exception("No Option to set in content.conf file")
57
58- def write_preference_value(self, section, option, value, sanitize=False):
59+ def write_preference_value(self, section, option, value):
60 """Write a new value to the preferences configuration file."""
61- if self.TEST_DIR and not sanitize:
62- self.taint('preferences', section, option)
63-
64 try:
65 self.preferences.set(section, option, value)
66 cfg_file = file(self.preferences_conf, 'w')
67@@ -200,14 +180,8 @@
68
69 return self.stage_width
70
71- def set_stage_width(self, new_width, sanitize=False):
72+ def set_stage_width(self, new_width):
73 '''Set the stage width for the in memory instance'''
74- # Taint data if in test mode
75- if self.TEST_DIR and not sanitize:
76- kwargs = {'new_width' : self.get_stage_width(),
77- 'sanitize' : True}
78- self.taint_in_memory(self.set_stage_width, kwargs)
79-
80 self.stage_width = new_width
81
82 def get_stage_height(self):
83@@ -223,14 +197,8 @@
84
85 return self.stage_height
86
87- def set_stage_height(self, new_height, sanitize=False):
88+ def set_stage_height(self, new_height):
89 '''Set the stage height for the in memory instance'''
90- # Taint data if in test mode
91- if self.TEST_DIR and not sanitize:
92- kwargs = {'new_height' : self.get_stage_height(),
93- 'sanitize' : True}
94- self.taint_in_memory(self.set_stage_height, kwargs)
95-
96 self.stage_height = new_height
97
98 def get_theme_path(self):
99@@ -503,53 +471,6 @@
100 """
101 return self.cfg_dir
102
103- def taint(self, kind, section, option):
104- '''Taint any data that is written to config files so that it can be
105- returned to a known state later'''
106- if kind == 'content':
107- value = self.content_config.get(section, option)
108- elif kind == 'preferences':
109- value = self.preferences.get(section, option)
110- else:
111- raise Exception(
112- "Taint type must be either 'content' or 'preferences'")
113-
114- key = "%s %s %s" % (kind, section, option)
115- self._tainted[key] = value
116-
117- def taint_in_memory(self, callback, kwargs):
118- '''Taint any data that is stored in memory. Callback is used as the
119- key to a tainted_in_memory dictionary whose stored value is a
120- dictionary of arguments (kwargs) needed to restore the original
121- value'''
122- self._tainted_in_memory[callback] = kwargs
123-
124- def sanitize(self):
125- '''Restore all tainted values to their original state and clear the
126- tainted dictionary'''
127- # Sanitize values in config files
128- for key in self._tainted.keys():
129- # tokens will contain type, section, and option
130- tokens = key.split()
131- if tokens[0] == 'content':
132- # Write in the sanitize mode so taint checking will be skipped
133- self.write_content_value(tokens[1], tokens[2],
134- self._tainted[key], sanitize=True)
135- elif tokens[0] == 'preferences':
136- # Write in the sanitize mode so taint checking will be skipped
137- self.write_preference_value(tokens[1], tokens[2],
138- self._tainted[key], sanitize=True)
139-
140- self.update_configuration()
141- self._tainted = {}
142-
143- # Sanitize values in memory
144- for callback in self._tainted_in_memory.keys():
145- kwargs = self._tainted_in_memory[callback]
146- callback(**kwargs)
147-
148- self._tainted_in_memory = {}
149-
150 def _is_valid_media_folder(self, folder_list):
151 """Return a folder list where eventual cache folders are removed"""
152 cache_dir = os.path.join(self.get_cfg_dir(), 'cache')
153
154=== modified file 'entertainerlib/tests/__init__.py'
155--- entertainerlib/tests/__init__.py 2009-05-10 17:36:49 +0000
156+++ entertainerlib/tests/__init__.py 2009-07-02 20:58:37 +0000
157@@ -20,7 +20,7 @@
158 '''Set up the basic file I/O requirements.'''
159 self.test_dir = self.get_temp_file()
160 os.mkdir(self.test_dir)
161- self.test_cfg_dir = '/tmp/cfg_dir'
162+ self.test_cfg_dir = self.get_temp_file()
163 self.config = Configuration(self.test_cfg_dir)
164 self.data_dir = os.path.dirname(__file__) + '/data'
165
166@@ -40,10 +40,6 @@
167 filename = create_new_filename()
168 return filename
169
170- def tearDown(self):
171- '''Sanitize any data that will be reused'''
172- self.config.sanitize()
173-
174
175 class EntertainerTestWithDatabase(EntertainerTest):
176 '''Test that requires a database'''
177
178=== modified file 'entertainerlib/tests/test_configuration.py'
179--- entertainerlib/tests/test_configuration.py 2009-06-30 01:06:01 +0000
180+++ entertainerlib/tests/test_configuration.py 2009-07-02 20:58:37 +0000
181@@ -40,12 +40,6 @@
182 self.configuration.set_stage_width(new_width)
183 self.assertEqual(self.configuration.get_stage_width(), new_width)
184
185- # Test that the value is properly tainted
186- self.assertEqual(
187- self.configuration._tainted_in_memory[
188- self.configuration.set_stage_width],
189- {'new_width' : 1366, 'sanitize' : True})
190-
191 def testGetStageHeight(self):
192 '''Test getting the stage height'''
193 self.assertEqual(self.configuration.get_stage_height(), 768)
194@@ -56,15 +50,9 @@
195 self.configuration.set_stage_height(new_height)
196 self.assertEqual(self.configuration.get_stage_height(), new_height)
197
198- # Test that the value is properly tainted
199- self.assertEqual(
200- self.configuration._tainted_in_memory[
201- self.configuration.set_stage_height],
202- {'new_height' : 768, 'sanitize' : True})
203-
204 def testGetCfgDir(self):
205 """Test getting the cfg dir"""
206- self.assertEqual(self.configuration.get_cfg_dir(), self.config.TEST_DIR)
207+ self.assertEqual(self.configuration.get_cfg_dir(), self.test_cfg_dir)
208
209 def testTrayIconEnabled(self):
210 """Test getting the display_icon boolean"""
211@@ -77,11 +65,6 @@
212 self.configuration.update_configuration()
213 self.assertEqual(self.configuration.get_weather_location(), new_value)
214
215- # Test that the value is properly tainted
216- self.assertEqual(
217- self.configuration._tainted['content Weather location'],
218- 'Bath,England')
219-
220 def test_write_preference_value(self):
221 """Test writing a value to the preference configuration file"""
222 new_value = True
223@@ -90,11 +73,6 @@
224 self.configuration.update_configuration()
225 self.assertEqual(self.configuration.tray_icon_enabled(), new_value)
226
227- # Test that the value is properly tainted
228- self.assertEqual(
229- self.configuration._tainted['preferences General display_icon'],
230- 'False')
231-
232 def testGetThemeName(self):
233 """Test getting the current theme name"""
234 current_name = "Default"
235@@ -113,53 +91,3 @@
236 self.assertEqual(self.configuration.display_hidden_files_folders(),
237 False)
238
239- def test_taint(self):
240- '''Test tainting configuration data'''
241- # Test tainting preferences data
242- kind = 'preferences'
243- section = 'General'
244- option = 'history_size'
245- self.configuration.taint(kind, section, option)
246- key = "%s %s %s" % (kind, section, option)
247- self.assertEqual(self.configuration._tainted[key], '8')
248-
249- # Test tainting content data
250- kind = 'content'
251- section = 'Videos'
252- option = 'download_metadata'
253- self.configuration.taint(kind, section, option)
254- key = "%s %s %s" % (kind, section, option)
255- self.assertEqual(self.configuration._tainted[key], 'True')
256-
257- # Test the bad branch
258- kind = 'bogus'
259- self.assertRaises(Exception, self.configuration.taint, kind, section,
260- option)
261-
262- def test_taint_in_memory(self):
263- '''Test tainting data already in memory'''
264- value = 'test'
265-
266- def set_value(value):
267- '''Dummy method used as a key'''
268-
269- kwargs = {'value' : value}
270- self.configuration.taint_in_memory(set_value, kwargs)
271- self.assertEqual(self.configuration._tainted_in_memory[set_value],
272- kwargs)
273-
274- def test_sanitize(self):
275- '''Test sanitizing configuration data'''
276- # Test sanitizing for config file data
277- new_value = 15
278- self.configuration.write_preference_value(
279- "General", "history_size", new_value)
280- self.configuration.update_configuration()
281- self.configuration.sanitize()
282- self.assertEqual(self.configuration.history_size(), 8)
283-
284- # Test sanitizing in memory data
285- self.configuration.set_stage_height(400)
286- self.configuration.sanitize()
287- self.assertEqual(self.configuration.get_stage_height(), 768)
288-
289
290=== modified file 'entertainerlib/tests/test_indexer.py'
291--- entertainerlib/tests/test_indexer.py 2009-06-30 05:37:55 +0000
292+++ entertainerlib/tests/test_indexer.py 2009-07-02 21:10:03 +0000
293@@ -3,14 +3,14 @@
294
295 from entertainerlib.configuration import Configuration
296 from entertainerlib.indexing.handlers import AviHandler
297-#from entertainerlib.indexing.indexer import Indexer
298+from entertainerlib.indexing.indexer import Indexer
299 from entertainerlib.tests import EntertainerTest
300
301
302 class IndexerTest(EntertainerTest):
303 '''Tests for `entertainerlib.indexing.indexer`.'''
304
305- def DISABLEDtest_constructor(self):
306+ def test_constructor(self):
307 '''Test the Indexer constructor.'''
308 indexer = Indexer()
309 self.assertTrue(isinstance(indexer, Indexer))
310@@ -18,7 +18,7 @@
311 self.assertTrue(isinstance(indexer.logger, Logger))
312 self.assertTrue(isinstance(indexer.configuration, Configuration))
313
314- def DISABLEDtest_supported_filetypes(self):
315+ def test_supported_filetypes(self):
316 '''Test Indexer.supported_filetypes.'''
317 indexer = Indexer()
318 self.assertEquals(len(indexer.supported_filetypes), 14)
319@@ -26,24 +26,24 @@
320 ['avi', 'jpeg', 'jpg', 'm4v', 'mkv', 'mov', 'mp3', 'mp4', 'mpeg',
321 'mpg', 'ogg', 'ogm', 'png', 'wmv'])
322
323- def DISABLEDtest_valid_get_filetype_handler(self):
324+ def test_valid_get_filetype_handler(self):
325 '''Test Indexer.get_filetype_handler with a valid filetype.'''
326 indexer = Indexer()
327 self.assertTrue(type(indexer.get_filetype_handler('avi')),
328 AviHandler)
329
330- def DISABLEDtest_invalid_get_filetype_handler(self):
331+ def test_invalid_get_filetype_handler(self):
332 '''Test Indexer.get_filetype_handler with an invalid filetype.'''
333 indexer = Indexer()
334 self.assertEquals(indexer.get_filetype_handler('aeiou'), None)
335
336- def DISABLEDtest_is_supported_filetype(self):
337+ def test_is_supported_filetype(self):
338 '''Test Indexer.is_supported_filetype with a supported filetype.'''
339 indexer = Indexer()
340 self.assertTrue(indexer.is_supported_filetype('foo.avi'))
341 self.assertTrue(indexer.is_supported_filetype('baz.bar.mp3'))
342
343- def DISABLEDtest_is_not_supported_filetype(self):
344+ def test_is_not_supported_filetype(self):
345 '''Test Indexer.is_supported_filetype with an unsupported filetype.'''
346 indexer = Indexer()
347 self.assertFalse(indexer.is_supported_filetype('not.aeiou'))

Subscribers

People subscribed via source and target branches

to all changes: