package refactoring

- reorganize classes
- make it run even when pysqlite2 is not installed
- some tests to complete mailconnection_factory coverage

darcs-hash:20060724214007-86b55-9b38308b8f645c2067c7b200f17532da62ec825c.gz
This commit is contained in:
David Rousselie
2006-07-24 23:40:07 +02:00
parent 3a5c32041a
commit dfb597aca3
21 changed files with 269 additions and 185 deletions

View File

@@ -1,6 +1,6 @@
## ##
## run_test.py ## run_test.py
## Login : <david.rousselie@happycoders.org> ## Login : David Rousselie <dax@happycoders.org>
## Started on Wed May 18 13:33:03 2005 David Rousselie ## Started on Wed May 18 13:33:03 2005 David Rousselie
## $Id: run_test.py,v 1.2 2005/09/18 20:24:07 David Rousselie Exp $ ## $Id: run_test.py,v 1.2 2005/09/18 20:24:07 David Rousselie Exp $
## ##
@@ -24,18 +24,21 @@ import coverage
coverage.erase() coverage.erase()
coverage.start() coverage.start()
import unittest import unittest
import sys import sys
sys.path.append("src")
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
del sys.setdefaultencoding del sys.setdefaultencoding
import tests import tests
from tests.test_mailconnection import * from tests.test_mailconnection import *
from tests.test_mailconnection_factory import * from tests.test_mailconnection_factory import *
from tests.test_component import * from tests.test_component import *
from tests.test_storage import * from tests.test_storage import *
from test import test_support from test import test_support
import jabber
import logging import logging
import jmc
if __name__ == '__main__': if __name__ == '__main__':
@@ -81,10 +84,10 @@ if __name__ == '__main__':
#test_support.run_suite(dbmstorage_suite) #test_support.run_suite(dbmstorage_suite)
test_support.run_suite(jmc_suite) test_support.run_suite(jmc_suite)
# coverage.stop() coverage.stop()
# coverage.analysis(jabber.mailconnection_factory) coverage.analysis(jmc.email.mailconnection_factory)
# coverage.analysis(jabber.mailconnection) coverage.analysis(jmc.email.mailconnection)
# coverage.analysis(jabber.component) coverage.analysis(jmc.jabber.component)
# coverage.analysis(jabber.x) coverage.analysis(jmc.jabber.x)
# coverage.report([jabber.mailconnection_factory, jabber.mailconnection, \ coverage.report([jmc.email.mailconnection_factory, jmc.email.mailconnection, \
# jabber.component, jabber.x]) jmc.jabber.component, jmc.jabber.x])

View File

View File

@@ -31,7 +31,7 @@ import poplib
import imaplib import imaplib
import socket import socket
from jabber.lang import Lang from jmc.utils.lang import Lang
IMAP4_TIMEOUT = 10 IMAP4_TIMEOUT = 10
POP3_TIMEOUT = 10 POP3_TIMEOUT = 10
@@ -131,7 +131,7 @@ class MailConnection(object):
""" Wrapper to mail connection and action. """ Wrapper to mail connection and action.
Abstract class, do not represent real mail connection type""" Abstract class, do not represent real mail connection type"""
_logger = logging.getLogger("jabber.MailConnection") _logger = logging.getLogger("jmc.MailConnection")
def __init__(self, login = "", password = "", host = "", \ def __init__(self, login = "", password = "", host = "", \
port = 110, ssl = False): port = 110, ssl = False):
@@ -327,7 +327,7 @@ class MailConnection(object):
action = property(get_action) action = property(get_action)
class IMAPConnection(MailConnection): class IMAPConnection(MailConnection):
_logger = logging.getLogger("jabber.IMAPConnection") _logger = logging.getLogger("jmc.IMAPConnection")
def __init__(self, login = "", password = "", host = "", \ def __init__(self, login = "", password = "", host = "", \
port = None, ssl = False, mailbox = "INBOX"): port = None, ssl = False, mailbox = "INBOX"):
@@ -405,7 +405,7 @@ class IMAPConnection(MailConnection):
type = property(get_type) type = property(get_type)
class POP3Connection(MailConnection): class POP3Connection(MailConnection):
_logger = logging.getLogger("jabber.POP3Connection") _logger = logging.getLogger("jmc.POP3Connection")
def __init__(self, login = "", password = "", host = "", \ def __init__(self, login = "", password = "", host = "", \
port = None, ssl = False): port = None, ssl = False):

View File

@@ -20,17 +20,18 @@
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ##
import mailconnection import jmc.email.mailconnection as mailconnection
from mailconnection import IMAPConnection, POP3Connection from jmc.email.mailconnection import IMAPConnection, POP3Connection
def get_new_mail_connection(type):
""" Static method to return an empty MailConnection object of given type """ Static method to return an empty MailConnection object of given type
:Parameters: :Parameters:
- 'type': type of connection to return : 'imap', 'imaps', 'pop3', 'pop3s' - 'type': type of connection to return : 'imap', 'imaps', 'pop3', 'pop3s'
:return: MailConnection of given type in parameter, None if unknown type :return: MailConnection of given type in parameter, None if unknown type
:returntype: 'MailConnection'""" :returntype: 'MailConnection'
def get_new_mail_connection(type): """
if type == "imap": if type == "imap":
return IMAPConnection() return IMAPConnection()
elif type == "imaps": elif type == "imaps":
@@ -41,19 +42,20 @@ def get_new_mail_connection(type):
return POP3Connection(ssl = True) return POP3Connection(ssl = True)
raise Exception, "Connection type \"" + type + "\" unknown" raise Exception, "Connection type \"" + type + "\" unknown"
def str_to_mail_connection(connection_string):
""" Static methode to create a MailConnection object filled from string """ Static methode to create a MailConnection object filled from string
:Parameters: :Parameters:
- 'connection_string': string containing MailConnection parameters separated - 'connection_string': string containing MailConnection parameters separated
by '#'. ex: 'pop3#login#password#host#110#True' by '#'. ex: 'pop3#login#password#host#110#chat_action#online_action#away_action#xa_action#dnd_action#offline_action#check_interval#liv_email_only(#Mailbox)'
:Types: :Types:
- 'connection_string': string - 'connection_string': string
:return: MailConnection of given type found in string parameter :return: MailConnection of given type found in string parameter
:returntype: 'MailConnection'""" :returntype: 'MailConnection'
def str_to_mail_connection(connection_string): """
arg_list = connection_string.split("#") arg_list = connection_string.split("#")
# optionals values must be the at the beginning of the list to pop them # optionals values must be the at the beginning of the list to pop them
# last # last
@@ -76,6 +78,7 @@ def str_to_mail_connection(connection_string):
offline_action = None offline_action = None
interval = None interval = None
live_email_only = False live_email_only = False
result = None
if type[0:4] == "imap": if type[0:4] == "imap":
if len(arg_list) == 9: if len(arg_list) == 9:
chat_action = int(arg_list.pop()) chat_action = int(arg_list.pop())
@@ -100,7 +103,7 @@ def str_to_mail_connection(connection_string):
ssl = (len(type) == 5), \ ssl = (len(type) == 5), \
port = port, \ port = port, \
mailbox = mailbox) mailbox = mailbox)
else: elif type[0:4] == "pop3":
if len(arg_list) == 8: if len(arg_list) == 8:
chat_action = int(arg_list.pop()) chat_action = int(arg_list.pop())
online_action = int(arg_list.pop()) online_action = int(arg_list.pop())

View File

View File

@@ -32,11 +32,12 @@ import os
import time import time
import traceback import traceback
import mailconnection from jmc.email.mailconnection import *
from mailconnection import * from jmc.jabber.x import *
from x import * from jmc.utils.storage import *
from storage import * from jmc.utils.lang import Lang
import mailconnection_factory import jmc.email.mailconnection_factory
import pyxmpp.jabberd import pyxmpp.jabberd
from pyxmpp.presence import Presence from pyxmpp.presence import Presence
from pyxmpp.message import Message from pyxmpp.message import Message
@@ -45,8 +46,6 @@ from pyxmpp.jid import JID
from pyxmpp.jabber.disco import DiscoItems, DiscoItem, DiscoInfo, DiscoIdentity from pyxmpp.jabber.disco import DiscoItems, DiscoItem, DiscoInfo, DiscoIdentity
from pyxmpp.jabberd.component import Component from pyxmpp.jabberd.component import Component
from jabber.lang import Lang
class ComponentFatalError(RuntimeError): class ComponentFatalError(RuntimeError):
pass pass
@@ -68,7 +67,7 @@ class MailComponent(Component):
port, \ port, \
disco_category = "gateway", \ disco_category = "gateway", \
disco_type = "headline") disco_type = "headline")
self.__logger = logging.getLogger("jabber.Component") self.__logger = logging.getLogger("jmc.jabber.Component")
self.__shutdown = 0 self.__shutdown = 0
self.__lang = Lang(default_lang) self.__lang = Lang(default_lang)
self.__name = name self.__name = name

View File

@@ -30,9 +30,9 @@ reload(sys)
sys.setdefaultencoding('utf-8') sys.setdefaultencoding('utf-8')
del sys.setdefaultencoding del sys.setdefaultencoding
from jabber import mailconnection from jmc import mailconnection
from jabber.component import MailComponent, ComponentFatalError from jmc.component import MailComponent, ComponentFatalError
from jabber.config import Config from jmc.config import Config
def main(config_file = "jmc.xml", isDebug = 0): def main(config_file = "jmc.xml", isDebug = 0):
try: try:
@@ -87,5 +87,5 @@ if __name__ == "__main__":
if (var_option & 2) == 2: if (var_option & 2) == 2:
main(sys.argv[file_num], debug_level) main(sys.argv[file_num], debug_level)
else: else:
main("jmc.xml", debug_level) main("/etc/jabber/jmc.xml", debug_level)

