Exemple #1
0
def _debug_repr_model(model):
    cpd_code_list = [_debug_repr_cpd(cpd) for cpd in model.cpds]
    code_fmt = ut.codeblock(
        '''
        import numpy as np
        import pgmpy
        import pgmpy.inference
        import pgmpy.factors
        import pgmpy.models

        {cpds}

        cpd_list = {nodes}
        input_graph = {edges}
        model = pgmpy.models.BayesianModel(input_graph)
        model.add_cpds(*cpd_list)
        infr = pgmpy.inference.BeliefPropagation(model)
        ''')

    code = code_fmt.format(
        cpds='\n'.join(cpd_code_list),
        nodes=ut.repr2(sorted(model.nodes()), strvals=True),
        edges=ut.repr2(sorted(model.edges()), nl=1),
    )
    ut.print_code(code)
    ut.copy_text_to_clipboard(code)
Exemple #2
0
def _debug_repr_model(model):
    cpd_code_list = [_debug_repr_cpd(cpd) for cpd in model.cpds]
    code_fmt = ut.codeblock('''
        import numpy as np
        import pgmpy
        import pgmpy.inference
        import pgmpy.factors
        import pgmpy.models

        {cpds}

        cpd_list = {nodes}
        input_graph = {edges}
        model = pgmpy.models.BayesianModel(input_graph)
        model.add_cpds(*cpd_list)
        infr = pgmpy.inference.BeliefPropagation(model)
        ''')

    code = code_fmt.format(
        cpds='\n'.join(cpd_code_list),
        nodes=ut.repr2(sorted(model.nodes()), strvals=True),
        edges=ut.repr2(sorted(model.edges()), nl=1),
    )
    ut.print_code(code)
    ut.copy_text_to_clipboard(code)
Exemple #3
0
 def exec_(script):
     import utool as ut
     print('+**** exec %s script *******' % (script.type_))
     print('repo = %r' % (repo,))
     with ut.ChdirContext(repo.dpath):
         if script.is_fpath_valid():
             normbuild_flag = '--no-rmbuild'
             if ut.get_argflag(normbuild_flag):
                 ut.cmd(script.fpath + ' ' + normbuild_flag)
             else:
                 ut.cmd(script.fpath)
         else:
             if script.text is not None:
                 print('ABOUT TO EXECUTE')
                 ut.print_code(script.text, 'bash')
                 if ut.are_you_sure('execute above script?'):
                     from os.path import join
                     scriptdir = ut.ensure_app_resource_dir('utool',
                                                            'build_scripts')
                     script_path = join(scriptdir,
                                        'script_' + script.type_ + '_' +
                                        ut.hashstr27(script.text) + '.sh')
                     ut.writeto(script_path, script.text)
                     _ = ut.cmd('bash ', script_path)  # NOQA
             else:
                 print("CANT QUITE EXECUTE THIS YET")
                 ut.print_code(script.text, 'bash')
     #os.system(scriptname)
     print('L**** exec %s script *******' % (script.type_))
Exemple #4
0
            def exec_(script):
                import utool as ut

                print("+**** exec %s script *******" % (script.type_))
                print("repo = %r" % (repo,))
                with ut.ChdirContext(repo.dpath):
                    if script.is_fpath_valid():
                        normbuild_flag = "--no-rmbuild"
                        if ut.get_argflag(normbuild_flag):
                            ut.cmd(script.fpath + " " + normbuild_flag)
                        else:
                            ut.cmd(script.fpath)
                    else:
                        if script.text is not None:
                            print("ABOUT TO EXECUTE")
                            ut.print_code(script.text, "bash")
                            if ut.are_you_sure("execute above script?"):
                                from os.path import join

                                scriptdir = ut.ensure_app_resource_dir("utool", "build_scripts")
                                script_path = join(
                                    scriptdir, "script_" + script.type_ + "_" + ut.hashstr27(script.text) + ".sh"
                                )
                                ut.writeto(script_path, script.text)
                                _ = ut.cmd("bash ", script_path)  # NOQA
                        else:
                            print("CANT QUITE EXECUTE THIS YET")
                            ut.print_code(script.text, "bash")
                # os.system(scriptname)
                print("L**** exec %s script *******" % (script.type_))
Exemple #5
0
 def exec_(script):
     import utool as ut
     print('+**** exec %s script *******' % (script.type_))
     print('repo = %r' % (repo,))
     with ut.ChdirContext(repo.dpath):
         if script.is_fpath_valid():
             normbuild_flag = '--no-rmbuild'
             if ut.get_argflag(normbuild_flag):
                 ut.cmd(script.fpath + ' ' + normbuild_flag)
             else:
                 ut.cmd(script.fpath)
         else:
             if script.text is not None:
                 print('ABOUT TO EXECUTE')
                 ut.print_code(script.text, 'bash')
                 if ut.are_you_sure('execute above script?'):
                     from os.path import join
                     scriptdir = ut.ensure_app_resource_dir('utool',
                                                            'build_scripts')
                     script_path = join(scriptdir,
                                        'script_' + script.type_ + '_' +
                                        ut.hashstr27(script.text) + '.sh')
                     ut.writeto(script_path, script.text)
                     _ = ut.cmd('bash ', script_path)  # NOQA
             else:
                 print("CANT QUITE EXECUTE THIS YET")
                 ut.print_code(script.text, 'bash')
     #os.system(scriptname)
     print('L**** exec %s script *******' % (script.type_))
Exemple #6
0
def dump_autogen_code(fpath, autogen_text, codetype='python', fullprint=None):
    """
    Helper that write a file if -w is given on command line, otherwise
    it just prints it out. It has the opption of comparing a diff to the file.
    """
    import utool as ut
    dowrite = ut.get_argflag(('-w', '--write'))
    show_diff = ut.get_argflag('--diff')
    num_context_lines = ut.get_argval('--diff', type_=int, default=None)
    show_diff = show_diff or num_context_lines is not None

    num_context_lines = ut.get_argval('--diff', type_=int, default=None)

    if fullprint is None:
        fullprint = True

    if fullprint is False:
        fullprint = ut.get_argflag('--print')

    print('[autogen] Autogenerated %s...\n+---\n' % (fpath,))
    if not dowrite:
        if fullprint:
            ut.print_code(autogen_text, lexer_name=codetype)
            print('\nL___')
        else:
            print('specify --print to write to stdout')
            pass
        print('specify -w to write, or --diff to compare')
        print('...would write to: %s' % fpath)
    if show_diff:
        if ut.checkpath(fpath, verbose=True):
            prev_text = ut.read_from(fpath)
            textdiff = ut.get_textdiff(prev_text, autogen_text,
                                       num_context_lines=num_context_lines)
            try:
                ut.print_difftext(textdiff)
            except UnicodeDecodeError:
                import unicodedata
                textdiff = unicodedata.normalize('NFKD', textdiff).encode('ascii', 'ignore')
                ut.print_difftext(textdiff)

        if dowrite:
            print('WARNING: Not writing. Remove --diff from command line')
    elif dowrite:
        ut.write_to(fpath, autogen_text)
