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
|
|
@ -27,13 +27,16 @@ if sys.platform == 'win32':
|
|||
import WConio
|
||||
|
||||
try:
|
||||
import tty, termios
|
||||
import tty
|
||||
import termios
|
||||
except ImportError:
|
||||
PROMPT = '--Press return to continue--'
|
||||
|
||||
def readch(): # NOQA
|
||||
return sys.stdin.readline()
|
||||
else:
|
||||
PROMPT = '--Press a key to continue--'
|
||||
|
||||
def readch():
|
||||
fd = sys.stdin.fileno()
|
||||
old_settings = termios.tcgetattr(fd)
|
||||
|
|
@ -49,6 +52,7 @@ else:
|
|||
|
||||
from weboob.capabilities.base import CapBaseObject
|
||||
from weboob.tools.application.console import ConsoleApplication
|
||||
from weboob.tools.ordereddict import OrderedDict
|
||||
|
||||
|
||||
__all__ = ['IFormatter', 'MandatoryFieldsNotFound']
|
||||
|
|
@ -90,7 +94,9 @@ class IFormatter(object):
|
|||
if sys.platform == 'win32':
|
||||
self.termrows = WConio.gettextinfo()[8]
|
||||
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):
|
||||
if self.outfile != sys.stdout:
|
||||
|
|
@ -143,7 +149,10 @@ class IFormatter(object):
|
|||
|
||||
formatted = self.format_obj(obj, alias)
|
||||
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:
|
||||
obj = obj.copy()
|
||||
|
|
@ -184,7 +193,7 @@ class IFormatter(object):
|
|||
"""
|
||||
return NotImplementedError()
|
||||
|
||||
|
||||
|
||||
class PrettyFormatter(IFormatter):
|
||||
def format_obj(self, obj, alias):
|
||||
title = self.get_title(obj)
|
||||
|
|
|
|||
|
|
@ -18,22 +18,25 @@
|
|||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from .iformatter import IFormatter
|
||||
from weboob.tools.json import json
|
||||
|
||||
from .iformatter import IFormatter
|
||||
|
||||
__all__ = ['JsonFormatter']
|
||||
|
||||
|
||||
class Encoder(json.JSONEncoder):
|
||||
"generic weboob objects encoder"
|
||||
"generic weboob object encoder"
|
||||
|
||||
def default(self, obj):
|
||||
try :
|
||||
try:
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
except TypeError:
|
||||
i = IFormatter()
|
||||
try :
|
||||
dico = i.to_dict(obj)
|
||||
except TypeError:
|
||||
try:
|
||||
dct = obj.to_dict()
|
||||
except AttributeError:
|
||||
return str(obj)
|
||||
for z in dico:
|
||||
for z in dct:
|
||||
return z
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue