SF.net SVN: fclient: [380] trunk/sandbox/fcp2/iohandler.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-04-09 07:31:43
|
Revision: 380
http://fclient.svn.sourceforge.net/fclient/?rev=380&view=rev
Author: jUrner
Date: 2008-04-09 00:31:51 -0700 (Wed, 09 Apr 2008)
Log Message:
-----------
moved errors and loggers to consts for easier access
Modified Paths:
--------------
trunk/sandbox/fcp2/iohandler.py
Modified: trunk/sandbox/fcp2/iohandler.py
===================================================================
--- trunk/sandbox/fcp2/iohandler.py 2008-04-09 07:31:36 UTC (rev 379)
+++ trunk/sandbox/fcp2/iohandler.py 2008-04-09 07:31:51 UTC (rev 380)
@@ -54,7 +54,7 @@
@return: always None
@raise L{IOConnectFailed}: in case something goes wrong
"""
- raise IOHandler.IOConnectFailed('Failed')
+ raise consts.IOConnectFailedError('Failed', consts.Logger.IO.error)
def read(self, n):
"""Should read n bytes from the device
@@ -63,7 +63,7 @@
@raise L{IOBroken}: in case something goes wrong
@raise L{IOTimeout}: if the read operation times out
"""
- raise OHandler.IOBroken('Broken')
+ raise consts.IOBrokenError('Broken', consts.Logger.IO.error)
def write(self, bytes):
"""Should write bytes to the device
@@ -71,7 +71,7 @@
@return: always None
@raise L{IOBroken}: in case something goes wrong
"""
- raise OHandler.IOBroken('Broken')
+ raise consts.IOBrokenError('Broken', consts.Logger.IO.error)
def close(self):
"""Should close the io device
@@ -79,7 +79,7 @@
@return: always None
@raise L{IOClosed}: if the device is already closed
"""
- raise OHandler.IOClosed('Closed')
+ raise consts.IOClosedError('Closed', consts.Logger.IO.error)
def isOpen(self):
"""Should check if the device is open
@@ -121,7 +121,7 @@
try:
self.socket.connect((host, port))
except socket.error, details:
- raise IOHandler.IOConnectFailed(details)
+ raise consts.IOConnectFailedError(details, consts.Logger.IO.error)
def read(self, n):
try:
@@ -129,10 +129,10 @@
if p == '':
raise socket.error('Socket closed by host')
except socket.timeout, details:
- raise IOHandler.IOTimeout(details)
+ raise consts.IOTimeoutError(details, consts.Logger.IO.error)
except socket.error, details:
self.close()
- raise IOHandler.IOBroken(details)
+ raise consts.IOBrokenError(details, consts.Logger.IO.error)
else:
return p
@@ -144,11 +144,11 @@
totalSend += n
except socket.error, details:
self.close()
- raise IOHandler.IOBroken(details)
+ raise consts.IOBrokenError(details, consts.Logger.IO.error)
def close(self):
if self.socket is None:
- raise IOHandler.IOClosed('Closed')
+ raise consts.IOClosedError('Closed', consts.Logger.IO.error)
self.socket.close()
self.socket = None
@@ -165,23 +165,6 @@
"""Handles message io
"""
- class MessageParseError(Exception):
- """Exception raised when a message could not be parsed succesfuly"""
-
- class IOConnectFailed(Exception):
- """Exception raised if the object can not be connected"""
-
- class IOClosed(Exception):
- """Exception raised if the object is closed"""
-
- class IOBroken(Exception):
- """Exception raised if the IO connection is broken"""
-
- class IOTimeout(Exception):
- """Exception raised when the io connection is closed"""
-
-
-
TerminatorEndMessage = '\nEndMessage\n'
TerminatorData = '\nData\n'
@@ -191,7 +174,6 @@
@paran ioPrototype: (L{IOObjectBase}) derrived class to handle message io
"""
self._ioPrototype = ioPrototype
- self._log = logging.getLogger(consts.LoggerNames.IOHandler)
self._receiveBuffer = ''
self.io = None
@@ -226,21 +208,21 @@
if self.isOpen():
self.close()
- self._log.info(consts.LogMessages.Connecting + ' %r' % kwargs)
+ consts.Logger.IOHandler.info(consts.LogMessages.Connecting + ' %r' % kwargs)
self.io = self._ioPrototype()
yield False # have to yield at least once to make unittests work (io has to be created!!)
try:
self.io.connect(**kwargs)
- except self.IOConnectFailed, details:
- self._log.info(consts.LogMessages.ConnectingFailed + ' %s %s' % (self.IOConnectFailed, details))
+ except consts.IOConnectFailedError, details:
+ consts.Logger.IOHandler.info(consts.LogMessages.ConnectingFailed + ' (%s)' % details)
yield False
else:
- self._log.info(consts.LogMessages.Connected)
+ consts.Logger.IOHandler.info(consts.LogMessages.Connected)
yield True
break
# continue polling
- self._log.info(consts.LogMessages.Retry)
+ consts.Logger.IOHandler.info(consts.LogMessages.Retry)
timeElapsed += timeout
time.sleep(timeout)
@@ -249,7 +231,7 @@
def close(self):
"""Closes the handler"""
- self._log.debug(consts.LogMessages.Closing)
+ consts.Logger.IOHandler.debug(consts.LogMessages.Closing)
self._receiveBuffer = ''
if self.io is not None:
self.io.close()
@@ -273,21 +255,12 @@
try:
#TODO: if \r\n is possible in Fcp, replace it by \n
p = self.io.read(n)
- if not p:
- raise ValueError('No bytes received and IO did not raise as expected?!?')
+ assert p, 'No bytes received and IO did not raise as expected?!?'
self._receiveBuffer += p
- except self.IOBroken, details:
- self._log.critical(consts.LogMessages.SocketDied)
- self.close()
- raise self.IOBroken(details)
- except self.IOTimeout, details: # nothing in the queue
- raise self.IOTimeout(details)
- except Exception, details:
- self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.format_exc())
- self.close()
- raise Exception(details)
+ except consts.IOTimeoutError, details: # nothing in queue
+ raise consts.IOTimeoutError(details)
+
-
def readMessage(self):
"""Reads the next message from io device
@return: (Message) next message from the socket
@@ -324,14 +297,14 @@
msgName = p.pop(0)
msgClass = message.MessagesAll.get(msgName, None)
if msgClass is None:
- self._log.debug(consts.LogMessages.CreatingNewMessageType + ' "%s"' % msgClassname)
+ consts.Logger.IOHandler.debug(consts.LogMessages.CreatingNewMessageType + ' "%s"' % msgClassname)
msgClass = message.newMessageType(msgName)
msg = msgClass()
# process param --> value fields
params = dict([line.split('=', 1) for line in p])
if not msg._restoreParams(params):
- raise self.MessageParseError('Invalid message parameters')
+ raise consts.MessageParseError('Invalid message parameters: ' + msg.pprint(), consts.Logger.IOHandler.error)
# get associated data if necessary
if mayHaveData:
@@ -350,7 +323,7 @@
self._receiveBuffer = chunk + self._receiveBuffer
raise IOTimeout(details)
- self._log.debug(consts.LogMessages.Received + msg.pprint())
+ consts.Logger.IOHandler.debug(consts.LogMessages.Received + msg.pprint())
return msg
@@ -360,25 +333,10 @@
@return: Message
@raise L{IOBroken}: if the connection to the io dies unexpectedly
"""
- self._log.debug(consts.LogMessages.Sending + msg.pprint())
- try:
- self.io.write(msg.toString())
- except self.IOBroken, details:
- self._log.critical(consts.LogMessages.SocketDied)
- self.close()
- raise self.IOBroken(details)
- except Exception, details:
- self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.format_exc())
- self.close()
- raise Exception(details)
-
+ consts.Logger.IOHandler.debug(consts.LogMessages.Sending + msg.pprint())
+ self.io.write(msg.toString())
- def setDebugVerbosity(self, debugVerbosity):
- """Adjusts the debug verbosity. See L{consts.DebugVerbosity}
- """
- self._log.setLevel(debugVerbosity)
-
-
+
def setIOPrototype(self, ioObject):
"""Sets the io prototype to be used by the handler
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|