예제 #1
0
def ensure_text(fname, text, repo_dpath='.', force=None, locals_={}, chmod=None):
    """
    Args:
        fname (str):  file name
        text (str):
        repo_dpath (str):  directory path string(default = '.')
        force (bool): (default = False)
        locals_ (dict): (default = {})

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_project import *  # NOQA
        >>> import utool as ut
        >>> result = setup_repo()
        >>> print(result)
    """
    import utool as ut
    ut.colorprint('Ensuring fname=%r' % (fname), 'yellow')

    # if not fname.endswith('__init__.py'):
    #     # HACK
    #     return

    if force is None and ut.get_argflag('--force-%s' % (fname,)):
        force = True
    text_ = ut.remove_codeblock_syntax_sentinals(text)
    fmtkw = locals_.copy()
    fmtkw['fname'] = fname
    text_ = text_.format(**fmtkw) + '\n'

    fpath = join(repo_dpath, fname)
    ut.dump_autogen_code(fpath, text_)
예제 #2
0
def ensure_text(fname, text, repo_dpath='.', force=None, locals_={}, chmod=None):
    """
    Args:
        fname (str):  file name
        text (str):
        repo_dpath (str):  directory path string(default = '.')
        force (bool): (default = False)
        locals_ (dict): (default = {})

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_git import *  # NOQA
        >>> import utool as ut
        >>> result = setup_repo()
        >>> print(result)
    """
    import utool as ut
    ut.colorprint('Ensuring fname=%r' % (fname), 'yellow')

    if force is None and ut.get_argflag('--force-%s' % (fname,)):
        force = True

    fpath = join(repo_dpath, fname)
    if force or not ut.checkpath(fpath, verbose=2, n=5):
        text_ = ut.remove_codeblock_syntax_sentinals(text)
        fmtkw = locals_.copy()
        fmtkw['fname'] = fname
        text_ = text_.format(**fmtkw) + '\n'
        ut.writeto(fpath, text_)
        try:
            if chmod:
                ut.chmod(fpath, chmod)
        except Exception as ex:
            ut.printex(ex, iswarning=True)
예제 #3
0
def code_cell(sourcecode):
    r"""
    Args:
        sourcecode (str):

    Returns:
        str: json formatted ipython notebook code cell

    CommandLine:
        python -m ibeis.templates.generate_notebook --exec-code_cell

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.templates.generate_notebook import *  # NOQA
        >>> sourcecode = notebook_cells.timestamp_distribution[1]
        >>> sourcecode = notebook_cells.initialize[1]
        >>> result = code_cell(sourcecode)
        >>> print(result)
    """
    import utool as ut
    sourcecode = ut.remove_codeblock_syntax_sentinals(sourcecode)
    cell_header = ut.codeblock(
        '''
        {
         "cell_type": "code",
         "execution_count": null,
         "metadata": {
          "collapsed": true
         },
         "outputs": [],
         "source":
        ''')
    cell_footer = ut.codeblock(
        '''
        }
        ''')
    if sourcecode is None:
        source_line_repr = ' []\n'
    else:
        lines = sourcecode.split('\n')
        line_list = [line + '\n' if count < len(lines) else line
                     for count, line in enumerate(lines, start=1)]
        #repr_line_list = [repr_single_for_md(line) for line in line_list]
        repr_line_list = [repr_single_for_md(line) for line in line_list]
        source_line_repr = ut.indent(',\n'.join(repr_line_list), ' ' * 2)
        source_line_repr = ' [\n' + source_line_repr + '\n ]\n'
    return (cell_header + source_line_repr + cell_footer)
예제 #4
0
def ensure_text(fname,
                text,
                repo_dpath='.',
                force=None,
                locals_={},
                chmod=None):
    """
    Args:
        fname (str):  file name
        text (str):
        repo_dpath (str):  directory path string(default = '.')
        force (bool): (default = False)
        locals_ (dict): (default = {})

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_git import *  # NOQA
        >>> import utool as ut
        >>> result = setup_repo()
        >>> print(result)
    """
    import utool as ut
    ut.colorprint('Ensuring fname=%r' % (fname), 'yellow')

    if force is None and ut.get_argflag('--force-%s' % (fname, )):
        force = True

    fpath = join(repo_dpath, fname)
    if force or not ut.checkpath(fpath, verbose=2, n=5):
        text_ = ut.remove_codeblock_syntax_sentinals(text)
        fmtkw = locals_.copy()
        fmtkw['fname'] = fname
        text_ = text_.format(**fmtkw) + '\n'
        ut.writeto(fpath, text_)
        try:
            if chmod:
                ut.chmod(fpath, chmod)
        except Exception as ex:
            ut.printex(ex, iswarning=True)