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
## Login : <david.rousselie@happycoders.org>
## Login : David Rousselie <dax@happycoders.org>
## 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 $
##
@@ -24,18 +24,21 @@ import coverage
coverage.erase()
coverage.start()
import unittest
import sys
sys.path.append("src")
reload(sys)
sys.setdefaultencoding('utf8')
del sys.setdefaultencoding
import tests
from tests.test_mailconnection import *
from tests.test_mailconnection_factory import *
from tests.test_component import *
from tests.test_storage import *
from test import test_support
import jabber
import logging
import jmc
if __name__ == '__main__':
@@ -81,10 +84,10 @@ if __name__ == '__main__':
#test_support.run_suite(dbmstorage_suite)
test_support.run_suite(jmc_suite)
# coverage.stop()
# coverage.analysis(jabber.mailconnection_factory)
# coverage.analysis(jabber.mailconnection)
# coverage.analysis(jabber.component)
# coverage.analysis(jabber.x)
# coverage.report([jabber.mailconnection_factory, jabber.mailconnection, \
# jabber.component, jabber.x])
coverage.stop()
coverage.analysis(jmc.email.mailconnection_factory)
coverage.analysis(jmc.email.mailconnection)
coverage.analysis(jmc.jabber.component)
coverage.analysis(jmc.jabber.x)
coverage.report([jmc.email.mailconnection_factory, jmc.email.mailconnection, \
jmc.jabber.component, jmc.jabber.x])

View File

View File

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

View File

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

View File

View File

@@ -32,11 +32,12 @@ import os
import time
import traceback
import mailconnection
from mailconnection import *
from x import *
from storage import *
import mailconnection_factory
from jmc.email.mailconnection import *
from jmc.jabber.x import *
from jmc.utils.storage import *
from jmc.utils.lang import Lang
import jmc.email.mailconnection_factory
import pyxmpp.jabberd
from pyxmpp.presence import Presence
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.jabberd.component import Component
from jabber.lang import Lang
class ComponentFatalError(RuntimeError):
pass
@@ -68,7 +67,7 @@ class MailComponent(Component):
port, \
disco_category = "gateway", \
disco_type = "headline")
self.__logger = logging.getLogger("jabber.Component")
self.__logger = logging.getLogger("jmc.jabber.Component")
self.__shutdown = 0
self.__lang = Lang(default_lang)
self.__name = name

View File

