From 39211b67e01ad8d4bbef6dcd9b7e71fdbb307b20 Mon Sep 17 00:00:00 2001 From: Michael Bienia Date: Thu, 11 Jun 2009 16:33:47 +0200 Subject: [PATCH] * ubuntutools/lp/lpapiwrapper.py: Implement a singleton for LP API access (with lazy initialisation) * ubuntutools/lp/functions.py: Use the new singleton --- ubuntutools/lp/functions.py | 8 +++---- ubuntutools/lp/lpapiwrapper.py | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 ubuntutools/lp/lpapiwrapper.py diff --git a/ubuntutools/lp/functions.py b/ubuntutools/lp/functions.py index 5024e94..1aa5aa6 100644 --- a/ubuntutools/lp/functions.py +++ b/ubuntutools/lp/functions.py @@ -21,14 +21,12 @@ import urllib2 import sys from udtexceptions import PackageNotFoundException, SeriesNotFoundException -import libsupport as lp_libsupport +from lpapiwrapper import Launchpad import launchpadlib from re import findall -# Takes time to initialise - move to top level so we only pay the penalty -# once. Should probably make this a proper class so we can instansiate -# singleton-style (lazily). -launchpad = lp_libsupport.get_launchpad("ubuntu-dev-tools") +# Singleton to access LP API +launchpad = Launchpad def getUbuntuDistribution(): ubuntu = launchpad.distributions['ubuntu'] diff --git a/ubuntutools/lp/lpapiwrapper.py b/ubuntutools/lp/lpapiwrapper.py new file mode 100644 index 0000000..47e228f --- /dev/null +++ b/ubuntutools/lp/lpapiwrapper.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# +# lpapiwrapper.py - wrapper class around the LP API for use in the +# ubuntu-dev-tools package +# +# Copyright © 2009 Michael Bienia +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# Please see the /usr/share/common-licenses/GPL file for the full text +# of the GNU General Public License license. +# +# Based on code written by Jonathan Davies + +# Uncomment for tracing LP API calls +#import httplib2 +#httplib2.debuglevel = 1 + +import libsupport + +# Singleton for Launchpad API access +class Launchpad(object): + __lp = None + + def __getattr__(self, attr): + if not self.__lp: + self.__lp = libsupport.get_launchpad('ubuntu-dev-tools') + return getattr(self.__lp, attr) + + def __call__(self): + return self +Launchpad = Launchpad()