예제 #1
0
    def test_code_out_err(self):

        subproc = os.path.join(this_base, 'subproc.py')

        returncode, out, err = k3proc.command('python', subproc, '222')

        self.assertEqual(222, returncode)
        self.assertEqual('out-1\nout-2\n', out)
        self.assertEqual('err-1\nerr-2\n', err)

        try:
            returncode, out, err = k3proc.command_ex('python', subproc, '222')
        except k3proc.CalledProcessError as e:
            self.assertEqual(222, e.returncode)
            self.assertEqual('out-1\nout-2\n', e.stdout)
            self.assertEqual('out-1\nout-2\n'.splitlines(), e.out)
            self.assertEqual('err-1\nerr-2\n', e.stderr)
            self.assertEqual('err-1\nerr-2\n'.splitlines(), e.err)
            self.assertEqual('python', e.cmd[0])
            self.assertTrue(e.cmd[1].endswith('subproc.py'))
            self.assertEqual('222', e.cmd[2])
            self.assertEqual({}, e.options)
        else:
            self.fail('expect k3proc.CalledProcessError to be raised')

        returncode, out, err = k3proc.command_ex('python', subproc, '0')
        self.assertEqual(0, returncode)
        self.assertEqual('out-1\nout-2\n', out)
        self.assertEqual('err-1\nerr-2\n', err)

        returncode, out, err = k3proc.command('python', subproc, '0')

        self.assertEqual(0, returncode)
        self.assertEqual('out-1\nout-2\n', out)
        self.assertEqual('err-1\nerr-2\n', err)
예제 #2
0
def mdtable_to_barehtml(md):
    '''
    Build markdown table into html without style.

    Args:
        md(str): markdown source.

    Returns:
        str of html
    '''

    # A table with wide column will cause pandoc to produce ``colgroup`` tag, which is not recognized by zhihu.
    # Reported in:
    #      https://github.com/drmingdrmer/md2zhihu/issues/22
    #
    # Thus we have to set a very big rendering window to disable this behavior
    #      https://github.com/jgm/pandoc/issues/2574

    _, html, _ = k3proc.command_ex(
        "pandoc",
        "-f",
        "markdown",
        "-t",
        "html",
        "--column",
        "100000",
        input=md,
    )
    lines = html.strip().split('\n')
    lines = [
        x for x in lines
        if x not in ('<thead>', '</thead>', '<tbody>', '</tbody>')
    ]

    return '\n'.join(lines)
예제 #3
0
def mermaid_to_svg(mmd):
    """
    Render mermaid to svg.
    See: https://mermaid-js.github.io/mermaid/#

    Requires:
        npm install @mermaid-js/mermaid-cli
    """

    with tempfile.TemporaryDirectory() as tdir:
        output_path = os.path.join(tdir, "mmd.svg")
        k3proc.command_ex(
            "mmdc",
            "-o",
            output_path,
            input=mmd,
        )
        return fread(output_path)
예제 #4
0
def html_to_image(tblhtml, imgdir):
    imgdir = os.path.abspath(imgdir)
    tmpdir = os.path.abspath("tmp")
    k3proc.shell_script('rm *.png *.html', cwd=tmpdir)

    tblmd5 = hashlib.md5(tobytes(tblhtml)).hexdigest()
    fn = "tbl_" + tblmd5 + ".png"

    if os.path.exists(os.path.join(imgdir, fn)):
        return fn

    html = tblhtml_start + tblhtml + tblhtml_end
    with open(jp(tmpdir, "tbl.html"), 'w') as f:
        f.write(html)

    dd("make html at:", jp(tmpdir, "tbl.html"))
    k3proc.command_ex(
        "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
        "--headless",
        "--screenshot",
        "--window-size=1000,2000",
        "--default-background-color=0",
        "tbl.html",
        cwd=tmpdir,
    )

    # TODO extend to match page width
    dd("crop to visible area")
    k3proc.command_ex(
        "convert",
        "screenshot.png",
        "-trim",
        "+repage",
        jp(imgdir, fn),
        cwd=tmpdir,
    )

    k3proc.shell_script('rm *.png *.html', cwd=tmpdir)
    return fn
