Implement multiple service support to Bugzilla API #31

Merged
merged 1 commit into from Aug 29, 2012

3 participants

@BYK
Owner

This diff introduces a "Service" class that namespaces each bugzilla
service and sets the proper service_url global on the API module.

Usage is as follows:

from pyresto.apis import bugzilla

bugzilla.mozilla.Bug.get('774141')

Any bugzilla service that is not predefined can be defined as follows:

my_service = bugzilla.Service('my_service', 'https://api.bugzilla.myserver.com')
my_service.Bug.get('3141592')
@berkerpeksag berkerpeksag was assigned Aug 29, 2012
@travisbot

This pull request passes (merged 2fa09746 into b89229c).

@berkerpeksag berkerpeksag and 1 other commented on an outdated diff Aug 29, 2012
pyresto/apis/bugzilla/__init__.py
__author__ = ('Berker Peksag <berker.peksag@gmail.com>',
'Burak Yigit Kaya <ben@byk.im>')
+
+__models_file__ = os.path.join(os.path.dirname(__file__), 'models.py')
+__models_code__ = compile(open(__models_file__).read(),
+ __models_file__, 'exec')
+
+__services__ = {
+ 'mozilla': 'https://api-dev.bugzilla.mozilla.org/latest/',
+ 'mozilla_test': 'https://api-dev.bugzilla.mozilla.org/test/latest/',
+ 'mozilla11': 'https://api-dev.bugzilla.mozilla.org/1.1/',
+ 'mozilla11_test': 'https://api-dev.bugzilla.mozilla.org/test/1.1/'
+}
+
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
@berkerpeksag
Collaborator

('Service',)

@berkerpeksag
Collaborator
__services__.keys()
@BYK
Owner
BYK added a note Aug 29, 2012

__services__.keys() is a list, we need a tuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@berkerpeksag berkerpeksag commented on the diff Aug 29, 2012
pyresto/apis/bugzilla/__init__.py
+__models_code__ = compile(open(__models_file__).read(),
+ __models_file__, 'exec')
+
+__services__ = dict(
+ mozilla='https://api-dev.bugzilla.mozilla.org/latest/',
+ mozilla_test='https://api-dev.bugzilla.mozilla.org/test/latest/',
+ mozilla11='https://api-dev.bugzilla.mozilla.org/1.1/',
+ mozilla11_test='https://api-dev.bugzilla.mozilla.org/test/1.1/'
+)
+
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
+
+
+class Service(object):
+ def __init__(self, name, url):
+ self.name = name
@berkerpeksag
Collaborator

service_name?

@BYK
Owner
BYK added a note Aug 29, 2012

I think this version is better. Any reasons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@berkerpeksag berkerpeksag commented on an outdated diff Aug 29, 2012
pyresto/apis/bugzilla/__init__.py
+)
+
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
+
+
+class Service(object):
+ def __init__(self, name, url):
+ self.name = name
+ self.module_name = '{0}.{1}'.format(__name__, self.name)
+ self.url = url
+ self.__namespace = None
+
+ @property
+ def namespace(self):
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
@berkerpeksag
Collaborator

43?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@berkerpeksag berkerpeksag commented on an outdated diff Aug 29, 2012
pyresto/apis/bugzilla/__init__.py
+__all__ = ('Service', ) + tuple(__services__.iterkeys())
+
+
+class Service(object):
+ def __init__(self, name, url):
+ self.name = name
+ self.module_name = '{0}.{1}'.format(__name__, self.name)
+ self.url = url
+ self.__namespace = None
+
+ @property
+ def namespace(self):
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
+ # https://speakerdeck.com/u/antocuni/p/python-white-magic
+ self.__namespace = imp.new_module(self.module_name)
@berkerpeksag
Collaborator

<3 imp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@berkerpeksag berkerpeksag commented on an outdated diff Aug 29, 2012
pyresto/apis/bugzilla/__init__.py
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
+ # https://speakerdeck.com/u/antocuni/p/python-white-magic
+ self.__namespace = imp.new_module(self.module_name)
+ self.__namespace.__service_url__ = self.url
+ exec __models_code__ in self.__namespace.__dict__
+
+ return self.__namespace
+
+ def __getattr__(self, item):
+ return getattr(self.namespace, item)
+
+
+# Create services
+globs = globals()
+for name, url in __services__.iteritems():
@berkerpeksag
Collaborator
__services__.items()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@berkerpeksag berkerpeksag commented on an outdated diff Aug 29, 2012
pyresto/apis/bugzilla/__init__.py
+ def namespace(self):
+ if self.__namespace is None:
+ # All these "namespacing tricks" are from (from slides 40+)
+ # https://speakerdeck.com/u/antocuni/p/python-white-magic
+ self.__namespace = imp.new_module(self.module_name)
+ self.__namespace.__service_url__ = self.url
+ exec __models_code__ in self.__namespace.__dict__
+
+ return self.__namespace
+
+ def __getattr__(self, item):
+ return getattr(self.namespace, item)
+
+
+# Create services
+globs = globals()
@berkerpeksag
Collaborator

_globals?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@travisbot

This pull request passes (merged ac029022 into b89229c).

@travisbot

This pull request passes (merged f82df22d into b89229c).

@BYK Implement multiple service support to Bugzilla API
This diff introduces a "Service" class that namespaces each bugzilla
service and sets the proper __service_url__ global on the API module.

Usage is as follows:

    from pyresto.apis import bugzilla

    bugzilla.mozilla.Bug.get('774141')

Any bugzilla service that is not predefined can be defined as follows:

    my_service = bugzilla.Service('my_service', 'https://api.bugzilla.myserver.com')
    my_service.Bug.get('3141592')
34e02f9
@travisbot

This pull request passes (merged 34e02f9 into b89229c).

@berkerpeksag berkerpeksag merged commit ecb2ad5 into master Aug 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment