Ejemplo n.º 1
0
 def test_preview_text_defaults(self):
     t = preview_text(self.TEXT)
     self.assertEqual(t, '''    {
     "replaces": "0.9.1",
     "hash": "f082b775fc70cf973d91c31ddbbcf36f21088377",
     "depends": "conda",
     "components": [''')
Ejemplo n.º 2
0
   def test_preview_max_chars(self):
       pass
       t = preview_text('united states of america\ncanada\nrepublic of congo\nsouth africa\n'
                        'democratic republic of tao tom\nunited federation of russia\n'
                        'united kingdom of great britain and northern ireland',
                        indent=2, max_lines=5, center_line=4, max_chars=100)
       self.assertEqual(t, '''  canada
 republic of congo
 south africa
 democratic republic...
 united federation o...''')
Ejemplo n.º 3
0
 def test_preview_surround(self):
     t = preview_text('one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine',
                      indent=2, max_lines=3, center_line=4, highlight=4, colored=False)
     self.assertEqual(t, '  three\n>>  four  <<\n  five')
Ejemplo n.º 4
0
 def test_preview_highlight(self):
     text = 'one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine'
     t1 = preview_text(text, indent=2, max_lines=3, center_line=4, highlight=4)
     t2 = preview_text(text, indent=2, max_lines=3, center_line=4, highlight=4, colored=True)
     self.assertEqual(t1, '  three\n\x1b[30m\x1b[103m  four\x1b[0m\n  five')
     self.assertEqual(t1, t2)
Ejemplo n.º 5
0
 def test_preview_prefix_suffix(self):
     t = preview_text('one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine',
                      indent=2, max_lines=3, center_line=4, prefix='  PREFIX\n', suffix='\n  SUFFIX')
     self.assertEqual(t, '  PREFIX\n  three\n  four\n  five\n  SUFFIX')
Ejemplo n.º 6
0
 def test_preview_center_line(self):
     t = preview_text('one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine', indent=2, max_lines=3, center_line=4)
     self.assertEqual(t, '  three\n  four\n  five')
Ejemplo n.º 7
0
 def test_preview_text_dedent(self):
     t = preview_text('  a\n  b', indent=0, dedent=True)
     self.assertEqual(t, 'a\nb')
Ejemplo n.º 8
0
def _loads(text=None,
           fp=None,
           template=None,
           colored=None,
           comments=None,
           **kwargs):
    """
    Deserialize `text` (a `str` or `unicode` instance containing a JSON
    document supporting template references `{$.key}`) to a Python object.

    :param text: serialized JSON string
    :param template: (optional) None, str, dict, list, io.IOBase - Causes template values to be sourced form this object
    :param kwargs: all the arguments that `json.loads <http://docs.python.org/
                   2/library/json.html#json.loads>`_ accepts.
    :returns: dict or list.
    """
    kwargs['object_pairs_hook'] = collections.OrderedDict
    template = _read_template(template, **kwargs)

    if colored is None:
        colored = _DEFAULT_COLORED
    if comments is None:
        comments = _DEFAULT_COMMENTS

    if isinstance(text, (bytes, bytearray)):
        text = text.decode(detect_encoding(text), 'surrogatepass')

    if fp is not None:
        fp.seek(0)
        text = fp.read()

        fp_desc = getattr(fp, 'name', type(fp).__name__)
        if colored:
            fp_desc = highlight_text(fp_desc,
                                     fg=Fore.LIGHTBLUE_EX,
                                     bg=Back.RESET)
        obj_desc = 'JSON File/Stream ({})'.format(fp_desc)
    else:
        obj_desc = 'JSON String'
        # test for empty string to preserve similarity to built-in `json` module
        if not text.strip():
            raise json.JSONDecodeError(
                'Expecting value - cannot parse empty {}'.format(obj_desc),
                text, 0)

    try:
        if comments:
            datum = _commentjson_loads(text, **kwargs)
        else:
            datum = json.loads(text, **kwargs)

    except lark.exceptions.UnexpectedEOF as e:
        last_line_pos = len(text) + 1
        for i in range(100):
            last_line_pos = text.rfind('\n', 0, last_line_pos)

            if text[last_line_pos:].strip() or last_line_pos < 1:
                break

        lineno = text.count('\n', 0, last_line_pos) + 1

        sample = preview_text(text,
                              max_chars=_PREVIEW_CHARS,
                              center_line=lineno,
                              highlight=lineno,
                              prefix='NEAR:\n',
                              suffix='\n...\n\nTOTAL LENGTH: {} chars'.format(
                                  len(text)),
                              colored=colored)
        error_desc = 'Error Decoding {} - Incomplete Document'.format(obj_desc)
        msg = '{}\n{}\n\nError at'.format(error_desc, sample)
        raise json.JSONDecodeError(msg, doc=text, pos=last_line_pos)

    except (lark.UnexpectedCharacters, lark.UnexpectedToken) as e:
        # handle exception from Lark parser (used for commentjson)
        sample = preview_text(text,
                              max_chars=_PREVIEW_CHARS,
                              center_line=e.line,
                              highlight=e.line,
                              prefix='NEAR:\n',
                              suffix='\n...\n\nTOTAL LENGTH: {} chars'.format(
                                  len(text)),
                              colored=colored)
        error_desc = 'Error Decoding {} - Unexpected Character\n{}'.format(
            obj_desc, e.get_context(text))
        msg = '{}\n{}\n\nError at'.format(error_desc, sample)
        raise json.JSONDecodeError(msg, doc=text, pos=e.pos_in_stream)

    except json.JSONDecodeError as e:
        sample = preview_text(text,
                              max_chars=_PREVIEW_CHARS,
                              center_line=e.lineno,
                              highlight=e.lineno,
                              prefix='NEAR:\n',
                              suffix='\n...\n\nTOTAL LENGTH: {} chars'.format(
                                  len(text)),
                              colored=colored)
        msg = '{} in {}\n{}\n\nError at'.format(e.msg, obj_desc, sample)

        # raise an exception similar to the built-in
        raise json.JSONDecodeError(msg, doc=e.doc, pos=e.pos) from None
    if isinstance(datum, dict):
        return TplDict(datum, template=template)
    elif isinstance(datum, list):
        return TplList(datum, template=template)
    else:
        return datum