Пример #1
0
    def load_msgs(self, loadfrom=1):
        """Connect, load session_email_parser's into memory, and disconnect.

        A connection is attempted and all messages starting
        from 'loadfrom' (default=1) are put into a list of         
        gantlet.session_email_parser and returned.  The messages are NOT
        removed from the POP server in the process.

        """
        n_failed = 0
        server = self.connect()
        try:
            if self.debug > 1: print(server.list())
            msgs = []
            (n_msgs, bytes) = server.stat()
            if self.debug > 1:
                print("Retrieving %d messages (%d bytes)" % (n_msgs, bytes))
            for i in range(loadfrom, n_msgs + 1):
                (hdr, content, bytes) = server.retr(i)
                content = '\n'.join(content)
                strfile = StringIO.StringIO(
                    content)  # make string look like file
                try:
                    msg = session_email_parser(strfile, i, content)
                    msgs.append(msg)
                except xml.sax._exceptions.SAXParseException:
                    print("Message %d had parse exception" % (i, ))
                    print("Exception message: " + sys.exc_value.getMessage())
                    print(content[0:4096] + "\n")
                    # print(content)
                    n_failed = n_failed + 1
        finally:
            server.quit()
        assert len(msgs) == (n_msgs - loadfrom - n_failed + 1)
        return msgs
Пример #2
0
    def load_msgs( self, loadfrom=1 ):
        """Connect, load session_email_parser's into memory, and disconnect.

        A connection is attempted and all messages starting
        from 'loadfrom' (default=1) are put into a list of         
        gantlet.session_email_parser and returned.  The messages are NOT
        removed from the POP server in the process.

        """
        n_failed=0
        server = self.connect( )
        try:
            if self.debug > 1 : print server.list()
            msgs = []
            (n_msgs, bytes) = server.stat()
            if self.debug > 1 :
                print "Retrieving %d messages (%d bytes)" % ( n_msgs, bytes )
            for i in range( loadfrom, n_msgs+1 ):
                (hdr, content, bytes) = server.retr(i)
                content = string.join( content, '\n')            
                strfile = StringIO.StringIO(content)  # make string look like file
                try:
                    msg =  session_email_parser( strfile, i, content )
                    msgs.append(msg)
                except xml.sax._exceptions.SAXParseException:
                    print "Message %d had parse exception" %(i,)
                    n_failed = n_failed + 1
        finally:
            server.quit()
        assert len(msgs) == (n_msgs - loadfrom - n_failed + 1)
        return msgs 
Пример #3
0
 def _visit( self, foo, dirname, names ):
     """ Used in by self.rescan() to walk the repository
     """
     print self
     print foo
     for name in names:
         if name[-3:]=='.gz':
             print name
             f = gzip.open( os.path.join( dirname, name ), 'r' )
             s = session_email_parser( f, id=len(self.index) )
             p = self.createpath(s)
             entry = s.attr.copy()
             entry['path']=p
             self.index.append( entry )
             f.close()
     return
Пример #4
0
 def _visit(self, foo, dirname, names):
     """ Used in by self.rescan() to walk the repository
     """
     print(self)
     print(foo)
     for name in names:
         if name[-7:] == '.xml.gz':
             print(name)
             f = gzip.open(os.path.join(dirname, name), 'r')
             s = session_email_parser(f, id=len(self.index))
             p = self.createpath(s)
             entry = s.attr.copy()
             entry['path'] = p
             self.index.append(entry)
             f.close()
     return