diff --git a/rattail/commands.py b/rattail/commands.py index d1b80e86002d454ca52700a2d310aca1da9735d0..dc7219ed566f403920ef8cb73e83b2876b8bed09 100644 --- a/rattail/commands.py +++ b/rattail/commands.py @@ -35,6 +35,7 @@ from edbob.commands import Subcommand from ._version import __version__ from .db import model +from .console import Progress class Command(commands.Command): @@ -159,7 +160,6 @@ class Dump(Subcommand): def run(self, args): from .db import get_session_class from .db.dump import dump_data - from .console import Progress if hasattr(model, args.model): cls = getattr(model, args.model) @@ -302,17 +302,16 @@ class LoadHostDataCommand(Subcommand): description = "Load data from host database" def run(self, args): - from .console import Progress - from rattail.db import load + from .db import get_engines + from .db import load - edbob.init_modules(['edbob.db']) - - if 'host' not in edbob.engines: - print "Host engine URL not configured." - return + engines = get_engines(edbob.config) + if 'host' not in engines: + sys.stderr.write("Host engine URL not configured.\n") + sys.exit(1) proc = load.LoadProcessor() - proc.load_all_data(edbob.engines['host'], Progress) + proc.load_all_data(engines['host'], Progress) class MakeConfigCommand(Subcommand): diff --git a/rattail/db/sync/__init__.py b/rattail/db/sync/__init__.py index bb3e82a41b60e920de1faa0e57c877f0df238e1a..ed78bf69615bd0e0ad92c7956f53ac25924cf489 100644 --- a/rattail/db/sync/__init__.py +++ b/rattail/db/sync/__init__.py @@ -39,25 +39,25 @@ from sqlalchemy.exc import OperationalError import edbob -from rattail.db import model +from .. import model +from .. import get_engines log = logging.getLogger(__name__) def get_sync_engines(): - edbob.init_modules(['edbob.db']) - keys = edbob.config.get('rattail.db', 'syncs') if not keys: return None - engines = {} + engines = get_engines(edbob.config) + sync_engines = {} for key in keys.split(','): key = key.strip() - engines[key] = edbob.engines[key] - log.debug("get_sync_engines: Found engine keys: %s" % ','.join(engines.keys())) - return engines + sync_engines[key] = engines[key] + log.debug("get_sync_engines: Found engine keys: %s" % ','.join(sync_engines.keys())) + return sync_engines class Synchronizer(object): diff --git a/rattail/db/sync/linux.py b/rattail/db/sync/linux.py index 1d98c93bd48dca08a47772bea90a0591d91c6532..307aeb1bbbe3c3743d2f926726c98add250247d7 100644 --- a/rattail/db/sync/linux.py +++ b/rattail/db/sync/linux.py @@ -27,9 +27,9 @@ """ import edbob -from edbob import db from ...daemon import Daemon +from .. import get_default_engine from . import get_sync_engines, synchronize_changes @@ -38,7 +38,8 @@ class SyncDaemon(Daemon): def run(self): remote_engines = get_sync_engines() if remote_engines: - synchronize_changes(db.engine, remote_engines) + local_engine = get_default_engine(edbob.config) + synchronize_changes(local_engine, remote_engines) def get_daemon(pidfile=None): diff --git a/rattail/db/sync/win32.py b/rattail/db/sync/win32.py index 3b467f8a7988dc964d7ec010b7dbbb1eccec65a4..73de8a89b2f8c999d5397e973ac50c1415e52e5f 100644 --- a/rattail/db/sync/win32.py +++ b/rattail/db/sync/win32.py @@ -31,10 +31,10 @@ import logging import threading import edbob -from edbob import db -from rattail.win32.service import Service -from rattail.db.sync import get_sync_engines, synchronize_changes +from ...win32.service import Service +from .. import get_default_engine +from . import get_sync_engines, synchronize_changes log = logging.getLogger(__name__) @@ -61,14 +61,13 @@ class DatabaseSynchronizerService(Service): if not Service.Initialize(self): return False - edbob.init_modules(['rattail.db']) - + local_engine = get_default_engine(edbob.config) remote_engines = get_sync_engines() if not remote_engines: return False thread = threading.Thread(target=synchronize_changes, - args=(db.engine, remote_engines)) + args=(local_engine, remote_engines)) thread.daemon = True thread.start() return True diff --git a/tests/db/sync/test_init.py b/tests/db/sync/test_init.py index 719360ecee1399600716261ab59140b4eeef5f71..88936523ef761240e466d33d323c22c11a353ceb 100644 --- a/tests/db/sync/test_init.py +++ b/tests/db/sync/test_init.py @@ -354,15 +354,15 @@ class SynchronizerTests(SyncTestCase): class ModuleTests(TestCase): - @patch('rattail.db.sync.edbob') - def test_get_sync_engines(self, edbob): + @patch.multiple('rattail.db.sync', edbob=DEFAULT, get_engines=DEFAULT) + def test_get_sync_engines(self, edbob, get_engines): # nothing configured edbob.config.get.return_value = None self.assertIsNone(sync.get_sync_engines()) # fake config with 2 out of 3 engines synced - edbob.engines = { + get_engines.return_value = { 'one': 'first', 'two': 'second', 'three': 'third', diff --git a/tests/db/sync/test_linux.py b/tests/db/sync/test_linux.py index a89b90ab38a9bde75e2af2b232311586de626ecb..53350746b02960ad75b5b8102e28b44f7845063d 100644 --- a/tests/db/sync/test_linux.py +++ b/tests/db/sync/test_linux.py @@ -8,10 +8,11 @@ from rattail.db.sync import linux class SyncDaemonTests(TestCase): @patch.multiple('rattail.db.sync.linux', - db=DEFAULT, + edbob=DEFAULT, + get_default_engine=DEFAULT, get_sync_engines=DEFAULT, synchronize_changes=DEFAULT) - def test_run(self, db, get_sync_engines, synchronize_changes): + def test_run(self, edbob, get_default_engine, get_sync_engines, synchronize_changes): daemon = linux.SyncDaemon('/tmp/rattail_dbsync.pid') @@ -19,12 +20,14 @@ class SyncDaemonTests(TestCase): get_sync_engines.return_value = None daemon.run() get_sync_engines.assert_called_once_with() + self.assertFalse(get_default_engine.called) self.assertFalse(synchronize_changes.called) # with remote engines configured get_sync_engines.return_value = 'fake_remotes' + get_default_engine.return_value = 'fake_local' daemon.run() - synchronize_changes.assert_called_once_with(db.engine, 'fake_remotes') + synchronize_changes.assert_called_once_with('fake_local', 'fake_remotes') class ModuleTests(TestCase):