toolsutils.py
branchstable
changeset 7677 134613d3b353
parent 7301 93e96700e0c0
child 7879 9aae456abab5
equal deleted inserted replaced
7670:6397a9051f65 7677:134613d3b353
   157         log('set permissions to 0600 for %s', filepath)
   157         log('set permissions to 0600 for %s', filepath)
   158     else:
   158     else:
   159         print '-> set permissions to 0600 for %s' % filepath
   159         print '-> set permissions to 0600 for %s' % filepath
   160     chmod(filepath, 0600)
   160     chmod(filepath, 0600)
   161 
   161 
   162 def read_config(config_file):
   162 def read_config(config_file, raise_if_unreadable=False):
   163     """read the instance configuration from a file and return it as a
   163     """read some simple configuration from `config_file` and return it as a
   164     dictionnary
   164     dictionary. If `raise_if_unreadable` is false (the default), an empty
   165 
   165     dictionary will be returned if the file is inexistant or unreadable, else
   166     :type config_file: str
   166     :exc:`ExecutionError` will be raised.
   167     :param config_file: path to the configuration file
       
   168 
       
   169     :rtype: dict
       
   170     :return: a dictionary with specified values associated to option names
       
   171     """
   167     """
   172     from logilab.common.fileutils import lines
   168     from logilab.common.fileutils import lines
   173     config = current = {}
   169     config = current = {}
   174     try:
   170     try:
   175         for line in lines(config_file, comments='#'):
   171         for line in lines(config_file, comments='#'):
   188                 continue
   184                 continue
   189             option = option.strip().replace(' ', '_')
   185             option = option.strip().replace(' ', '_')
   190             value = value.strip()
   186             value = value.strip()
   191             current[option] = value or None
   187             current[option] = value or None
   192     except IOError, ex:
   188     except IOError, ex:
   193         warning('missing or non readable configuration file %s (%s)',
   189         if raise_if_unreadable:
   194                 config_file, ex)
   190             raise ExecutionError('%s. Are you logged with the correct user '
       
   191                                  'to use this instance?' % ex)
       
   192         else:
       
   193             warning('missing or non readable configuration file %s (%s)',
       
   194                     config_file, ex)
   195     return config
   195     return config
   196 
   196 
   197 
   197 
   198 _HDLRS = {}
   198 _HDLRS = {}
   199 
   199