Use JCLRunner to run JMC

darcs-hash:20070518143604-86b55-bfcecd31705b8fd96d92283ac3c3b701002dc80a.gz
This commit is contained in:
David Rousselie
2007-05-18 16:36:04 +02:00
parent 3325257e51
commit c13ad7ec93
7 changed files with 270 additions and 55 deletions

24
conf/jmc.conf Normal file
View File

@@ -0,0 +1,24 @@
[jabber]
server: localhost
port: 5347
secret: secret
service_jid: jmc.localhost
#supported language: en, fr (See src/jmc/lang.py to add more)
language: en
[db]
#type: mysql
type: sqlite
#host: root@localhost
host:
name: /var/spool/jabber/jmc.db
#url: %(type)%(host)%(name)?debug=1&debugThreading=1
db_url: %(type)s://%(host)s%(name)s
[component]
pid_file: /var/run/jabber/jmc.pid
[jmc]
mail_default_encoding: iso-8859-1
check_interval: 1

22
jmc.xml
View File

@@ -1,22 +0,0 @@
<config>
<jabber>
<server>127.0.0.1</server>
<port>5347</port>
<secret>secret</secret>
<service>jmc.localhost</service>
<connectsleep>5</connectsleep>
<!-- check jmc/utils/lang.py for available languages -->
<language>fr</language>
<vCard>
<FN>Jabber Mail Component</FN>
<DESC>A Jabber mail server component</DESC>
<URL>http://people.happycoders.org/dax/jabber/jmc/</URL>
</vCard>
</jabber>
<storage>DBM</storage>
<spooldir>./spool/jabber</spooldir>
<pidfile>./run/jabber/jmc.pid</pidfile>
<!-- default check interval in minutes -->
<check_interval>1</check_interval>
<mail_default_encoding>iso-8859-1</mail_default_encoding>
</config>

View File

@@ -20,41 +20,15 @@
## 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 logging
import sys import sys
reload(sys) reload(sys)
sys.setdefaultencoding('utf-8') sys.setdefaultencoding('utf-8')
del sys.setdefaultencoding del sys.setdefaultencoding
from sqlobject import * import jmc
from pyxmpp.message import Message from jmc.runner import JMCRunner
from jcl.model import account if __name__ == "__main__":
from jcl.model.account import Account, PresenceAccount runner = JMCRunner("Jabber Mail Component", jmc.version)
runner.configure()
from jmc.jabber.component import MailComponent runner.run()
from jmc.model.account import MailAccount, IMAPAccount, POP3Account
DB_PATH = "/tmp/jmc.db"
DB_URL = DB_PATH# + "?debug=1&debugThreading=1"
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
Account.createTable(ifNotExists = True)
PresenceAccount.createTable(ifNotExists = True)
MailAccount.createTable(ifNotExists = True)
IMAPAccount.createTable(ifNotExists = True)
POP3Account.createTable(ifNotExists = True)
del account.hub.threadConnection
component = MailComponent("jmc.localhost", \
"secret", \
"127.0.0.1", \
5347, \
"sqlite://" + DB_URL)
component.run()
logger.debug("JMC is exiting")

63
src/jmc/runner.py Normal file
View File

