1 """
2 This modules implements the procedures that handles nMOLDYN PREFERENCES.
3
4 Procedures:
5 * saveConfigurationFile: saves a PREFERENCES file.
6 * loadConfigurationFile: loads a PREFERENCES file.
7 """
8
9
10 from ConfigParser import ConfigParser
11 import copy
12 import os
13 import sys
14
15
16 from nMOLDYN.Core.Error import Error
17 from nMOLDYN.Core.Logger import LogMessage
18 from nMOLDYN.Core.Preferences import PREFERENCES
19
21 """Saves |config| configuration to |cfgFilename| file name.
22
23 @param cfgFilename: if not None, the name of the PREFERENCES file to save else the configuration file
24 will be saved to a plat-form dependant location:
25 -$USERPROFILE/Application Data/nMOLDYN/nMOLDYN.ini on Windows
26 -$HOME/Library/Preferences/nMLDYN/nMOLDYN.pref on MacOS
27 -$HOME/.nMOLDYN on Linux
28 @type: cfgFilename: string.
29
30 @param config: the configuration to save. By default, the default PREFERENCES stored in
31 nMOLDYN.Core.Preferences.nMOLDYNPreferences class.
32 @type config: instance of a dummy class whose attributes must be the ones defined in
33 nMOLDYN.Core.Preferences.nMOLDYNPreferences class.
34 """
35
36 if cfgFilename is None:
37
38 try:
39
40 if sys.platform == 'win32':
41 cfgFileDir = os.path.join(os.environ['USERPROFILE'],'Application Data','nMOLDYN')
42 cfgFilename = os.path.abspath(os.path.join(cfgFileDir, 'nMOLDYN.ini'))
43
44 elif sys.platform == 'darwin':
45 cfgFileDir = os.path.join(os.environ['HOME'],'Library','Preferences','nMOLDYN')
46 cfgFilename = os.path.abspath(os.path.join(cfgFileDir, 'nMOLDYN.pref'))
47
48
49 else:
50 cfgFileDir = os.environ['HOME']
51 cfgFilename = os.path.abspath(os.path.join(cfgFileDir, '.nMOLDYN'))
52
53 except KeyError:
54 raise Error('Unvalid default location for the nMOLDYN configuration file.')
55
56 else:
57 if not os.path.exists(cfgFileDir):
58 os.makedirs(cfgFileDir)
59
60 try:
61
62 configFile = open(cfgFilename,'w')
63
64 except IOError:
65 raise Error('Impossible to open the file %s for writing. May be a permission problem.' % cfgFilename)
66
67 else:
68 cfg = ConfigParser()
69 cfg.add_section('nmoldyn')
70
71
72 for key in vars(PREFERENCES):
73 cfg.set('nmoldyn',key,getattr(PREFERENCES,key))
74
75 for key in vars(config):
76 if key in vars(PREFERENCES):
77 cfg.set('nmoldyn',key,getattr(config,key))
78
79
80 cfg.write(configFile)
81
82 configFile.close()
83
85 """Loads a configuration from |cfgFilename| file name and updates |config| PREFERENCES.
86
87 @param cfgFilename: if not None, the name of the PREFERENCES file to load else the configuration file
88 will be loaded from a plat-form dependant location:
89 -$USERPROFILE/Application Data/nMOLDYN/nMOLDYN.ini on Windows
90 -$HOME/Library/Preferences/nMLDYN/nMOLDYN.pref on MacOS
91 -$HOME/.nMOLDYN on Linux
92 @type: cfgFilename: string.
93
94 @param config: the configuration to load. By default, the default PREFERENCES stored in
95 nMOLDYN.Core.Preferences.nMOLDYNPreferences class.
96 @type config: instance of a dummy class whose attributes must be the ones defined in
97 nMOLDYN.Core.Preferences.nMOLDYNPreferences class.
98 """
99
100 if cfgFilename is None:
101 try:
102
103 if sys.platform == 'win32':
104 cfgFilename = os.path.join(os.environ['USERPROFILE'],'Application Data','nMOLDYN','nMOLDYN.ini')
105
106 elif sys.platform == 'darwin':
107 cfgFilename = os.path.join('Library','Preferences','nMOLDYN','nMOLDYN.pref')
108
109 else:
110 cfgFilename = os.path.join(os.environ['HOME'],'.nMOLDYN')
111
112 except KeyError:
113 return
114
115 if os.path.exists(cfgFilename):
116 try:
117
118 loadedCfg = ConfigParser()
119 loadedCfg.read(cfgFilename)
120
121 except:
122 LogMessage('warning','The file %s does not exist or is not a proper configuration file.' % cfgFilename,['file','console'])
123
124 else:
125 for key, value in loadedCfg.items('nmoldyn'):
126 if key in vars(PREFERENCES):
127 setattr(config,key,value)
128