incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [2/3] git commit: [#6670] Refactor to keep import task DRY
Date Mon, 07 Oct 2013 20:29:42 GMT
[#6670] Refactor to keep import task DRY

Signed-off-by: Tim Van Steenburgh <tvansteenburgh@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/2b554a6e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/2b554a6e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/2b554a6e

Branch: refs/heads/master
Commit: 2b554a6ee9e4d0679df626398e4974d50d647564
Parents: 0295646
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Fri Oct 4 21:30:34 2013 +0000
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Mon Oct 7 20:28:35 2013 +0000

----------------------------------------------------------------------
 ForgeImporters/forgeimporters/base.py           | 20 ++++++++++++++------
 ForgeImporters/forgeimporters/forge/tracker.py  | 18 +++---------------
 ForgeImporters/forgeimporters/github/code.py    | 15 +++------------
 ForgeImporters/forgeimporters/github/tracker.py | 14 +++-----------
 ForgeImporters/forgeimporters/google/code.py    | 15 +++------------
 ForgeImporters/forgeimporters/google/tracker.py | 15 +++------------
 ForgeImporters/forgeimporters/trac/tickets.py   | 14 +++-----------
 7 files changed, 32 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/2b554a6e/ForgeImporters/forgeimporters/base.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/base.py b/ForgeImporters/forgeimporters/base.py
index d2a006a..415df67 100644
--- a/ForgeImporters/forgeimporters/base.py
+++ b/ForgeImporters/forgeimporters/base.py
@@ -22,7 +22,7 @@ import urllib
 import urllib2
 from collections import defaultdict
 import traceback
-from urlparse import urlparse, parse_qs
+from urlparse import urlparse
 try:
     from cStringIO import StringIO
 except ImportError:
@@ -33,7 +33,6 @@ from tg import expose, validate, flash, redirect, config
 from tg.decorators import with_trailing_slash
 from pylons import app_globals as g
 from pylons import tmpl_context as c
-from pylons import request
 from formencode import validators as fev, schema
 from webob import exc
 
@@ -45,7 +44,6 @@ from allura.lib import helpers as h
 from allura.lib import exceptions
 from allura.lib import validators as v
 from allura.app import SitemapEntry
-from allura.tasks.mail_tasks import sendmail
 from allura import model as M
 
 from paste.deploy.converters import aslist
@@ -105,8 +103,10 @@ class ImportErrorHandler(object):
 
 
 @task(notifications_disabled=True)
-def import_tool(importer_name, project_name=None, mount_point=None, mount_label=None, **kw):
-    importer = ToolImporter.by_name(importer_name)
+def import_tool(importer_path, project_name=None, mount_point=None, mount_label=None, **kw):
+    smod, sklass = importer_path.rsplit('.', 1)
+    mod = __import__(smod, fromlist=[sklass])
+    importer = getattr(mod, sklass)()
     with ImportErrorHandler(importer, project_name, c.project) as handler:
         app = importer.import_tool(c.project, c.user, project_name=project_name,
                 mount_point=mount_point, mount_label=mount_label, **kw)
@@ -268,7 +268,7 @@ class ProjectImporter(BaseController):
             kw['tools'] = [kw['tools'], ]
 
         for importer_name in kw['tools']:
-            import_tool.post(importer_name, **kw)
+            ToolImporter.by_name(importer_name).post(**kw)
         M.AuditLog.log('import project from %s' % self.source)
 
         flash('Welcome to the %s Project System! '
@@ -426,6 +426,14 @@ class ToolImporter(object):
     def tool_icon(self, theme, size):
         return theme.app_icon_url(aslist(self.target_app)[0], size)
 
+    def post(self, **kw):
+        """Post a task that will call ``import_tool()`` on this instance.
+
+        """
+        klass = self.__class__
+        importer_path = '{0}.{1}'.format(klass.__module__, klass.__name__)
+        import_tool.post(importer_path, **kw)
+
 
 class ToolsValidator(fev.Set):
     """

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/2b554a6e/ForgeImporters/forgeimporters/forge/tracker.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/forge/tracker.py b/ForgeImporters/forgeimporters/forge/tracker.py
index 819bf8c..4d1b74d 100644
--- a/ForgeImporters/forgeimporters/forge/tracker.py
+++ b/ForgeImporters/forgeimporters/forge/tracker.py
@@ -16,12 +16,9 @@
 #       under the License.
 
 import os
-from collections import defaultdict
-from datetime import datetime
 import json
 
 import dateutil.parser
-from formencode import validators as fev
 from pylons import tmpl_context as c
 from pylons import app_globals as g
 from ming.orm import session, ThreadLocalORMSession
@@ -40,7 +37,7 @@ from tg.decorators import (
 from allura.controllers import BaseController
 from allura.lib import helpers as h
 from allura.lib.plugin import ImportIdConverter
-from allura.lib.decorators import require_post, task
+from allura.lib.decorators import require_post
 from allura.lib import validators as v
 from allura import model as M
 
@@ -49,21 +46,12 @@ from forgetracker import model as TM
 from forgeimporters.base import (
         ToolImporter,
         ToolImportForm,
-        ImportErrorHandler,
         File,
         get_importer_upload_path,
         save_importer_upload,
         )
 
 
-@task(notifications_disabled=True)
-def import_tool(**kw):
-    importer = ForgeTrackerImporter()
-    with ImportErrorHandler(importer, kw.get('project_name'), c.project) as handler:
-        app = importer.import_tool(c.project, c.user, **kw)
-        handler.success(app)
-
-
 class ForgeTrackerImportForm(ToolImportForm):
     tickets_json = v.JsonFile(required=True)
 
@@ -87,9 +75,9 @@ class ForgeTrackerImportController(BaseController):
     @require_post()
     @validate(ForgeTrackerImportForm(ForgeTrackerApp), error_handler=index)
     def create(self, tickets_json, mount_point, mount_label, **kw):
-        if ForgeTrackerImporter().enforce_limit(c.project):
+        if self.importer.enforce_limit(c.project):
             save_importer_upload(c.project, 'tickets.json', json.dumps(tickets_json))
-            import_tool.post(
+            self.importer.post(
                     mount_point=mount_point,
                     mount_label=mount_label,
                 )

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/2b554a6e/ForgeImporters/forgeimporters/github/code.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/code.py b/ForgeImporters/forgeimporters/github/code.py
index 2b7883d..42e847d 100644
--- a/ForgeImporters/forgeimporters/github/code.py
+++ b/ForgeImporters/forgeimporters/github/code.py
@@ -29,7 +29,7 @@ from tg.decorators import (
         without_trailing_slash,
         )
 
-from allura.lib.decorators import require_post, task
+from allura.lib.decorators import require_post
 from allura.controllers import BaseController
 
 from forgegit.git_main import ForgeGitApp
@@ -37,19 +37,10 @@ from forgegit.git_main import ForgeGitApp
 from forgeimporters.base import (
         ToolImporter,
         ToolImportForm,
-        ImportErrorHandler,
         )
 from forgeimporters.github import GitHubProjectExtractor
 
 
-@task(notifications_disabled=True)
-def import_tool(**kw):
-    importer = GitHubRepoImporter()
-    with ImportErrorHandler(importer, kw.get('project_name'), c.project) as handler:
-        app = importer.import_tool(c.project, c.user, **kw)
-        handler.success(app)
-
-
 class GitHubRepoImportForm(ToolImportForm):
     gh_project_name = fev.UnicodeString(not_empty=True)
     gh_user_name = fev.UnicodeString(not_empty=True)
@@ -74,8 +65,8 @@ class GitHubRepoImportController(BaseController):
     @require_post()
     @validate(GitHubRepoImportForm(ForgeGitApp), error_handler=index)
     def create(self, gh_project_name, gh_user_name, mount_point, mount_label, **kw):
-        if GitHubRepoImporter().enforce_limit(c.project):
-            import_tool.post(
+        if self.importer.enforce_limit(c.project):
+            self.importer.post(
                     project_name=gh_project_name,
                     user_name=gh_user_name,
                     mount_point=mount_point,

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/2b554a6e/ForgeImporters/forgeimporters/github/tracker.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/tracker.py b/ForgeImporters/forgeimporters/github/tracker.py
index e3e8536..7adc00c 100644
--- a/ForgeImporters/forgeimporters/github/tracker.py
+++ b/ForgeImporters/forgeimporters/github/tracker.py
@@ -38,7 +38,7 @@ from tg.decorators import (
 from allura import model as M
 from allura.controllers import BaseController
 from allura.lib import helpers as h
-from allura.lib.decorators import require_post, task
+from allura.lib.decorators import require_post
 from ming.orm import session, ThreadLocalORMSession
 from pylons import tmpl_context as c
 from pylons import app_globals as g
@@ -47,15 +47,7 @@ from . import GitHubProjectExtractor
 from ..base import ToolImporter
 from forgetracker.tracker_main import ForgeTrackerApp
 from forgetracker import model as TM
-from forgeimporters.base import ToolImportForm, ImportErrorHandler
-
-
-@task(notifications_disabled=True)
-def import_tool(**kw):
-    importer = GitHubTrackerImporter()
-    with ImportErrorHandler(importer, kw.get('project_name'), c.project) as handler:
-        app = importer.import_tool(c.project, c.user, **kw)
-        handler.success(app)
+from forgeimporters.base import ToolImportForm
 
 
 class GitHubTrackerImportForm(ToolImportForm):
@@ -83,7 +75,7 @@ class GitHubTrackerImportController(BaseController):
     @require_post()
     @validate(GitHubTrackerImportForm(ForgeTrackerApp), error_handler=index)
     def create(self, gh_project_name, gh_user_name, mount_point, mount_label, **kw):
-        import_tool.post(
+        self.importer.post(
                 project_name=gh_project_name,
                 user_name=gh_user_name,
                 mount_point=mount_point,

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/2b554a6e/ForgeImporters/forgeimporters/google/code.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/code.py b/ForgeImporters/forgeimporters/google/code.py
index 2a6b7f1..8ff16a8 100644
--- a/ForgeImporters/forgeimporters/google/code.py
+++ b/ForgeImporters/forgeimporters/google/code.py
@@ -35,12 +35,11 @@ from tg.decorators import (
 
 from allura.controllers import BaseController
 from allura.lib import validators as v
-from allura.lib.decorators import require_post, task
+from allura.lib.decorators import require_post
 from allura import model as M
 
 from forgeimporters.base import (
         ToolImporter,
-        ImportErrorHandler,
         )
 from forgeimporters.google import GoogleCodeProjectExtractor
 
@@ -85,14 +84,6 @@ def get_repo_class(type_):
     return REPO_APPS[type_]
 
 
-@task(notifications_disabled=True)
-def import_tool(**kw):
-    importer = GoogleRepoImporter()
-    with ImportErrorHandler(importer, kw.get('project_name'), c.project) as handler:
-        app = importer.import_tool(c.project, c.user, **kw)
-        handler.success(app)
-
-
 class GoogleRepoImportForm(fe.schema.Schema):
     gc_project_name = fev.UnicodeString(not_empty=True)
     mount_point = fev.UnicodeString()
@@ -141,8 +132,8 @@ class GoogleRepoImportController(BaseController):
     @require_post()
     @validate(GoogleRepoImportForm(), error_handler=index)
     def create(self, gc_project_name, mount_point, mount_label, **kw):
-        if GoogleRepoImporter().enforce_limit(c.project):
-            import_tool.post(
+        if self.importer.enforce_limit(c.project):
+            self.importer.post(
                     project_name=gc_project_name,
                     mount_point=mount_point,
                     mount_label=mount_label)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/2b554a6e/ForgeImporters/forgeimporters/google/tracker.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/tracker.py b/ForgeImporters/forgeimporters/google/tracker.py
index 0802354..ddf5d35 100644
--- a/ForgeImporters/forgeimporters/google/tracker.py
+++ b/ForgeImporters/forgeimporters/google/tracker.py
@@ -38,7 +38,7 @@ from tg.decorators import (
 from allura.controllers import BaseController
 from allura.lib import helpers as h
 from allura.lib.plugin import ImportIdConverter
-from allura.lib.decorators import require_post, task
+from allura.lib.decorators import require_post
 from allura import model as M
 
 from forgetracker.tracker_main import ForgeTrackerApp
@@ -47,18 +47,9 @@ from forgeimporters.google import GoogleCodeProjectExtractor
 from forgeimporters.base import (
         ToolImporter,
         ToolImportForm,
-        ImportErrorHandler,
         )
 
 
-@task(notifications_disabled=True)
-def import_tool(**kw):
-    importer = GoogleCodeTrackerImporter()
-    with ImportErrorHandler(importer, kw.get('project_name'), c.project) as handler:
-        app = importer.import_tool(c.project, c.user, **kw)
-        handler.success(app)
-
-
 class GoogleCodeTrackerImportForm(ToolImportForm):
     gc_project_name = fev.UnicodeString(not_empty=True)
 
@@ -82,8 +73,8 @@ class GoogleCodeTrackerImportController(BaseController):
     @require_post()
     @validate(GoogleCodeTrackerImportForm(ForgeTrackerApp), error_handler=index)
     def create(self, gc_project_name, mount_point, mount_label, **kw):
-        if GoogleCodeTrackerImporter().enforce_limit(c.project):
-            import_tool.post(
+        if self.importer.enforce_limit(c.project):
+            self.importer.post(
                     project_name=gc_project_name,
                     mount_point=mount_point,
                     mount_label=mount_label,

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/2b554a6e/ForgeImporters/forgeimporters/trac/tickets.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/tickets.py b/ForgeImporters/forgeimporters/trac/tickets.py
index 7361373..757f6ed 100644
--- a/ForgeImporters/forgeimporters/trac/tickets.py
+++ b/ForgeImporters/forgeimporters/trac/tickets.py
@@ -39,7 +39,7 @@ from tg.decorators import (
         )
 
 from allura.controllers import BaseController
-from allura.lib.decorators import require_post, task
+from allura.lib.decorators import require_post
 from allura.lib.import_api import AlluraImportApiClient
 from allura.lib import validators as v
 from allura.lib import helpers as h
@@ -52,20 +52,11 @@ from allura.scripts.trac_export import (
 from forgeimporters.base import (
         ToolImporter,
         ToolImportForm,
-        ImportErrorHandler,
         )
 from forgetracker.tracker_main import ForgeTrackerApp
 from forgetracker.scripts.import_tracker import import_tracker
 
 
-@task(notifications_disabled=True)
-def import_tool(**kw):
-    importer = TracTicketImporter()
-    with ImportErrorHandler(importer, kw.get('trac_url'), c.project) as handler:
-        app = importer.import_tool(c.project, c.user, **kw)
-        handler.success(app)
-
-
 class TracTicketImportForm(ToolImportForm):
     trac_url = fev.URL(not_empty=True)
     user_map = v.UserMapJsonFile(as_string=True)
@@ -92,7 +83,8 @@ class TracTicketImportController(BaseController):
     @validate(TracTicketImportForm(ForgeTrackerApp), error_handler=index)
     def create(self, trac_url, mount_point, mount_label, user_map=None, **kw):
         if self.importer.enforce_limit(c.project):
-            self.task.post(
+            self.importer.post(
+                    project_name=trac_url,
                     mount_point=mount_point,
                     mount_label=mount_label,
                     trac_url=trac_url,


Mime
View raw message