+ got rid of minor bugs
[qpalma.git] / scripts / SettingsParser.py
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
5 #
6 # Written (W) 2008 Fabio De Bona
7 # Copyright (C) 2008 Max-Planck-Society
8
9 #
10 # This file contains the main interface to the QPalma pipeline.
11 #
12
13 import os
14 import os.path
15 import sys
16
17 jp = os.path.join
18
19
20 def parseSettingsFile(filename):
21 """
22 This function parse all key value pairs from the given filename
23 """
24 global_settings = {}
25 for line in open(filename):
26 if (not line.strip()) or line.startswith('#'):
27 pass
28 else:
29 key, val = line.strip().replace(' ', '').split('=')
30 global_settings[key] = val
31
32 return global_settings
33
34
35 def makeSettings(global_settings):
36 """
37
38 """
39 assert os.path.exists(global_settings['result_dir']),'Error: You have to specify a existing result directory!'
40 result_dir = global_settings['result_dir']
41 global_settings['approximation_dir'] = jp(result_dir, 'approximation')
42 global_settings['preproc_dir'] = jp(result_dir, 'preprocessing')
43 global_settings['postproc_dir'] = jp(result_dir, 'postprocessing')
44 global_settings['prediction_dir'] = jp(result_dir, 'prediction')
45 global_settings['training_dir'] = jp(result_dir, 'training')
46
47 for dir_name in ['approximation_dir', 'preproc_dir', 'postproc_dir',\
48 'prediction_dir', 'training_dir']:
49 try:
50 os.mkdir(global_settings[dir_name])
51 continue
52 except:
53 print ('Error: There was a problem generating the subdirectory: %s' % dir_name)
54
55 try:
56 os.mkdir(global_settings['global_log_fn'])
57 except:
58 print 'Error: There was a problem generating the logfile %s' % global_settings['global_log_fn']
59
60
61 try:
62 global_settings['num_splits'] = int(global_settings['num_splits'])
63 except:
64 print 'Error: num_splits has to be a positive integer'
65
66 return global_settings
67
68
69 def checkSettings(global_settings):
70 for (key, val,) in global_settings.items():
71 if key.endswith('_fn'):
72 assert os.path.exists(val), 'Error: Path/File %s with value %s does not seem to exist!' % (key,val)
73 if key.endswith('_dir'):
74 assert os.path.exists(val), 'Error: Path/File %s with value %s does not seem to exist!' % (key,val)
75
76 return True
77
78
79 def parseSettings(filename):
80 global_settings = parseSettingsFile(filename)
81 global_settings = makeSettings(global_settings)
82 assert checkSettings(global_settings),'Check your settings some entries were invalid!'
83
84 return global_settings