Exemple #7
0
def make_individual_latex_figures(ibs, fpaths_list, flat_case_labels,
                                  cfgx2_shortlbl, case_figdir,
                                  analysis_fpath_list):
    # HACK MAKE LATEX CONVINENCE STUFF
    #print('LATEX HACK')
    if len(fpaths_list) == 0:
        print('nothing to render')
        return
    RENDER = ut.get_argflag('--render')
    DUMP_FIGDEF = ut.get_argflag(('--figdump', '--dump-figdef', '--figdef'))

    if not (DUMP_FIGDEF or RENDER):  # HACK
        return

    latex_code_blocks = []
    latex_block_keys = []

    caption_prefix = ut.get_argval('--cappref', type_=str, default='')
    caption_suffix = ut.get_argval('--capsuf', type_=str, default='')
    cmdaug = ut.get_argval('--cmdaug', type_=str, default='custom')

    selected = None

    for case_idx, (fpaths,
                   labels) in enumerate(zip(fpaths_list, flat_case_labels)):
        if labels is None:
            labels = [cmdaug]
        if len(fpaths) < 4:
            nCols = len(fpaths)
        else:
            nCols = 2

        _cmdname = ibs.get_dbname() + ' Case ' + ' '.join(labels) + '_' + str(
            case_idx)
        #print('_cmdname = %r' % (_cmdname,))
        cmdname = ut.latex_sanitize_command_name(_cmdname)
        label_str = cmdname
        if len(caption_prefix) == 0:
            caption_str = ut.escape_latex(
                'Casetags: ' + ut.list_str(labels, nl=False, strvals=True) +
                ', db=' + ibs.get_dbname() + '. ')
        else:
            caption_str = ''

        use_sublbls = len(cfgx2_shortlbl) > 1
        if use_sublbls:
            caption_str += ut.escape_latex(
                'Each figure shows a different configuration: ')
            sublbls = [
                '(' + chr(97 + count) + ') '
                for count in range(len(cfgx2_shortlbl))
            ]
        else:
            #caption_str += ut.escape_latex('This figure depicts correct and
            #incorrect matches from configuration: ')
            sublbls = [''] * len(cfgx2_shortlbl)

        def wrap_tt(text):
            return r'{\tt ' + text + '}'

        _shortlbls = cfgx2_shortlbl
        _shortlbls = list(map(ut.escape_latex, _shortlbls))
        # Adjust spacing for breaks
        #tex_small_space = r''
        tex_small_space = r'\hspace{0pt}'
        # Remove query specific config flags in individual results
        _shortlbls = [
            re.sub('\\bq[^,]*,?', '', shortlbl) for shortlbl in _shortlbls
        ]
        # Let config strings be broken over newlines
        _shortlbls = [
            re.sub('\\+', tex_small_space + '+' + tex_small_space, shortlbl)
            for shortlbl in _shortlbls
        ]
        _shortlbls = [
            re.sub(', *', ',' + tex_small_space, shortlbl)
            for shortlbl in _shortlbls
        ]
        _shortlbls = list(map(wrap_tt, _shortlbls))
        cfgx2_texshortlbl = [
            '\n    ' + lbl + shortlbl
            for lbl, shortlbl in zip(sublbls, _shortlbls)
        ]

        caption_str += ut.conj_phrase(cfgx2_texshortlbl, 'and') + '.\n    '
        caption_str = '\n    ' + caption_prefix + caption_str + caption_suffix
        caption_str = caption_str.rstrip()
        figure_str = ut.get_latex_figure_str(fpaths,
                                             nCols=nCols,
                                             label_str=label_str,
                                             caption_str=caption_str,
                                             use_sublbls=None,
                                             use_frame=True)
        latex_block = ut.latex_newcommand(cmdname, figure_str)
        latex_block = '\n%----------\n' + latex_block
        latex_code_blocks.append(latex_block)
        latex_block_keys.append(cmdname)

    # HACK
    remove_fpath = ut.truepath('~/latex/crall-candidacy-2015') + '/'

    latex_fpath = join(case_figdir, 'latex_cases.tex')

    if selected is not None:
        selected_keys = selected
    else:
        selected_keys = latex_block_keys

    selected_blocks = ut.dict_take(
        dict(zip(latex_block_keys, latex_code_blocks)), selected_keys)

    figdef_block = '\n'.join(selected_blocks)
    figcmd_block = '\n'.join(['\\' + key for key in latex_block_keys])

    selected_block = figdef_block + '\n\n' + figcmd_block

    # HACK: need full paths to render
    selected_block_renderable = selected_block
    selected_block = selected_block.replace(remove_fpath, '')
    if RENDER:
        ut.render_latex_text(selected_block_renderable)

    if DUMP_FIGDEF:
        ut.writeto(latex_fpath, selected_block)

    #if NOT DUMP AND NOT RENDER:
    #    print('STANDARD LATEX RESULTS')
    #    cmdname = ibs.get_dbname() + 'Results'
    #    latex_block  = ut.get_latex_figure_str2(analysis_fpath_list, cmdname, nCols=1)
    #    ut.print_code(latex_block, 'latex')
    if DUMP_FIGDEF or RENDER:
        ut.print_code(selected_block, 'latex')
Exemple #8
0
def register_custom_build_script(scriptname, script):
    flag = '--build-' + scriptname
    print('flag = %r' % (flag,))
    if ut.get_argflag(flag):
        print('Requested opencv build')
        ut.print_code(script, 'bash')
Exemple #9
0
def make_individual_latex_figures(ibs, fpaths_list, flat_case_labels,
                                  cfgx2_shortlbl, case_figdir,
                                  analysis_fpath_list):
    # HACK MAKE LATEX CONVINENCE STUFF
    #print('LATEX HACK')
    if len(fpaths_list) == 0:
        print('nothing to render')
        return
    RENDER = ut.get_argflag('--render')
    DUMP_FIGDEF = ut.get_argflag(('--figdump', '--dump-figdef', '--figdef'))

    if not (DUMP_FIGDEF or RENDER):  # HACK
        return

    latex_code_blocks = []
    latex_block_keys = []

    caption_prefix = ut.get_argval('--cappref', type_=str, default='')
    caption_suffix = ut.get_argval('--capsuf', type_=str, default='')
    cmdaug = ut.get_argval('--cmdaug', type_=str, default='custom')

    selected = None

    for case_idx, (fpaths, labels) in enumerate(zip(fpaths_list, flat_case_labels)):
        if labels is None:
            labels = [cmdaug]
        if len(fpaths) < 4:
            nCols = len(fpaths)
        else:
            nCols = 2

        _cmdname = ibs.get_dbname() + ' Case ' + ' '.join(labels) + '_' + str(case_idx)
        #print('_cmdname = %r' % (_cmdname,))
        cmdname = ut.latex_sanitize_command_name(_cmdname)
        label_str = cmdname
        if len(caption_prefix) == 0:
            caption_str = ut.escape_latex('Casetags: ' +
                                          ut.list_str(labels, nl=False, strvals=True) +
                                          ', db=' + ibs.get_dbname() + '. ')
        else:
            caption_str = ''

        use_sublbls = len(cfgx2_shortlbl) > 1
        if use_sublbls:
            caption_str += ut.escape_latex('Each figure shows a different configuration: ')
            sublbls = ['(' + chr(97 + count) + ') ' for count in range(len(cfgx2_shortlbl))]
        else:
            #caption_str += ut.escape_latex('This figure depicts correct and
            #incorrect matches from configuration: ')
            sublbls = [''] * len(cfgx2_shortlbl)
        def wrap_tt(text):
            return r'{\tt ' + text + '}'
        _shortlbls = cfgx2_shortlbl
        _shortlbls = list(map(ut.escape_latex, _shortlbls))
        # Adjust spacing for breaks
        #tex_small_space = r''
        tex_small_space = r'\hspace{0pt}'
        # Remove query specific config flags in individual results
        _shortlbls = [re.sub('\\bq[^,]*,?', '', shortlbl) for shortlbl in _shortlbls]
        # Let config strings be broken over newlines
        _shortlbls = [re.sub('\\+', tex_small_space + '+' + tex_small_space, shortlbl)
                      for shortlbl in _shortlbls]
        _shortlbls = [re.sub(', *', ',' + tex_small_space, shortlbl)
                      for shortlbl in _shortlbls]
        _shortlbls = list(map(wrap_tt, _shortlbls))
        cfgx2_texshortlbl = ['\n    ' + lbl + shortlbl
                             for lbl, shortlbl in zip(sublbls, _shortlbls)]

        caption_str += ut.conj_phrase(cfgx2_texshortlbl, 'and') + '.\n    '
        caption_str = '\n    ' + caption_prefix + caption_str + caption_suffix
        caption_str = caption_str.rstrip()
        figure_str  = ut.get_latex_figure_str(fpaths,
                                                nCols=nCols,
                                                label_str=label_str,
                                                caption_str=caption_str,
                                                use_sublbls=None,
                                                use_frame=True)
        latex_block = ut.latex_newcommand(cmdname, figure_str)
        latex_block = '\n%----------\n' + latex_block
        latex_code_blocks.append(latex_block)
        latex_block_keys.append(cmdname)

    # HACK
    remove_fpath = ut.truepath('~/latex/crall-candidacy-2015') + '/'

    latex_fpath = join(case_figdir, 'latex_cases.tex')

    if selected is not None:
        selected_keys = selected
    else:
        selected_keys = latex_block_keys

    selected_blocks = ut.dict_take(dict(zip(latex_block_keys, latex_code_blocks)), selected_keys)

    figdef_block = '\n'.join(selected_blocks)
    figcmd_block = '\n'.join(['\\' + key for key in latex_block_keys])

    selected_block = figdef_block + '\n\n' + figcmd_block

    # HACK: need full paths to render
    selected_block_renderable = selected_block
    selected_block = selected_block.replace(remove_fpath, '')
    if RENDER:
        ut.render_latex_text(selected_block_renderable)

    if DUMP_FIGDEF:
        ut.writeto(latex_fpath, selected_block)

    #if NOT DUMP AND NOT RENDER:
    #    print('STANDARD LATEX RESULTS')
    #    cmdname = ibs.get_dbname() + 'Results'
    #    latex_block  = ut.get_latex_figure_str2(analysis_fpath_list, cmdname, nCols=1)
    #    ut.print_code(latex_block, 'latex')
    if DUMP_FIGDEF or RENDER:
        ut.print_code(selected_block, 'latex')
