Centralize "empty" comparisons

And always use "is" instead of "==".
This commit is contained in:
Laurent Bachelier 2012-03-27 12:08:33 +02:00
commit 3b2bafdf12
3 changed files with 23 additions and 14 deletions

View file

@ -24,7 +24,7 @@ import sys
import os
from weboob.capabilities.video import ICapVideo, BaseVideo
from weboob.capabilities.base import NotLoaded, NotAvailable
from weboob.capabilities.base import empty
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.media_player import InvalidMediaPlayer, MediaPlayer, MediaPlayerNotFound
from weboob.tools.application.formatters.iformatter import IFormatter
@ -49,9 +49,9 @@ class VideoListFormatter(IFormatter):
else:
result = u'%s* (%s) %s%s\n' % (self.BOLD, item['id'], item['title'], self.NC)
result += ' %s' % (item['duration'] if item['duration'] else item['date'])
if item['author'] not in (NotLoaded, NotAvailable, None):
if not empty(item['author']):
result += ' - %s' % item['author']
if item['rating'] not in (NotLoaded, NotAvailable, None):
if not empty(item['rating']):
result += u' (%s/%s)' % (item['rating'], item['rating_max'])
return result

View file

@ -28,7 +28,19 @@ from weboob.tools.ordereddict import OrderedDict
__all__ = ['FieldNotFound', 'NotAvailable', 'NotLoaded', 'IBaseCap',
'Field', 'IntField', 'FloatField', 'StringField', 'BytesField',
'DateField', 'DeltaField', 'CapBaseObject']
'DateField', 'DeltaField', 'CapBaseObject', 'empty']
def empty(value):
"""
Checks if a value is empty (None, NotLoaded or NotAvailable).
:rtype: :class:`bool`
"""
for cls in (None, NotLoaded, NotAvailable):
if value is cls:
return True
return False
class FieldNotFound(Exception):
@ -278,8 +290,7 @@ class CapBaseObject(object):
:param excepts: if specified, do not change fields listed
"""
for key, old_value in self.iter_fields():
if old_value in (None, NotLoaded, NotAvailable) and \
not key in excepts:
if empty(old_value) and key not in excepts:
setattr(self, key, value)
def iter_fields(self):
@ -314,7 +325,7 @@ class CapBaseObject(object):
except KeyError:
object.__setattr__(self, name, value)
else:
if value not in (NotLoaded, NotAvailable, None):
if not empty(value):
try:
# Try to convert value to the wanted one.
nvalue = attr.convert(value)
@ -329,10 +340,7 @@ class CapBaseObject(object):
# raise ValueError.
pass
if not isinstance(value, attr.types) and \
value is not NotLoaded and \
value is not NotAvailable and \
value is not None:
if not isinstance(value, attr.types) and not empty(value):
raise ValueError(
'Value for "%s" needs to be of type %r, not %r' % (
name, attr.types, type(value)))

View file

@ -20,7 +20,7 @@
from prettytable import PrettyTable
from weboob.capabilities.base import NotLoaded, NotAvailable
from weboob.capabilities.base import empty
from .iformatter import IFormatter
@ -52,7 +52,7 @@ class TableFormatter(IFormatter):
for i in xrange(len(self.keys)):
available = False
for line in self.queue:
if line[i] is not NotLoaded and line[i] is not NotAvailable and line[i] is not None:
if not empty(line[i]):
available = True
break
if available:
@ -63,7 +63,7 @@ class TableFormatter(IFormatter):
s = ''
if self.display_header and self.header:
if self.HTML:
s+= '<p>%s</p>' % self.header
s += '<p>%s</p>' % self.header
else:
s += self.header
s += "\n"
@ -92,5 +92,6 @@ class TableFormatter(IFormatter):
def set_header(self, string):
self.header = string
class HTMLTableFormatter(TableFormatter):
HTML = True