View File

View File

@@ -24,7 +24,7 @@ import libxml2
import os import os
from pyxmpp.jid import JID from pyxmpp.jid import JID
from component import ComponentFatalError from jmc.jabber.component import ComponentFatalError
class Config: class Config:
def __init__(self, config_file): def __init__(self, config_file):

33
src/jmc/utils/release.py Normal file
View File

@@ -0,0 +1,33 @@
##
## release.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Mon Jul 24 22:37:00 2006 dax
## $Id$
##
## Copyright (C) 2006 dax
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
version = "0.2.2"
author = "David Rousselie"
email = "dax@happycoders.org"
license = "GPL"
long_description = """Jabber Mail Component
JMC is a jabber service to check email from POP3 and IMAP4 server and retrieve
them or just a notification of new emails. Jabber users can register multiple
email accounts.
"""

View File

@@ -25,10 +25,10 @@ import re
import os.path import os.path
import sys import sys
import anydbm import anydbm
import mailconnection_factory
import logging import logging
from UserDict import UserDict from UserDict import UserDict
from pysqlite2 import dbapi2 as sqlite
import jmc.email.mailconnection_factory as mailconnection_factory
class Storage(UserDict): class Storage(UserDict):
@@ -106,7 +106,7 @@ class Storage(UserDict):
pass pass
class DBMStorage(Storage): class DBMStorage(Storage):
_logger = logging.getLogger("jabber.DBMStorage") _logger = logging.getLogger("jmc.utils.DBMStorage")
def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None): def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None):
# print "DBM INIT" # print "DBM INIT"
@@ -166,9 +166,12 @@ class DBMStorage(Storage):
Storage.__delitem__(self, pk_tuple) Storage.__delitem__(self, pk_tuple)
self.sync() self.sync()
# Do not fail if pysqlite is not installed
try:
from pysqlite2 import dbapi2 as sqlite
class SQLiteStorage(Storage): class SQLiteStorage(Storage):
_logger = logging.getLogger("jabber.SQLiteStorage") _logger = logging.getLogger("jmc.utils.SQLiteStorage")
def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None): def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None):
self.__connection = None self.__connection = None
@@ -289,4 +292,6 @@ class SQLiteStorage(Storage):
pk_tuple[1])) pk_tuple[1]))
self.__connection.commit() self.__connection.commit()
cursor.close() cursor.close()
except ImportError:
pass

View File

@@ -26,8 +26,8 @@ import dummy_server
import time import time
import traceback import traceback
from pyxmpp import xmlextra from pyxmpp import xmlextra
from jabber.component import * from jmc.jabber.component import *
from jabber.config import Config from jmc.utils.config import Config
class TestStreamHandler(xmlextra.StreamHandler): class TestStreamHandler(xmlextra.StreamHandler):
def __init__(self, expected_balises = []): def __init__(self, expected_balises = []):

View File

@@ -22,7 +22,7 @@
## ##
import unittest import unittest
from jabber.mailconnection import IMAPConnection, \ from jmc.email.mailconnection import IMAPConnection, \
POP3Connection, \ POP3Connection, \
MailConnection MailConnection
import dummy_server import dummy_server

View File

