Centralize "empty" comparisons
And always use "is" instead of "==".
This commit is contained in:
parent
a99cd8a0e5
commit
3b2bafdf12
3 changed files with 23 additions and 14 deletions
|
|
@ -24,7 +24,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from weboob.capabilities.video import ICapVideo, BaseVideo
|
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.repl import ReplApplication
|
||||||
from weboob.tools.application.media_player import InvalidMediaPlayer, MediaPlayer, MediaPlayerNotFound
|
from weboob.tools.application.media_player import InvalidMediaPlayer, MediaPlayer, MediaPlayerNotFound
|
||||||
from weboob.tools.application.formatters.iformatter import IFormatter
|
from weboob.tools.application.formatters.iformatter import IFormatter
|
||||||
|
|
@ -49,9 +49,9 @@ class VideoListFormatter(IFormatter):
|
||||||
else:
|
else:
|
||||||
result = u'%s* (%s) %s%s\n' % (self.BOLD, item['id'], item['title'], self.NC)
|
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'])
|
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']
|
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'])
|
result += u' (%s/%s)' % (item['rating'], item['rating_max'])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,19 @@ from weboob.tools.ordereddict import OrderedDict
|
||||||
|
|
||||||
__all__ = ['FieldNotFound', 'NotAvailable', 'NotLoaded', 'IBaseCap',
|
__all__ = ['FieldNotFound', 'NotAvailable', 'NotLoaded', 'IBaseCap',
|
||||||
'Field', 'IntField', 'FloatField', 'StringField', 'BytesField',
|
'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):
|
class FieldNotFound(Exception):
|
||||||
|
|
@ -278,8 +290,7 @@ class CapBaseObject(object):
|
||||||
:param excepts: if specified, do not change fields listed
|
:param excepts: if specified, do not change fields listed
|
||||||
"""
|
"""
|
||||||
for key, old_value in self.iter_fields():
|
for key, old_value in self.iter_fields():
|
||||||
if old_value in (None, NotLoaded, NotAvailable) and \
|
if empty(old_value) and key not in excepts:
|
||||||
not key in excepts:
|
|
||||||
setattr(self, key, value)
|
setattr(self, key, value)
|
||||||
|
|
||||||
def iter_fields(self):
|
def iter_fields(self):
|
||||||
|
|
@ -314,7 +325,7 @@ class CapBaseObject(object):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
object.__setattr__(self, name, value)
|
object.__setattr__(self, name, value)
|
||||||
else:
|
else:
|
||||||
if value not in (NotLoaded, NotAvailable, None):
|
if not empty(value):
|
||||||
try:
|
try:
|
||||||
# Try to convert value to the wanted one.
|
# Try to convert value to the wanted one.
|
||||||
nvalue = attr.convert(value)
|
nvalue = attr.convert(value)
|
||||||
|
|
@ -329,10 +340,7 @@ class CapBaseObject(object):
|
||||||
# raise ValueError.
|
# raise ValueError.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not isinstance(value, attr.types) and \
|
if not isinstance(value, attr.types) and not empty(value):
|
||||||
value is not NotLoaded and \
|
|
||||||
value is not NotAvailable and \
|
|
||||||
value is not None:
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'Value for "%s" needs to be of type %r, not %r' % (
|
'Value for "%s" needs to be of type %r, not %r' % (
|
||||||
name, attr.types, type(value)))
|
name, attr.types, type(value)))
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
from prettytable import PrettyTable
|
from prettytable import PrettyTable
|
||||||
|
|
||||||
from weboob.capabilities.base import NotLoaded, NotAvailable
|
from weboob.capabilities.base import empty
|
||||||
|
|
||||||
from .iformatter import IFormatter
|
from .iformatter import IFormatter
|
||||||
|
|
||||||
|
|
@ -52,7 +52,7 @@ class TableFormatter(IFormatter):
|
||||||
for i in xrange(len(self.keys)):
|
for i in xrange(len(self.keys)):
|
||||||
available = False
|
available = False
|
||||||
for line in self.queue:
|
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
|
available = True
|
||||||
break
|
break
|
||||||
if available:
|
if available:
|
||||||
|
|
@ -63,7 +63,7 @@ class TableFormatter(IFormatter):
|
||||||
s = ''
|
s = ''
|
||||||
if self.display_header and self.header:
|
if self.display_header and self.header:
|
||||||
if self.HTML:
|
if self.HTML:
|
||||||
s+= '<p>%s</p>' % self.header
|
s += '<p>%s</p>' % self.header
|
||||||
else:
|
else:
|
||||||
s += self.header
|
s += self.header
|
||||||
s += "\n"
|
s += "\n"
|
||||||
|
|
@ -92,5 +92,6 @@ class TableFormatter(IFormatter):
|
||||||
def set_header(self, string):
|
def set_header(self, string):
|
||||||
self.header = string
|
self.header = string
|
||||||
|
|
||||||
|
|
||||||
class HTMLTableFormatter(TableFormatter):
|
class HTMLTableFormatter(TableFormatter):
|
||||||
HTML = True
|
HTML = True
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue