Commit 596a2f6de844ed2b102068e93be6ab868f8cb2fd

Authored by Paulo Henrique Junqueira Amorim
1 parent 565670eb

ADD: Implemented write config file with thread

Showing 2 changed files with 77 additions and 4 deletions   Show diff stats
invesalius/gui/frame.py
... ... @@ -110,8 +110,13 @@ class Frame(wx.Frame):
110 110 def __bind_events_wx(self):
111 111 self.Bind(wx.EVT_SIZE, self.OnSize)
112 112 self.Bind(wx.EVT_MENU, self.OnMenuClick)
  113 + self.Bind(wx.EVT_CLOSE, self.CloseWindow)
113 114 #self.Bind(wx.EVT_CLOSE, self.OnExit)
114   -
  115 +
  116 + def CloseWindow(self, evt):
  117 + ps.Publisher().sendMessage("Stop Config Recording")
  118 + self.Destroy()
  119 +
115 120 def __init_aui(self):
116 121  
117 122 # Tell aui_manager to manage this frame
... ...
invesalius/session.py
  1 +import ConfigParser
1 2 import os
  3 +from threading import Thread
  4 +import time
  5 +import wx.lib.pubsub as ps
2 6  
3 7 import constants as const
4 8 from utils import Singleton
5 9  
  10 +import wx.lib.pubsub as ps
6 11 class Session(object):
7 12 # Only one session will be initialized per time. Therefore, we use
8 13 # Singleton design pattern for implementing it
... ... @@ -19,9 +24,9 @@ class Session(object):
19 24 self.mode = const.MODE_RP
20 25 # const.MODE_RP, const.MODE_NAVIGATOR, const.MODE_RADIOLOGY,
21 26 # const.MODE_ODONTOLOGY
22   -
  27 +
23 28 # InVesalius default projects' directory
24   - homedir = os.path.expanduser('~')
  29 + homedir = self.homedir = os.path.expanduser('~')
25 30 invdir = os.path.join(homedir, ".invesalius", "temp")
26 31 if not os.path.isdir(invdir):
27 32 os.makedirs(invdir)
... ... @@ -32,7 +37,16 @@ class Session(object):
32 37  
33 38 # Recent projects list
34 39 self.recent_projects = []
35   -
  40 +
  41 + ws = self.ws = WriteSession(self)
  42 + ws.start()
  43 +
  44 + ps.Publisher().subscribe(self.StopRecording, "Stop Config Recording")
  45 +
  46 +
  47 + def StopRecording(self, pubsub_evt):
  48 + self.ws.Stop()
  49 +
36 50 # GUI language
37 51 self.language = "en_GB" # "pt_BR", "es"
38 52  
... ... @@ -110,3 +124,57 @@ class Session(object):
110 124 dict = plistlib.readPlist(main_plist)
111 125 for key in dict:
112 126 setattr(self, key, dict[key])
  127 +
  128 + def ReadSession(self):
  129 + config = ConfigParser.ConfigParser()
  130 + path = os.path.join(self.homedir ,'.invesalius', 'config.cfg')
  131 + config.read(path)
  132 +
  133 + self.mode = config.get('session', 'mode')
  134 + self.project_status = config.get('session', 'status')
  135 + self.debug = config.get('session','debug')
  136 + self.recent_projects = eval(config.get('project','recent_projects'))
  137 + self.homedir = config.get('paths','homedir')
  138 + self.invdir = config.get('paths','invdir')
  139 +
  140 +
  141 +class WriteSession(Thread):
  142 +
  143 + def __init__ (self, session):
  144 + Thread.__init__(self)
  145 + self.session = session
  146 + self.runing = 1
  147 +
  148 + def run(self):
  149 + while self.runing:
  150 + time.sleep(10)
  151 + self.Write()
  152 +
  153 + def Stop(self):
  154 + print "VAI PARAR A THREAD................"
  155 + self.runing = 0
  156 +
  157 + def Write(self):
  158 +
  159 + config = ConfigParser.RawConfigParser()
  160 +
  161 + config.add_section('session')
  162 + config.set('session', 'mode', self.session.mode)
  163 + config.set('session', 'status', self.session.project_status)
  164 + config.set('session','debug', self.session.debug)
  165 +
  166 + config.add_section('project')
  167 + config.set('project', 'recent_projects', self.session.recent_projects)
  168 +
  169 + config.add_section('paths')
  170 + config.set('paths','homedir',self.session.homedir)
  171 + config.set('paths','invdir',self.session.invdir)
  172 + path = os.path.join(self.session.homedir ,
  173 + '.invesalius', 'config.cfg')
  174 + configfile = open(path, 'wb')
  175 + config.write(configfile)
  176 + configfile.close()
  177 +
  178 +
  179 +
  180 +
113 181 \ No newline at end of file
... ...