improvement of the fillobj
This commit is contained in:
parent
ef27837042
commit
1398c0f7d1
2 changed files with 30 additions and 26 deletions
|
|
@ -23,6 +23,7 @@ from optparse import OptionGroup, OptionParser
|
||||||
|
|
||||||
from weboob.core.ouiboube import Weboob
|
from weboob.core.ouiboube import Weboob
|
||||||
from weboob.tools.config.iconfig import ConfigError
|
from weboob.tools.config.iconfig import ConfigError
|
||||||
|
from weboob.tools.backend import NotSupportedObject
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['BackendNotFound', 'BaseApplication', 'ConfigError']
|
__all__ = ['BackendNotFound', 'BaseApplication', 'ConfigError']
|
||||||
|
|
@ -185,6 +186,34 @@ class BaseApplication(object):
|
||||||
version = '%s v%s' % (self.APPNAME, self.VERSION)
|
version = '%s v%s' % (self.APPNAME, self.VERSION)
|
||||||
return version
|
return version
|
||||||
|
|
||||||
|
def _complete_obj(self, backend, obj, fields):
|
||||||
|
if fields:
|
||||||
|
try:
|
||||||
|
backend.fillobj(obj, fields)
|
||||||
|
except NotSupportedObject, e:
|
||||||
|
logging.warning(u'Could not retrieve required fields (%s): %s' % (','.join(fields), e))
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def _complete_iter(self, backend, count, fields, res):
|
||||||
|
for i, sub in enumerate(res):
|
||||||
|
if count and i == count:
|
||||||
|
break
|
||||||
|
sub = self._complete_obj(backend, sub, fields)
|
||||||
|
yield sub
|
||||||
|
|
||||||
|
def complete(self, backend, count, selected_fields, function, *args, **kwargs):
|
||||||
|
res = getattr(backend, function)(*args, **kwargs)
|
||||||
|
|
||||||
|
if self.selected_fields:
|
||||||
|
fields = set(self.selected_fields) - set('*')
|
||||||
|
else:
|
||||||
|
fields = None
|
||||||
|
|
||||||
|
if hasattr(res, '__iter__'):
|
||||||
|
return self._complete_iter(backend, count, fields, res)
|
||||||
|
else:
|
||||||
|
return self._complete_obj(backend, fields, res)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def run(klass, args=None):
|
def run(klass, args=None):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,6 @@ import sys
|
||||||
|
|
||||||
from weboob.core import CallErrors
|
from weboob.core import CallErrors
|
||||||
from weboob.core.backends import BackendsConfig
|
from weboob.core.backends import BackendsConfig
|
||||||
from weboob.tools.backend import NotSupportedObject
|
|
||||||
|
|
||||||
from .base import BackendNotFound, BaseApplication
|
from .base import BackendNotFound, BaseApplication
|
||||||
from .formatters.load import formatters, load_formatter
|
from .formatters.load import formatters, load_formatter
|
||||||
|
|
@ -276,28 +275,4 @@ class ConsoleApplication(BaseApplication):
|
||||||
Call Weboob.do(), after having filled the yielded object, if selected fields are given by user.
|
Call Weboob.do(), after having filled the yielded object, if selected fields are given by user.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def inner(backend, count, selected_fields, function, *args, **kwargs):
|
return self.weboob.do(self.complete, self.options.count, self.selected_fields, function, *args, **kwargs)
|
||||||
res = getattr(backend, function)(*args, **kwargs)
|
|
||||||
if self.selected_fields:
|
|
||||||
fields = set(self.selected_fields) - set('*')
|
|
||||||
else:
|
|
||||||
fields = None
|
|
||||||
if hasattr(res, '__iter__'):
|
|
||||||
for i, sub in enumerate(res):
|
|
||||||
if self.options.count and i == self.options.count:
|
|
||||||
break
|
|
||||||
if fields:
|
|
||||||
try:
|
|
||||||
backend.fillobj(sub, fields)
|
|
||||||
except NoSupportedObject, e:
|
|
||||||
logging.warning(u'Could not retrieve required fields (%s): %s' % (','.join(fields), e))
|
|
||||||
yield sub
|
|
||||||
else:
|
|
||||||
if fields:
|
|
||||||
try:
|
|
||||||
backend.fillobj(sub, fields)
|
|
||||||
except NoSupportedObject, e:
|
|
||||||
logging.warning(u'Could not retrieve required fields (%s): %s' % (','.join(fields), e))
|
|
||||||
return res
|
|
||||||
|
|
||||||
return self.weboob.do(inner, function, *args, **kwargs)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue