Reconcile the two previous commits (json/to_dict)
Also fix some to_dict move things that were incomplete. And some PEP8 fixes. closes #1060 closes #1061
This commit is contained in:
parent
1b47455225
commit
a6fbcbaec7
3 changed files with 40 additions and 16 deletions
|
|
@ -62,7 +62,8 @@ class FieldNotFound(Exception):
|
||||||
"""
|
"""
|
||||||
def __init__(self, obj, field):
|
def __init__(self, obj, field):
|
||||||
Exception.__init__(self,
|
Exception.__init__(self,
|
||||||
u'Field "%s" not found for object %s' % (field, obj))
|
u'Field "%s" not found for object %s' % (field, obj))
|
||||||
|
|
||||||
|
|
||||||
class ConversionWarning(UserWarning):
|
class ConversionWarning(UserWarning):
|
||||||
"""
|
"""
|
||||||
|
|
@ -71,12 +72,14 @@ class ConversionWarning(UserWarning):
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AttributeCreationWarning(UserWarning):
|
class AttributeCreationWarning(UserWarning):
|
||||||
"""
|
"""
|
||||||
A non-field attribute has been created with a name not
|
A non-field attribute has been created with a name not
|
||||||
prefixed with a _.
|
prefixed with a _.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class NotAvailableMeta(type):
|
class NotAvailableMeta(type):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return unicode(self).decode('utf-8')
|
return unicode(self).decode('utf-8')
|
||||||
|
|
@ -158,6 +161,7 @@ class Field(object):
|
||||||
"""
|
"""
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
class IntField(Field):
|
class IntField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`int` and :class:`long` types.
|
A field which accepts only :class:`int` and :class:`long` types.
|
||||||
|
|
@ -168,6 +172,7 @@ class IntField(Field):
|
||||||
def convert(self, value):
|
def convert(self, value):
|
||||||
return int(value)
|
return int(value)
|
||||||
|
|
||||||
|
|
||||||
class DecimalField(Field):
|
class DecimalField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`decimal` type.
|
A field which accepts only :class:`decimal` type.
|
||||||
|
|
@ -180,6 +185,7 @@ class DecimalField(Field):
|
||||||
return value
|
return value
|
||||||
return Decimal(value)
|
return Decimal(value)
|
||||||
|
|
||||||
|
|
||||||
class FloatField(Field):
|
class FloatField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`float` type.
|
A field which accepts only :class:`float` type.
|
||||||
|
|
@ -190,6 +196,7 @@ class FloatField(Field):
|
||||||
def convert(self, value):
|
def convert(self, value):
|
||||||
return float(value)
|
return float(value)
|
||||||
|
|
||||||
|
|
||||||
class StringField(Field):
|
class StringField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`unicode` strings.
|
A field which accepts only :class:`unicode` strings.
|
||||||
|
|
@ -200,6 +207,7 @@ class StringField(Field):
|
||||||
def convert(self, value):
|
def convert(self, value):
|
||||||
return to_unicode(value)
|
return to_unicode(value)
|
||||||
|
|
||||||
|
|
||||||
class BytesField(Field):
|
class BytesField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`str` strings.
|
A field which accepts only :class:`str` strings.
|
||||||
|
|
@ -212,6 +220,7 @@ class BytesField(Field):
|
||||||
value = value.encode('utf-8')
|
value = value.encode('utf-8')
|
||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
class DateField(Field):
|
class DateField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`datetime.date` and :class:`datetime.datetime` types.
|
A field which accepts only :class:`datetime.date` and :class:`datetime.datetime` types.
|
||||||
|
|
@ -219,6 +228,7 @@ class DateField(Field):
|
||||||
def __init__(self, doc, **kwargs):
|
def __init__(self, doc, **kwargs):
|
||||||
Field.__init__(self, doc, datetime.date, datetime.datetime, **kwargs)
|
Field.__init__(self, doc, datetime.date, datetime.datetime, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class TimeField(Field):
|
class TimeField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`datetime.time` and :class:`datetime.time` types.
|
A field which accepts only :class:`datetime.time` and :class:`datetime.time` types.
|
||||||
|
|
@ -226,6 +236,7 @@ class TimeField(Field):
|
||||||
def __init__(self, doc, **kwargs):
|
def __init__(self, doc, **kwargs):
|
||||||
Field.__init__(self, doc, datetime.time, datetime.datetime, **kwargs)
|
Field.__init__(self, doc, datetime.time, datetime.datetime, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class DeltaField(Field):
|
class DeltaField(Field):
|
||||||
"""
|
"""
|
||||||
A field which accepts only :class:`datetime.timedelta` type.
|
A field which accepts only :class:`datetime.timedelta` type.
|
||||||
|
|
@ -233,6 +244,7 @@ class DeltaField(Field):
|
||||||
def __init__(self, doc, **kwargs):
|
def __init__(self, doc, **kwargs):
|
||||||
Field.__init__(self, doc, datetime.timedelta, **kwargs)
|
Field.__init__(self, doc, datetime.timedelta, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class _CapBaseObjectMeta(type):
|
class _CapBaseObjectMeta(type):
|
||||||
def __new__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
|
fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
|
||||||
|
|
@ -254,6 +266,7 @@ class _CapBaseObjectMeta(type):
|
||||||
new_class.__doc__ += '\n:var %s: %s' % (name, doc)
|
new_class.__doc__ += '\n:var %s: %s' % (name, doc)
|
||||||
return new_class
|
return new_class
|
||||||
|
|
||||||
|
|
||||||
class CapBaseObject(object):
|
class CapBaseObject(object):
|
||||||
"""
|
"""
|
||||||
This is the base class for a capability object.
|
This is the base class for a capability object.
|
||||||
|
|
@ -370,7 +383,8 @@ class CapBaseObject(object):
|
||||||
# If the value was converted
|
# If the value was converted
|
||||||
if nvalue is not value:
|
if nvalue is not value:
|
||||||
warnings.warn('Value %s was converted from %s to %s' %
|
warnings.warn('Value %s was converted from %s to %s' %
|
||||||
(name, type(value), type(nvalue)), ConversionWarning, stacklevel=2)
|
(name, type(value), type(nvalue)),
|
||||||
|
ConversionWarning, stacklevel=2)
|
||||||
value = nvalue
|
value = nvalue
|
||||||
except Exception:
|
except Exception:
|
||||||
# error during conversion, it will probably not
|
# error during conversion, it will probably not
|
||||||
|
|
@ -399,5 +413,3 @@ class CapBaseObject(object):
|
||||||
|
|
||||||
fields_iterator = self.iter_fields()
|
fields_iterator = self.iter_fields()
|
||||||
return OrderedDict(iter_decorate(fields_iterator))
|
return OrderedDict(iter_decorate(fields_iterator))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,16 @@ if sys.platform == 'win32':
|
||||||
import WConio
|
import WConio
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import tty, termios
|
import tty
|
||||||
|
import termios
|
||||||
except ImportError:
|
except ImportError:
|
||||||
PROMPT = '--Press return to continue--'
|
PROMPT = '--Press return to continue--'
|
||||||
|
|
||||||
def readch(): # NOQA
|
def readch(): # NOQA
|
||||||
return sys.stdin.readline()
|
return sys.stdin.readline()
|
||||||
else:
|
else:
|
||||||
PROMPT = '--Press a key to continue--'
|
PROMPT = '--Press a key to continue--'
|
||||||
|
|
||||||
def readch():
|
def readch():
|
||||||
fd = sys.stdin.fileno()
|
fd = sys.stdin.fileno()
|
||||||
old_settings = termios.tcgetattr(fd)
|
old_settings = termios.tcgetattr(fd)
|
||||||
|
|
@ -49,6 +52,7 @@ else:
|
||||||
|
|
||||||
from weboob.capabilities.base import CapBaseObject
|
from weboob.capabilities.base import CapBaseObject
|
||||||
from weboob.tools.application.console import ConsoleApplication
|
from weboob.tools.application.console import ConsoleApplication
|
||||||
|
from weboob.tools.ordereddict import OrderedDict
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['IFormatter', 'MandatoryFieldsNotFound']
|
__all__ = ['IFormatter', 'MandatoryFieldsNotFound']
|
||||||
|
|
@ -90,7 +94,9 @@ class IFormatter(object):
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
self.termrows = WConio.gettextinfo()[8]
|
self.termrows = WConio.gettextinfo()[8]
|
||||||
else:
|
else:
|
||||||
self.termrows = int(subprocess.Popen('stty size', shell=True, stdout=subprocess.PIPE).communicate()[0].split()[0])
|
self.termrows = int(
|
||||||
|
subprocess.Popen('stty size', shell=True, stdout=subprocess.PIPE).communicate()[0].split()[0]
|
||||||
|
)
|
||||||
|
|
||||||
def output(self, formatted):
|
def output(self, formatted):
|
||||||
if self.outfile != sys.stdout:
|
if self.outfile != sys.stdout:
|
||||||
|
|
@ -143,7 +149,10 @@ class IFormatter(object):
|
||||||
|
|
||||||
formatted = self.format_obj(obj, alias)
|
formatted = self.format_obj(obj, alias)
|
||||||
else:
|
else:
|
||||||
obj = self.to_dict(obj)
|
try:
|
||||||
|
return OrderedDict(obj)
|
||||||
|
except ValueError:
|
||||||
|
raise TypeError('Please give a CapBaseObject or a dict')
|
||||||
|
|
||||||
if selected_fields is not None and not '*' in selected_fields:
|
if selected_fields is not None and not '*' in selected_fields:
|
||||||
obj = obj.copy()
|
obj = obj.copy()
|
||||||
|
|
|
||||||
|
|
@ -18,22 +18,25 @@
|
||||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
from .iformatter import IFormatter
|
|
||||||
from weboob.tools.json import json
|
from weboob.tools.json import json
|
||||||
|
|
||||||
|
from .iformatter import IFormatter
|
||||||
|
|
||||||
__all__ = ['JsonFormatter']
|
__all__ = ['JsonFormatter']
|
||||||
|
|
||||||
|
|
||||||
class Encoder(json.JSONEncoder):
|
class Encoder(json.JSONEncoder):
|
||||||
"generic weboob objects encoder"
|
"generic weboob object encoder"
|
||||||
|
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
try :
|
try:
|
||||||
return json.JSONEncoder.default(self, obj)
|
return json.JSONEncoder.default(self, obj)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
i = IFormatter()
|
try:
|
||||||
try :
|
dct = obj.to_dict()
|
||||||
dico = i.to_dict(obj)
|
except AttributeError:
|
||||||
except TypeError:
|
|
||||||
return str(obj)
|
return str(obj)
|
||||||
for z in dico:
|
for z in dct:
|
||||||
return z
|
return z
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue