Changeset d93fcf


Ignore:
Timestamp:
08/18/2010 07:32:11 PM (14 years ago)
Author:
Andrew Resch <andrewresch@gmail.com>
Children:
b45e01
Parents:
a2d75a
Message:

Fix #1341 issue where Config would try to cancel the save_timer when it is None.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • deluge/config.py

    ra2d75a rd93fcf  
    4646<content dict>
    4747
    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 
     48The version dict contains two keys: file and format.  The format version is
     49controlled by the Config class.  It should only be changed when anything below
     50it is changed directly by the Config class.  An example of this would be if we
    5151changed the serializer for the content to something different.
    5252
     
    9494    """
    9595    Find json objects in a string.
    96    
     96
    9797    :param s: the string to find json objects in
    9898    :type s: string
    99    
     99
    100100    :returns: a list of tuples containing start and end locations of json objects in the string `s`
    101101    :rtype: [(start, end), ...]
    102    
     102
    103103    """
    104104    objects = []
     
    120120
    121121    return objects
    122    
    123    
     122
     123
    124124class Config(object):
    125125    """
     
    349349
    350350        objects = find_json_objects(data)
    351        
     351
    352352        if not len(objects):
    353353            # No json objects found, try depickling it
     
    356356            except Exception, e:
    357357                log.exception(e)
    358                 log.warning("Unable to load config file: %s", filename)               
     358                log.warning("Unable to load config file: %s", filename)
    359359        elif len(objects) == 1:
    360360            start, end = objects[0]
     
    363363            except Exception, e:
    364364                log.exception(e)
    365                 log.warning("Unable to load config file: %s", filename)               
     365                log.warning("Unable to load config file: %s", filename)
    366366        elif len(objects) == 2:
    367367            try:
     
    372372            except Exception, e:
    373373                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
    376376        log.debug("Config %s version: %s.%s loaded: %s", filename,
    377377            self.__version["format"], self.__version["file"], self.__config)
     
    397397            start, end = objects[1]
    398398            loaded_data = json.loads(data[start:end])
    399 
    400399            if self.__config == loaded_data and self.__version == version:
    401400                # The config has not changed so lets just return
    402                 self._save_timer.cancel()
     401                if self._save_timer:
     402                    self._save_timer.cancel()
    403403                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)
    408406
    409407        # Save the new config and make sure it's written to disk
     
    411409            log.debug("Saving new config file %s", filename + ".new")
    412410            f = open(filename + ".new", "wb")
    413             json.dump(self.__version, f, indent=2)           
     411            json.dump(self.__version, f, indent=2)
    414412            json.dump(self.__config, f, indent=2)
    415413            f.flush()
    416414            os.fsync(f.fileno())
    417415            f.close()
    418         except Exception, e:
     416        except IOError, e:
    419417            log.error("Error writing new config file: %s", e)
    420418            return False
  • tests/test_config.py

    ra2d75a rd93fcf  
    77from deluge.config import Config
    88
    9 DEFAULTS = {"string": "foobar", "int": 1, "float": 0.435, "bool": True, "tuple": (1, 2)}
     9DEFAULTS = {"string": "foobar", "int": 1, "float": 0.435, "bool": True}
    1010
    1111class ConfigTestCase(unittest.TestCase):
     
    1616        config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir)
    1717        self.assertEquals(DEFAULTS, config.config)
    18        
     18
    1919        config = Config("test.conf", config_dir=self.config_dir)
    2020        self.assertEquals({}, config.config)
    21    
     21
    2222    def test_set_get_item(self):
    2323        config = Config("test.conf", config_dir=self.config_dir)
     
    2727        config["foo"] = 2
    2828        self.assertEquals(config.get_item("foo"), 2)
    29        
     29
    3030        config._save_timer.cancel()
    3131
     
    3333        def check_config():
    3434            config = Config("test.conf", config_dir=self.config_dir)
    35            
     35
    3636            self.assertEquals(config["string"], "foobar")
    3737            self.assertEquals(config["float"], 0.435)
    38                
     38
    3939        # Test loading an old config from 1.1.x
    4040        import pickle
    4141        pickle.dump(DEFAULTS, open(os.path.join(self.config_dir, "test.conf"), "wb"))
    42        
     42
    4343        check_config()
    44                
     44
    4545        # Test opening a previous 1.2 config file of just a json object
    4646        import json
     
    4848
    4949        check_config()
    50        
     50
    5151        # Test opening a previous 1.2 config file of having the format versions
    5252        # as ints
     
    5656        json.dump(DEFAULTS, f, indent=2)
    5757        f.close()
    58        
     58
    5959        check_config()
    60        
     60
    6161        # Test the 1.2 config format
    6262        v = {"format": 1, "file": 1}
     
    6565        json.dump(DEFAULTS, f, indent=2)
    6666        f.close()
    67        
     67
    6868        check_config()
    6969
    7070    def test_save(self):
    7171        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
    7279        config["string"] = "baz"
    7380        config["int"] = 2
     
    7582        self.assertTrue(ret)
    7683        del config
    77        
     84
    7885        config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir)
    7986        self.assertEquals(config["string"], "baz")
     
    8592        config["int"] = 2
    8693        self.assertTrue(config._save_timer.active())
    87        
     94
    8895        def check_config(config):
    8996            self.assertTrue(not config._save_timer.active())
     
    9299            self.assertEquals(config["string"], "baz")
    93100            self.assertEquals(config["int"], 2)
    94            
     101
    95102        from twisted.internet.task import deferLater
    96103        from twisted.internet import reactor
     
    100107    def test_find_json_objects(self):
    101108        s = """{
    102   "file": 1, 
     109  "file": 1,
    103110  "format": 1
    104111}{
    105   "ssl": true, 
    106   "enabled": false, 
     112  "ssl": true,
     113  "enabled": false,
    107114  "port": 8115
    108115}\n"""
    109        
     116
    110117        from deluge.config import find_json_objects
    111        
     118
    112119        objects = find_json_objects(s)
    113120        self.assertEquals(len(objects), 2)
    114 
Note: See TracChangeset for help on using the changeset viewer.