def _clean_user_namespace(self, venv): """ clean ipython username to remove old project's code TODO: document and refactor """ msg = "cleaning user namespace" smash_log.info(msg) self.report(msg) names = [] pm = get_smash().project_manager pdir = pm._current_project and pm.project_map[pm._current_project] namespace = get_smash().shell.user_ns for name, obj in namespace.items(): try: fname = inspect.getfile(obj) except TypeError: # happens for anything that's not a module, class # method, function, traceback, frame or code object fname = None if fname: test = fname.startswith(venv) if pdir is not None: test = test or fname.startswith(pdir) if test and \ not name.startswith('_') and \ not name.startswith(SMASH_DIR): names.append(name) for name in names: del self.smash.shell.user_ns[name] if names: self.report("wiped from namespace: {0}".format(names))
def update(self): tmp = self._plugins for name in tmp: tmp2 = lambda self=self, name=name: get_smash()._installed_plugins[name] tmp3 = get_smash()._installed_plugins[name].__qmark__() tmp2.__doc__ = tmp3 prop = property(tmp2) setattr(self.__class__, name, prop)
def source(fname): """ add all functions mentioned in fname to user namespace """ namespace = source_file_namespace(fname) user_ns = get_smash().shell.user_ns for fxn_name, fxn_bridge in namespace.items(): if fxn_name in user_ns: print 'overwriting name: ',fxn_name get_smash().shell.magics_manager.register_function( fxn_bridge, magic_name=fxn_name)
def _unload_alias_group(self, group_name): smash_log.info('unloading alias group: {0}'.format(group_name)) aliases, macros = self._get_alias_group(group_name) for alias in aliases: name, cmd = alias try: get_smash().shell.alias_manager.undefine_alias(name) except ValueError: continue
def handle(self, line_info): """ when the dot handler is engaged, we send a signal and then prefilter the input to empty-string, which is effectively a no-op. anyone that wants to act on the dot-cmd can receive the signal. """ line = line_info.line.strip()[1:] get_smash().publish(C_DOT_CMD, line) return ""
def _load_alias_group(self, group_name): smash_log.info('loading alias group: {0}'.format(group_name)) aliases, macros = self._get_alias_group(group_name) for alias in aliases: name, cmd = alias get_smash().shell.alias_manager.define_alias(name, cmd) smash_log.info(' alias: {0}'.format(name)) self.report("Loaded {0} aliases".format(len(aliases))) for m in macros: name, macro = m self._load_macro(macro, name=name)
def load_ipython_extension(ip): """ called by %load_ext magic""" ip = get_ipython() smash = get_smash() sp = SmashPrompt(ip) smash.prompt_manager = sp return sp
def source_file_namespace(fname): """ returns a dictionary of { fxn_name : FunctionMagic() } for bash functions in `fname` """ if not os.path.exists(fname): raise ValueError("{0} does not exist".format(fname)) fname = os.path.abspath(fname) smash_log.info("attempting to source: {0}".format(fname)) fxns = get_functions_from_file(fname) smash_log.info("found functions: {0}".format(fxns)) out = dict() for fxn_name in fxns: cmd = FunctionMagic(fxn_name, source=fname) out[fxn_name] = cmd get_smash().shell.magics_manager.register_function( cmd, magic_name=fxn_name) return out
def run_continuously(self): """ main loop for scheduler """ count = 0 while not self.ask_stop: if not get_smash(): scheduler_log.info('smash not ready') continue count += 1 time.sleep(WAIT_INTERVAL) if count >= REPORT_COUNT: scheduler_log.info(str(self)) count = 0 self.scheduler.run_pending() scheduler_log.info('scheduler stopping')
def __qmark__(self): """ user-friendly information when the input is "plugins?" """ alias_map = get_smash().project_manager.alias_map out = [console.red('Smash Aliases:') + ' ({0} total, {1} groups)'.format( len(self._aliases), len(alias_map))] for group_name in alias_map.keys(): g_aliases = alias_map[group_name] max_summary = 3 summary = [x[0] for x in g_aliases[:max_summary]] if len(g_aliases) > max_summary: summary += ['..'] summary = ', '.join(summary) out += [' : "{0}" with {1} aliases: {2})'.format( group_name, len(g_aliases), summary)] return '\n'.join(out)
def update(self): return tmp = self._prompts def fxn(name): return self.smash._installed_prompts[name] for name in tmp: tmp2 = lambda himself: fxn(name) tmp3 = get_smash()._installed_prompts[name].__qmark__() tmp2.__doc__ = tmp3 prop = property(tmp2) setattr(self.__class__, name, prop) whitelist = ['edit', 'smash', 'update'] for x in dir(self): if not x.startswith('_') and \ x not in tmp and \ x not in whitelist: raise ValueError("interface is not clean")
def load_ipython_extension(ip): ip = get_smash().shell return SmashCompleter(ip).install()
def uninstall_plugin(self, parameter_s=""): plugin_name = parameter_s.strip().split()[-1] get_smash()._installed_plugins[plugin_name].uninstall()
def __call__(self, parameter_s=''): self.original(parameter_s) get_smash().publish(C_REHASH_EVENT)
def __init__(self, parent): self._parent = parent self._parent.__class__.interface = self get_smash().shell.user_ns[self._user_ns_var] = self
def _plugins(self): return get_smash()._installed_plugins
def _load_macro(self, macro, name=None): smash_log.debug('[{0}]'.format(dict(macro=macro, name=name))) assert isinstance(macro, basestring) macro = 'get_ipython().run_cell("""{0}""")'.format(macro) macro = Macro(macro) get_smash().shell.user_ns[name] = macro
def fxn(self, name): return get_smash().shell.alias_manager.linemagics.get(name)
def edit(self): get_smash().shell.run_cell("ed_config")
def __init__(self, *args): self.smash = get_smash() self.shell = self.smash.shell self.original = self.shell.magics_manager.magics['line'][self.name] self.patched = True
def edit(self): get_smash().shell.run_cell('ed {0}'.format(PROMPT_CONFIG_PATH))
def _prompts(self): return get_smash().prompt_manager.prompt_components
def edit(self): get_smash().shell.run_cell('ed_aliases')
def unload_ipython_extension(ip): plugin_name = splitext(ops(__file__)[-1])[0] raise Exception(plugin_name) get_smash().plugins[plugin_name].uninstall()
def _aliases(self): return [x[0].replace('-', '.') for x in get_smash().shell.alias_manager.aliases]
def unload_ipython_extension(ip): get_smash()
def blue(himself): return get_smash().shell.alias_manager.linemagics.get(name)