shakaran

Untitled

Jun 17th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8; tab-width: 4; mode: python -*-
  3. # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
  4. # vi: set ft=python sts=4 ts=4 sw=4 noet
  5. #
  6. # This file is part of FreeStation.
  7. #
  8. # FreeStation is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # FreeStation is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Fail2Ban; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21.  
  22. # Author: Ángel Guzmán Maeso
  23. #
  24. # $Revision$
  25.  
  26. __author__ = 'Ángel Guzmán Maeso'
  27. __version__ = '$Revision$'
  28. __date__ = '$Date$'
  29. __copyright__ = 'Copyright (c) 2012 Ángel Guzmán Maeso'
  30. __license__ = 'GPL'
  31.  
  32. import sys, os
  33. sys.path.append(os.path.dirname(__file__))
  34. sys.path.append(os.path.split(os.path.dirname(__file__))[0])
  35.  
  36. import gi
  37. try:
  38. gi.require_version('Gtk', '3.0')
  39. except ValueError:
  40. print 'Could not find required Gtk 3.0 library. Aborting.'
  41. sys.exit(1)
  42.  
  43. from gi.repository import Gtk, Gdk, GObject #@UnresolvedImport
  44. from constants import APP_NAME
  45. from threading import Thread, Condition
  46.  
  47. from logger import Logger
  48. LOG = Logger('FreeStationApp').get()
  49.  
  50. class FreeStationApp2(Thread):
  51.  
  52. __name__ = APP_NAME #@ReservedAssignment
  53.  
  54. # Class variable referring to the one and only app for use
  55. _self = None
  56.  
  57. def __init__(self, alive_time = None, enable_gui = True):
  58. """
  59. @param alive_time msec for live app, normally used by test. Default None for ignore
  60. """
  61. Thread.__init__(self)
  62. self.setDaemon(True)
  63.  
  64. if FreeStationApp2._self != None:
  65. raise RuntimeError('Only a single instance of a FreeStationApp can be instantiated.')
  66.  
  67. FreeStationApp2._self = self
  68.  
  69. # State variables. These are not class static variables.
  70. self._condition = Condition()
  71. self._done = False
  72.  
  73. self.alive_time = alive_time
  74. self.enable_gui = enable_gui
  75.  
  76. def run(self, ):
  77. LOG.debug('Parent PID: {0}'.format(os.getppid()))
  78. LOG.debug('FreeStationApp PID: {0}'.format(os.getpid()))
  79. LOG.debug('Thread name: {0}'.format(self.getName()))
  80.  
  81. GObject.threads_init()
  82. Gdk.threads_init()
  83.  
  84. Gdk.flush()
  85. Gdk.threads_enter()
  86.  
  87. if self.alive_time:
  88. print 'Use alive time'
  89. GObject.timeout_add(self.alive_time, self.on_destroy, None, None)
  90.  
  91. if self.enable_gui:
  92. from widgets.GUI2 import GUI2
  93. self.gui = GUI2(self)
  94.  
  95. Gtk.main()
  96.  
  97. Gdk.threads_leave()
  98.  
  99. # Destroy the object. Wait for the thread to terminate and cleanup
  100. # the internal state.
  101. def destroy(self):
  102. self._condition.acquire()
  103. self._done = True
  104. self._condition.notify()
  105. self._condition.release()
  106.  
  107. # Wait for the thread to terminate
  108. try:
  109. self.join()
  110. except RuntimeError:
  111. # If fails for "thread aliens" skip
  112. # http://docs.python.org/library/threading.html#thread-objects
  113. pass
  114.  
  115. Gdk.threads_leave()
  116. FreeStationApp2._self = None
  117.  
  118. def on_destroy(self, widget, event):
  119. LOG.debug('Destroying application')
  120. LOG.debug('Parent PID: {0}'.format(os.getppid()))
  121. LOG.debug('FreeStationApp PID: {0}'.format(os.getpid()))
  122. LOG.debug('Thread name: {0}'.format(self.getName()))
  123.  
  124. if Gtk.main_level() > 0:
  125. Gtk.main_quit()
  126.  
  127. self.destroy()
Advertisement
Add Comment
Please, Sign In to add comment