Exemple #1
0
    def write(self, output, resources, **kw):
        """
        Consume and write Jinja output.

        See base for more...
        """
        io.unicode_std_stream().write(output)
Exemple #2
0
def test_UnicodeStdStream_nowrap():
    # If we replace stdout with a StringIO, it shouldn't get wrapped.
    orig_stdout = sys.stdout
    sys.stdout = StringIO()
    try:
        nt.assert_is(unicode_std_stream(), sys.stdout)
        assert not sys.stdout.closed
    finally:
        sys.stdout = orig_stdout
Exemple #3
0
def test_UnicodeStdStream_nowrap():
    # If we replace stdout with a StringIO, it shouldn't get wrapped.
    orig_stdout = sys.stdout
    sys.stdout = StringIO()
    try:
        nt.assert_is(unicode_std_stream(), sys.stdout)
        assert not sys.stdout.closed
    finally:
        sys.stdout = orig_stdout
Exemple #4
0
def test_UnicodeStdStream():
    # Test wrapping a bytes-level stdout
    if PY3:
        stdoutb = stdlib_io.BytesIO()
        stdout = stdlib_io.TextIOWrapper(stdoutb, encoding="ascii")
    else:
        stdout = stdoutb = stdlib_io.BytesIO()

    orig_stdout = sys.stdout
    sys.stdout = stdout
    try:
        sample = u"@łe¶ŧ←"
        unicode_std_stream().write(sample)

        output = stdoutb.getvalue().decode("utf-8")
        nt.assert_equal(output, sample)
        assert not stdout.closed
    finally:
        sys.stdout = orig_stdout
Exemple #5
0
def test_UnicodeStdStream():
    # Test wrapping a bytes-level stdout
    if PY3:
        stdoutb = stdlib_io.BytesIO()
        stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii')
    else:
        stdout = stdoutb = stdlib_io.BytesIO()

    orig_stdout = sys.stdout
    sys.stdout = stdout
    try:
        sample = u"@łe¶ŧ←"
        unicode_std_stream().write(sample)

        output = stdoutb.getvalue().decode('utf-8')
        nt.assert_equal(output, sample)
        assert not stdout.closed
    finally:
        sys.stdout = orig_stdout
Exemple #6
0
def main(args, help=''):
    if args.version:
        print(__version__)
        sys.exit()

    if args.examples:
        print(examples)
        sys.exit()

    # if no stdin and no input file
    if args.input_file == '-' and sys.stdin.isatty():
        sys.stdout.write(help)
        sys.exit()

    elif args.input_file == '-':
        input_file = sys.stdin

    elif args.input_file != '-':
        input_file = io.open(args.input_file, 'r', encoding='utf-8')

    else:
        sys.exit('malformed input')

    # pre-process markdown by using knitr on it
    if args.knit:
        knitr = Knitr()
        input_file = knitr.knit(input_file, opts_chunk=args.knit)

    if args.rmagic:
        args.precode.append(r"%load_ext rpy2.ipython")

    if args.render:
        template_file = markdown_figure_template
    else:
        template_file = markdown_template

    template_file = args.template or template_file

    # reader and writer classes with args and kwargs to
    # instantiate with
    readers = {'notebook': nbformat,
               'markdown': MarkdownReader(precode='\n'.join(args.precode),
                                          magic=args.magic,
                                          match=args.match,
                                          caption_comments=args.render)
               }

    writers = {'notebook': nbformat,
               'markdown': MarkdownWriter(template_file,
                                          strip_outputs=args.strip_outputs)
               }

    informat = args.informat or ftdetect(input_file.name) or 'markdown'
    outformat = args.outformat or ftdetect(args.output) or 'notebook'

    if args.render:
        outformat = 'markdown'

    reader = readers[informat]
    writer = writers[outformat]

    with input_file as ip:
        notebook = reader.read(ip, as_version=4)

    if args.run:
        run(notebook)

    if args.strip_outputs:
        strip(notebook)

    output_ext = {'markdown': '.md',
                  'notebook': '.ipynb'}

    if not args.output and args.input_file != '-':
        # overwrite
        fout = os.path.splitext(args.input_file)[0] + output_ext[outformat]
        # grab the output here so we don't obliterate the file if
        # there is an error
        output = writer.writes(notebook)
        with io.open(fout, 'w', encoding='utf-8') as op:
            op.write(output)

    elif not args.output and args.input_file == '-':
        # overwrite error (input is stdin)
        sys.exit('Cannot overwrite with no input file given.')

    elif args.output == '-':
        # write stdout
        writer.write(notebook, unicode_std_stream('stdout'))

    elif args.output != '-':
        # write to filename
        with io.open(args.output, 'w', encoding='utf-8') as op:
            writer.write(notebook, op)
Exemple #7
0
def main(args, help=''):
    if args.version:
        print(__version__)
        sys.exit()

    if args.examples:
        print(examples)
        sys.exit()

    # if no stdin and no input file
    if args.input_file == '-' and sys.stdin.isatty():
        sys.stdout.write(help)
        sys.exit()

    elif args.input_file == '-':
        input_file = sys.stdin

    elif args.input_file != '-':
        input_file = io.open(args.input_file, 'r', encoding='utf-8')

    else:
        sys.exit('malformed input')

    # pre-process markdown by using knitr on it
    if args.knit:
        knitr = Knitr()
        input_file = knitr.knit(input_file, opts_chunk=args.knit)

    if args.rmagic:
        args.precode.append(r"%load_ext rpy2.ipython")

    if args.render:
        template_file = markdown_figure_template
    else:
        template_file = markdown_template

    template_file = args.template or template_file

    # reader and writer classes with args and kwargs to
    # instantiate with
    readers = {
        'notebook':
        nbformat,
        'markdown':
        MarkdownReader(precode='\n'.join(args.precode),
                       magic=args.magic,
                       match=args.match)
    }

    writers = {
        'notebook': nbformat,
        'markdown': MarkdownWriter(template_file,
                                   strip_outputs=args.strip_outputs)
    }

    informat = args.informat or ftdetect(input_file.name) or 'markdown'
    outformat = args.outformat or ftdetect(args.output) or 'notebook'

    if args.render:
        outformat = 'markdown'

    reader = readers[informat]
    writer = writers[outformat]

    with input_file as ip:
        notebook = reader.read(ip, as_version=4)

    if args.run:
        run(notebook)

    if args.strip_outputs:
        strip(notebook)

    output_ext = {'markdown': '.md', 'notebook': '.ipynb'}

    if not args.output and args.input_file != '-':
        # overwrite
        fout = os.path.splitext(args.input_file)[0] + output_ext[outformat]
        # grab the output here so we don't obliterate the file if
        # there is an error
        output = writer.writes(notebook)
        with io.open(fout, 'w', encoding='utf-8') as op:
            op.write(output)

    elif not args.output and args.input_file == '-':
        # overwrite error (input is stdin)
        sys.exit('Cannot overwrite with no input file given.')

    elif args.output == '-':
        # write stdout
        writer.write(notebook, unicode_std_stream('stdout'))

    elif args.output != '-':
        # write to filename
        with io.open(args.output, 'w', encoding='utf-8') as op:
            writer.write(notebook, op)