@@ -0,0 +1,63 @@
##
## runner.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Thu May 17 21:58:32 2007 David Rousselie
## $Id$
##
## Copyright (C) 2007 David Rousselie
## 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
##
from jcl.runner import JCLRunner
from jmc.model.account import MailAccount, IMAPAccount, POP3Account
from jmc.jabber.component import MailComponent
from jmc.lang import Lang
class JMCRunner(JCLRunner):
def __init__(self, component_name, component_version):
JCLRunner.__init__(self, component_name, component_version)
# define new options
self.check_interval = 1
self.mail_default_encoding = "iso-8859-1"
self.options += [("i:", "check-interval=", "jmc", \
" INTERVAL\t\t\tInterval unit in minute between mail checks", \
lambda arg: setattr(self, "check_interval", int(arg))), \
("e:", "mail-default-encoding=", "jmc", \
" ENCODING\t\tDefault encoding of the component", \
lambda arg: setattr(self, "mail_default_encoding", arg))]
# override JCL default
self.service_jid = "jmc.localhost"
self.db_url = "sqlite:///var/spool/jabber/jmc.db"
self.pid_file = "/var/run/jabber/jmc.pid"
def setup_db(self):
JCLRunner.setup_db(self)
MailAccount.createTable(ifNotExists = True)
IMAPAccount.createTable(ifNotExists = True)
POP3Account.createTable(ifNotExists = True)
def run(self):
def run_func():
component = MailComponent(jid = self.service_jid, \
secret = self.secret, \
server = self.server, \
port = self.port, \
db_connection_str = self.db_url, \
lang = Lang(self.language))
MailAccount.default_encoding = self.mail_default_encoding
component.check_interval = self.check_interval
component.run()
self._run(run_func)

View File

@@ -3,13 +3,14 @@ __revision__ = ""
import unittest import unittest
from jmc.tests import lang from jmc.tests import lang, runner
from jmc.jabber import tests as jabber from jmc.jabber import tests as jabber
from jmc.model import tests as model from jmc.model import tests as model
def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(lang.suite()) suite.addTest(lang.suite())
suite.addTest(runner.suite())
suite.addTest(jabber.suite()) suite.addTest(jabber.suite())
suite.addTest(model.suite()) suite.addTest(model.suite())
return suite return suite

25
src/jmc/tests/jmc.conf Normal file
View File

@@ -0,0 +1,25 @@
[jabber]
server: test_localhost
port: 42
secret: test_secret
service_jid: test_jmc.localhost
#supported language: en, fr (See src/jmc/lang.py to add more)
language: test_en
[db]
#type: mysql
type: test_sqlite
#host: root@localhost
host: root@localhost
name: /var/spool/jabber/test_jmc.db
#url: %(type)%(host)%(name)?debug=1&debugThreading=1
db_url: %(type)s://%(host)s%(name)s
[component]
pid_file: /var/run/jabber/test_jmc.pid
[jmc]
mail_default_encoding: test_iso-8859-1
check_interval: 2

150
src/jmc/tests/runner.py Normal file
View File

@@ -0,0 +1,150 @@
##
## runner.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Fri May 18 13:43:37 2007 David Rousselie
## $Id$
##
## Copyright (C) 2007 David Rousselie
## 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
##
import unittest
import sys
import os
from sqlobject import *
from jcl.tests.runner import JCLRunner_TestCase
from jcl.model import account
from jcl.model.account import Account, PresenceAccount
import jmc
from jmc.runner import JMCRunner
from jmc.model.account import MailAccount, IMAPAccount, POP3Account
if sys.platform == "win32":
DB_PATH = "/c|/temp/test.db"
else:
DB_PATH = "/tmp/test.db"
DB_URL = "sqlite://" + DB_PATH# + "?debug=1&debugThreading=1"
class JMCRunner_TestCase(JCLRunner_TestCase):
def setUp(self):
self.runner = JMCRunner("Jabber Mail Component", jmc.version)
def tearDown(self):
self.runner = None
sys.argv = [""]
def test_configure_default(self):
self.runner.configure()
self.assertEquals(self.runner.config_file, None)
self.assertEquals(self.runner.server, "localhost")
self.assertEquals(self.runner.port, 5347)
self.assertEquals(self.runner.secret, "secret")
self.assertEquals(self.runner.service_jid, "jmc.localhost")
self.assertEquals(self.runner.language, "en")
self.assertEquals(self.runner.db_url, "sqlite:///var/spool/jabber/jmc.db")
self.assertEquals(self.runner.pid_file, "/var/run/jabber/jmc.pid")
self.assertFalse(self.runner.debug)
self.assertEquals(self.runner.mail_default_encoding, "iso-8859-1")
self.assertEquals(self.runner.check_interval, 1)
def test_configure_configfile(self):
self.runner.config_file = "src/jmc/tests/jmc.conf"
self.runner.configure()
self.assertEquals(self.runner.server, "test_localhost")
self.assertEquals(self.runner.port, 42)
self.assertEquals(self.runner.secret, "test_secret")
self.assertEquals(self.runner.service_jid, "test_jmc.localhost")
self.assertEquals(self.runner.language, "test_en")
self.assertEquals(self.runner.db_url, "test_sqlite://root@localhost/var/spool/jabber/test_jmc.db")
self.assertEquals(self.runner.pid_file, "/var/run/jabber/test_jmc.pid")
self.assertFalse(self.runner.debug)
self.assertEquals(self.runner.mail_default_encoding, "test_iso-8859-1")
self.assertEquals(self.runner.check_interval, 2)
def test_configure_commandline_shortopt(self):
sys.argv = ["", "-c", "src/jmc/tests/jmc.conf", \
"-S", "test2_localhost", \
"-P", "43", \
"-s", "test2_secret", \
"-j", "test2_jmc.localhost", \
"-l", "test2_en", \
"-u", "sqlite:///tmp/test_jmc.db", \
"-p", "/tmp/test_jmc.pid", \
"-e", "test2_iso-8859-1", \
"-i", "3"]
self.runner.configure()
self.assertEquals(self.runner.server, "test2_localhost")
self.assertEquals(self.runner.port, 43)
self.assertEquals(self.runner.secret, "test2_secret")
self.assertEquals(self.runner.service_jid, "test2_jmc.localhost")
self.assertEquals(self.runner.language, "test2_en")
self.assertEquals(self.runner.db_url, "sqlite:///tmp/test_jmc.db")
self.assertEquals(self.runner.pid_file, "/tmp/test_jmc.pid")
self.assertFalse(self.runner.debug)
self.assertEquals(self.runner.mail_default_encoding, "test2_iso-8859-1")
self.assertEquals(self.runner.check_interval, 3)
def test_configure_commandline_longopt(self):
sys.argv = ["", "--config-file", "src/jmc/tests/jmc.conf", \
"--server", "test2_localhost", \
"--port", "43", \
"--secret", "test2_secret", \
"--service-jid", "test2_jmc.localhost", \
"--language", "test2_en", \
"--db-url", "sqlite:///tmp/test_jmc.db", \
"--pid-file", "/tmp/test_jmc.pid", \
"--mail-default-encoding", "test2_iso-8859-1", \
"--check-interval", "4"]
self.runner.configure()
self.assertEquals(self.runner.server, "test2_localhost")
self.assertEquals(self.runner.port, 43)
self.assertEquals(self.runner.secret, "test2_secret")
self.assertEquals(self.runner.service_jid, "test2_jmc.localhost")
self.assertEquals(self.runner.language, "test2_en")
self.assertEquals(self.runner.db_url, "sqlite:///tmp/test_jmc.db")
self.assertEquals(self.runner.pid_file, "/tmp/test_jmc.pid")
self.assertFalse(self.runner.debug)
self.assertEquals(self.runner.mail_default_encoding, "test2_iso-8859-1")
self.assertEquals(self.runner.check_interval, 4)
def test__run(self):
self.runner.pid_file = "/tmp/jmc.pid"
self.runner.db_url = DB_URL
def do_nothing():
pass
self.runner._run(do_nothing)
account.hub.threadConnection = connectionForURI(self.runner.db_url)
# dropTable should succeed because tables should exist
Account.dropTable()
PresenceAccount.dropTable()
MailAccount.dropTable()
IMAPAccount.dropTable()
POP3Account.dropTable()
del account.hub.threadConnection
os.unlink(DB_PATH)
self.assertFalse(os.access("/tmp/jmc.pid", os.F_OK))
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(JMCRunner_TestCase, 'test'))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')