diff --git a/weboob/capabilities/base.py b/weboob/capabilities/base.py index 667fcb1c..15c678ce 100644 --- a/weboob/capabilities/base.py +++ b/weboob/capabilities/base.py @@ -62,7 +62,30 @@ class CapBaseObject(object): self.id = id self.backend = backend + def __iscomplete__(self): + """ + Return True if the object is completed. + + It is usefull when the object is a field of an other object which is + going to be filled. + + The default behavior is to iter on fields (with iter_fields) and if + a field is NotLoaded, return False. + """ + for key, value in self.iter_fields(): + if value is NotLoaded: + return False + return True + def iter_fields(self): + """ + Iterate on the FIELDS keys and values. + + Can be overloaded to iterate on other things. + + @return [iter(key,value)] iterator on key, value + """ + if self.FIELDS is None: for key, value in iter_fields(self): if key != 'backend': diff --git a/weboob/capabilities/contact.py b/weboob/capabilities/contact.py index ebf0ee79..3a87c3c4 100644 --- a/weboob/capabilities/contact.py +++ b/weboob/capabilities/contact.py @@ -33,9 +33,10 @@ class ProfileNode(object): self.sufix = sufix self.flags = flags -class ContactPhoto(object): +class ContactPhoto(CapBaseObject): def __init__(self, name): - self.name = name + CapBaseObject.__init__(self, name) + self.name = name #useless, but keep compatibility self.url = u'' self.data = '' self.thumbnail_url = u'' diff --git a/weboob/capabilities/video.py b/weboob/capabilities/video.py index d6725b2a..94dfc74e 100644 --- a/weboob/capabilities/video.py +++ b/weboob/capabilities/video.py @@ -22,7 +22,7 @@ from .base import IBaseCap, NotLoaded, CapBaseObject __all__ = ['BaseVideo', 'ICapVideo'] -class VideoThumbnail(object): +class VideoThumbnail(CapBaseObject): def __init__(self, url): self.url = url.replace(' ', '%20') self.data = NotLoaded diff --git a/weboob/tools/backend.py b/weboob/tools/backend.py index e8cab303..e0b96044 100644 --- a/weboob/tools/backend.py +++ b/weboob/tools/backend.py @@ -21,7 +21,7 @@ import os from threading import RLock from logging import debug -from weboob.capabilities.base import IBaseCap, NotLoaded +from weboob.capabilities.base import IBaseCap, NotLoaded, CapBaseObject __all__ = ['BaseBackend', 'ObjectNotAvailable'] @@ -193,6 +193,9 @@ class BaseBackend(object): return False def fillobj(self, obj, fields): + def not_loaded(v): + return (v is NotLoaded or isinstance(value, CapBaseObject) and not value.__iscomplete__()) + missing_fields = [] for field in fields: if not hasattr(obj, field): @@ -200,17 +203,12 @@ class BaseBackend(object): value = getattr(obj, field) missing = False - if isinstance(value, dict): - for v in value.itervalues(): - if hasattr(v, '__iscomplete__') and not v.__iscomplete__(): + if hasattr(value, '__iter__'): + for v in (value.itervalues() if isinstance(value, dict) else value): + if not_loaded(v): missing = True break - elif isinstance(value, (list,tuple)): - for v in value: - if hasattr(v, '__iscomplete__') and not v.__iscomplete__(): - missing = True - break - elif value is NotLoaded or hasattr(value, '__iscomplete__') and not value.__iscomplete__(): + elif not_loaded(value): missing = True if missing: