session.py
9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#--------------------------------------------------------------------------
# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: http://www.softwarepublico.gov.br
# Contact: invesalius@cti.gov.br
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
#--------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
#--------------------------------------------------------------------------
import ConfigParser
import os
import shutil
import sys
from threading import Thread
import time
#import wx.lib.pubsub as ps
from wx.lib.pubsub import pub as Publisher
from utils import Singleton, debug
from random import randint
class Session(object):
# Only one session will be initialized per time. Therefore, we use
# Singleton design pattern for implementing it
__metaclass__= Singleton
def __init__(self):
self.temp_item = False
def CreateItens(self):
import constants as const
self.project_path = ()
self.debug = False
self.project_status = const.PROJ_CLOSE
# const.PROJ_NEW*, const.PROJ_OPEN, const.PROJ_CHANGE*,
# const.PROJ_CLOSE
self.mode = const.MODE_RP
# const.MODE_RP, const.MODE_NAVIGATOR, const.MODE_RADIOLOGY,
# const.MODE_ODONTOLOGY
# InVesalius default projects' directory
homedir = self.homedir = os.path.expanduser('~')
tempdir = os.path.join(homedir, ".invesalius", "temp")
if not os.path.isdir(tempdir):
os.makedirs(tempdir)
self.tempdir = tempdir
# GUI language
self.language = "" # "pt_BR", "es"
self.random_id = randint(0,pow(10,16))
#print self.random_id
# Recent projects list
self.recent_projects = [(const.SAMPLE_DIR, "Cranium.inv3")]
self.last_dicom_folder = ''
self.surface_interpolation = 1
self.rendering = 0
self.WriteSessionFile()
def SaveConfigFileBackup(self):
path = os.path.join(self.homedir ,
'.invesalius', 'config.cfg')
path_dst = os.path.join(self.homedir ,
'.invesalius', 'config.backup')
shutil.copy(path, path_dst)
def RecoveryConfigFile(self):
homedir = self.homedir = os.path.expanduser('~')
try:
path = os.path.join(self.homedir ,
'.invesalius', 'config.backup')
path_dst = os.path.join(self.homedir ,
'.invesalius', 'config.cfg')
shutil.copy(path, path_dst)
return True
except(IOError):
return False
def CloseProject(self):
import constants as const
debug("Session.CloseProject")
self.project_path = ()
self.project_status = const.PROJ_CLOSE
#self.mode = const.MODE_RP
self.temp_item = False
self.WriteSessionFile()
def SaveProject(self, path=()):
import constants as const
debug("Session.SaveProject")
self.project_status = const.PROJ_OPEN
if path:
self.project_path = path
self.__add_to_list(path)
if self.temp_item:
self.temp_item = False
self.WriteSessionFile()
def ChangeProject(self):
import constants as const
debug("Session.ChangeProject")
self.project_status = const.PROJ_CHANGE
def CreateProject(self, filename):
import constants as const
debug("Session.CreateProject")
Publisher.sendMessage('Begin busy cursor')
# Set session info
self.project_path = (self.tempdir, filename)
self.project_status = const.PROJ_NEW
self.temp_item = True
self.WriteSessionFile()
return self.tempdir
def OpenProject(self, filepath):
import constants as const
debug("Session.OpenProject")
# Add item to recent projects list
item = (path, file) = os.path.split(filepath)
self.__add_to_list(item)
# Set session info
self.project_path = item
self.project_status = const.PROJ_OPEN
self.WriteSessionFile()
def RemoveTemp(self):
if self.temp_item:
(dirpath, file) = self.project_path
path = os.path.join(dirpath, file)
os.remove(path)
self.temp_item = False
def WriteSessionFile(self):
config = ConfigParser.RawConfigParser()
config.add_section('session')
config.set('session', 'mode', self.mode)
config.set('session', 'status', self.project_status)
config.set('session','debug', self.debug)
config.set('session', 'language', self.language)
config.set('session', 'random_id', self.random_id)
config.set('session', 'surface_interpolation', self.surface_interpolation)
config.set('session', 'rendering', self.rendering)
config.add_section('project')
config.set('project', 'recent_projects', self.recent_projects)
config.add_section('paths')
config.set('paths','homedir',self.homedir)
config.set('paths','tempdir',self.tempdir)
try:
config.set('paths','last_dicom_folder',self.last_dicom_folder.encode('utf-8'))
except (UnicodeEncodeError, UnicodeDecodeError):
config.set('paths','last_dicom_folder',self.last_dicom_folder)
path = os.path.join(self.homedir ,
'.invesalius', 'config.cfg')
configfile = open(path, 'wb')
config.write(configfile)
configfile.close()
def __add_to_list(self, item):
import constants as const
# Last projects list
l = self.recent_projects
# If item exists, remove it from list
if l.count(item):
l.remove(item)
# Add new item
l.insert(0, item)
# Remove oldest projects from list
if len(l)>const.PROJ_MAX:
for i in xrange(len(l)-const.PROJ_MAX):
l.pop()
def GetLanguage(self):
return self.language
def SetLanguage(self, language):
self.language = language
def GetRandomId(self):
return self.random_id
def SetRandomId(self, random_id):
self.random_id = random_id
def GetLastDicomFolder(self):
return self.last_dicom_folder
def SetLastDicomFolder(self, folder):
self.last_dicom_folder = folder
self.WriteSessionFile()
def ReadLanguage(self):
config = ConfigParser.ConfigParser()
home_path = os.path.expanduser('~')
path = os.path.join(home_path ,'.invesalius', 'config.cfg')
try:
config.read(path)
self.language = config.get('session','language')
return self.language
except (ConfigParser.NoSectionError,
ConfigParser.NoOptionError,
ConfigParser.MissingSectionHeaderError):
return False
def ReadRandomId(self):
config = ConfigParser.ConfigParser()
home_path = os.path.expanduser('~')
path = os.path.join(home_path ,'.invesalius', 'config.cfg')
try:
config.read(path)
self.random_id = config.get('session','random_id')
return self.random_id
except (ConfigParser.NoSectionError,
ConfigParser.NoOptionError,
ConfigParser.MissingSectionHeaderError):
return False
def ReadSession(self):
config = ConfigParser.ConfigParser()
home_path = os.path.expanduser('~')
path = os.path.join(home_path ,'.invesalius', 'config.cfg')
try:
config.read(path)
self.mode = config.get('session', 'mode')
self.project_status = config.get('session', 'status')
self.debug = config.get('session','debug')
self.language = config.get('session','language')
self.recent_projects = eval(config.get('project','recent_projects'))
self.homedir = config.get('paths','homedir')
self.tempdir = config.get('paths','tempdir')
self.last_dicom_folder = config.get('paths','last_dicom_folder')
self.last_dicom_folder = self.last_dicom_folder.decode('utf-8')
self.surface_interpolation = config.get('session', 'surface_interpolation')
self.rendering = config.get('session', 'rendering')
self.random_id = config.get('session','random_id')
return True
except(ConfigParser.NoSectionError, ConfigParser.MissingSectionHeaderError,
ConfigParser.ParsingError):
if (self.RecoveryConfigFile()):
self.ReadSession()
return True
else:
return False
except(ConfigParser.NoOptionError):
#Added to fix new version compatibility
self.surface_interpolation = 0
self.rendering = 0
self.random_id = randint(0,pow(10,16))
self.WriteSessionFile()
return True