def interactive(idx=0, DongleClass=RfCat, intro=''): global d import rflib.chipcon_nic as rfnic import atexit d = DongleClass(idx=idx) d.setModeIDLE() # this puts the dongle into Idle mode atexit.register(cleanupInteractiveAtExit) gbls = globals() lcls = locals() try: import IPython.Shell ipsh = IPython.Shell.IPShell(argv=[''], user_ns=lcls, user_global_ns=gbls) print intro ipsh.mainloop(intro) except ImportError, e: try: from IPython.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! ipsh.mainloop(intro) except ImportError, e: print e shell = code.InteractiveConsole(gbls) shell.interact(intro)
def repl(ctx, config, community, network, keyfile, password, trezor, trezor_path, derivation_path, chain, artifactdir, input, timeout): config = Config.from_yaml(config, Chain.from_str(chain)) if network not in config.network_configs: click.echo('No such network {0} defined, check configuration', network) sys.exit(1) network = configure_network(config, network, keyfile, password, trezor, trezor_path, derivation_path) deployer = Deployer(community, network, artifactdir) # Default to homechain.json/sidechain.json if not input: input = chain + 'chain.json' click.echo('Waiting for deployment results') if not wait_for_file(input, timeout): click.echo('Timeout waiting for deployment results file') sys.exit(1) with open(input, 'r') as f: deployer.load_results(f) user_ns = { 'config': config, 'network': network, 'deployer': deployer, } shell = TerminalInteractiveShell(user_ns=user_ns) shell.mainloop()
def shell(): from api.app import rowboat namespace = {} try: from IPython.terminal.interactiveshell import TerminalInteractiveShell console = TerminalInteractiveShell(user_ns=namespace) print 'Starting iPython Shell' except ImportError: import code import rlcompleter c = rlcompleter.Completer(namespace) # Setup readline for autocomplete. try: # noinspection PyUnresolvedReferences import readline readline.set_completer(c.complete) readline.parse_and_bind('tab: complete') readline.parse_and_bind('set show-all-if-ambiguous on') readline.parse_and_bind('"\C-r": reverse-search-history') readline.parse_and_bind('"\C-s": forward-search-history') except ImportError: pass console = code.InteractiveConsole(namespace) print 'Starting Poverty Shell (install IPython to use improved shell)' with rowboat.app.app_context(): console.interact()
def dbg_interact(lcls, gbls): intro = "Let's interact!" try: import IPython.Shell ipsh = IPython.Shell.IPShell(argv=[''], user_ns=lcls, user_global_ns=gbls) print(intro) ipsh.mainloop() except ImportError as e: try: from IPython.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! print(intro) ipsh.mainloop() except ImportError as e: try: from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! print(intro) ipsh.mainloop() except ImportError, e: print(e) shell = code.InteractiveConsole(gbls) print(intro) shell.interact()
def __call__(self): from IPython.terminal.prompts import Prompts, Token from IPython.terminal.interactiveshell import TerminalInteractiveShell token = self._token class RFQuackShellPrompts(Prompts): def in_prompt_tokens(self, cli=None): return [(Token, token), (Token.Prompt, '> ')] TerminalInteractiveShell.prompts_class = RFQuackShellPrompts shell = TerminalInteractiveShell() shell.autocall = 0 shell.show_banner(self._banner) q = RFQuack(self._transport, "rfquack", shell, self._select_first_dongle) shell_objs = dict(q=q, pb=rfquack_pb2) # For each message field add a fake object with the same name, # this to achieve a **very basic** autocomp. for message_type in rfquack_pb2.DESCRIPTOR.message_types_by_name.values( ): for message_field in message_type.fields: shell_objs[ message_field. name] = "This is a fake variable, used for autocompletion" shell.push(shell_objs) shell.mainloop()
def get_ipython(): from IPython.terminal.interactiveshell import TerminalInteractiveShell if TerminalInteractiveShell._instance: return TerminalInteractiveShell.instance() config = tools.default_config() config.TerminalInteractiveShell.simple_prompt = True # Create and initialize our test-friendly IPython instance. shell = TerminalInteractiveShell.instance(config=config) return shell
def test_magic_registration(self, ip: TerminalInteractiveShell): """Test registering a magic.""" res = ip.run_cell(raw_cell=MAGIC_DEFINITION) self.assertions.assertTrue(res.success) magics = ip.run_line_magic(magic_name='picatrixmagics', line='') self.assertions.assertFalse(magics[magics.name == 'my_silly_magic'].empty) line = ip.run_line_magic( magic_name='my_silly_magic', line='--magnitude 23 this is my string') expected_return = ( 'This magical magic produced 23 magics of this is my string') self.assertions.assertEqual(line, expected_return)
def test_context_date(self, ip: TerminalInteractiveShell): """Test querying for contex surrounding a date.""" _ = self._get_sketch(ip) df = ip.run_line_magic( magic_name='timesketch_context_date', line= ('--minutes 10 --fields datetime,message,data_type,event_identifier' ',username,workstation 2020-09-18T22:24:36')) self.assertions.assertTrue(df.shape[0] > 5000) logon_df = df[df.event_identifier == 4624] logged_in_users = list(logon_df.username.unique()) self.assertions.assertTrue('Administrator' in logged_in_users) self.assertions.assertTrue('DWM-1' in logged_in_users) df.sort_values('datetime', inplace=True) first_series = df.iloc[0] last_series = df.iloc[-1] first_time = first_series.datetime last_time = last_series.datetime delta = last_time - first_time delta_rounded = delta.round('min') # This should be 10 minutes or 600 seconds. self.assertions.assertTrue(delta_rounded.total_seconds() == 600.0)
def main(obj): click.echo("Welcome to IPython designed for running RHAMT code.") ipython = TerminalInteractiveShell.instance() for code_import in IMPORTS: click.echo(f"> {code_import}") ipython.run_cell(code_import) # create default app ipython.run_cell("app = Application()") click.echo("> app = $magic Application") from rhamt.utils.path import CONF_PATH custom_import_path = CONF_PATH / "rhmt_shell_startup.py" if custom_import_path.exists(): with open(custom_import_path, "r") as custom_import_file: custom_import_code = custom_import_file.read() click.echo(f"Importing custom code:\n{custom_import_code.strip()}") ipython.run_cell(custom_import_code) else: click.echo( "You can create your own python file with imports you use frequently. " "Just create a conf/rhmt_shell_startup.py file in your repo. " "This file can contain arbitrary python code that is executed in this context." ) ipython.interact()
def test_list_views(self, ip: TerminalInteractiveShell): """Test listing up the available views for a sketch.""" _ = self._get_sketch(ip) views = ip.run_line_magic(magic_name='timesketch_list_views', line='') expected_views = set( ['18:Szechuan Hits', '19:Szechuan All Hits', '16:email_addresses']) self.assertions.assertEqual(set(views.keys()), expected_views)
def run_ipython_shell_v11(locals, globals, first_time): '''IPython shell from IPython version 0.11''' if first_time: banner = "Hit Ctrl-D to return to PuDB." else: banner = "" try: # IPython 1.0 got rid of the frontend intermediary, and complains with # a deprecated warning when you use it. from IPython.terminal.interactiveshell import TerminalInteractiveShell from IPython.terminal.ipapp import load_default_config except ImportError: from IPython.frontend.terminal.interactiveshell import \ TerminalInteractiveShell from IPython.frontend.terminal.ipapp import load_default_config # XXX: in the future it could be useful to load a 'pudb' config for the # user (if it exists) that could contain the user's macros and other # niceities. config = load_default_config() shell = TerminalInteractiveShell.instance(config=config, banner2=banner) # XXX This avoids a warning about not having unique session/line numbers. # See the HistoryManager.writeout_cache method in IPython.core.history. shell.history_manager.new_session() # Save the originating namespace old_locals = shell.user_ns old_globals = shell.user_global_ns # Update shell with current namespace _update_ns(shell, locals, globals) shell.mainloop(banner) # Restore originating namespace _update_ns(shell, old_locals, old_globals)
def main(): """Use quickstart to ensure we have correct env, then execute imports in ipython and done.""" quickstart.main( quickstart.parser.parse_args(['--mk-virtualenv', sys.prefix])) print('Welcome to IPython designed for running CFME QE code.') ipython = TerminalInteractiveShell.instance() from cfme.utils import appliance appliance.CREATE_IS_PEDANTIC = False for code_import in IMPORTS: print('> {}'.format(code_import)) ipython.run_cell(code_import) from cfme.utils.path import conf_path custom_import_path = conf_path.join('miq_python_startup.py') if custom_import_path.exists(): with open(custom_import_path.strpath, 'r') as custom_import_file: custom_import_code = custom_import_file.read() print('Importing custom code:\n{}'.format(custom_import_code.strip())) ipython.run_cell(custom_import_code) else: print( 'You can create your own python file with imports you use frequently. ' 'Just create a conf/miq_python_startup.py file in your repo. ' 'This file can contain arbitrary python code that is executed in this context.' ) ipython.interact()
def __init__(self, color_scheme=None, completekey=None, stdin=None, stdout=None, context=5): # Parent constructor: try: self.context = int(context) if self.context <= 0: raise ValueError("Context must be a positive integer") except (TypeError, ValueError): raise ValueError("Context must be a positive integer") OldPdb.__init__(self, completekey, stdin, stdout) # IPython changes... self.shell = get_ipython() if self.shell is None: # No IPython instance running, we must create one from IPython.terminal.interactiveshell import TerminalInteractiveShell self.shell = TerminalInteractiveShell.instance() if color_scheme is not None: warnings.warn("The `color_scheme` argument is deprecated since version 5.1", DeprecationWarning) else: color_scheme = self.shell.colors self.aliases = {} # Create color table: we copy the default one from the traceback # module and add a few attributes needed for debugging self.color_scheme_table = exception_colors() # shorthands C = coloransi.TermColors cst = self.color_scheme_table cst["NoColor"].colors.prompt = C.NoColor cst["NoColor"].colors.breakpoint_enabled = C.NoColor cst["NoColor"].colors.breakpoint_disabled = C.NoColor cst["Linux"].colors.prompt = C.Green cst["Linux"].colors.breakpoint_enabled = C.LightRed cst["Linux"].colors.breakpoint_disabled = C.Red cst["LightBG"].colors.prompt = C.Blue cst["LightBG"].colors.breakpoint_enabled = C.LightRed cst["LightBG"].colors.breakpoint_disabled = C.Red cst["Neutral"].colors.prompt = C.Blue cst["Neutral"].colors.breakpoint_enabled = C.LightRed cst["Neutral"].colors.breakpoint_disabled = C.Red self.set_colors(color_scheme) # Add a python parser so we can syntax highlight source while # debugging. self.parser = PyColorize.Parser() # Set the prompt - the default prompt is '(Pdb)' self.prompt = prompt
def main(no_quickstart): """Use quickstart to ensure we have correct env, then execute imports in ipython and done.""" if not no_quickstart: from cfme.scripting import quickstart quickstart.main(quickstart.args_for_current_venv()) print('Welcome to IPython designed for running CFME QE code.') ipython = TerminalInteractiveShell.instance() for code_import in IMPORTS: print('> {}'.format(code_import)) ipython.run_cell(code_import) from cfme.utils.path import conf_path custom_import_path = conf_path.join('miq_python_startup.py') if custom_import_path.exists(): with open(custom_import_path.strpath, 'r') as custom_import_file: custom_import_code = custom_import_file.read() print('Importing custom code:\n{}'.format(custom_import_code.strip())) ipython.run_cell(custom_import_code) else: print( 'You can create your own python file with imports you use frequently. ' 'Just create a conf/miq_python_startup.py file in your repo. ' 'This file can contain arbitrary python code that is executed in this context.' ) ipython.interact()
def _setup_client(self, ip: TerminalInteractiveShell): """Setup the TimesketchAPI object into the IPython session.""" ip.run_cell(raw_cell='from timesketch_api_client import client') res = ip.run_cell( raw_cell=('_client = client.TimesketchApi(\n' ' host_uri="https://demo.timesketch.org",\n' ' username="******",\n' ' password="******",\n' ' verify=True,\n' ' auth_mode="userpass")')) self.assertions.assertTrue(res.success) res = ip.run_cell(raw_cell=( 'from picatrix.lib import state\n' 'state_obj = state.state()\n' 'state_obj.add_to_cache(\'timesketch_client\', _client)\n')) self.assertions.assertTrue(res.success)
def get_python_console(namespace=None): """ Return a interactive python console instance with caller's stack """ if namespace is None: import inspect frame = inspect.currentframe() caller = frame.f_back if not caller: logging.error("can't find caller who start this console.") caller = frame namespace = dict(caller.f_globals) namespace.update(caller.f_locals) try: from IPython.terminal.interactiveshell import TerminalInteractiveShell shell = TerminalInteractiveShell(user_ns=namespace) except ImportError: try: import readline import rlcompleter readline.set_completer(rlcompleter.Completer(namespace).complete) readline.parse_and_bind("tab: complete") except ImportError: pass import code shell = code.InteractiveConsole(namespace) shell._quit = False def exit(): shell._quit = True def readfunc(prompt=""): if shell._quit: raise EOFError return six.moves.input(prompt) # inject exit method shell.ask_exit = exit shell.raw_input = readfunc return shell
def get_jeanmishell(): try: import IPython.Shell return IPython.Shell.IPShell(argv=[''], user_ns=locals(), user_global_ns=globals()) except ImportError as e: try: from IPython.terminal.interactiveshell import TerminalInteractiveShell except ImportError: try: from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell except ImportError: return None ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(globals()) ipsh.user_global_ns.update(locals()) ipsh.autocall = 2 return ipsh
def _get_shell(self): if ipy.get_ipython() is None: shell = ipy_shell.instance() if shell is None: self._create_profile() self._get_shell() shell.user_ns['shell'] = shell return ipy.get_ipython()
def init_shell(self): banner1 = 'Umeng development shell, do whatever you want.\n' if DEVELOP_MODE else \ 'Umeng production shell, use it carefully!\n' self.shell = TerminalInteractiveShell.instance(config=self.config, display_banner=False, profile_dir=self.profile_dir, ipython_dir=self.ipython_dir, banner1=banner1, banner2='') self.shell.configurables.append(self)
def prefilter(self, IP, line, continuation): """prefilter pre-processes input to do PyRAF substitutions before passing it on to IPython. NOTE: this is ONLY used for VERY_OLD_IPY, since we use the transform hooks for the later versions. """ line = self.cmd(str(line)) # use type str here, not unicode return TerminalInteractiveShell._prefilter(IP, line, continuation)
def __init__(self,color_scheme='NoColor',completekey=None, stdin=None, stdout=None, context=5): # Parent constructor: try: self.context=int(context) if self.context <= 0: raise ValueError("Context must be a positive integer") except (TypeError, ValueError): raise ValueError("Context must be a positive integer") OldPdb.__init__(self,completekey,stdin,stdout) # IPython changes... self.shell = get_ipython() if self.shell is None: # No IPython instance running, we must create one from IPython.terminal.interactiveshell import \ TerminalInteractiveShell self.shell = TerminalInteractiveShell.instance() self.aliases = {} # Create color table: we copy the default one from the traceback # module and add a few attributes needed for debugging self.color_scheme_table = exception_colors() # shorthands C = coloransi.TermColors cst = self.color_scheme_table cst['NoColor'].colors.prompt = C.NoColor cst['NoColor'].colors.breakpoint_enabled = C.NoColor cst['NoColor'].colors.breakpoint_disabled = C.NoColor cst['Linux'].colors.prompt = C.Green cst['Linux'].colors.breakpoint_enabled = C.LightRed cst['Linux'].colors.breakpoint_disabled = C.Red cst['LightBG'].colors.prompt = C.Blue cst['LightBG'].colors.breakpoint_enabled = C.LightRed cst['LightBG'].colors.breakpoint_disabled = C.Red self.set_colors(color_scheme) # Add a python parser so we can syntax highlight source while # debugging. self.parser = PyColorize.Parser() # Set the prompt - the default prompt is '(Pdb)' Colors = cst.active_colors if color_scheme == 'NoColor': self.prompt = prompt else: # The colour markers are wrapped by bytes 01 and 02 so that readline # can calculate the width. self.prompt = u'\x01%s\x02%s\x01%s\x02' % (Colors.prompt, prompt, Colors.Normal)
def dbg_interact(lcls, gbls, intro=None): shelltype = STYPE_NONE if intro is None: intro = "Let's interact!" print(intro) try: from IPython import embed shelltype = STYPE_IPYTHON_NEW except ImportError as e: try: import IPython.Shell ipsh = IPython.Shell.IPShell(argv=[''], user_ns=lcls, user_global_ns=gbls) shelltype = STYPE_IPYTHON except ImportError as e: try: from IPython.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! shelltype = STYPE_IPYTHON except ImportError as e: try: from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! shelltype = STYPE_IPYTHON except ImportError as e: print(e) shell = code.InteractiveConsole(gbls) shelltype = STYPE_IPYTHON if shelltype == STYPE_IPYTHON_NEW: globals().update(gbls) locals().update(lcls) embed() elif shelltype == STYPE_IPYTHON: ipsh.mainloop() elif shelltype == STYPE_CODE_INTERACT: shell.interact() else: print("SORRY, NO INTERACTIVE OPTIONS AVAILABLE!! wtfo?")
def init_shell(self): self.shell = TerminalInteractiveShell(prompts_class=MyPrompt, highlighting_style='emacs', display_banner=False, profile_dir=self.profile_dir, ipython_dir=self.ipython_dir, banner1=get_banner(), banner2='') self.shell.configurables.append(self)
def run(): shell = TerminalInteractiveShell.instance() shell.user_ns['shell'] = shell shell.define_macro('x', 'a,b=1,2') shell.define_macro('y', 'a,b=3,4') shell.config['using'] = True a, b = 0, 0 x, y = shell.user_ns['x'], shell.user_ns['y'] ipy.embed(display_banner=False, config=shell.config)
def init_shell(self): self.shell = TerminalInteractiveShell.instance( config=self.config, display_banner=False, profile_dir=self.profile_dir, ipython_dir=self.ipython_dir, banner1=get_banner(), banner2='') self.shell.configurables.append(self)
def main(): ipython = TerminalInteractiveShell.instance() for import_statement in DEFAULT_IMPORTS: click.echo(f'> {import_statement}') ipython.run_cell(import_statement) # This can be improved to read a default conf file location # So that users can write their own default imports for their local clone ipython.interact()
def __init__(self, color_scheme='NoColor', completekey=None, stdin=None, stdout=None, context=5): # Parent constructor: try: self.context = int(context) if self.context <= 0: raise ValueError("Context must be a positive integer") except (TypeError, ValueError): raise ValueError("Context must be a positive integer") OldPdb.__init__(self, completekey, stdin, stdout) # IPython changes... self.shell = get_ipython() if self.shell is None: # No IPython instance running, we must create one from IPython.terminal.interactiveshell import \ TerminalInteractiveShell self.shell = TerminalInteractiveShell.instance() self.aliases = {} # Create color table: we copy the default one from the traceback # module and add a few attributes needed for debugging self.color_scheme_table = exception_colors() # shorthands C = coloransi.TermColors cst = self.color_scheme_table cst['NoColor'].colors.prompt = C.NoColor cst['NoColor'].colors.breakpoint_enabled = C.NoColor cst['NoColor'].colors.breakpoint_disabled = C.NoColor cst['Linux'].colors.prompt = C.Green cst['Linux'].colors.breakpoint_enabled = C.LightRed cst['Linux'].colors.breakpoint_disabled = C.Red cst['LightBG'].colors.prompt = C.Blue cst['LightBG'].colors.breakpoint_enabled = C.LightRed cst['LightBG'].colors.breakpoint_disabled = C.Red self.set_colors(color_scheme) # Add a python parser so we can syntax highlight source while # debugging. self.parser = PyColorize.Parser() # Set the prompt - the default prompt is '(Pdb)' self.prompt = prompt
def main(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cto_tree.settings") try: if len(sys.argv) >= 2 and sys.argv[1] == '--plain': raise ImportError user_ns = locals() if has_ushell: app = UmengIPythonApp.instance() app.initialize() app.shell.user_ns.update(user_ns) sys.exit(app.start()) else: from IPython.terminal.interactiveshell import TerminalInteractiveShell shell = TerminalInteractiveShell(user_ns=user_ns) shell.mainloop() except ImportError: import code shell = code.InteractiveConsole(locals=locals()) shell.interact()
def init_shell(self): """initialize the InteractiveShell instance""" # Create an InteractiveShell instance. # shell.display_banner should always be False for the terminal # based app, because we call shell.show_banner() by hand below # so the banner shows *before* all extension loading stuff. self.shell = TerminalInteractiveShell.instance(parent=self, display_banner=False, profile_dir=self.profile_dir, ipython_dir=self.ipython_dir, user_ns=self.user_ns) self.shell.configurables.append(self)
def init_shell(self): self.shell = TerminalInteractiveShell.instance( config=self.config, display_banner=False, profile_dir=self.profile_dir, ipython_dir=self.ipython_dir, banner1=get_banner(), banner2='' ) self.shell.configurables.append(self)
def setUp(self) -> None: super().setUp() ip = get_ipython() if ip is None: ip = TerminalInteractiveShell().instance() self.ip = ip self.ip.magic('load_ext graph_notebook.magics') self.ip.run_cell_magic('graph_notebook_config', '', json.dumps(self.config.to_dict()))
def run(self): install.run(self) try: from IPython.terminal.interactiveshell import TerminalInteractiveShell startup_dir = TerminalInteractiveShell.instance().get_ipython( ).profile_dir.startup_dir copyfile(os.path.join(here, 'ipypandex', '__init__.py'), os.path.join(startup_dir, '90-ipypandex.py')) except ModuleNotFoundError: raise RuntimeError( "ipypandex requires an ipython installation to work")
def post_mortem(self): try: traceback.print_exc() tb = sys.exc_info()[2] if self.force_ipdb or is_ipython(): # https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py from IPython.terminal.interactiveshell import ( TerminalInteractiveShell) p = TerminalInteractiveShell().debugger_cls() p.botframe = sys._getframe().f_back # I do not know why this # is nessesary, but without this hack it does not work p.interaction(None, tb) else: pdb.post_mortem(tb) except Exception as e: print('#' * 40) print('debug_on does not alwais work with sacred. ' 'Use -D for sacred applications') print('#' * 40) raise e
def raw_input(self, *args, **kw): try: return TerminalInteractiveShell.raw_input(self, *args, **kw) # needs decoding (see below)? except EOFError: r = raw_input("Do you wish to commit your " "database changes? [yes]") if not r.lower().startswith("n"): if using_sqlalchemy: self.push("session.flush()") else: self.push("hub.commit()") raise EOFError
def run_ipython_shell_v11(globals, locals): """IPython shell from IPython version 0.11""" if SHELL_FIRST_TIME: banner = "Hit Ctrl-D to return to PuDB." SHELL_FIRST_TIME.pop() else: banner = "" try: # IPython 1.0 got rid of the frontend intermediary, and complains with # a deprecated warning when you use it. from IPython.terminal.interactiveshell import TerminalInteractiveShell from IPython.terminal.ipapp import load_default_config except ImportError: from IPython.frontend.terminal.interactiveshell import \ TerminalInteractiveShell from IPython.frontend.terminal.ipapp import load_default_config # XXX: in the future it could be useful to load a 'pudb' config for the # user (if it exists) that could contain the user's macros and other # niceities. config = load_default_config() shell = TerminalInteractiveShell.instance(config=config, banner2=banner) # XXX This avoids a warning about not having unique session/line numbers. # See the HistoryManager.writeout_cache method in IPython.core.history. shell.history_manager.new_session() # Save the originating namespace old_locals = shell.user_ns old_globals = shell.user_global_ns # Update shell with current namespace _update_ipython_ns(shell, globals, locals) args = [] if ipython_version() < (5, 0, 0): args.append(banner) else: print(banner) # XXX Quick and dirty way to fix issues with IPython 8.0.0+, introduced # by commit 08d54c0e367b535fd88aca5273fd09e5e70d08f8. # Setting _atexit_once_called = True will prevent call to # IPython.core.interactiveshell.InteractiveShell._atexit_once() from inside # IPython.terminal.interactiveshell.TerminalInteractiveShell.mainloop() # This allows us to repeatedly re-call mainloop() and the whole # run_ipython_shell_v11() function shell._atexit_once_called = True shell.mainloop(*args) del shell._atexit_once_called # Restore originating namespace _update_ipython_ns(shell, old_globals, old_locals)
def main(): """Use quickstart to ensure we have correct env, then execute imports in ipython and done.""" quickstart.main(quickstart.parser.parse_args(['--mk-virtualenv', sys.prefix])) print('Welcome to IPython designed for running CFME QE code.') ipython = TerminalInteractiveShell.instance() for code_import in IMPORTS: print('> {}'.format(code_import)) ipython.run_cell(code_import) from cfme.utils.path import conf_path custom_import_path = conf_path.join('miq_python_startup.py') if custom_import_path.exists(): with open(custom_import_path.strpath, 'r') as custom_import_file: custom_import_code = custom_import_file.read() print('Importing custom code:\n{}'.format(custom_import_code.strip())) ipython.run_cell(custom_import_code) else: print( 'You can create your own python file with imports you use frequently. ' 'Just create a conf/miq_python_startup.py file in your repo. ' 'This file can contain arbitrary python code that is executed in this context.') ipython.interact()
def interactive(idx=0, DongleClass=RfCat, intro=''): global d import rflib.chipcon_nic as rfnic import atexit d = DongleClass(idx=idx) d.setModeRX() # this puts the dongle into receive mode atexit.register(cleanupInteractiveAtExit) gbls = globals() lcls = locals() try: import IPython.Shell ipsh = IPython.Shell.IPShell(argv=[''], user_ns=lcls, user_global_ns=gbls) print intro ipsh.mainloop(intro) except ImportError, e: try: from IPython.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! ipsh.mainloop(intro) except ImportError, e: try: from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! ipsh.mainloop(intro) except ImportError, e: print e shell = code.InteractiveConsole(gbls) shell.interact(intro)
def interactive(port='/dev/ttyACM0', InterfaceClass=CanInterface, intro='', load_filename=None): global c import atexit c = InterfaceClass(port=port, load_filename=load_filename) atexit.register(cleanupInteractiveAtExit) gbls = globals() lcls = locals() try: import IPython.Shell ipsh = IPython.Shell.IPShell(argv=[''], user_ns=lcls, user_global_ns=gbls) print intro ipsh.mainloop(intro) except ImportError, e: try: from IPython.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! ipsh.mainloop(intro) except ImportError, e: try: from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell ipsh = TerminalInteractiveShell() ipsh.user_global_ns.update(gbls) ipsh.user_global_ns.update(lcls) ipsh.autocall = 2 # don't require parenthesis around *everything*. be smart! ipsh.mainloop(intro) except ImportError, e: print e shell = code.InteractiveConsole(gbls) shell.interact(intro)
def __init__(self, *args, **kwargs): #Initialization based on: from IPython.testing.globalipapp import start_ipython self._curr_exec_line = 0 # Store certain global objects that IPython modifies _displayhook = sys.displayhook _excepthook = sys.excepthook # Create and initialize our IPython instance. shell = TerminalInteractiveShell.instance() shell.showtraceback = _showtraceback # IPython is ready, now clean up some global state... # Deactivate the various python system hooks added by ipython for # interactive convenience so we don't confuse the doctest system sys.displayhook = _displayhook sys.excepthook = _excepthook # So that ipython magics and aliases can be doctested (they work by making # a call into a global _ip object). Also make the top-level get_ipython # now return this without recursively calling here again. get_ipython = shell.get_ipython try: import __builtin__ except: import builtins as __builtin__ __builtin__._ip = shell __builtin__.get_ipython = get_ipython # We want to print to stdout/err as usual. io.stdout = original_stdout io.stderr = original_stderr self._curr_exec_lines = [] self.ipython = shell
def ipython(): config = default_config() config.TerminalInteractiveShell.simple_prompt = True shell = TerminalInteractiveShell.instance(config=config) return shell
def start_ipython(): """Start a global IPython shell, which we need for IPython-specific syntax. """ global get_ipython # This function should only ever run once! if hasattr(start_ipython, 'already_called'): return start_ipython.already_called = True # Store certain global objects that IPython modifies _displayhook = sys.displayhook _excepthook = sys.excepthook _main = sys.modules.get('__main__') # Create custom argv and namespaces for our IPython to be test-friendly config = tools.default_config() # Create and initialize our test-friendly IPython instance. shell = TerminalInteractiveShell.instance(config=config, ) # A few more tweaks needed for playing nicely with doctests... # remove history file shell.tempfiles.append(config.HistoryManager.hist_file) # These traps are normally only active for interactive use, set them # permanently since we'll be mocking interactive sessions. shell.builtin_trap.activate() # Modify the IPython system call with one that uses getoutput, so that we # can capture subcommands and print them to Python's stdout, otherwise the # doctest machinery would miss them. shell.system = py3compat.MethodType(xsys, shell) shell._showtraceback = py3compat.MethodType(_showtraceback, shell) # IPython is ready, now clean up some global state... # Deactivate the various python system hooks added by ipython for # interactive convenience so we don't confuse the doctest system sys.modules['__main__'] = _main sys.displayhook = _displayhook sys.excepthook = _excepthook # So that ipython magics and aliases can be doctested (they work by making # a call into a global _ip object). Also make the top-level get_ipython # now return this without recursively calling here again. _ip = shell get_ipython = _ip.get_ipython builtin_mod._ip = _ip builtin_mod.get_ipython = get_ipython # To avoid extra IPython messages during testing, suppress io.stdout/stderr io.stdout = StreamProxy('stdout') io.stderr = StreamProxy('stderr') # Override paging, so we don't require user interaction during the tests. def nopage(strng, start=0, screen_lines=0, pager_cmd=None): print(strng) page.orig_page = page.page page.page = nopage return _ip
def raw_input(self, prompt=""): # TODO correct eof issue with custom prompt. prompt = "iridium >>>" return TerminalInteractiveShell.raw_input(self, prompt)
def __init__(self, color_scheme="NoColor", completekey=None, stdin=None, stdout=None): # Parent constructor: if has_pydb and completekey is None: OldPdb.__init__(self, stdin=stdin, stdout=io.stdout) else: OldPdb.__init__(self, completekey, stdin, stdout) # IPython changes... self.is_pydb = has_pydb self.shell = get_ipython() if self.shell is None: # No IPython instance running, we must create one from IPython.terminal.interactiveshell import TerminalInteractiveShell self.shell = TerminalInteractiveShell.instance() if self.is_pydb: # interactiveshell.py's ipalias seems to want pdb's checkline # which located in pydb.fn import pydb.fns self.checkline = lambda filename, lineno: pydb.fns.checkline(self, filename, lineno) self.curframe = None self.do_restart = self.new_do_restart self.old_all_completions = self.shell.Completer.all_completions self.shell.Completer.all_completions = self.all_completions self.do_list = decorate_fn_with_doc(self.list_command_pydb, OldPdb.do_list) self.do_l = self.do_list self.do_frame = decorate_fn_with_doc(self.new_do_frame, OldPdb.do_frame) self.aliases = {} # Create color table: we copy the default one from the traceback # module and add a few attributes needed for debugging self.color_scheme_table = exception_colors() # shorthands C = coloransi.TermColors cst = self.color_scheme_table cst["NoColor"].colors.prompt = C.NoColor cst["NoColor"].colors.breakpoint_enabled = C.NoColor cst["NoColor"].colors.breakpoint_disabled = C.NoColor cst["Linux"].colors.prompt = C.Green cst["Linux"].colors.breakpoint_enabled = C.LightRed cst["Linux"].colors.breakpoint_disabled = C.Red cst["LightBG"].colors.prompt = C.Blue cst["LightBG"].colors.breakpoint_enabled = C.LightRed cst["LightBG"].colors.breakpoint_disabled = C.Red self.set_colors(color_scheme) # Add a python parser so we can syntax highlight source while # debugging. self.parser = PyColorize.Parser() # Set the prompt Colors = cst.active_colors self.prompt = u"%s%s%s" % (Colors.prompt, prompt, Colors.Normal) # The default prompt is '(Pdb)'
def __init__(self,color_scheme='NoColor',completekey=None, stdin=None, stdout=None, context=5): # Parent constructor: try: self.context=int(context) if self.context <= 0: raise ValueError("Context must be a positive integer") except (TypeError, ValueError): raise ValueError("Context must be a positive integer") if has_pydb and completekey is None: OldPdb.__init__(self,stdin=stdin,stdout=io.stdout) else: OldPdb.__init__(self,completekey,stdin,stdout) # IPython changes... self.is_pydb = has_pydb self.shell = get_ipython() if self.shell is None: # No IPython instance running, we must create one from IPython.terminal.interactiveshell import \ TerminalInteractiveShell self.shell = TerminalInteractiveShell.instance() if self.is_pydb: # interactiveshell.py's ipalias seems to want pdb's checkline # which located in pydb.fn import pydb.fns self.checkline = lambda filename, lineno: \ pydb.fns.checkline(self, filename, lineno) self.curframe = None self.do_restart = self.new_do_restart self.old_all_completions = self.shell.Completer.all_completions self.shell.Completer.all_completions=self.all_completions self.do_list = decorate_fn_with_doc(self.list_command_pydb, OldPdb.do_list) self.do_l = self.do_list self.do_frame = decorate_fn_with_doc(self.new_do_frame, OldPdb.do_frame) self.aliases = {} # Create color table: we copy the default one from the traceback # module and add a few attributes needed for debugging self.color_scheme_table = exception_colors() # shorthands C = coloransi.TermColors cst = self.color_scheme_table cst['NoColor'].colors.prompt = C.NoColor cst['NoColor'].colors.breakpoint_enabled = C.NoColor cst['NoColor'].colors.breakpoint_disabled = C.NoColor cst['Linux'].colors.prompt = C.Green cst['Linux'].colors.breakpoint_enabled = C.LightRed cst['Linux'].colors.breakpoint_disabled = C.Red cst['LightBG'].colors.prompt = C.Blue cst['LightBG'].colors.breakpoint_enabled = C.LightRed cst['LightBG'].colors.breakpoint_disabled = C.Red self.set_colors(color_scheme) # Add a python parser so we can syntax highlight source while # debugging. self.parser = PyColorize.Parser() # Set the prompt - the default prompt is '(Pdb)' Colors = cst.active_colors if color_scheme == 'NoColor': self.prompt = prompt else: # The colour markers are wrapped by bytes 01 and 02 so that readline # can calculate the width. self.prompt = u'\x01%s\x02%s\x01%s\x02' % (Colors.prompt, prompt, Colors.Normal)
def main(): import optparse parser = optparse.OptionParser() # Shall we fuzz the request, response, or both? # Set via optparse in main global sr_request # search/replace tuple for requests - (True, [search, replace]) where true means to use regex global sr_response # search/replace tuple for responses - (True, [search, replace]) where true means to use regex global fuzz_request global fuzz_response # Other module-wide variables global debug global term global logger global fwdr parser.add_option("-l", "--local-addr", dest="local_addr", default="127.0.0.1", help="Local address to bind to") parser.add_option("-p", "--local-port", type="int", dest="local_port", default=1234, help="Local port to bind to") parser.add_option("-r", "--remote-addr", dest="remote_addr", help="Remote address to bind to") parser.add_option("-P", "--remote-port", type="int", dest="remote_port", default=80, help="Remote port to bind to") parser.add_option( "--search-request", dest="search_request", default="", help="String that if found will be replaced by --replace-request's value", ) parser.add_option( "--replace-request", dest="replace_request", default="", help="String to replace the value of --search-request" ) parser.add_option( "--search-response", dest="search_response", default="", help="String that if found will be replaced by --replace-request's value", ) parser.add_option( "--replace-response", dest="replace_response", default="", help="String to replace the value of --search-request", ) parser.add_option( "--regex-request", action="store_true", dest="request_use_regex", help="Requests: Use regular expressions for search and replace instead of string constants", ) parser.add_option( "--regex-response", action="store_true", dest="response_use_regex", help="Responses: Use regular expressions for search and replace instead of string constants", ) parser.add_option( "--fuzz-request", action="store_true", dest="fuzz_request", help="Fuzz the request which the proxy gets from the connecting client \ prior to sending it to the remote host", ) parser.add_option( "--fuzz-response", action="store_true", dest="fuzz_response", help="Fuzz the response which the proxy gets from the remote host prior \ to sending it to the conecting client", ) parser.add_option( "-i", "--run-info", dest="run_info", default="", help="Additional information string to add to database run_info entry", ) parser.add_option( "-d", "--debug", type="int", dest="debug", default=0, help="Debug level (0-5, 0: No debugging; 1: Simple conneciton \ information; 2: Simple data information; 3: Listener data display; 4: \ Sender data display; 5: All data display)", ) (options, args) = parser.parse_args() if not options.remote_addr or not options.remote_port: parser.print_help() exit(1) # Validate options for search/replace if (options.search_request and not options.replace_request) or ( options.replace_request and not options.search_request ): print >> sys.stderr, "Both --search-request and --replace-request must be provided together" exit(1) if (options.search_response and not options.replace_response) or ( options.replace_response and not options.search_response ): print >> sys.stderr, "Both --search-response and --replace-response must be provided together" exit(1) # Setup a TerminalController for formatted output term = TerminalController() # Print the current run information print ( term.render( """\nSetting up asynch. TCP proxy with the following settings: ${GREEN}Local binding Address: %s Local binding Port: %s${NORMAL} ${RED}Remote host address: %s Remote host port: %s${NORMAL} """ ) % (options.local_addr, options.local_port, options.remote_addr, options.remote_port) ) # Set the debug value debug = options.debug # If run info was passed in on the command line, use that for the run_info table # additional info field (It will have what's being fuzzed prepended to it as well) run_additional_info = options.run_info # Print the selected debug value if debug > 0: if debug == 1: print (" Debug: Level 1 (Show simple connection information)") elif debug == 2: print (" Debug: Level 2 (Show simple data information, such as the size of sent/received messages)") elif debug == 3: print (" Debug: Level 3 (Show listener data and size of sent/received messages)") elif debug == 4: print (" Debug: Level 4 (Show sender data and size of sent/received messages)") elif debug == 5: print ( " Debug: Level 5 (Show all possible information, including the size of sent/received messages, and their data for listener and sender)" ) print ("") # Display and setup search/replace things if options.search_request and options.replace_request: sr_request = [ None, options.search_request.decode("string-escape"), options.replace_request.decode("string-escape"), ] # Check if we want to use regex instead of string constants if options.request_use_regex: # Use regex instead of string replace print ( term.render( "Running regex search/replace on ${BOLD}REQUESTS${NORMAL} with regex: 's/%s/%s'" % (sr_request[1], sr_request[2]) ) ) sr_request[0] = True else: print ( term.render( "Running string search/replace on ${BOLD}REQUESTS${NORMAL} with search/replace: 's/%s/%s'" % (sr_request[1], sr_request[2]) ) ) sr_request[0] = False else: sr_request = None if options.search_response and options.replace_response: sr_response = [ None, options.search_response.decode("string-escape"), options.replace_response.decode("string-escape"), ] # Check if we want to use regex instead of string constants if options.response_use_regex: print ( term.render( "Running regex search/replace on ${BOLD}RESPONSES${NORMAL} with regex: 's/%s/%s'" % (sr_response[1], sr_response[2]) ) ) sr_response[0] = True else: print ( term.render( "Running string search/replace on ${BOLD}RESPONSES${NORMAL} with search/replace: 's/%s/%s'" % (sr_response[1], sr_response[2]) ) ) sr_response[0] = False else: sr_response = None # Setup which to fuzz - request, response, neither, both? if options.fuzz_request: fuzz_request = options.fuzz_request run_additional_info = "Fuzzing REQUESTS; " + run_additional_info print (term.render("Fuzzing ${BOLD}REQUESTS${NORMAL}")) else: fuzz_request = False if options.fuzz_response: fuzz_response = options.fuzz_response run_additional_info = "Fuzzing RESPONSES; " + run_additional_info print (term.render("Fuzzing ${BOLD}RESPONSES${NORMAL}")) else: fuzz_response = False if not (options.fuzz_response or options.fuzz_request): run_additional_info = "Fuzzing NONE; " + run_additional_info print ( term.render( "Fuzzing ${BOLD}<NOTHING>${NORMAL} (Maybe you wanted ${BOLD}--fuzz-request or --fuzz-response${NORMAL}?)" ) ) if fuzz_request and fuzz_response: print ( term.render( "${YELLOW}\nWARNING! WARNING!\n${BOLD}Fuzzing BOTH the request and response is probably a bad idea, ensure this is what you want to do!${NORMAL}${YELLOW}\nWARNING! WARNING!\n${NORMAL}" ) ) # host, db, username, passwd if logging_enabled: logger = postgresLogger("postgreshost", "dbname", "dbuser", "dbpass") logger.log_run_info("CompanyName", "ProjectName-v1.2.3", run_additional_info) # create object that spawns reciever/sender pairs upon connection fwdr = forwarder(options.local_addr, options.local_port, options.remote_addr, options.remote_port) print ("Listener running...") # asyncore.loop() # A quick hack to be able to control fuzz on/off while running # separate asyncore.loop into its own thread so we can have terminal control asyncThread = Thread(target=asyncore.loop) asyncThread.start() # start a console (ipython) from IPython.terminal.interactiveshell import TerminalInteractiveShell shell = TerminalInteractiveShell(user_ns=globals()) shell.mainloop() # cleanup otherwise thread wont die and program hangs fwdr.close() # asyncore.close_all() asyncThread._Thread__stop()