[Python-checkins] CVS: python/dist/src/Lib nntplib.py,1.17,1.18

Barry A. Warsaw bwarsaw@cnri.reston.va.us
Thu, 10 Feb 2000 15:25:56 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Lib
In directory anthem:/home/bwarsaw/projects/python/Lib

Modified Files:
	nntplib.py 
Log Message:
Added new exception classes:

    NNTPError - derived from Exception, it's the base class for all
    other exceptions in this module

    NNTPReplyError - what used to be error_reply

    NNTPTemporaryError - what used to be error_temp

    NNTPPermanentError - what used to be error_perm

    NNTPProtocolError - what used to be error_proto

    NNTPDataError - what used to be error_data

All the old names are retained for backwards compatibility; they point
to the class that replaces them.  Also, any code in this module that
raises an exception, now does so with the exception class.

NNTP.__init__(): Added a new optional argument `readermode', which is
a flag that defaults to false.  When set to true, the "mode reader"
command is sent to the NNTP server before user authentication.  Reader
mode is sometimes necessary if you are connecting to an NNTP server on
the local machine and intend to call reader-specific comamnds, such as
`group'.  If you get unexpected NNTPPermanentErrors, you might need to
set readermode.  Patch provided by Thomas Wouters (who include the
standard disclaimer on is patches@python.org submission), and inspired
by Jim Tittsler.


Index: nntplib.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/nntplib.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -r1.17 -r1.18
*** nntplib.py	2000/02/04 15:10:33	1.17
--- nntplib.py	2000/02/10 20:25:53	1.18
***************
*** 35,46 ****
  
  
! # Exception raised when an error or invalid response is received
! error_reply = 'nntplib.error_reply'	# unexpected [123]xx reply
! error_temp = 'nntplib.error_temp'	# 4xx errors
! error_perm = 'nntplib.error_perm'	# 5xx errors
! error_proto = 'nntplib.error_proto'	# response does not begin with [1-5]
! error_data = 'nntplib.error_data'	# error in response data
  
  
  # Standard port used by NNTP servers
  NNTP_PORT = 119
--- 35,78 ----
  
  
! 
! # Exceptions raised when an error or invalid response is received
! class NNTPError(Exception):
! 	"""Base class for all nntplib exceptions"""
! 	def __init__(self, *args):
! 		apply(Exception.__init__, (self,)+args)
! 		try:
! 			self.response = args[0]
! 		except IndexError:
! 			self.response = 'No response given'
! 
! class NNTPReplyError(NNTPError):
! 	"""Unexpected [123]xx reply"""
! 	pass
! 
! class NNTPTemporaryError(NNTPError):
! 	"""4xx errors"""
! 	pass
! 
! class NNTPPermanentError(NNTPError):
! 	"""5xx errors"""
! 	pass
! 
! class NNTPProtocolError(NNTPError):
! 	"""Response does not begin with [1-5]"""
! 	pass
! 
! class NNTPDataError(NNTPError):
! 	"""Error in response data"""
! 	pass
! 
! # for backwards compatibility
! error_reply = NNTPReplyError
! error_temp = NNTPTemporaryError
! error_perm = NNTPPermanentError
! error_proto = NNTPProtocolError
! error_data = NNTPDataError
  
  
+ 
  # Standard port used by NNTP servers
  NNTP_PORT = 119
***************
*** 55,67 ****
  
  
  # The class itself
- 
  class NNTP:
! 
! 	def __init__(self, host, port = NNTP_PORT, user=None, password=None):
  		"""Initialize an instance.  Arguments:
  		- host: hostname to connect to
! 		- port: port to connect to (default the standard NNTP port)"""
! 
  		self.host = host
  		self.port = port
--- 87,109 ----
  
  
+ 
  # The class itself
  class NNTP:
! 	def __init__(self, host, port=NNTP_PORT, user=None, password=None,
! 		     readermode=None):
  		"""Initialize an instance.  Arguments:
  		- host: hostname to connect to
! 		- port: port to connect to (default the standard NNTP port)
! 		- user: username to authenticate with
! 		- password: password to use with username
! 		- readermode: if true, send 'mode reader' command after
! 		              connecting.
! 
! 	        readermode is sometimes necessary if you are connecting to an
! 	        NNTP server on the local machine and intend to call
! 	        reader-specific comamnds, such as `group'.  If you get
! 	        unexpected NNTPPermanentErrors, you might need to set
! 	        readermode.
! 		"""
  		self.host = host
  		self.port = port
***************
*** 71,84 ****
  		self.debugging = 0
  		self.welcome = self.getresp()
  		if user:
  			resp = self.shortcmd('authinfo user '+user)
  			if resp[:3] == '381':
  				if not password:
! 					raise error_reply, resp
  				else:
  					resp = self.shortcmd(
  						'authinfo pass '+password)
  					if resp[:3] != '281':
! 						raise error_perm, resp
  
  	def getwelcome(self):
--- 113,137 ----
  		self.debugging = 0
  		self.welcome = self.getresp()
+ 		if readermode:
+ 			try:
+ 				self.welcome = self.shortcmd('mode reader')
+ 			except NNTPPermanentError:
+ 				# error 500, probably 'not implemented'
+ 				pass
  		if user:
  			resp = self.shortcmd('authinfo user '+user)
  			if resp[:3] == '381':
  				if not password:
