<s> from __future__ import unicode_literals <EOL> from coreapi . codecs . base import BaseCodec <EOL> from coreapi . compat import string_types <EOL> from coreapi . document import Document , Link , Array , Object , Error <EOL> import click <EOL> import json <EOL> def _colorize_document ( text ) : <EOL> return click . style ( text , fg = '<STR_LIT>' ) <EOL> def _colorize_error ( text ) : <EOL> return click . style ( text , fg = '<STR_LIT>' ) <EOL> def _colorize_keys ( text ) : <EOL> return click . style ( text , fg = '<STR_LIT>' ) <EOL> def _to_plaintext ( node , indent = <NUM_LIT:0> , base_url = None , colorize = False , extra_offset = None ) : <EOL> colorize_document = _colorize_document if colorize else lambda x : x <EOL> colorize_error = _colorize_error if colorize else lambda x : x <EOL> colorize_keys = _colorize_keys if colorize else lambda x : x <EOL> if isinstance ( node , Document ) : <EOL> head_indent = '<STR_LIT:U+0020>' * indent <EOL> body_indent = '<STR_LIT:U+0020>' * ( indent + <NUM_LIT:1> ) <EOL> body = '<STR_LIT:\n>' . join ( [ <EOL> body_indent + colorize_keys ( str ( key ) + '<STR_LIT>' ) + <EOL> _to_plaintext ( value , indent + <NUM_LIT:1> , base_url = base_url , colorize = colorize , extra_offset = len ( str ( key ) ) ) <EOL> for key , value in node . data . items ( ) <EOL> ] + [ <EOL> body_indent + colorize_keys ( str ( key ) + '<STR_LIT:(>' ) + <EOL> _fields_to_plaintext ( value , colorize = colorize ) + colorize_keys ( '<STR_LIT:)>' ) <EOL> for key , value in node . links . items ( ) <EOL> ] ) <EOL> head = colorize_document ( '<STR_LIT>' % ( <EOL> node . title . strip ( ) or '<STR_LIT>' , <EOL> json . dumps ( node . url ) <EOL> ) ) <EOL> return head if ( not body ) else head + '<STR_LIT:\n>' + body <EOL> elif isinstance ( node , Object ) : <EOL> head_indent = '<STR_LIT:U+0020>' * indent <EOL> body_indent = '<STR_LIT:U+0020>' * ( indent + <NUM_LIT:1> ) <EOL> body = '<STR_LIT:\n>' . join ( [ <EOL> body_indent + colorize_keys ( str ( key ) ) + '<STR_LIT>' + <EOL> _to_plaintext ( value , indent + <NUM_LIT:1> , base_url = base_url , colorize = colorize , extra_offset = len ( str ( key ) ) ) <EOL> for key , value in node . data . items ( ) <EOL> ] + [ <EOL> body_indent + colorize_keys ( str ( key ) + '<STR_LIT:(>' ) + <EOL> _fields_to_plaintext ( value , colorize = colorize ) + colorize_keys ( '<STR_LIT:)>' ) <EOL> for key , value in node . links . items ( ) <EOL> ] ) <EOL> return '<STR_LIT:{}>' if ( not body ) else '<STR_LIT>' + body + '<STR_LIT:\n>' + head_indent + '<STR_LIT:}>' <EOL> if isinstance ( node , Error ) : <EOL> head_indent = '<STR_LIT:U+0020>' * indent <EOL> body_indent = '<STR_LIT:U+0020>' * ( indent + <NUM_LIT:1> ) <EOL> body = '<STR_LIT:\n>' . join ( [ <EOL> body_indent + colorize_keys ( str ( key ) + '<STR_LIT>' ) + <EOL> _to_plaintext ( value , indent + <NUM_LIT:1> , base_url = base_url , colorize = colorize , extra_offset = len ( str ( key ) ) ) <EOL> for key , value in node . items ( ) <EOL> ] ) <EOL> head = colorize_error ( '<STR_LIT>' % node . title . strip ( ) if node . title else '<STR_LIT>' ) <EOL> return head if ( not body ) else head + '<STR_LIT:\n>' + body <EOL> elif isinstance ( node , Array ) : <EOL> head_indent = '<STR_LIT:U+0020>' * indent <EOL> body_indent = '<STR_LIT:U+0020>' * ( indent + <NUM_LIT:1> ) <EOL> body = '<STR_LIT>' . join ( [ <EOL> body_indent + _to_plaintext ( value , indent + <NUM_LIT:1> , base_url = base_url , colorize = colorize ) <EOL> for value in node <EOL> ] ) <EOL> return '<STR_LIT>' if ( not body ) else '<STR_LIT>' + body + '<STR_LIT:\n>' + head_indent + '<STR_LIT:]>' <EOL> elif isinstance ( node , Link ) : <EOL> return ( <EOL> colorize_keys ( '<STR_LIT>' ) + <EOL> _fields_to_plaintext ( node , colorize = colorize ) + <EOL> colorize_keys ( '<STR_LIT:)>' ) <EOL> ) <EOL> if isinstance ( node , string_types ) and ( extra_offset is not None ) and ( '<STR_LIT:\n>' in node ) : <EOL> text = json . dumps ( node ) <EOL> spacing = ( '<STR_LIT:U+0020>' * indent ) + ( '<STR_LIT:U+0020>' * extra_offset ) + '<STR_LIT:U+0020>' <EOL> return text . replace ( '<STR_LIT>' , '<STR_LIT:\n>' + spacing ) <EOL> return json . dumps ( node ) <EOL> def _fields_to_plaintext ( link , colorize = False ) : <EOL> colorize_keys = _colorize_keys if colorize else lambda x : x <EOL> return colorize_keys ( '<STR_LIT:U+002CU+0020>' ) . join ( [ <EOL> field . name for field in link . fields if field . required <EOL> ] + [ <EOL> '<STR_LIT>' % field . name for field in link . fields if not field . required <EOL> ] ) <EOL> class CoreTextCodec ( BaseCodec ) : <EOL> """<STR_LIT>""" <EOL> media_type = '<STR_LIT>' <EOL> def dump ( self , node , colorize = False , ** kwargs ) : <EOL> return _to_plaintext ( node , colorize = colorize ) </s>
