[Idle-dev] CVS: idle configHandler.py,1.7,1.8

Stephen M. Gava elguavas@users.sourceforge.net
Thu, 03 Jan 2002 03:58:37 -0800


Update of /cvsroot/idlefork/idle
In directory usw-pr-cvs1:/tmp/cvs-serv1884

Modified Files:
	configHandler.py 
Log Message:
further development of highlighting and editor preference handling, 
plus misc fixes cleanups for new config system


Index: configHandler.py
===================================================================
RCS file: /cvsroot/idlefork/idle/configHandler.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** configHandler.py	2001/12/03 00:37:28	1.7
--- configHandler.py	2002/01/03 11:58:35	1.8
***************
*** 1,12 ****
- ##---------------------------------------------------------------------------##
- ##
- ## idle - configuration data handler, based on and replacing IdleConfig.py 
- ## elguavas
- ## 
- ##---------------------------------------------------------------------------##
  """
! Provides access to stored idle configuration information
! """
  
  import os
  import sys
--- 1,12 ----
  """
! Provides access to stored idle configuration information.
  
+ Throughout this module there is an emphasis on returning useable defaults if
+ there is a problem returning a requested configuration value back to idle.
+ This is to allow idle to continue to function in spite of errors in the
+ retrieval of config information. When a default is returned instead of a 
+ requested config value, a message is printed to stderr to aid in 
+ configuration problem notification and resolution. 
+ """
  import os
  import sys
***************
*** 24,31 ****
          ConfigParser.__init__(self,defaults=cfgDefaults)
      
!     def Get(self, section, option, default=None, type=None):
          """
          Get an option value for given section/option or return default.
          If type is specified, return as type.
          """
          if type=='bool': 
--- 24,32 ----
          ConfigParser.__init__(self,defaults=cfgDefaults)
      
!     def Get(self, section, option, type=None): #,default=None)
          """
          Get an option value for given section/option or return default.
          If type is specified, return as type.
+         If a default is returned a warning is printed to stderr.
          """
          if type=='bool': 
***************
*** 38,43 ****
              #return getVal(section, option, raw, vars)
              return getVal(section, option)
!         else:
!             return default
  
      def GetOptionList(self,section):
--- 39,50 ----
              #return getVal(section, option, raw, vars)
              return getVal(section, option)
! #         #the following handled in IdleConf.GetOption instead
! #         else: 
! #             warning=('\n Warning: configHandler.py - IdleConfParser.Get -\n'+
! #                        ' problem retrieving configration option '+`option`+'\n'+
! #                        ' from section '+`section`+'.\n'+
! #                        ' returning default value: '+`default`+'\n')
! #             sys.stderr.write(warning)
! #             return default
  
      def GetOptionList(self,section):
***************
*** 76,83 ****
          (idle install dir)/config-keys.def
      user config  files
!         (user home dir)/.idlerc/idle-main.cfg
!         (user home dir)/.idlerc/idle-extensions.cfg
!         (user home dir)/.idlerc/idle-highlight.cfg
!         (user home dir)/.idlerc/idle-keys.cfg
      """
      def __init__(self):
--- 83,90 ----
          (idle install dir)/config-keys.def
      user config  files
!         (user home dir)/.idlerc/config-main.cfg
!         (user home dir)/.idlerc/config-extensions.cfg
!         (user home dir)/.idlerc/config-highlight.cfg
!         (user home dir)/.idlerc/config-keys.cfg
      """
      def __init__(self):
***************
*** 91,95 ****
      def CreateConfigHandlers(self):
          """
!         set up a dictionary config parsers for default and user 
          configurations respectively
          """
--- 98,102 ----
      def CreateConfigHandlers(self):
          """
!         set up a dictionary of config parsers for default and user 
          configurations respectively
          """
***************
*** 113,117 ****
          for cfgType in configTypes: #build config file names
              defCfgFiles[cfgType]=os.path.join(idledir,'config-'+cfgType+'.def')                    
!             usrCfgFiles[cfgType]=os.path.join(userdir,'idle-'+cfgType+'.cfg')                    
          for cfgType in configTypes: #create config parsers
              self.defaultCfg[cfgType]=IdleConfParser(defCfgFiles[cfgType])
--- 120,124 ----
          for cfgType in configTypes: #build config file names
              defCfgFiles[cfgType]=os.path.join(idledir,'config-'+cfgType+'.def')                    
!             usrCfgFiles[cfgType]=os.path.join(userdir,'config-'+cfgType+'.cfg')                    
          for cfgType in configTypes: #create config parsers
              self.defaultCfg[cfgType]=IdleConfParser(defCfgFiles[cfgType])
***************
*** 133,136 ****
--- 140,148 ----
              return self.defaultCfg[configType].Get(section, option, type=type)
          else:
+             warning=('\n Warning: configHandler.py - IdleConf.GetOption -\n'+
+                        ' problem retrieving configration option '+`option`+'\n'+
+                        ' from section '+`section`+'.\n'+
+                        ' returning default value: '+`default`+'\n')
+             sys.stderr.write(warning)
              return default
      
***************
*** 153,157 ****
          return cfgParser.sections()
      
!     def GetHighlight(self, theme, element):
          #get some fallback defaults
          defaultFg=self.GetOption('highlight', theme, 'normal' + "-foreground",
--- 165,175 ----
          return cfgParser.sections()
      
!     def GetHighlight(self, theme, element, fgBg=None):
!         """
!         return individual highlighting theme elements.
!         fgBg - string ('fg'or'bg') or None, if None return a dictionary
!         containing fg and bg colours (appropriate for passing to Tkinter in, 
!         e.g., a tag_config call), otherwise fg or bg colour only as specified. 
!         """
          #get some fallback defaults
          defaultFg=self.GetOption('highlight', theme, 'normal' + "-foreground",
***************
*** 161,170 ****
          #try for requested element colours
          fore = self.GetOption('highlight', theme, element + "-foreground")
!         back = self.GetOption('highlight', theme, element + "-background")
          #fall back if required
          if not fore: fore=defaultFg
          if not back: back=defaultBg
!         return {"foreground": fore,
!                 "background": back}
  
      def GetTheme(self, name=None):
--- 179,201 ----
          #try for requested element colours
          fore = self.GetOption('highlight', theme, element + "-foreground")
!         back = None
!         if element == 'cursor': #there is no config value for cursor bg
!             back = None
!         else:    
!             back = self.GetOption('highlight', theme, element + "-background")
          #fall back if required
          if not fore: fore=defaultFg
          if not back: back=defaultBg
!         highlight={"foreground": fore,"background": back}
!         if not fgBg: #return dict of both colours
!             return highlight
!         else: #return specified colour only
!             if fgBg == 'fg':
!                 return highlight["foreground"]
!             if fgBg == 'bg':
!                 return highlight["background"]
!             else:    
!                 raise 'Invalid fgBg specified'
!             
  
      def GetTheme(self, name=None):
***************
*** 175,178 ****
--- 206,242 ----
          pass
      
+     def CurrentTheme(self):
+         """
+         Returns the name of the currently active theme        
+         """
+         return self.GetOption('main','Theme','name')
+         
+ 
+     def CurrentKeys(self):
+         """
+         Returns the name of the currently active theme        
+         """
+         return self.GetOption('main','Keys','name')
+     
+     def GetExtensions(self, activeOnly=1):
+         """
+         Gets a list of all idle extensions declared in the config files.
+         activeOnly - boolean, if true only return active (enabled) extensions
+         """
+         extns=self.GetSectionList('default','extensions')
+         userExtns=self.GetSectionList('user','extensions')
+         for extn in userExtns:
+             if extn not in extns: #user has added own extension
+                 extns.append(extn) 
+         if activeOnly:
+             activeExtns=[]
+             for extn in extns:
+                 if self.GetOption('extensions',extn,'enable',default=1,type='bool'):
+                     #the extension is enabled
+                     activeExtns.append(extn)
+             return activeExtns
+         else:
+             return extns        
+ 
      def GetKeys(self, keySetName=None):
          """