Commit bd0b4462b0c392da044497b99b91401b3902af11
1 parent
9bde801a
Exists in
master
and in
6 other branches
ENH: Create backup of the config.cfg FIX: #197
Showing
2 changed files
with
31 additions
and
9 deletions
Show diff stats
invesalius/invesalius.py
invesalius/session.py
1 | 1 | import ConfigParser |
2 | 2 | import os |
3 | +import shutil | |
3 | 4 | from threading import Thread |
4 | 5 | import time |
5 | 6 | |
... | ... | @@ -51,6 +52,25 @@ class Session(object): |
51 | 52 | def StopRecording(self, pubsub_evt): |
52 | 53 | self.ws.Stop() |
53 | 54 | |
55 | + def SaveConfigFileBackup(self): | |
56 | + path = os.path.join(self.homedir , | |
57 | + '.invesalius', 'config.cfg') | |
58 | + path_dst = os.path.join(self.homedir , | |
59 | + '.invesalius', 'config.backup') | |
60 | + shutil.copy(path, path_dst) | |
61 | + | |
62 | + def RecoveryConfigFile(self): | |
63 | + homedir = self.homedir = os.path.expanduser('~') | |
64 | + try: | |
65 | + path = os.path.join(self.homedir , | |
66 | + '.invesalius', 'config.backup') | |
67 | + path_dst = os.path.join(self.homedir , | |
68 | + '.invesalius', 'config.cfg') | |
69 | + shutil.copy(path, path_dst) | |
70 | + return True | |
71 | + except(IOError): | |
72 | + return False | |
73 | + | |
54 | 74 | |
55 | 75 | def CloseProject(self): |
56 | 76 | import constants as const |
... | ... | @@ -171,7 +191,6 @@ class Session(object): |
171 | 191 | return False |
172 | 192 | |
173 | 193 | def ReadSession(self): |
174 | - | |
175 | 194 | config = ConfigParser.ConfigParser() |
176 | 195 | home_path = os.path.expanduser('~') |
177 | 196 | path = os.path.join(home_path ,'.invesalius', 'config.cfg') |
... | ... | @@ -186,15 +205,16 @@ class Session(object): |
186 | 205 | self.tempdir = config.get('paths','tempdir') |
187 | 206 | self.last_dicom_folder = config.get('paths','last_dicom_folder') |
188 | 207 | self.last_dicom_folder = self.last_dicom_folder.decode('utf-8') |
189 | - #print "_______________________________" | |
190 | - #print self.last_dicom_folder | |
191 | - #print type(self.last_dicom_folder) | |
192 | - #print "_______________________________" | |
193 | 208 | return True |
194 | - except(ConfigParser.NoSectionError, | |
195 | - ConfigParser.NoOptionError, | |
196 | - ConfigParser.MissingSectionHeaderError): | |
197 | - return False | |
209 | + | |
210 | + except(ConfigParser.NoSectionError, ConfigParser.NoOptionError, | |
211 | + ConfigParser.MissingSectionHeaderError, ConfigParser.ParsingError): | |
212 | + | |
213 | + if (self.RecoveryConfigFile()): | |
214 | + self.ReadSession() | |
215 | + return True | |
216 | + else: | |
217 | + return False | |
198 | 218 | |
199 | 219 | |
200 | 220 | class WriteSession(Thread): | ... | ... |