Package mathbench :: Package basement :: Module configuration
[hide private]

Source Code for Module mathbench.basement.configuration

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  The tools to save/load/use the user's customisations. 
  6  """ 
  7  import sys 
  8  import os 
  9  from ConfigParser import SafeConfigParser 
 10                   
 11  import wx 
 12  import wx.py.dispatcher as dispatcher 
 13   
 14  MB_DEFAULT_OPTIONS=""" 
 15  [Auto Completion] 
 16  Show_Auto_Completion = 1 
 17  Include_Magic_Attributes = 1 
 18  Include_Single_Underscores = 1 
 19  Include_Double_Underscores = 0 
 20   
 21  [Call Tips] 
 22  Show_Call_Tips = 1 
 23  Insert_Call_Tips = 1 
 24   
 25  [View] 
 26  Wrap_Lines = 1 
 27  Show_Line_Numbers = 1 
 28   
 29  [Development] 
 30  Verbosity_Level = INFO 
 31  """ 
 32   
33 -class MathBenchConfig(SafeConfigParser):
34 """ 35 Hold the configuration for the whole application and makes it 36 possible to save the default and prefered options of the users. 37 38 It is implemented as a singleton. 39 """ 40 41 __instance = None 42 43 _filepath = ".config" 44
45 - def __init__(self):
46 """ 47 Save the default if the file does not exists, else just read it. 48 filepath should the the full path to the file (base + name) 49 """ 50 if self.__instance is not None: 51 raise Exception("Instanciating a second singleton... arg !") 52 # make sure the config file exists 53 if not os.path.isfile(self._filepath): 54 config_file = open(self._filepath,'w') 55 config_file.write(MB_DEFAULT_OPTIONS) 56 config_file.close() 57 # load the file 58 SafeConfigParser.__init__(self) 59 self.read(self._filepath)
60
61 - def save(self):
62 """ 63 Write the config in the file 64 """ 65 config_file = open(self._filepath,'w') 66 self.write(config_file) 67 config_file.close()
68 69 @staticmethod
70 - def setFilePath(filepath):
71 """ 72 Set the config file's path. 73 To be called before any initialisation. 74 """ 75 MathBenchConfig._filepath = filepath
76 77 @staticmethod
78 - def getConfig():
79 """ 80 Return the singleton's instance 81 """ 82 if MathBenchConfig.__instance is None: 83 MathBenchConfig.__instance = MathBenchConfig() 84 # print MathBenchConfig.__instance 85 return MathBenchConfig.__instance
86 87 88 89 90 # # This should make it possible to have the whole file behave like a 91 # # singleton 92 # if "configuration" in __name__: 93 94 95 # def GetConfig(): 96 # """ 97 # Return the *unique* config parser object. 98 # """ 99 # return MathBenchConfig.getConfig() 100 101 # def SetFilePath(): 102 # """ 103 # Return the *unique* config parser object. 104 # """ 105 # return MathBenchConfig.getConfig() 106