home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / nntplib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  12.1 KB  |  459 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import re
  5. import socket
  6. __all__ = [
  7.     'NNTP',
  8.     'NNTPReplyError',
  9.     'NNTPTemporaryError',
  10.     'NNTPPermanentError',
  11.     'NNTPProtocolError',
  12.     'NNTPDataError',
  13.     'error_reply',
  14.     'error_temp',
  15.     'error_perm',
  16.     'error_proto',
  17.     'error_data']
  18.  
  19. class NNTPError(Exception):
  20.     
  21.     def __init__(self, *args):
  22.         Exception.__init__(self, *args)
  23.         
  24.         try:
  25.             self.response = args[0]
  26.         except IndexError:
  27.             self.response = 'No response given'
  28.  
  29.  
  30.  
  31.  
  32. class NNTPReplyError(NNTPError):
  33.     pass
  34.  
  35.  
  36. class NNTPTemporaryError(NNTPError):
  37.     pass
  38.  
  39.  
  40. class NNTPPermanentError(NNTPError):
  41.     pass
  42.  
  43.  
  44. class NNTPProtocolError(NNTPError):
  45.     pass
  46.  
  47.  
  48. class NNTPDataError(NNTPError):
  49.     pass
  50.  
  51. error_reply = NNTPReplyError
  52. error_temp = NNTPTemporaryError
  53. error_perm = NNTPPermanentError
  54. error_proto = NNTPProtocolError
  55. error_data = NNTPDataError
  56. NNTP_PORT = 119
  57. LONGRESP = [
  58.     '100',
  59.     '215',
  60.     '220',
  61.     '221',
  62.     '222',
  63.     '224',
  64.     '230',
  65.     '231',
  66.     '282']
  67. CRLF = '\r\n'
  68.  
  69. class NNTP:
  70.     
  71.     def __init__(self, host, port = NNTP_PORT, user = None, password = None, readermode = None, usenetrc = True):
  72.         self.host = host
  73.         self.port = port
  74.         self.sock = socket.create_connection((host, port))
  75.         self.file = self.sock.makefile('rb')
  76.         self.debugging = 0
  77.         self.welcome = self.getresp()
  78.         readermode_afterauth = 0
  79.         if readermode:
  80.             
  81.             try:
  82.                 self.welcome = self.shortcmd('mode reader')
  83.             except NNTPPermanentError:
  84.                 pass
  85.             except NNTPTemporaryError:
  86.                 e = None
  87.                 if user and e.response[:3] == '480':
  88.                     readermode_afterauth = 1
  89.                 else:
  90.                     raise 
  91.             
  92.  
  93.         
  94.         try:
  95.             if usenetrc and not user:
  96.                 import netrc
  97.                 credentials = netrc.netrc()
  98.                 auth = credentials.authenticators(host)
  99.                 if auth:
  100.                     user = auth[0]
  101.                     password = auth[2]
  102.                 
  103.         except IOError:
  104.             pass
  105.  
  106.         if user:
  107.             resp = self.shortcmd('authinfo user ' + user)
  108.             if resp[:3] == '381':
  109.                 if not password:
  110.                     raise NNTPReplyError(resp)
  111.                 resp = self.shortcmd('authinfo pass ' + password)
  112.                 if resp[:3] != '281':
  113.                     raise NNTPPermanentError(resp)
  114.             if readermode_afterauth:
  115.                 
  116.                 try:
  117.                     self.welcome = self.shortcmd('mode reader')
  118.                 except NNTPPermanentError:
  119.                     pass
  120.                 
  121.  
  122.  
  123.     
  124.     def getwelcome(self):
  125.         if self.debugging:
  126.             print '*welcome*', repr(self.welcome)
  127.         return self.welcome
  128.  
  129.     
  130.     def set_debuglevel(self, level):
  131.         self.debugging = level
  132.  
  133.     debug = set_debuglevel
  134.     
  135.     def putline(self, line):
  136.         line = line + CRLF
  137.         if self.debugging > 1:
  138.             print '*put*', repr(line)
  139.         self.sock.sendall(line)
  140.  
  141.     
  142.     def putcmd(self, line):
  143.         if self.debugging:
  144.             print '*cmd*', repr(line)
  145.         self.putline(line)
  146.  
  147.     
  148.     def getline(self):
  149.         line = self.file.readline()
  150.         if self.debugging > 1:
  151.             print '*get*', repr(line)
  152.         if not line:
  153.             raise EOFError
  154.         if line[-2:] == CRLF:
  155.             line = line[:-2]
  156.         elif line[-1:] in CRLF:
  157.             line = line[:-1]
  158.         return line
  159.  
  160.     
  161.     def getresp(self):
  162.         resp = self.getline()
  163.         if self.debugging:
  164.             print '*resp*', repr(resp)
  165.         c = resp[:1]
  166.         if c == '4':
  167.             raise NNTPTemporaryError(resp)
  168.         if c == '5':
  169.             raise NNTPPermanentError(resp)
  170.         if c not in '123':
  171.             raise NNTPProtocolError(resp)
  172.         return resp
  173.  
  174.     
  175.     def getlongresp(self, file = None):
  176.         openedFile = None
  177.         
  178.         try:
  179.             if isinstance(file, str):
  180.                 openedFile = file = open(file, 'w')
  181.             resp = self.getresp()
  182.             if resp[:3] not in LONGRESP:
  183.                 raise NNTPReplyError(resp)
  184.             list = []
  185.             while None:
  186.                 line = self.getline()
  187.                 if line == '.':
  188.                     break
  189.                 if line[:2] == '..':
  190.                     line = line[1:]
  191.                 if file:
  192.                     file.write(line + '\n')
  193.                     continue
  194.             if openedFile:
  195.                 openedFile.close()
  196.             return (resp, list)
  197.  
  198.  
  199.     
  200.     def shortcmd(self, line):
  201.         self.putcmd(line)
  202.         return self.getresp()
  203.  
  204.     
  205.     def longcmd(self, line, file = None):
  206.         self.putcmd(line)
  207.         return self.getlongresp(file)
  208.  
  209.     
  210.     def newgroups(self, date, time, file = None):
  211.         return self.longcmd('NEWGROUPS ' + date + ' ' + time, file)
  212.  
  213.     
  214.     def newnews(self, group, date, time, file = None):
  215.         cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time
  216.         return self.longcmd(cmd, file)
  217.  
  218.     
  219.     def list(self, file = None):
  220.         (resp, list) = self.longcmd('LIST', file)
  221.         for i in range(len(list)):
  222.             list[i] = tuple(list[i].split())
  223.         
  224.         return (resp, list)
  225.  
  226.     
  227.     def description(self, group):
  228.         (resp, lines) = self.descriptions(group)
  229.         if len(lines) == 0:
  230.             return ''
  231.         return None[0][1]
  232.  
  233.     
  234.     def descriptions(self, group_pattern):
  235.         line_pat = re.compile('^(?P<group>[^ \t]+)[ \t]+(.*)$')
  236.         (resp, raw_lines) = self.longcmd('LIST NEWSGROUPS ' + group_pattern)
  237.         if resp[:3] != '215':
  238.             (resp, raw_lines) = self.longcmd('XGTITLE ' + group_pattern)
  239.         lines = []
  240.         for raw_line in raw_lines:
  241.             match = line_pat.search(raw_line.strip())
  242.             if match:
  243.                 lines.append(match.group(1, 2))
  244.                 continue
  245.         return (resp, lines)
  246.  
  247.     
  248.     def group(self, name):
  249.         resp = self.shortcmd('GROUP ' + name)
  250.         if resp[:3] != '211':
  251.             raise NNTPReplyError(resp)
  252.         words = resp.split()
  253.         count = first = last = 0
  254.         n = len(words)
  255.         if n > 1:
  256.             count = words[1]
  257.             if n > 2:
  258.                 first = words[2]
  259.                 if n > 3:
  260.                     last = words[3]
  261.                     if n > 4:
  262.                         name = words[4].lower()
  263.                     
  264.                 
  265.             
  266.         return (resp, count, first, last, name)
  267.  
  268.     
  269.     def help(self, file = None):
  270.         return self.longcmd('HELP', file)
  271.  
  272.     
  273.     def statparse(self, resp):
  274.         if resp[:2] != '22':
  275.             raise NNTPReplyError(resp)
  276.         words = resp.split()
  277.         nr = 0
  278.         id = ''
  279.         n = len(words)
  280.         if n > 1:
  281.             nr = words[1]
  282.             if n > 2:
  283.                 id = words[2]
  284.             
  285.         return (resp, nr, id)
  286.  
  287.     
  288.     def statcmd(self, line):
  289.         resp = self.shortcmd(line)
  290.         return self.statparse(resp)
  291.  
  292.     
  293.     def stat(self, id):
  294.         return self.statcmd('STAT ' + id)
  295.  
  296.     
  297.     def next(self):
  298.         return self.statcmd('NEXT')
  299.  
  300.     
  301.     def last(self):
  302.         return self.statcmd('LAST')
  303.  
  304.     
  305.     def artcmd(self, line, file = None):
  306.         (resp, list) = self.longcmd(line, file)
  307.         (resp, nr, id) = self.statparse(resp)
  308.         return (resp, nr, id, list)
  309.  
  310.     
  311.     def head(self, id):
  312.         return self.artcmd('HEAD ' + id)
  313.  
  314.     
  315.     def body(self, id, file = None):
  316.         return self.artcmd('BODY ' + id, file)
  317.  
  318.     
  319.     def article(self, id):
  320.         return self.artcmd('ARTICLE ' + id)
  321.  
  322.     
  323.     def slave(self):
  324.         return self.shortcmd('SLAVE')
  325.  
  326.     
  327.     def xhdr(self, hdr, str, file = None):
  328.         pat = re.compile('^([0-9]+) ?(.*)\n?')
  329.         (resp, lines) = self.longcmd('XHDR ' + hdr + ' ' + str, file)
  330.         for i in range(len(lines)):
  331.             line = lines[i]
  332.             m = pat.match(line)
  333.             if m:
  334.                 lines[i] = m.group(1, 2)
  335.                 continue
  336.         return (resp, lines)
  337.  
  338.     
  339.     def xover(self, start, end, file = None):
  340.         (resp, lines) = self.longcmd('XOVER ' + start + '-' + end, file)
  341.         xover_lines = []
  342.         for line in lines:
  343.             elem = line.split('\t')
  344.             
  345.             try:
  346.                 xover_lines.append((elem[0], elem[1], elem[2], elem[3], elem[4], elem[5].split(), elem[6], elem[7]))
  347.             continue
  348.             except IndexError:
  349.                 raise NNTPDataError(line)
  350.                 continue
  351.             
  352.  
  353.         
  354.         return (resp, xover_lines)
  355.  
  356.     
  357.     def xgtitle(self, group, file = None):
  358.         line_pat = re.compile('^([^ \t]+)[ \t]+(.*)$')
  359.         (resp, raw_lines) = self.longcmd('XGTITLE ' + group, file)
  360.         lines = []
  361.         for raw_line in raw_lines:
  362.             match = line_pat.search(raw_line.strip())
  363.             if match:
  364.                 lines.append(match.group(1, 2))
  365.                 continue
  366.         return (resp, lines)
  367.  
  368.     
  369.     def xpath(self, id):
  370.         resp = self.shortcmd('XPATH ' + id)
  371.         if resp[:3] != '223':
  372.             raise NNTPReplyError(resp)
  373.         
  374.         try:
  375.             (resp_num, path) = resp.split()
  376.         except ValueError:
  377.             raise NNTPReplyError(resp)
  378.  
  379.         return (resp, path)
  380.  
  381.     
  382.     def date(self):
  383.         resp = self.shortcmd('DATE')
  384.         if resp[:3] != '111':
  385.             raise NNTPReplyError(resp)
  386.         elem = resp.split()
  387.         if len(elem) != 2:
  388.             raise NNTPDataError(resp)
  389.         date = elem[1][2:8]
  390.         time = elem[1][-6:]
  391.         if len(date) != 6 or len(time) != 6:
  392.             raise NNTPDataError(resp)
  393.         return (resp, date, time)
  394.  
  395.     
  396.     def post(self, f):
  397.         resp = self.shortcmd('POST')
  398.         if resp[0] != '3':
  399.             raise NNTPReplyError(resp)
  400.         while None:
  401.             line = f.readline()
  402.             if not line:
  403.                 break
  404.             if line[-1] == '\n':
  405.                 line = line[:-1]
  406.             if line[:1] == '.':
  407.                 line = '.' + line
  408.             continue
  409.             self.putline('.')
  410.             return self.getresp()
  411.  
  412.     
  413.     def ihave(self, id, f):
  414.         resp = self.shortcmd('IHAVE ' + id)
  415.         if resp[0] != '3':
  416.             raise NNTPReplyError(resp)
  417.         while None:
  418.             line = f.readline()
  419.             if not line:
  420.                 break
  421.             if line[-1] == '\n':
  422.                 line = line[:-1]
  423.             if line[:1] == '.':
  424.                 line = '.' + line
  425.             continue
  426.             self.putline('.')
  427.             return self.getresp()
  428.  
  429.     
  430.     def quit(self):
  431.         resp = self.shortcmd('QUIT')
  432.         self.file.close()
  433.         self.sock.close()
  434.         del self.file
  435.         del self.sock
  436.         return resp
  437.  
  438.  
  439. if __name__ == '__main__':
  440.     import os
  441.     if 'news':
  442.         pass
  443.     newshost = os.environ['NNTPSERVER']
  444.     if newshost.find('.') == -1:
  445.         mode = 'readermode'
  446.     else:
  447.         mode = None
  448.     s = NNTP(newshost, readermode = mode)
  449.     (resp, count, first, last, name) = s.group('comp.lang.python')
  450.     print resp
  451.     print 'Group', name, 'has', count, 'articles, range', first, 'to', last
  452.     (resp, subs) = s.xhdr('subject', first + '-' + last)
  453.     print resp
  454.     for item in subs:
  455.         print '%7s %s' % item
  456.     
  457.     resp = s.quit()
  458.     print resp
  459.