예제 #5
0
def graphviz_to_img(gv, typ):
    """
    Render graphviz to svg.

    Requires:
        brew install graphviz
    """

    _, out, _ = k3proc.command_ex(
        "dot",
        "-T" + typ,
        input=to_bytes(gv),
        text=False,
    )
    return out
예제 #6
0
def cmp_image(want, got):

    da = skimage.io.imread(want)
    db = skimage.io.imread(got)
    if da.shape != db.shape:
        k3proc.command_ex(
            'convert',
            # height then width
            '-resize',
            '%dx%d!' % (da.shape[1], da.shape[0]),
            got,
            got)
        db = skimage.io.imread(got)

    img1 = skimage.img_as_float(da)
    img2 = skimage.img_as_float(db)

    print("img1:-------------", want)
    print(img1.shape)
    print("img2:-------------", got)
    print(img2.shape)

    p = ssim(img1, img2, multichannel=True)
    return p
예제 #7
0
def md_to_html(md):
    '''
    Build markdown source into html.

    Args:
        md(str): markdown source.

    Returns:
        str of html
    '''

    _, html, _ = k3proc.command_ex(
        "pandoc",
        "-f",
        "markdown",
        "-t",
        "html",
        input=md,
    )

    return html_style + html
예제 #8
0
def render_to_img(mime, input, typ, width=1000, height=2000, asset_base=None):
    '''
    Render content that is renderable in chrome to image.
    Such as html, svg etc into image.
    It uses a headless chrome to render the page.
    Requirement: Chrome, imagemagick

    Args:
        mime(str): a full mime type such as ``image/jpeg`` or a shortcut ``jpg``.

        input(str): content of the input, such as jpeg data or svg source file.

        typ(string): specifies output image type such as "png", "jpg"

        width(int): specifies the window width to render a page. Default 1000.

        height(int): specifies the window height to render a page. Default 2000.

        asset_base(str): specifies the path to assets dir. E.g. the image base path in a html page.

    Returns:
        bytes of the png data
    '''

    if 'html' in mime:
        input = r'<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' + input

        # Only for html page we need to add asset_base url.
        if asset_base is not None:
            input = r'<base href="file://{}/">'.format(asset_base) + input

    m = mimetypes.get(mime) or mime

    # Use the input mime type as temp page suffix.
    suffix = mime

    # If the input ``mime`` is a full mime type such as `application/xhtml+xml`,
    # convert it back to file suffix.
    for k, v in mimetypes.items():
        if v == m:
            suffix = k
            break

    chrome = 'google-chrome'
    if sys.platform == 'darwin':
        # mac
        chrome = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

    with tempfile.TemporaryDirectory() as tdir:
        # Write page content into a temp file.
        # Since chrome does not recoganize the `<base>` tag encoded in a
        # data-uri.
        fn = os.path.join(tdir, 'xxx.' + suffix)
        flags = 'w'
        if isinstance(input, bytes):
            flags = 'wb'
        with open(fn, flags) as f:
            f.write(input)

        k3proc.command_ex(
            chrome,
            "--headless",
            "--screenshot",
            "--window-size={},{}".format(width, height),
            "--default-background-color=0",
            fn,
            cwd=tdir,
        )

        if typ == 'png':
            moreargs = []
        else:
            # flatten alpha channel
            moreargs = ['-background', 'white', '-flatten', '-alpha', 'off']

        # crop to visible area
        _, out, _ = k3proc.command_ex(
            "convert",
            pjoin(tdir, "screenshot.png"),
            "-trim",
            "+repage",
            *moreargs,
            typ + ":-",
            text=False,
        )

    return out