Package qm :: Module platform_unix
[hide private]
[frames] | no frames]

Source Code for Module qm.platform_unix

  1  ######################################################################## 
  2  # 
  3  # File:   platform_unix.py 
  4  # Author: Alex Samuel 
  5  # Date:   2001-05-13 
  6  # 
  7  # Contents: 
  8  #   Platform-specific function for UNIX and UNIX-like systems. 
  9  # 
 10  # Copyright (c) 2001, 2002, 2003 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  # For license terms see the file COPYING. 
 13  # 
 14  ######################################################################## 
 15   
 16  ######################################################################## 
 17  # imports 
 18  ######################################################################## 
 19   
 20  import base64 
 21  import common 
 22  import cPickle 
 23  import cStringIO 
 24  import MimeWriter 
 25  import os 
 26  import posix 
 27  import qm 
 28  import quopri 
 29  import select 
 30  import signal 
 31  import string 
 32  import sys 
 33  import traceback 
 34   
 35  ######################################################################## 
 36  # constants 
 37  ######################################################################## 
 38   
 39  if sys.platform[:5] == "linux": 
 40      # GNU/Linux systems generally use 'bash' as the default shell. 
 41      # Invoke it with options to inhibit parsing of user startup files. 
 42      default_shell = ["/bin/bash", "-norc", "-noprofile"] 
 43  else: 
 44      # Other UNIX systems use the Bourne shell. 
 45      default_shell = ["/bin/sh"] 
 46   
 47   
 48  ######################################################################## 
 49  # classes 
 50  ######################################################################## 
 51   
52 -class SignalException(common.QMException):
53 """An exception raised in response to a signal.""" 54
55 - def __init__(self, signal_number):
56 """Create a new signal exception. 57 58 'signal_number' -- The signal number.""" 59 60 # Construct a text argument for the exception. 61 message = "Signal %d" % signal_number 62 # Include the signal name, if available. 63 signal_name = get_signal_name(signal_number) 64 if signal_name is not None: 65 message = message + " (%s)" % signal_name 66 # Initialize the base class. 67 common.QMException.__init__(self, message) 68 # Store the signal number. 69 self.__signal_number = signal_number
70 71
72 - def GetSignalNumber(self):
73 """Return the number of the signal that caused this exception.""" 74 75 return self.__signal_number
76 77 78 79 ######################################################################## 80 # functions 81 ######################################################################## 82
83 -def open_in_browser(url):
84 """Open a browser window and point it at 'url'. 85 86 The browser is run in a separate, independent process.""" 87 88 # Escape single quotes in the URL. 89 url = string.replace(url, "'", "%27") 90 # Which browser to use? 91 browser = common.rc.Get("browser", "mozilla", "common") 92 # Invoke the browser. 93 os.system("%s '%s' &" % (browser, url))
94 95
96 -def get_signal_name(signal_number):
97 """Return the name for signal 'signal_number'. 98 99 returns -- The signal's name, or 'None'.""" 100 101 # A hack: look for an attribute in the 'signal' module whose 102 # name starts with "SIG" and whose value is the signal number. 103 for attribute_name in dir(signal): 104 if len(attribute_name) > 3 \ 105 and attribute_name[:3] == "SIG" \ 106 and getattr(signal, attribute_name) == signal_number: 107 return attribute_name 108 # No match. 109 return None
110 111
112 -def install_signal_handler(signal_number):
113 """Install a handler to translate a signal into an exception. 114 115 The signal handler raises a 'SignalException' exception in 116 response to a signal.""" 117 118 signal.signal(signal_number, _signal_handler)
119 120
121 -def _signal_handler(signal_number, execution_frame):
122 """Generic signal handler that raises an exception.""" 123 124 raise SignalException(signal_number)
125 126
127 -def get_host_name():
128 """Return the name of this computer.""" 129 130 return posix.uname()[1]
131 132 ######################################################################## 133 # initialization 134 ######################################################################## 135
136 -def _initialize():
137 """Perform module initialization.""" 138 139 # Install signal handlers for several common signals. 140 for s in (signal.SIGALRM, 141 signal.SIGHUP, 142 signal.SIGTERM, 143 signal.SIGUSR1, 144 signal.SIGUSR2): 145 install_signal_handler(s)
146 147 _initialize() 148 149 ######################################################################## 150 # Local Variables: 151 # mode: python 152 # indent-tabs-mode: nil 153 # fill-column: 72 154 # End: 155