~woutc/specto/specto-woutc

« back to all changes in this revision

Viewing changes to spectlib/watch.py

  • Committer: Wout Clymans
  • Date: 2008-11-26 18:18:51 UTC
  • Revision ID: wout@wout-laptop-20081126181851-0jwsnpkcpeap0tw3
- Faster startup and less calls!

Before:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   176472    0.551    0.000    0.551    0.000 {hasattr}
   176438    0.856    0.000    1.407    0.000 iniparser.py:324(__setattr__)
   138328    0.323    0.000    0.323    0.000 {isinstance}

Now:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    24431    0.055    0.000    0.055    0.000 {min}
    17784    0.059    0.000    0.059    0.000 {hasattr}
    17750    0.088    0.000    0.147    0.000 iniparser.py:324(__setattr__)

Hope this helps and improves the specto experience

Show diffs side-by-side

added added

removed removed

Lines of Context:
433
433
            self.valid = False
434
434
            self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
435
435
        
436
 
    def read_all_watches(self):
 
436
    def read_all_watches(self, startup=False):
437
437
        """
438
438
        Read the watch options from the config file
439
439
        and return a dictionary containing the info needed to start the watches.
440
440
        """
441
441
        watch_value_db = {}
442
442
 
443
 
        try:
444
 
            self.cfg = ini_namespace(file(self.file_name))
445
 
        except:
446
 
            self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
447
 
            return False
 
443
        if startup == False:
 
444
            try:
 
445
                self.cfg = ini_namespace(file(self.file_name))
 
446
            except:
 
447
                self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
 
448
                return False
448
449
  
449
450
        names = self.cfg._sections.keys()
450
451
        i = 0
451
452
        for name_ in names:
452
 
            watch_value_db[i] = self.read_watch(name_)
 
453
            watch_value_db[i] = self.read_watch(name_, startup)
453
454
            i += 1
454
455
        return watch_value_db
455
456
    
456
 
    def read_watch(self,name):
 
457
    def read_watch(self,name, startup=False):
457
458
        """
458
459
        Read the watch options from one watch.
459
460
        """
460
461
        watch_options = {}
461
 
              
462
 
        try:
463
 
            self.cfg = ini_namespace(file(self.file_name))
464
 
        except:
465
 
            self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
466
 
            return False
 
462
        
 
463
        if startup == False:            
 
464
            try:
 
465
                self.cfg = ini_namespace(file(self.file_name))
 
466
            except:
 
467
                self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
 
468
                return False
467
469
        
468
470
        name = self.hide_brackets(name)    
469
471
        options = self.cfg._sections[name]._options.keys()
470
472
        
471
473
        for option_ in options:
472
474
            if option_ == "password" and not self.check_old_version(self.cfg[name]['type']): #don't use decoding for old watches.list
473
 
                option = self.read_option(name, option_)
 
475
                option = self.read_option(name, option_, startup)
474
476
                option = self.decode_password(name, option)
475
477
            else:
476
 
                option = self.read_option(name, option_)
 
478
                option = self.read_option(name, option_, startup)
477
479
                
478
480
            watch_options_ = { option_: option }
479
481
            watch_options.update(watch_options_)
482
484
                
483
485
        return watch_options
484
486
    
485
 
    def read_option(self, name, option):
 
487
    def read_option(self, name, option, startup=False):
486
488
        """ Read one option from a watch """
487
 
        cfg = ini_namespace(file(self.file_name))
 
489
        if startup == False:            
 
490
            try:
 
491
                self.cfg = ini_namespace(file(self.file_name))
 
492
            except:
 
493
                self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
 
494
                return False
488
495
        try:
489
 
            return cfg[name][option]
 
496
            return self.cfg[name][option]
490
497
        except:
491
498
            return 0
492
499