From 31bd33bcff9b263f08f55d558f10854db76723b8 Mon Sep 17 00:00:00 2001 From: iElectric Date: Thu, 4 Jun 2009 22:36:29 +0000 Subject: [PATCH] use entrypoints terminology to parse dotted model class names --- CHANGELOG | 1 + TODO | 3 +++ migrate/versioning/util/__init__.py | 20 ++++++++++++-------- test/versioning/test_shell.py | 6 +++++- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ca12a72..6958671 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ 0.5.4 +- use entrypoints terminology to specify dotted model names (module.model.User) [Domen Kozar] - added engine_dict and engine_arg_* parameters to all api functions [Domen Kozar] - make --echo parameter a bit more forgivable (better Python API support) [Domen Kozar] - apply patch to refactor cmd line parsing for Issue 54 by Domen Kozar diff --git a/TODO b/TODO index a2f7736..75ee438 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,6 @@ - better MySQL support - fix unit tests for other databases than PostgreSQL (MySQL and SQLite fail at test_changeset.test_fk(..)) +- document dotted_name parsing changes +- document shell parsing +- document engine parameters usage/parsing diff --git a/migrate/versioning/util/__init__.py b/migrate/versioning/util/__init__.py index 60d190f..530bfd2 100644 --- a/migrate/versioning/util/__init__.py +++ b/migrate/versioning/util/__init__.py @@ -1,23 +1,27 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import warnings from decorator import decorator +from pkg_resources import EntryPoint from migrate.versioning import exceptions from migrate.versioning.util.keyedinstance import KeyedInstance from migrate.versioning.util.importpath import import_path -def loadModel(model): - ''' Import module and use module-level variable -- assume model is of form "mod1.mod2.varname". ''' - if isinstance(model, basestring): - varname = model.split('.')[-1] - modules = '.'.join(model.split('.')[:-1]) - module = __import__(modules, globals(), {}, ['dummy-not-used'], -1) - return getattr(module, varname) +def loadModel(dotted_name): + ''' Import module and use module-level variable -- assume model is of form "mod1.mod2:varname". ''' + if isinstance(dotted_name, basestring): + if ':' not in dotted_name: + # backwards compatibility + warnings.warn('model should be in form of module.model:User' + 'and not module.model.User', DeprecationWarning) + dotted_name = ':'.join(dotted_name.rsplit('.', 1)) + return EntryPoint.parse('x=%s' % dotted_name).load(False) else: # Assume it's already loaded. - return model + return dotted_name def asbool(obj): """Do everything to use object as bool""" diff --git a/test/versioning/test_shell.py b/test/versioning/test_shell.py index 6d8fe20..b16dccb 100644 --- a/test/versioning/test_shell.py +++ b/test/versioning/test_shell.py @@ -495,7 +495,7 @@ class TestShellDatabase(Shell, fixture.DB): self.assertEquals(self.cmd_db_version(self.url,repos_path),0) # Setup helper script. - model_module = 'testmodel.meta' + model_module = 'testmodel:meta' self.assertSuccess(self.cmd('manage',script_path,'--repository=%s --url=%s --model=%s' % (repos_path, self.url, model_module))) self.assert_(os.path.exists(script_path)) @@ -522,6 +522,10 @@ class TestShellDatabase(Shell, fixture.DB): # Model is defined but database is empty. output, exitcode = self.output_and_exitcode('python %s compare_model_to_db' % script_path) assert "tables missing in database: tmp_account_rundiffs" in output, output + + # Test Deprecation + output, exitcode = self.output_and_exitcode('python %s compare_model_to_db --model=testmodel.meta' % script_path) + assert "tables missing in database: tmp_account_rundiffs" in output, output # Update db to latest model. output, exitcode = self.output_and_exitcode('python %s update_db_from_model' % script_path)