mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-13 08:01:09 +00:00
* lpapiwrapper.py:
- add a base class for all wrapper classes - add a wrapper class for "distro_series"
This commit is contained in:
parent
509360d40d
commit
c397ad9f7b
@ -236,6 +236,60 @@ class LpApiWrapper(object):
|
|||||||
'''
|
'''
|
||||||
return any(t.name == team for t in cls.getMe().super_teams)
|
return any(t.name == team for t in cls.getMe().super_teams)
|
||||||
|
|
||||||
|
|
||||||
|
class MetaWrapper(type):
|
||||||
|
'''
|
||||||
|
A meta class used for wrapping LP API objects.
|
||||||
|
'''
|
||||||
|
def __init__(cls, name, bases, attrd):
|
||||||
|
super(MetaWrapper, cls).__init__(name, bases, attrd)
|
||||||
|
if 'resource_type' not in attrd:
|
||||||
|
raise TypeError('Class needs an associated resource type')
|
||||||
|
cls._cache = dict()
|
||||||
|
|
||||||
|
|
||||||
|
class BaseWrapper(object):
|
||||||
|
'''
|
||||||
|
A base class from which other wrapper classes are derived.
|
||||||
|
'''
|
||||||
|
__metaclass__ = MetaWrapper
|
||||||
|
resource_type = None # it's a base class after all
|
||||||
|
|
||||||
|
def __new__(cls, lpobject):
|
||||||
|
if isinstance(lpobject, str):
|
||||||
|
if lpobject.startswith('http'):
|
||||||
|
# might be an LP API URL
|
||||||
|
# look it up in our cache
|
||||||
|
data = cls._cache.get(lpobject)
|
||||||
|
if data:
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
# try to fetch it from LP
|
||||||
|
fetch = getattr(cls, 'fetch', None)
|
||||||
|
if fetch and callable(fetch):
|
||||||
|
lpobject = fetch(lpobject)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError("Don't know how to fetch '%s' from LP" % lpobject)
|
||||||
|
else:
|
||||||
|
raise TypeError("'%s' doesn't look like a LP API URL" % lpobject)
|
||||||
|
|
||||||
|
if isinstance(lpobject, Entry) and lpobject.resource_type_link in cls.resource_type:
|
||||||
|
# check if it's already cached
|
||||||
|
data = cls._cache.get(lpobject.self_link)
|
||||||
|
if not data:
|
||||||
|
# create a new instance
|
||||||
|
data = object.__new__(cls)
|
||||||
|
data._lpobject = lpobject
|
||||||
|
# and add it to our cache
|
||||||
|
cls._cache[lpobject.self_link] = data
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
raise TypeError("'%s' is not a '%s' object" % (str(lpobject), str(cls.resource_type)))
|
||||||
|
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
return getattr(self._entry, attr)
|
||||||
|
|
||||||
|
|
||||||
class _UbuntuSeries(object):
|
class _UbuntuSeries(object):
|
||||||
'''
|
'''
|
||||||
Wrapper class around a LP Ubuntu series object.
|
Wrapper class around a LP Ubuntu series object.
|
||||||
@ -249,6 +303,12 @@ class _UbuntuSeries(object):
|
|||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
return getattr(self._series, attr)
|
return getattr(self._series, attr)
|
||||||
|
|
||||||
|
class DistroSeries(BaseWrapper):
|
||||||
|
'''
|
||||||
|
Wrapper class around a LP distro series object.
|
||||||
|
'''
|
||||||
|
resource_type = 'https://api.edge.launchpad.net/beta/#distro_series'
|
||||||
|
|
||||||
class _SourcePackage(object):
|
class _SourcePackage(object):
|
||||||
'''
|
'''
|
||||||
Wrapper class around a LP source package object.
|
Wrapper class around a LP source package object.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user