Changeset ded6bb
- Timestamp:
- 08/01/2009 02:26:26 AM (16 years ago)
- Branches:
- 2.0.x, develop, extjs4-port, master
- Children:
- 9b0d4f
- Parents:
- 838cef
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
deluge/config.py
r838cef rded6bb 41 41 really. 42 42 43 The format of the config file is as follows:44 45 < format version as int>46 <con fig file version as int>47 <content> 48 49 The format version is controlled by the Config class. It should only be changed 50 when anything below it is changed directly by the Config class. An example of 51 this would be if wechanged the serializer for the content to something different.43 The format of the config file is two json encoded dicts: 44 45 <version dict> 46 <content dict> 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 51 changed the serializer for the content to something different. 52 52 53 53 The config file version is changed by the 'owner' of the config file. This is … … 91 91 return property(doc=func.__doc__, **func()) 92 92 93 def find_json_objects(s): 94 """ 95 Find json objects in a string. 96 97 :param s: the string to find json objects in 98 :type s: string 99 100 :returns: a list of tuples containing start and end locations of json objects in the string `s` 101 :rtype: [(start, end), ...] 102 103 """ 104 objects = [] 105 opens = 0 106 start = s.find("{") 107 offset = start 108 109 if start < 0: 110 return [] 111 112 for index, c in enumerate(s[offset:]): 113 if c == "{": 114 opens += 1 115 elif c == "}": 116 opens -= 1 117 if opens == 0: 118 objects.append((start, index+offset+1)) 119 start = index + offset + 1 120 121 return objects 122 123 93 124 class Config(object): 94 125 """ … … 106 137 107 138 # These hold the version numbers and they will be set when loaded 108 self.__format_version = 1 109 self.__file_version = 1 139 self.__version = { 140 "format": 1, 141 "file": 1 142 } 110 143 111 144 # This will get set with a reactor.callLater whenever a config option 112 145 # is set. 113 self._ _save_timer = None146 self._save_timer = None 114 147 115 148 if defaults: … … 135 168 """ 136 169 Sets item 'key' to 'value' in the config dictionary, but does not allow 137 changing the item's type unless it is None 170 changing the item's type unless it is None. If the types do not match, 171 it will attempt to convert it to the set type before raising a ValueError. 138 172 139 173 :param key: string, item to change to change 140 174 :param value: the value to change item to, must be same type as what is currently in the config 141 175 142 :raises ValueError: raised when the type of value is not the same as what is currently in the config 176 :raises ValueError: raised when the type of value is not the same as\ 177 what is currently in the config and it could not convert the value 143 178 144 179 **Usage** … … 187 222 188 223 # We set the save_timer for 5 seconds if not already set 189 if not self._ _save_timer or not self.__save_timer.active():190 self._ _save_timer = reactor.callLater(5, self.save)224 if not self._save_timer or not self._save_timer.active(): 225 self._save_timer = reactor.callLater(5, self.save) 191 226 192 227 def __getitem__(self, key): … … 305 340 306 341 try: 307 data = open(filename, "rb") 342 data = open(filename, "rb").read() 308 343 except IOError, e: 309 344 log.warning("Unable to open config file %s: %s", filename, e) 310 345 return 311 346 312 try: 313 self.__format_version = int(data.readline()) 314 except ValueError: 315 data.seek(0) 316 else: 347 objects = find_json_objects(data) 348 349 if not len(objects): 350 # No json objects found, try depickling it 317 351 try: 318 self.__file_version = int(data.readline()) 319 except ValueError: 320 pass 321 322 fdata = data.read() 323 data.close() 324 325 try: 326 self.__config.update(json.loads(fdata)) 327 except Exception, e: 328 log.exception(e) 329 try: 330 self.__config.update(pickle.loads(fdata)) 352 self.__config.update(pickle.loads(data)) 331 353 except Exception, e: 332 354 log.exception(e) 333 log.warning("Unable to load config file: %s", filename) 334 335 336 355 log.warning("Unable to load config file: %s", filename) 356 elif len(objects) == 1: 357 start, end = objects[0] 358 try: 359 self.__config.update(json.loads(data[start:end])) 360 except Exception, e: 361 log.exception(e) 362 log.warning("Unable to load config file: %s", filename) 363 elif len(objects) == 2: 364 try: 365 start, end = objects[0] 366 self.__version.update(json.loads(data[start:end])) 367 start, end = objects[1] 368 self.__config.update(json.loads(data[start:end])) 369 except Exception, e: 370 log.exception(e) 371 log.warning("Unable to load config file: %s", filename) 372 337 373 log.debug("Config %s version: %s.%s loaded: %s", filename, 338 self.__ format_version, self.__file_version, self.__config)374 self.__version["format"], self.__version["file"], self.__config) 339 375 340 376 def save(self, filename=None): … … 352 388 # We will only write a new config file if there is a difference 353 389 try: 354 data = open(filename, "rb") 355 data.readline() 356 data.readline() 357 loaded_data = json.loads(data.read()) 358 data.close() 359 if self.__config == loaded_data: 390 data = open(filename, "rb").read() 391 objects = find_json_objects(data) 392 start, end = objects[0] 393 version = json.loads(data[start:end]) 394 start, end = objects[1] 395 loaded_data = json.loads(data[start:end]) 396 397 if self.__config == loaded_data and self.__version == version: 360 398 # The config has not changed so lets just return 361 self._ _save_timer = None399 self._save_timer = None 362 400 return 363 401 except Exception, e: 364 402 log.warning("Unable to open config file: %s", filename) 365 403 366 self._ _save_timer = None404 self._save_timer = None 367 405 368 406 # Save the new config and make sure it's written to disk … … 370 408 log.debug("Saving new config file %s", filename + ".new") 371 409 f = open(filename + ".new", "wb") 372 f.write(str(self.__format_version) + "\n") 373 f.write(str(self.__file_version) + "\n") 410 json.dump(self.__version, f, indent=2) 374 411 json.dump(self.__config, f, indent=2) 375 412 f.flush() … … 415 452 raise ValueError("output_version needs to be greater than input_range") 416 453 417 if self.__ file_versionnot in input_range:454 if self.__version["file"] not in input_range: 418 455 log.debug("File version %s is not in input_range %s, ignoring converter function..", 419 self.__ file_version, input_range)456 self.__version["file"], input_range) 420 457 return 421 458 … … 425 462 log.exception(e) 426 463 log.error("There was an exception try to convert config file %s %s to %s", 427 self.__config_file, self.__ file_version, output_version)464 self.__config_file, self.__version["file"], output_version) 428 465 raise e 429 466 else: 430 self.__ file_version= output_version467 self.__version["file"] = output_version 431 468 self.save() 432 469 -
tests/common.py
r838cef rded6bb 9 9 config_directory = tempfile.mkdtemp() 10 10 deluge.configmanager.set_config_dir(config_directory) 11 return config_directory 11 12 12 13 import gettext
Note:
See TracChangeset
for help on using the changeset viewer.