def test_config_dict(): short_path = os.path.normcase(os.path.normpath('~/foo/bar')) full_path = os.path.normcase(os.path.expanduser(os.path.normpath('~/foo/bar'))) # Path should be expanded. assert DictionaryConfig(short_path).path == full_path assert DictionaryConfig(full_path).path == full_path # Short path is available through `short_path`. assert DictionaryConfig(full_path).short_path == short_path assert DictionaryConfig(short_path).short_path == short_path # Enabled default to True. assert DictionaryConfig('foo').enabled assert not DictionaryConfig('foo', False).enabled # When converting to a dict (for dumping to JSON), # a dictionary with the shortened path is used. assert DictionaryConfig(full_path).to_dict() == \ {'path': short_path, 'enabled': True} assert DictionaryConfig(short_path, False).to_dict() == \ {'path': short_path, 'enabled': False} # Test from_dict creation helper. assert DictionaryConfig.from_dict({'path': short_path}) == \ DictionaryConfig(short_path) assert DictionaryConfig.from_dict({'path': full_path, 'enabled': False}) == \ DictionaryConfig(short_path, False)
def test_dictionary_config(self): short_path = os.path.normcase(os.path.normpath('~/foo/bar')) full_path = os.path.normcase(os.path.expanduser(os.path.normpath('~/foo/bar'))) dc = DictionaryConfig(short_path) # Path should be expanded. self.assertEqual(dc.path, full_path) # Shortened path is available through short_path. self.assertEqual(dc.short_path, short_path) # Enabled default to True. self.assertEqual(dc.enabled, True) # Check conversion to dict: short path should be used. self.assertEqual(dc.to_dict(), {'path': short_path, 'enabled': True}) # Test replace method. dc = dc.replace(enabled=False) self.assertEqual(dc.path, full_path) self.assertEqual(dc.enabled, False) # Test creation from dict. self.assertEqual(DictionaryConfig.from_dict({'path': short_path, 'enabled': False}), dc)
def load_dictionary_stack_from_backup(path): try: with open(path, 'r') as f: try: dictionaries = json.load(f) except json.JSONDecodeError: dictionaries = None if dictionaries: old_dictionaries = [ DictionaryConfig.from_dict(x) for x in dictionaries ] os.remove(path) #backup recovered, delete file return old_dictionaries else: return None except IOError: # No backup file, no problem return None