Skip to content

Commit

Permalink
Use setuptools entry points instead of DeferredImport
Browse files Browse the repository at this point in the history
This way, it is possible to use services implemented in external
packages

Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
  • Loading branch information
puiterwijk committed Oct 29, 2015
1 parent eb785fb commit 0750259
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 107 deletions.
4 changes: 2 additions & 2 deletions bugwarrior/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from taskw.warrior import TaskWarriorBase

from bugwarrior.config import get_taskrc_path, load_config
from bugwarrior.services import aggregate_issues, SERVICES
from bugwarrior.services import aggregate_issues, get_service
from bugwarrior.db import (
get_defined_udas_as_strings,
synchronize,
Expand Down Expand Up @@ -91,7 +91,7 @@ def targets():
section.startswith('flavor.'):
continue
service_name = config.get(section, 'service')
service_class = SERVICES[service_name]
service_class = get_service(service_name)
for option in config.options(section):
value = config.get(section, option)
if not value:
Expand Down
6 changes: 3 additions & 3 deletions bugwarrior/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ def validate_config(config, main_section):
if not service:
die("No 'service' in [%s]" % target)

if service not in SERVICES:
if not get_service(service):
die("'%s' in [%s] is not a valid service." % (service, target))

# Call the service-specific validator
SERVICES[service].validate_config(config, target)
get_service(service).validate_config(config, target)


def load_config(main_section):
Expand Down Expand Up @@ -168,4 +168,4 @@ def get_taskrc_path(conf, main_section):


# This needs to be imported here and not above to avoid a circular-import.
from bugwarrior.services import SERVICES
from bugwarrior.services import get_service
8 changes: 4 additions & 4 deletions bugwarrior/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,11 @@ def _bool_option(section, option, default):


def build_key_list(targets):
from bugwarrior.services import SERVICES
from bugwarrior.services import get_service

keys = {}
for target in targets:
keys[target] = SERVICES[target].ISSUE_CLASS.UNIQUE_KEY
keys[target] = get_service(target).ISSUE_CLASS.UNIQUE_KEY
return keys


Expand Down Expand Up @@ -509,11 +509,11 @@ def build_uda_config_overrides(targets):
"""

from bugwarrior.services import SERVICES
from bugwarrior.services import get_service

targets_udas = {}
for target in targets:
targets_udas.update(SERVICES[target].ISSUE_CLASS.UDAS)
targets_udas.update(get_service(target).ISSUE_CLASS.UDAS)
return {
'uda': targets_udas
}
Expand Down
33 changes: 11 additions & 22 deletions bugwarrior/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import multiprocessing
import time

from pkg_resources import iter_entry_points

from dateutil.parser import parse as parse_date
from dateutil.tz import tzlocal
from jinja2 import Template
Expand All @@ -11,7 +13,6 @@

from taskw.task import Task

from bugwarrior.utils import DeferredImportingDict
from bugwarrior.config import asbool
from bugwarrior.db import MARKUP, URLShortener, ABORT_PROCESSING

Expand All @@ -24,26 +25,14 @@
# date string to be parsed as if it were in your local timezone
LOCAL_TIMEZONE = 'LOCAL_TIMEZONE'

# Constant dict to be used all around town.
# It will defer actually importing a service until someone tries to access it
# in the dict. This should help expose odd ImportErrors in a more obvious way
# for end users. See https://github.com/ralphbean/bugwarrior/issues/132
SERVICES = DeferredImportingDict({
'github': 'bugwarrior.services.github:GithubService',
'gitlab': 'bugwarrior.services.gitlab:GitlabService',
'bitbucket': 'bugwarrior.services.bitbucket:BitbucketService',
'trac': 'bugwarrior.services.trac:TracService',
'bugzilla': 'bugwarrior.services.bz:BugzillaService',
'teamlab': 'bugwarrior.services.teamlab:TeamLabService',
'redmine': 'bugwarrior.services.redmine:RedMineService',
'activecollab2': 'bugwarrior.services.activecollab2:ActiveCollab2Service',
'activecollab': 'bugwarrior.services.activecollab:ActiveCollabService',
'jira': 'bugwarrior.services.jira:JiraService',
'megaplan': 'bugwarrior.services.megaplan:MegaplanService',
'phabricator': 'bugwarrior.services.phab:PhabricatorService',
'versionone': 'bugwarrior.services.versionone:VersionOneService',
'pagure': 'bugwarrior.services.pagure:PagureService',
})
def get_service(service_name):
epoint = iter_entry_points(group='bugwarrior.service', name=service_name)
try:
epoint = epoint.next()
except StopIteration:
return None

return epoint.load()


class IssueService(object):
Expand Down Expand Up @@ -496,7 +485,7 @@ def _aggregate_issues(conf, main_section, target, queue, service_name):
start = time.time()

try:
service = SERVICES[service_name](conf, main_section, target)
service = get_service(service_name)(conf, main_section, target)
issue_count = 0
for issue in service.issues():
queue.put(issue)
Expand Down
38 changes: 0 additions & 38 deletions bugwarrior/utils.py

This file was deleted.

15 changes: 15 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,20 @@
bugwarrior-pull = bugwarrior:pull
bugwarrior-vault = bugwarrior:vault
bugwarrior-uda = bugwarrior:uda
[bugwarrior.service]
github=bugwarrior.services.github:GithubService
gitlab=bugwarrior.services.gitlab:GitlabService
bitbucket=bugwarrior.services.bitbucket:BitbucketService
trac=bugwarrior.services.trac:TracService
bugzilla=bugwarrior.services.bz:BugzillaService
teamlab=bugwarrior.services.teamlab:TeamLabService
redmine=bugwarrior.services.redmine:RedMineService
activecollab2=bugwarrior.services.activecollab2:ActiveCollab2Service
activecollab=bugwarrior.services.activecollab:ActiveCollabService
jira=bugwarrior.services.jira:JiraService
megaplan=bugwarrior.services.megaplan:MegaplanService
phabricator=bugwarrior.services.phab:PhabricatorService
versionone=bugwarrior.services.versionone:VersionOneService
pagure=bugwarrior.services.pagure:PagureService
""",
)
38 changes: 0 additions & 38 deletions tests/test_utils.py

This file was deleted.

0 comments on commit 0750259

Please sign in to comment.