! 					raise NNTPReplyError(resp)
  				else:
  					resp = self.shortcmd(
  						'authinfo pass '+password)
  					if resp[:3] != '281':
! 						raise NNTPPermanentError(resp)
! 
! 	# Get the welcome message from the server
! 	# (this is read and squirreled away by __init__()).
! 	# If the response code is 200, posting is allowed;
! 	# if it 201, posting is not allowed
  
  	def getwelcome(self):
***************
*** 129,137 ****
  		c = resp[:1]
  		if c == '4':
! 			raise error_temp, resp
  		if c == '5':
! 			raise error_perm, resp
  		if c not in '123':
! 			raise error_proto, resp
  		return resp
  
--- 182,190 ----
  		c = resp[:1]
  		if c == '4':
! 			raise NNTPTemporaryError(resp)
  		if c == '5':
! 			raise NNTPPermanentError(resp)
  		if c not in '123':
! 			raise NNTPProtocolError(resp)
  		return resp
  
***************
*** 141,145 ****
  		resp = self.getresp()
  		if resp[:3] not in LONGRESP:
! 			raise error_reply, resp
  		list = []
  		while 1:
--- 194,198 ----
  		resp = self.getresp()
  		if resp[:3] not in LONGRESP:
! 			raise NNTPReplyError(resp)
  		list = []
  		while 1:
***************
*** 207,211 ****
  		resp = self.shortcmd('GROUP ' + name)
  		if resp[:3] <> '211':
! 			raise error_reply, resp
  		words = string.split(resp)
  		count = first = last = 0
--- 260,264 ----
  		resp = self.shortcmd('GROUP ' + name)
  		if resp[:3] <> '211':
! 			raise NNTPReplyError(resp)
  		words = string.split(resp)
  		count = first = last = 0
***************
*** 231,235 ****
  		"""Internal: parse the response of a STAT, NEXT or LAST command."""
  		if resp[:2] <> '22':
! 			raise error_reply, resp
  		words = string.split(resp)
  		nr = 0
--- 284,288 ----
  		"""Internal: parse the response of a STAT, NEXT or LAST command."""
  		if resp[:2] <> '22':
! 			raise NNTPReplyError(resp)
  		words = string.split(resp)
  		nr = 0
***************
*** 350,354 ****
  						    elem[7]))
  			except IndexError:
! 				raise error_data,line
  		return resp,xover_lines
  
--- 403,407 ----
  						    elem[7]))
  			except IndexError:
! 				raise NNTPDataError(line)
  		return resp,xover_lines
  
***************
*** 378,386 ****
  		resp = self.shortcmd("XPATH " + id)
  		if resp[:3] <> '223':
! 			raise error_reply, resp
  		try:
  			[resp_num, path] = string.split(resp)
  		except ValueError:
! 			raise error_reply, resp
  		else:
  			return resp, path
--- 431,439 ----
  		resp = self.shortcmd("XPATH " + id)
  		if resp[:3] <> '223':
! 			raise NNTPReplyError(resp)
  		try:
  			[resp_num, path] = string.split(resp)
  		except ValueError:
! 			raise NNTPReplyError(resp)
  		else:
  			return resp, path
***************
*** 396,407 ****
  		resp = self.shortcmd("DATE")
  		if resp[:3] <> '111':
! 			raise error_reply, resp
  		elem = string.split(resp)
  		if len(elem) != 2:
! 			raise error_data, resp
  		date = elem[1][2:8]
  		time = elem[1][-6:]
  		if len(date) != 6 or len(time) != 6:
! 			raise error_data, resp
  		return resp, date, time
  
--- 449,460 ----
  		resp = self.shortcmd("DATE")
  		if resp[:3] <> '111':
! 			raise NNTPReplyError(resp)
  		elem = string.split(resp)
  		if len(elem) != 2:
! 			raise NNTPDataError(resp)
  		date = elem[1][2:8]
  		time = elem[1][-6:]
  		if len(date) != 6 or len(time) != 6:
! 			raise NNTPDataError(resp)
  		return resp, date, time
  
***************
*** 416,420 ****
  		# Raises error_??? if posting is not allowed
  		if resp[0] <> '3':
! 			raise error_reply, resp
  		while 1:
  			line = f.readline()
--- 469,473 ----
  		# Raises error_??? if posting is not allowed
  		if resp[0] <> '3':
! 			raise NNTPReplyError(resp)
  		while 1:
  			line = f.readline()
***************
*** 440,444 ****
  		# Raises error_??? if the server already has it
  		if resp[0] <> '3':
! 			raise error_reply, resp
  		while 1:
  			line = f.readline()
--- 493,497 ----
  		# Raises error_??? if the server already has it
  		if resp[0] <> '3':
! 			raise NNTPReplyError(resp)
  		while 1:
  			line = f.readline()
***************
*** 466,470 ****
  def _test():
  	"""Minimal test function."""
! 	s = NNTP('news')
  	resp, count, first, last, name = s.group('comp.lang.python')
  	print resp
--- 519,523 ----
  def _test():
  	"""Minimal test function."""
! 	s = NNTP('news', readermode='reader')
  	resp, count, first, last, name = s.group('comp.lang.python')
  	print resp