@@ -30,9 +30,9 @@ reload(sys)
sys.setdefaultencoding('utf-8')
del sys.setdefaultencoding
from jabber import mailconnection
from jabber.component import MailComponent, ComponentFatalError
from jabber.config import Config
from jmc import mailconnection
from jmc.component import MailComponent, ComponentFatalError
from jmc.config import Config
def main(config_file = "jmc.xml", isDebug = 0):
try:
@@ -87,5 +87,5 @@ if __name__ == "__main__":
if (var_option & 2) == 2:
main(sys.argv[file_num], debug_level)
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
from pyxmpp.jid import JID
from component import ComponentFatalError
from jmc.jabber.component import ComponentFatalError
class Config:
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 sys
import anydbm
import mailconnection_factory
import logging
from UserDict import UserDict
from pysqlite2 import dbapi2 as sqlite
import jmc.email.mailconnection_factory as mailconnection_factory
class Storage(UserDict):
@@ -106,7 +106,7 @@ class Storage(UserDict):
pass
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):
# print "DBM INIT"
@@ -166,127 +166,132 @@ class DBMStorage(Storage):
Storage.__delitem__(self, pk_tuple)
self.sync()
# Do not fail if pysqlite is not installed
try:
from pysqlite2 import dbapi2 as sqlite
class SQLiteStorage(Storage):
_logger = logging.getLogger("jabber.SQLiteStorage")
class SQLiteStorage(Storage):
_logger = logging.getLogger("jmc.utils.SQLiteStorage")
def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None):
self.__connection = None
Storage.__init__(self, nb_pk_fields, spool_dir, db_file)
def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None):
self.__connection = None
Storage.__init__(self, nb_pk_fields, spool_dir, db_file)
def create(self):
SQLiteStorage._logger.debug("creating new Table")
cursor = self.__connection.cursor()
cursor.execute("""
create table account(
jid STRING,
name STRING,
type STRING,
login STRING,
password STRING,
host STRING,
port INTEGER,
chat_action INTEGER,
online_action INTEGER,
away_action INTEGER,
xa_action INTEGER,
dnd_action INTEGER,
offline_action INTEGER,
interval INTEGER,
live_email_only BOOLEAN,
mailbox STRING,
PRIMARY KEY(jid, name)
)
""")
self.__connection.commit()
cursor.close()
def create(self):
SQLiteStorage._logger.debug("creating new Table")
cursor = self.__connection.cursor()
cursor.execute("""
create table account(
jid STRING,
name STRING,
type STRING,
login STRING,
password STRING,
host STRING,
port INTEGER,
chat_action INTEGER,
online_action INTEGER,
away_action INTEGER,
xa_action INTEGER,
dnd_action INTEGER,
offline_action INTEGER,
interval INTEGER,
live_email_only BOOLEAN,
mailbox STRING,
PRIMARY KEY(jid, name)
)
""")
self.__connection.commit()
cursor.close()
def __del__(self):
self.__connection.close()
def __del__(self):
self.__connection.close()
def sync(self):
pass
def sync(self):
pass
def load(self):
if not os.path.exists(self.file):
self.__connection = sqlite.connect(self.file)
self.create()
else:
self.__connection = sqlite.connect(self.file)
cursor = self.__connection.cursor()
cursor.execute("""select * from account""")
result = {}
for row in cursor.fetchall():
# print "Creating new " + row[self.nb_pk_fields] + " connection."
account_type = row[self.nb_pk_fields]
account = result["#".join(row[0:self.nb_pk_fields])] = mailconnection_factory.get_new_mail_connection(account_type)
account.login = row[self.nb_pk_fields + 1]
account.password = row[self.nb_pk_fields + 2]
if account.password is None:
account.store_password = False
def load(self):
if not os.path.exists(self.file):
self.__connection = sqlite.connect(self.file)
self.create()
else:
account.store_password = True
account.host = row[self.nb_pk_fields + 3]
account.port = int(row[self.nb_pk_fields + 4])
account.chat_action = int(row[self.nb_pk_fields + 5])
account.online_action = int(row[self.nb_pk_fields + 6])
account.away_action = int(row[self.nb_pk_fields + 7])
account.xa_action = int(row[self.nb_pk_fields + 8])
account.dnd_action = int(row[self.nb_pk_fields + 9])
account.offline_action = int(row[self.nb_pk_fields + 10])
account.interval = int(row[self.nb_pk_fields + 11])
account.live_email_only = (row[self.nb_pk_fields + 12] == 1)
if account_type[0:4] == "imap":
account.mailbox = row[self.nb_pk_fields + 13]
# for field_index in range(self.nb_pk_fields + 1, len(row)):
# print "\tSetting " + str(cursor.description[field_index][0]) + \
# " to " + str(row[field_index])
# setattr(account,
# cursor.description[field_index][0],
# row[field_index])
cursor.close()
return result
self.__connection = sqlite.connect(self.file)
cursor = self.__connection.cursor()
cursor.execute("""select * from account""")
result = {}
for row in cursor.fetchall():
# print "Creating new " + row[self.nb_pk_fields] + " connection."
account_type = row[self.nb_pk_fields]
account = result["#".join(row[0:self.nb_pk_fields])] = mailconnection_factory.get_new_mail_connection(account_type)
account.login = row[self.nb_pk_fields + 1]
account.password = row[self.nb_pk_fields + 2]
if account.password is None:
account.store_password = False
else:
account.store_password = True
account.host = row[self.nb_pk_fields + 3]
account.port = int(row[self.nb_pk_fields + 4])
account.chat_action = int(row[self.nb_pk_fields + 5])
account.online_action = int(row[self.nb_pk_fields + 6])
account.away_action = int(row[self.nb_pk_fields + 7])
account.xa_action = int(row[self.nb_pk_fields + 8])
account.dnd_action = int(row[self.nb_pk_fields + 9])
account.offline_action = int(row[self.nb_pk_fields + 10])
account.interval = int(row[self.nb_pk_fields + 11])
account.live_email_only = (row[self.nb_pk_fields + 12] == 1)
if account_type[0:4] == "imap":
account.mailbox = row[self.nb_pk_fields + 13]
# for field_index in range(self.nb_pk_fields + 1, len(row)):
# print "\tSetting " + str(cursor.description[field_index][0]) + \
# " to " + str(row[field_index])
# setattr(account,
# cursor.description[field_index][0],
# row[field_index])
cursor.close()
return result
def __setitem__(self, pk_tuple, obj):
Storage.__setitem__(self, pk_tuple, obj)
cursor = self.__connection.cursor()
mailbox = None
password = None
if obj.type[0:4] == "imap":
mailbox = obj.mailbox
if obj.store_password == True:
password = obj.password
cursor.execute("""
insert or replace into account values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(pk_tuple[0],
pk_tuple[1],
obj.type,
obj.login,
password,
obj.host,
obj.port,
obj.chat_action,
obj.online_action,
obj.away_action,
obj.xa_action,
obj.dnd_action,
obj.offline_action,
obj.interval,
obj.live_email_only,
mailbox))
self.__connection.commit()
cursor.close()
def __setitem__(self, pk_tuple, obj):
Storage.__setitem__(self, pk_tuple, obj)
cursor = self.__connection.cursor()
mailbox = None
password = None
if obj.type[0:4] == "imap":
mailbox = obj.mailbox
if obj.store_password == True:
password = obj.password
cursor.execute("""
insert or replace into account values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(pk_tuple[0],
pk_tuple[1],
obj.type,
obj.login,
password,
obj.host,
obj.port,
obj.chat_action,
obj.online_action,
obj.away_action,
obj.xa_action,
obj.dnd_action,
obj.offline_action,
obj.interval,
obj.live_email_only,
mailbox))
self.__connection.commit()
cursor.close()
def __delitem__(self, pk_tuple):
Storage.__delitem__(self, pk_tuple)
cursor = self.__connection.cursor()
cursor.execute("""
delete from account where jid = ? and name = ?
""",
(pk_tuple[0],
pk_tuple[1]))
self.__connection.commit()
cursor.close()
def __delitem__(self, pk_tuple):
Storage.__delitem__(self, pk_tuple)
cursor = self.__connection.cursor()
cursor.execute("""
delete from account where jid = ? and name = ?
""",
(pk_tuple[0],
pk_tuple[1]))
self.__connection.commit()
cursor.close()
except ImportError:
pass