Exemple #10
0
def compile_latex_text(input_text, fnum=1, dpath=None, verbose=True, fname=None, title=None, nest_in_doc=None, **kwargs):
    r"""
    pdflatex -shell-escape --synctex=-1 -src-specials -interaction=nonstopmode /home/joncrall/code/ibeis/tmptex/latex_formatter_temp.tex

    CommandLine:
        python -m utool.util_latex --test-compile_latex_text --show

    Example1:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_latex import *  # NOQA
        >>> import utool as ut
        >>> verbose = True
        >>> #dpath = '/home/joncrall/code/ibeis/aidchallenge'
        >>> dpath = dirname(ut.grab_test_imgpath())
        >>> #ut.vd(dpath)
        >>> orig_fpath_list = ut.list_images(dpath, fullpath=True)
        >>> figure_str = ut.get_latex_figure_str(orig_fpath_list, width_str='2.4in', nCols=2)
        >>> input_text = figure_str
        >>> pdf_fpath = ut.compile_latex_text(input_text, dpath=dpath, verbose=verbose)
        >>> output_pdf_fpath = ut.compress_pdf(pdf_fpath)
        >>> print(pdf_fpath)
        >>> ut.quit_if_noshow()
        >>> ut.startfile(pdf_fpath)

        fpath_list
        def clipwhite_ondisk(fpath_in):
            import utool as ut
            import vtool as vt
            fpath_out = ut.augpath(fpath_in, '_clipwhite')
            img = vt.imread(fpath_in)
            thresh = 128
            fillval = [255, 255, 255]
            cropped_img = vt.crop_out_imgfill(img, fillval=fillval, thresh=thresh)
            vt.imwrite(fpath_out, cropped_img)
            return fpath_out
        fpath_list_ = [clipwhite_ondisk(fpath) for fpath in fpath_list]
        tmpfig = join(dpath, 'tmpfig')
        ut.ensuredir(tmpfig)
        # Weirdness
        from os.path import *
        new_fpath_list = []
        for fpath in fpath_list_:
            fname, ext = splitext(basename(fpath))
            fname_ = ut.hashstr(fname, alphabet=ut.ALPHABET_16) + ext
            fpath_ = join(tmpfig, fname_)
            ut.move(fpath, fpath_)
            new_fpath_list.append(fpath_)
        new_rel_fpath_list = [ut.relpath_unix(fpath_, dpath) for fpath_ in new_fpath_list]

    """
    #import pylab as plt
    #import matplotlib as mpl
    #verbose = True
    if nest_in_doc or (nest_in_doc is None and input_text.find('documentclass') == -1):
        text = make_full_document(input_text, title=title)
    #text = make_full_document(input_text, title=title)
    cwd = os.getcwd()
    if dpath is None:
        text_dir = join(cwd, 'tmptex')
    else:
        text_dir = dpath
    util_path.ensuredir(text_dir, verbose=verbose)
    if fname is None:
        fname = 'latex_formatter_temp'
    text_fname = fname + '.tex'
    #text_fname = 'latex_formatter_temp.tex'
    text_fpath = join(text_dir, text_fname)
    pdf_fpath = splitext(text_fpath)[0] + '.pdf'
    #jpg_fpath = splitext(text_fpath)[0] + '.jpg'
    try:
        os.chdir(text_dir)
        util_io.write_to(text_fpath, text)
        pdflatex_args = ('pdflatex', '-shell-escape', '--synctex=-1', '-src-specials', '-interaction=nonstopmode')
        args = pdflatex_args + (text_fpath,)
        util_cplat.cmd(*args, verbose=verbose, **kwargs)
        assert util_path.checkpath(pdf_fpath, verbose=verbose), 'latex failed'
    except Exception as ex:
        import utool as ut
        print('Error compiling')
        ut.print_code(text, 'latex')
        ut.printex(ex, 'LATEX ERROR')
        raise
    finally:
        os.chdir(cwd)
    return pdf_fpath
Exemple #11
0
def compile_latex_text(input_text,
                       fnum=1,
                       dpath=None,
                       verbose=True,
                       fname=None,
                       title=None,
                       nest_in_doc=None,
                       **kwargs):
    r"""
    pdflatex -shell-escape --synctex=-1 -src-specials -interaction=nonstopmode /home/joncrall/code/ibeis/tmptex/latex_formatter_temp.tex

    CommandLine:
        python -m utool.util_latex --test-compile_latex_text --show

    Example1:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_latex import *  # NOQA
        >>> import utool as ut
        >>> verbose = True
        >>> #dpath = '/home/joncrall/code/ibeis/aidchallenge'
        >>> dpath = dirname(ut.grab_test_imgpath())
        >>> #ut.vd(dpath)
        >>> orig_fpath_list = ut.list_images(dpath, fullpath=True)
        >>> figure_str = ut.get_latex_figure_str(orig_fpath_list, width_str='2.4in', nCols=2)
        >>> input_text = figure_str
        >>> pdf_fpath = ut.compile_latex_text(input_text, dpath=dpath, verbose=verbose)
        >>> output_pdf_fpath = ut.compress_pdf(pdf_fpath)
        >>> print(pdf_fpath)
        >>> ut.quit_if_noshow()
        >>> ut.startfile(pdf_fpath)

        fpath_list
        def clipwhite_ondisk(fpath_in):
            import utool as ut
            import vtool as vt
            fpath_out = ut.augpath(fpath_in, '_clipwhite')
            img = vt.imread(fpath_in)
            thresh = 128
            fillval = [255, 255, 255]
            cropped_img = vt.crop_out_imgfill(img, fillval=fillval, thresh=thresh)
            vt.imwrite(fpath_out, cropped_img)
            return fpath_out
        fpath_list_ = [clipwhite_ondisk(fpath) for fpath in fpath_list]
        tmpfig = join(dpath, 'tmpfig')
        ut.ensuredir(tmpfig)
        # Weirdness
        from os.path import *
        new_fpath_list = []
        for fpath in fpath_list_:
            fname, ext = splitext(basename(fpath))
            fname_ = ut.hashstr(fname, alphabet=ut.ALPHABET_16) + ext
            fpath_ = join(tmpfig, fname_)
            ut.move(fpath, fpath_)
            new_fpath_list.append(fpath_)
        new_rel_fpath_list = [ut.relpath_unix(fpath_, dpath) for fpath_ in new_fpath_list]

    """
    #import pylab as plt
    #import matplotlib as mpl
    #verbose = True
    if nest_in_doc or (nest_in_doc is None
                       and input_text.find('documentclass') == -1):
        text = make_full_document(input_text, title=title)
    #text = make_full_document(input_text, title=title)
    cwd = os.getcwd()
    if dpath is None:
        text_dir = join(cwd, 'tmptex')
    else:
        text_dir = dpath
    util_path.ensuredir(text_dir, verbose=verbose)
    if fname is None:
        fname = 'latex_formatter_temp'
    text_fname = fname + '.tex'
    #text_fname = 'latex_formatter_temp.tex'
    text_fpath = join(text_dir, text_fname)
    pdf_fpath = splitext(text_fpath)[0] + '.pdf'
    #jpg_fpath = splitext(text_fpath)[0] + '.jpg'
    try:
        os.chdir(text_dir)
        util_io.write_to(text_fpath, text)
        pdflatex_args = ('pdflatex', '-shell-escape', '--synctex=-1',
                         '-src-specials', '-interaction=nonstopmode')
        args = pdflatex_args + (text_fpath, )
        util_cplat.cmd(*args, verbose=verbose, **kwargs)
        assert util_path.checkpath(pdf_fpath, verbose=verbose), 'latex failed'
    except Exception as ex:
        import utool as ut
        print('Error compiling')
        ut.print_code(text, 'latex')
        ut.printex(ex, 'LATEX ERROR')
        raise
    finally:
        os.chdir(cwd)
    return pdf_fpath
