def auto_cmdline(): import ubelt as ub from xdoctest import static_analysis as static import vim # import imp # imp.reload(static) modname, moddir = get_current_modulename() funcname, searchlines, pos, foundline = find_pyfunc_above_cursor() if static.is_modname_importable(modname, exclude=['.']): text = ub.codeblock( ''' CommandLine: python -m {modname} {funcname} ''').format(funcname=funcname, modname=modname) else: modpath = ub.compressuser(vim.current.buffer.name) text = ub.codeblock( ''' CommandLine: python {modpath} {funcname} ''').format(funcname=funcname, modpath=modpath) def get_indent(line): """ returns the preceding whitespace """ n_whitespace = len(line) - len(line.lstrip()) prefix = line[:n_whitespace] return prefix prefix = get_indent(foundline) text = ub.indent(text, prefix + ' ') return text
def make_model_file(input_fpath, arch='segnet_basic', mode='predict', dpath=None, modelkw={}, params=None): # assert input_fpath, 'must specify' model_fname = '{}_{}_model.prototext'.format(arch, mode) model_fpath = join(dpath, model_fname) text = make_prototext(input_fpath, arch=arch, mode=mode, params=params, **modelkw) ub.writeto(model_fpath, text) print('made model_fpath = {!r}'.format(ub.compressuser(model_fpath))) return model_fpath
def prepare_input(self): """ Prepare the text file containing inputs that can be passed to caffe. """ self.prepare_images() if self.input_fpath is None: assert self.base_dpath is not None assert self.input_id is not None self.input_dpath = ub.ensuredir((self.base_dpath, self.input_id)) # TODO: remove or refactor (holdover from caffe) self.input_fpath = make_input_file(self.im_paths, self.gt_paths, dpath=self.input_dpath) print('{} input_fpath = {!r}'.format( self.tag, ub.compressuser(self.input_fpath)))
def make_solver_file(input_fpath, arch='segnet_basic', dpath=None, modelkw={}, params=None, gpu_num=0): assert input_fpath, 'must specify' model_fpath = make_model_file(input_fpath, arch=arch, mode='fit', dpath=dpath, modelkw=modelkw) solver_fname = '{}_solver.prototext'.format(arch) solver_fpath = join(dpath, solver_fname) snapshot_dpath = ub.ensuredir((dpath, 'snapshots')) snapshot_prefix = snapshot_dpath + '/' text = make_solver(model_fpath, snapshot_prefix=snapshot_prefix, params=params, gpu_num=gpu_num) ub.writeto(solver_fpath, text) print('made solver_fpath = {!r}'.format(ub.compressuser(solver_fpath))) return solver_fpath
def current_gvim_edit(op='e', fpath=''): r""" CommandLine: python -m vimtk.xctrl XCtrl.current_gvim_edit sp ~/.bashrc """ fpath = ub.compressuser(ub.truepath(fpath)) # print('fpath = %r' % (fpath,)) cplat.copy_text_to_clipboard(fpath) doscript = [ ('focus', 'gvim'), ('key', 'Escape'), ('type2', ';' + op + ' ' + fpath), # ('type2', ';' + op + ' '), # ('key', 'ctrl+v'), ('key', 'KP_Enter'), ] XCtrl.do(*doscript, verbose=0, sleeptime=.001)
def test_pathlib(): try: import pathlib base = pathlib.Path(ub.ensure_app_cache_dir('ubelt')) dpath = base.joinpath('test_pathlib_mkdir') # ensuredir ub.delete(dpath) assert not dpath.exists() got = ub.ensuredir(dpath) assert got.exists() # compressuser assert ub.compressuser(base) == '~/.cache/ubelt' assert ub.augpath(base, prefix='foo') == '/home/joncrall/.cache/fooubelt' ub.expandpath(base) except Exception: import pytest pytest.skip('pathlib is not installed')
def test_compressuser_without_home(): username = basename(expanduser('~')) not_the_user = '******' + username ub.compressuser(not_the_user) == not_the_user
def make_default_module_maintest(modpath, test_code=None, argv=None, force_full=False): """ Args: modname (str): module name Returns: str: text source code CommandLine: python -m utool.util_autogen --test-make_default_module_maintest References: http://legacy.python.org/dev/peps/pep-0338/ Example: >>> import sys, ubelt as ub >>> sys.path.append(ub.truepath('~/local/vim/rc/')) >>> from pyvim_funcs import * >>> import pyvim_funcs >>> modpath = pyvim_funcs.__file__ >>> argv = None >>> text = make_default_module_maintest(modpath) >>> print(text) """ # if not use_modrun: # if ub.WIN32: # augpath = 'set PYTHONPATH=%PYTHONPATH%' + os.pathsep + moddir # else: # augpath = 'export PYTHONPATH=$PYTHONPATH' + os.pathsep + moddir # cmdline = augpath + '\n' + cmdline import ubelt as ub from xdoctest import static_analysis as static modname = static.modpath_to_modname(modpath) moddir, rel_modpath = static.split_modpath(modpath) if not force_full: info = ub.cmd('python -c "import sys; print(sys.path)"') default_path = eval(info['out'], {}) is_importable = static.is_modname_importable(modname, exclude=['.'], sys_path=default_path) if not force_full and is_importable: cmdline = 'python -m ' + modname else: if ub.WIN32: modpath = ub.compressuser(modpath, home='%HOME%') cmdline = 'python -B ' + modpath.replace('\\', '/') else: modpath = ub.compressuser(modpath, home='~') cmdline = 'python ' + modpath if test_code is None: test_code = ub.codeblock( r''' import xdoctest xdoctest.doctest_module(__file__) ''') if argv is None: argv = ['all'] if argv is None: argv = [] cmdline_ = ub.indent(cmdline + ' ' + ' '.join(argv), ' ' * 8).lstrip(' ') test_code = ub.indent(test_code, ' ' * 4).lstrip(' ') text = ub.codeblock( r''' if __name__ == '__main__': {rr}""" CommandLine: {cmdline_} """ {test_code} ''' ).format(cmdline_=cmdline_, test_code=test_code, rr='{r}') text = text.format(r='r' if '\\' in text else '') return text