source: publico/PloneImapClient/trunk/Cache.py @ 2591

Última Alteração nesse arquivo desde 2591 foi 2591, incluída por johnnysouza, 13 anos atrás

nada significativo

File size: 5.6 KB
Linha 
1# File: Cache.py
2#
3# Copyright (c) 2006 by Interlegis
4# Generator: ArchGenXML Version 1.4.0-RC2 svn/development
5#            http://plone.org/products/archgenxml
6#
7# GNU General Public Licence (GPL)
8#
9# This program is free software; you can redistribute it and/or modify it under
10# the terms of the GNU General Public License as published by the Free Software
11# Foundation; either version 2 of the License, or (at your option) any later
12# version.
13# This program is distributed in the hope that it will be useful, but WITHOUT
14# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16# details.
17# You should have received a copy of the GNU General Public License along with
18# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19# Place, Suite 330, Boston, MA  02111-1307  USA
20#
21__author__  = '''STC Interlegis <stc@interlegis.gov.br>'''
22__docformat__ = 'plaintext'
23
24
25##code-section module-header #fill in your manual code here
26import Header
27import email
28##/code-section module-header
29
30
31
32
33class Cache:
34    ''' <p>cache das informacao do email de cada usuario</p>'''
35    __implements__ = ()
36
37    ##code-section class-header_Cache #fill in your manual code here
38    ##/code-section class-header_Cache
39
40
41    def __init__(self,*args,**kwargs):
42        self._init_attributes(*args,**kwargs)
43        self.atualizar()
44
45
46    def _init_attributes(self,*args,**kwargs):
47        #attributes
48        self._folders=None
49        self._headers=None
50        self._folderAtual=None
51        self._tool=None
52        self.user=None
53        self._message=None
54
55        # automatically set attributes where mutators exist
56        for key in kwargs.keys():
57            # camel case: variable -> setVariable
58            mutatorName = 'set'+key[0].upper()+key[1:]
59            mutator = getattr(self, mutatorName)
60            if mutator is not None and callable(mutator):
61                mutator(kwargs[key])
62
63
64    def _atualizar_headers(self,folder):
65        self.set_folderAtual(folder)
66        self._headers = []
67       
68        index = self._tool.getConecaoIndex()
69        self._tool.conecoes[index][1].select(folder)
70       
71        typ, headers  = self._tool.conecoes[index][1].fetch('1:*','(UID FLAGS BODY.PEEK[HEADER.FIELDS (FROM TO CC IN-REPLY-TO SUBJECT DATE)])')
72       
73        if typ == 'No':
74            return False
75
76        for i in xrange(0,len(headers),2):
77            info = headers[i][0] + headers[i+1]
78            header = Header.Header(info, headers[i][1])
79            self._headers.append(header)
80
81        return True
82       
83
84    def atualizar(self,):
85        index = self._tool.getConecaoIndex()
86        typ, lista = self._tool.conecoes[index][1].list()
87        folders = []
88        for i in lista:
89            if i.find(r'\Noselect') < 0:
90                if i[-1] == '"':
91                    name = i.split('"')[-2]
92                else:
93                    name = i.split(' ')[-1]
94                #
95                #
96                # PROBLEMA DE PERFORMANCE NO COMANDO STATUS
97                #
98                #
99                typ, [nmsg] = self._tool.conecoes[index][1].status(name,'(MESSAGES UNSEEN)')
100                # separa tudo
101                nmsg = nmsg.split('(')[-1].split(')')[0].split(' ')
102                nmsg[1] = int(nmsg[1])
103                nmsg[3] = int(nmsg[3])
104                # o numero de nao lidas deve ser menor ou igual ao total
105                if nmsg[1] < nmsg[3]:
106                    nlidas = nmsg[1]
107                    total = nmsg[3]
108                else:
109                    total = nmsg[1]
110                    nlidas = nmsg[3]
111                folders.append([name,nlidas,total])
112        self._folders = folders
113       
114        self._atualizar_headers('INBOX')
115
116
117    def get_headers(self, folder):
118        if folder == self.get_folderAtual():
119            return self._headers
120        else:
121            self._atualizar_headers(folder)
122            return self._headers
123
124
125    def getMail(self,folder,UID):
126        if self._message:
127            if self._message[0] == UID:
128                return self._message[1]
129
130        index = self._tool.getConecaoIndex()
131        typ, data = self._tool.conecoes[index][1].select(folder)
132        typ, data = self._tool.conecoes[index][1].UID('fetch',UID,'(RFC822)')
133        msg = email.message_from_string(data[0][1])
134        self.markAsRead(folder,UID)
135        self._message = ( UID, msg )
136        return self._message[1]
137       
138
139    def set_message(self,value):
140        self._message=value
141
142    def get_message(self):
143        return self._message
144
145
146    def set_folderAtual(self,value):
147        self._folderAtual=value
148
149
150    def get_tool(self):
151        return self._tool
152
153
154    def markAsRead(self,folder,UID):
155        """
156        Marca um e-mail como lido
157        TODO implementar marcar como lida no servidor mas sem ler
158        """
159        for i in xrange(len(self._headers)):
160            if self._headers[i].get_UID() == UID:
161                flags = list(self._headers[i].get_flags())
162                if not r'\Seen' in flags:
163                    flags.append(r'\Seen')
164                    self._headers[i].set_flags(tuple(flags))
165                    return
166        return
167                   
168
169    def get_folders(self):
170        return self._folders
171
172
173    def getUser(self):
174        return self.user
175
176
177    def get_folderAtual(self):
178        return self._folderAtual
179
180
181    def setUser(self,value):
182        self.user=value
183
184
185    def set_folders(self,value):
186        self._folders=value
187
188
189    def set_tool(self,value):
190        self._tool=value
191
192
193##code-section module-footer #fill in your manual code here
194##/code-section module-footer
195
196
197
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.