Exemple #12
0
def compile_latex_text(input_text, dpath=None, fname=None, verbose=True,
                       move=True, nest_in_doc=None, title=None,
                       preamb_extra=None):
    r"""
    CommandLine:
        python -m utool.util_latex --test-compile_latex_text --show

    Ignore:
        pdflatex -shell-escape --synctex=-1 -src-specials -interaction=nonstopmode\
            ~/code/ibeis/tmptex/latex_formatter_temp.tex

    Example1:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_latex import *  # NOQA
        >>> import utool as ut
        >>> verbose = True
        >>> #dpath = '/home/joncrall/code/ibeis/aidchallenge'
        >>> dpath = dirname(ut.grab_test_imgpath())
        >>> #ut.vd(dpath)
        >>> orig_fpaths = ut.list_images(dpath, fullpath=True)
        >>> figure_str = ut.get_latex_figure_str(orig_fpaths, width_str='2.4in', nCols=2)
        >>> input_text = figure_str
        >>> pdf_fpath = ut.compile_latex_text(input_text, dpath=dpath,
        >>>                                   verbose=verbose)
        >>> output_pdf_fpath = ut.compress_pdf(pdf_fpath)
        >>> print(pdf_fpath)
        >>> ut.quit_if_noshow()
        >>> ut.startfile(pdf_fpath)
    """
    import utool as ut
    if verbose:
        print('[ut] compile_latex_text')

    if nest_in_doc is None:
        nest_in_doc = 'documentclass' not in input_text
    if nest_in_doc:
        text = make_full_document(input_text, title=title,
                                  preamb_extra=preamb_extra)
    if not dpath:
        dpath = os.getcwd()
    if fname is None:
        fname = 'temp_latex'

    # Create temporary work directly
    work_dpath = join(dpath, '.tmptex')
    ut.ensuredir(work_dpath, verbose=verbose > 1)

    fname_tex = ut.ensure_ext(fname, '.tex')
    fname_pdf = ut.ensure_ext(fname, '.pdf')

    tex_fpath = join(work_dpath, fname_tex)
    pdf_fpath_output = join(work_dpath, fname_pdf)
    ut.write_to(tex_fpath, text)

    with ut.ChdirContext(work_dpath, verbose=verbose > 1):
        # print(text)
        args = ' '.join([
            'lualatex', '-shell-escape', '--synctex=-1', '-src-specials',
            '-interaction=nonstopmode', tex_fpath
        ])
        info = ut.cmd2(args, verbose=verbose > 1)
        if not ut.checkpath(pdf_fpath_output, verbose=verbose > 1):
            print('Error compiling LaTeX')
            ut.print_code(text, 'latex')
            print(info['out'])
            raise RuntimeError('latex failed ')

    if move:
        pdf_fpath = join(dpath, fname_pdf)
        ut.move(pdf_fpath_output, pdf_fpath, verbose=verbose > 1)
    else:
        pdf_fpath = pdf_fpath_output
    return pdf_fpath