I’m writing an application with a config file used with ConfigParser module.
This config file is actually stored in %APPDATA%/PressLogReader/PressLogReader.conf.
But the problem is that the %APPDATA%/PressLogReader (and the config file) doesn’t exists at the first start of the application, so the app crashes.
Maybe I can create the %APPDATA%/PressLogReader folder at the install of the application but I think that it’s a bad solution.
Have you got a better solution ?
There is my Conf class :
APP_DIRNAME = 'PressLogReader'
if 'APPDATA' in os.environ:
APP_CONFIG_PATH = os.path.join(os.environ['APPDATA'], APP_DIRNAME)
elif 'XDG_CONFIG_HOME' in os.environ:
APP_CONFIG_PATH = os.path.join(os.environ['XDG_CONFIG_HOME'], APP_DIRNAME)
else:
APP_CONFIG_PATH = os.path.join(os.environ['HOME'], '.config', APP_DIRNAME)
APP_CONF_FILE = os.path.join(APP_CONFIG_PATH, 'PressLogReader.conf')
class Config :
def __init__(self) :
self.parser = ConfigParser.ConfigParser()
params = {
'logFile': 'C:\Program Files\syslog\log\messages',
'filterByIP': False,
'IpFilter': ''
}
if not os.access(APP_CONF_FILE, os.F_OK | os.W_OK) :
self.parser.add_section('general')
for arg in params:
self.parser.set('general', arg, params[arg])
self.write()
self.parser.read(APP_CONF_FILE)
self.getLogFile()
self.getFilterByIp()
self.getIpFilter()
def write(self) :
with open(APP_CONF_FILE, 'wb') as conf_file:
self.parser.write(conf_file)
def getLogFile(self) :
try :
self.log_file = self.parser.get('general', 'logFile')
except ConfigParser.NoSectionError :
self.parser.add_section('general')
self.parser.set('general', 'logFile', params['logFile'])
except ConfigParser.NoOptionError :
self.parser.set('general', 'logFile', params['logFile'])
finally :
self.log_file = params['logFile']
def getFilterByIp(self) :
try :
self.filter_by_ip = self.parser.getboolean('general', 'filterByIP')
except ConfigParser.NoSectionError :
self.parser.add_section('general')
self.parser.set('general', 'filterByIP', params['filterByIP'])
except ConfigParser.NoOptionError :
self.parser.set('general', 'filterByIP', params['filterByIP'])
finally :
self.filter_by_ip = params['filterByIP']
def getIpFilter(self) :
try :
self.ip_filter = self.parser.get('general', 'IpFilter')
except ConfigParser.NoSectionError :
self.parser.add_section('general')
self.parser.set('general', 'IpFilter', params['IpFilter'])
except ConfigParser.NoOptionError :
self.parser.set('general', 'IpFilter', params['IpFilter'])
finally :
self.ip_filter = params['IpFilter']
This snippet will check if the
PressLogReaderfile exists in the folder in os.environ that is associated with the keyAPPDATA:You could extend it by reworking it so that it checked each of the places that your config file might have been placed before creating the new folder if you liked. The meat of the solution is the same, so it’s left to the reader as an exercise.
By the way, it’s our config file – why do you need to worry about it being in multiple places? Do you have legacy code that required it be placed somewhere else, or is this perhaps for cross platform-iness?
EDIT:
You can also use
os.path.exists(file_path)to check whether a file exists on the system. Therefore, maybe rather than checkingos.path.exists(os.path.join(os.environ['APPDATA'], APP_DIRNAME))you might checkos.path.exists(os.path.join(os.environ['APPDATA'], APP_DIRNAME, 'PressLogReader.conf'))in each of the directories you need to check.