View File

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

View File

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

View File

@@ -21,28 +21,49 @@
##
import unittest
from jabber.mailconnection_factory import *
from jabber import mailconnection
from jmc.email.mailconnection_factory import *
from jmc.email.mailconnection import *
import jmc.email.mailconnection as mailconnection
class MailConnectionFactory_TestCase(unittest.TestCase):
def test_new_mail_connection_imap(self):
mc = get_new_mail_connection("imap")
# TODO
self.assertEquals(mc, mc)
self.assertEquals(mc, IMAPConnection())
def test_new_mail_connection_imaps(self):
mc = get_new_mail_connection("imaps")
self.assertEquals(mc, mc)
self.assertEquals(mc, IMAPConnection(ssl = True))
def test_new_mail_connection_pop3(self):
mc = get_new_mail_connection("pop3")
self.assertEquals(mc, mc)
self.assertEquals(mc, POP3Connection())
def test_new_mail_connection_pop3s(self):
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):
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")
self.assertEquals(mc.get_type(), "imap")
self.assertEquals(mc.login, "login")
@@ -77,6 +98,23 @@ class MailConnectionFactory_TestCase(unittest.TestCase):
self.assertEquals(mc.interval, 5)
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):
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")
@@ -161,3 +199,6 @@ class MailConnectionFactory_TestCase(unittest.TestCase):
self.assertEquals(mc.interval, 4)
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 unittest
import dummy_server
from jabber.storage import *
from jabber import mailconnection
from jabber.mailconnection import *
from jmc.utils.storage import *
from jmc.email.mailconnection import *
import jmc.email.mailconnection as mailconnection
class Storage_TestCase(unittest.TestCase):
def test_init(self):

View File

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