Changeset d93fcf
- Timestamp:
- 08/18/2010 07:32:11 PM (14 years ago)
- Children:
- b45e01
- Parents:
- a2d75a
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
deluge/config.py
ra2d75a rd93fcf 46 46 <content dict> 47 47 48 The version dict contains two keys: file and format. The format version is 49 controlled by the Config class. It should only be changed when anything below 50 it is changed directly by the Config class. An example of this would be if we 48 The version dict contains two keys: file and format. The format version is 49 controlled by the Config class. It should only be changed when anything below 50 it is changed directly by the Config class. An example of this would be if we 51 51 changed the serializer for the content to something different. 52 52 … … 94 94 """ 95 95 Find json objects in a string. 96 96 97 97 :param s: the string to find json objects in 98 98 :type s: string 99 99 100 100 :returns: a list of tuples containing start and end locations of json objects in the string `s` 101 101 :rtype: [(start, end), ...] 102 102 103 103 """ 104 104 objects = [] … … 120 120 121 121 return objects 122 123 122 123 124 124 class Config(object): 125 125 """ … … 349 349 350 350 objects = find_json_objects(data) 351 351 352 352 if not len(objects): 353 353 # No json objects found, try depickling it … … 356 356 except Exception, e: 357 357 log.exception(e) 358 log.warning("Unable to load config file: %s", filename) 358 log.warning("Unable to load config file: %s", filename) 359 359 elif len(objects) == 1: 360 360 start, end = objects[0] … … 363 363 except Exception, e: 364 364 log.exception(e) 365 log.warning("Unable to load config file: %s", filename) 365 log.warning("Unable to load config file: %s", filename) 366 366 elif len(objects) == 2: 367 367 try: … … 372 372 except Exception, e: 373 373 log.exception(e) 374 log.warning("Unable to load config file: %s", filename) 375 374 log.warning("Unable to load config file: %s", filename) 375 376 376 log.debug("Config %s version: %s.%s loaded: %s", filename, 377 377 self.__version["format"], self.__version["file"], self.__config) … … 397 397 start, end = objects[1] 398 398 loaded_data = json.loads(data[start:end]) 399 400 399 if self.__config == loaded_data and self.__version == version: 401 400 # The config has not changed so lets just return 402 self._save_timer.cancel() 401 if self._save_timer: 402 self._save_timer.cancel() 403 403 return 404 except Exception, e: 405 log.warning("Unable to open config file: %s", filename) 406 407 404 except IOError, e: 405 log.warning("Unable to open config file: %s because: %s", filename, e) 408 406 409 407 # Save the new config and make sure it's written to disk … … 411 409 log.debug("Saving new config file %s", filename + ".new") 412 410 f = open(filename + ".new", "wb") 413 json.dump(self.__version, f, indent=2) 411 json.dump(self.__version, f, indent=2) 414 412 json.dump(self.__config, f, indent=2) 415 413 f.flush() 416 414 os.fsync(f.fileno()) 417 415 f.close() 418 except Exception, e:416 except IOError, e: 419 417 log.error("Error writing new config file: %s", e) 420 418 return False -
tests/test_config.py
ra2d75a rd93fcf 7 7 from deluge.config import Config 8 8 9 DEFAULTS = {"string": "foobar", "int": 1, "float": 0.435, "bool": True , "tuple": (1, 2)}9 DEFAULTS = {"string": "foobar", "int": 1, "float": 0.435, "bool": True} 10 10 11 11 class ConfigTestCase(unittest.TestCase): … … 16 16 config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) 17 17 self.assertEquals(DEFAULTS, config.config) 18 18 19 19 config = Config("test.conf", config_dir=self.config_dir) 20 20 self.assertEquals({}, config.config) 21 21 22 22 def test_set_get_item(self): 23 23 config = Config("test.conf", config_dir=self.config_dir) … … 27 27 config["foo"] = 2 28 28 self.assertEquals(config.get_item("foo"), 2) 29 29 30 30 config._save_timer.cancel() 31 31 … … 33 33 def check_config(): 34 34 config = Config("test.conf", config_dir=self.config_dir) 35 35 36 36 self.assertEquals(config["string"], "foobar") 37 37 self.assertEquals(config["float"], 0.435) 38 38 39 39 # Test loading an old config from 1.1.x 40 40 import pickle 41 41 pickle.dump(DEFAULTS, open(os.path.join(self.config_dir, "test.conf"), "wb")) 42 42 43 43 check_config() 44 44 45 45 # Test opening a previous 1.2 config file of just a json object 46 46 import json … … 48 48 49 49 check_config() 50 50 51 51 # Test opening a previous 1.2 config file of having the format versions 52 52 # as ints … … 56 56 json.dump(DEFAULTS, f, indent=2) 57 57 f.close() 58 58 59 59 check_config() 60 60 61 61 # Test the 1.2 config format 62 62 v = {"format": 1, "file": 1} … … 65 65 json.dump(DEFAULTS, f, indent=2) 66 66 f.close() 67 67 68 68 check_config() 69 69 70 70 def test_save(self): 71 71 config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) 72 # We do this twice because the first time we need to save the file to disk 73 # and the second time we do a compare and we should not write 74 ret = config.save() 75 self.assertTrue(ret) 76 ret = config.save() 77 self.assertTrue(ret) 78 72 79 config["string"] = "baz" 73 80 config["int"] = 2 … … 75 82 self.assertTrue(ret) 76 83 del config 77 84 78 85 config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) 79 86 self.assertEquals(config["string"], "baz") … … 85 92 config["int"] = 2 86 93 self.assertTrue(config._save_timer.active()) 87 94 88 95 def check_config(config): 89 96 self.assertTrue(not config._save_timer.active()) … … 92 99 self.assertEquals(config["string"], "baz") 93 100 self.assertEquals(config["int"], 2) 94 101 95 102 from twisted.internet.task import deferLater 96 103 from twisted.internet import reactor … … 100 107 def test_find_json_objects(self): 101 108 s = """{ 102 "file": 1, 109 "file": 1, 103 110 "format": 1 104 111 }{ 105 "ssl": true, 106 "enabled": false, 112 "ssl": true, 113 "enabled": false, 107 114 "port": 8115 108 115 }\n""" 109 116 110 117 from deluge.config import find_json_objects 111 118 112 119 objects = find_json_objects(s) 113 120 self.assertEquals(len(objects), 2) 114
Note:
See TracChangeset
for help on using the changeset viewer.