@@ -21,28 +21,49 @@
## ##
import unittest import unittest
from jabber.mailconnection_factory import * from jmc.email.mailconnection_factory import *
from jabber import mailconnection from jmc.email.mailconnection import *
import jmc.email.mailconnection as mailconnection
class MailConnectionFactory_TestCase(unittest.TestCase): class MailConnectionFactory_TestCase(unittest.TestCase):
def test_new_mail_connection_imap(self): def test_new_mail_connection_imap(self):
mc = get_new_mail_connection("imap") mc = get_new_mail_connection("imap")
# TODO self.assertEquals(mc, IMAPConnection())
self.assertEquals(mc, mc)
def test_new_mail_connection_imaps(self): def test_new_mail_connection_imaps(self):
mc = get_new_mail_connection("imaps") mc = get_new_mail_connection("imaps")
self.assertEquals(mc, mc) self.assertEquals(mc, IMAPConnection(ssl = True))
def test_new_mail_connection_pop3(self): def test_new_mail_connection_pop3(self):
mc = get_new_mail_connection("pop3") mc = get_new_mail_connection("pop3")
self.assertEquals(mc, mc) self.assertEquals(mc, POP3Connection())
def test_new_mail_connection_pop3s(self): def test_new_mail_connection_pop3s(self):
mc = get_new_mail_connection("pop3s") mc = get_new_mail_connection("pop3s")
self.assertEquals(mc, mc) self.assertEquals(mc, POP3Connection(ssl = True))
def test_new_mail_connection_unknown(self):
self.assertRaises(Exception, get_new_mail_connection, "unknown")
def test_str_to_mail_connection_imap_v01_v02(self): def test_str_to_mail_connection_imap_v01_v02(self):
mc = str_to_mail_connection("imap#login#passwd#host#193#False#INBOX")
self.assertEquals(mc.get_type(), "imap")
self.assertEquals(mc.login, "login")
self.assertEquals(mc.password, "passwd")
self.assertEquals(mc.store_password, True)
self.assertEquals(mc.host, "host")
self.assertEquals(mc.port, 193)
self.assertEquals(mc.mailbox, "INBOX")
self.assertEquals(mc.chat_action, mailconnection.DIGEST)
self.assertEquals(mc.online_action, mailconnection.DIGEST)
self.assertEquals(mc.away_action, mailconnection.DIGEST)
self.assertEquals(mc.xa_action, mailconnection.DIGEST)
self.assertEquals(mc.dnd_action, mailconnection.DIGEST)
self.assertEquals(mc.offline_action, mailconnection.DO_NOTHING)
self.assertEquals(mc.interval, 5)
self.assertEquals(mc.live_email_only, False)
def test_str_to_mail_connection_imap_v01_v02_retrieve(self):
mc = str_to_mail_connection("imap#login#passwd#host#193#True#INBOX") mc = str_to_mail_connection("imap#login#passwd#host#193#True#INBOX")
self.assertEquals(mc.get_type(), "imap") self.assertEquals(mc.get_type(), "imap")
self.assertEquals(mc.login, "login") self.assertEquals(mc.login, "login")
@@ -77,6 +98,23 @@ class MailConnectionFactory_TestCase(unittest.TestCase):
self.assertEquals(mc.interval, 5) self.assertEquals(mc.interval, 5)
self.assertEquals(mc.live_email_only, False) self.assertEquals(mc.live_email_only, False)
def test_str_to_mail_connection_pop3_v01_v02_retrieve(self):
mc = str_to_mail_connection("pop3#login#passwd#host#110#True")
self.assertEquals(mc.get_type(), "pop3")
self.assertEquals(mc.login, "login")
self.assertEquals(mc.password, "passwd")
self.assertEquals(mc.store_password, True)
self.assertEquals(mc.host, "host")
self.assertEquals(mc.port, 110)
self.assertEquals(mc.chat_action, mailconnection.RETRIEVE)
self.assertEquals(mc.online_action, mailconnection.RETRIEVE)
self.assertEquals(mc.away_action, mailconnection.RETRIEVE)
self.assertEquals(mc.xa_action, mailconnection.RETRIEVE)
self.assertEquals(mc.dnd_action, mailconnection.RETRIEVE)
self.assertEquals(mc.offline_action, mailconnection.DO_NOTHING)
self.assertEquals(mc.interval, 5)
self.assertEquals(mc.live_email_only, False)
def test_str_to_mail_connection_imap(self): def test_str_to_mail_connection_imap(self):
mc = str_to_mail_connection("imap#login#passwd#host#193#0#0#0#1#1#2#4#True#INBOX") mc = str_to_mail_connection("imap#login#passwd#host#193#0#0#0#1#1#2#4#True#INBOX")
self.assertEquals(mc.get_type(), "imap") self.assertEquals(mc.get_type(), "imap")
@@ -161,3 +199,6 @@ class MailConnectionFactory_TestCase(unittest.TestCase):
self.assertEquals(mc.interval, 4) self.assertEquals(mc.interval, 4)
self.assertEquals(mc.live_email_only, True) self.assertEquals(mc.live_email_only, True)
def test_str_to_mail_connection_unknown(self):
self.assertRaises(Exception, str_to_mail_connection, ("unknown#login#passwd#host#995#0#0#0#1#1#2#4#True"))

View File

@@ -23,9 +23,9 @@
import os import os
import unittest import unittest
import dummy_server import dummy_server
from jabber.storage import * from jmc.utils.storage import *
from jabber import mailconnection from jmc.email.mailconnection import *
from jabber.mailconnection import * import jmc.email.mailconnection as mailconnection
class Storage_TestCase(unittest.TestCase): class Storage_TestCase(unittest.TestCase):
def test_init(self): def test_init(self):

View File

@@ -21,7 +21,7 @@
## ##
import unittest import unittest
from jabber.x import * from jmc.jabber.x import *
class X_TestCase(unittest.TestCase): class X_TestCase(unittest.TestCase):
def setUp(self): def setUp(self):