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 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 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() shellfailed = False 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: shellfailed = True if shellfailed: try: import IPython.Shell ipsh = IPython.Shell.IPShell(argv=[''], user_ns=lcls, user_global_ns=gbls) print(intro) ipsh.mainloop() except ImportError as e: shellfailed = True if shellfailed: 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 as e: shellfailed = True if shellfailed: print("falling back to straight Python... (%r)" % 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 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 interact(lcls, gbls): shellexception = None 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() except ImportError as e: shellexception = e if shellexception: try: import IPython.Shell ipsh = IPython.Shell.IPShell(argv=[''], user_ns=lcls, user_global_ns=gbls) ipsh.mainloop() except ImportError as e: shellexception = e if shellexception: 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() except ImportError as e: shellexception = e if shellexception: print("falling back to straight Python... (%r)" % shellexception) shell = code.InteractiveConsole(gbls) shell.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 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 __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 = 2 shell.show_banner(self._banner) q = RFQuack(self._transport, shell) q.idle() shell.push(dict(q=q, rfquack_pb2=rfquack_pb2)) shell.mainloop()
def main(): # pylint: disable=too-many-statements # Set up a simple argument parser. parser = binhoArgumentParser( description="Convenience shell for working with Binho host adapters.") parser.add_argument( "-e", "--exec", metavar="code", type=str, help="Executes the provided code as though it were passed " + "to a Binho host adapter shell, and then terminates.", dest="code", ) parser.add_argument( "-E", "--pre-exec", metavar="code", type=str, help="Executes the provided code as though it were passed " + "to a Binho host adapter shell, but does not explicitly terminate.", dest="prelude", ) parser.add_argument( "-f", "--file", metavar="file", type=str, help="Executes the relevant file before starting the given shell.", ) parser.add_argument( "-M", "--automagic", dest="automagic", action="store_true", help="Enable automagic, so lazy developers don't have to type %%.", ) parser.add_argument( "-P", "--avoid-parens", dest="avoidparens", action="store_true", help= "Enable full autocall, so bare methods are executed, rather than printed.", ) parser.add_argument( "-A", "--autoreload", dest="autoreload", action="store_true", help= "Attempts to reload python modules automatically as they change; so current objects import new \ functionality. This may sometimes break your shell.", ) parser.add_argument( "-S", "--singleton", dest="singleton", action="store_true", help= "Connect via a singleton that persists across device reconnects. Note: device state is not preserved.", ) args = parser.parse_args() if args.singleton: connect_function = parser.get_singleton_for_specified_device else: connect_function = parser.find_specified_device binho = connect_function() if binho.inBootloaderMode: print( "{} found on {}, but it cannot be used now because it's in DFU mode" .format(binho.productName, binho.commPort)) sys.exit(errno.ENODEV) elif binho.inDAPLinkMode: print( "{} found on {}, but it cannot be used now because it's in DAPlink mode" .format(binho.productName, binho.commPort)) print("Tip: Exit DAPLink mode using 'binho daplink -q' command") sys.exit(errno.ENODEV) # Break into IPython for the shell. if not args.code: print( "Spawning an IPython shell for easy access to your Binho host adapter." ) print( "Like normal python, you can use help(object) to get help for that object.\n" ) print( "Try help(binho.gpio) to see the documentation for the Binho host adapter GPIO;" ) print( "try dir(binho) to see a list of properties on the Binho Host Adapter object, and" ) print( "try binho.available_interfaces() and binho.available_programmers() to see" ) print( "the interfaces you can work with, and the programmers you can create.\n" ) singleton_text = "singleton " if args.singleton else "" print( "A Binho host adapter {}object has been created for you as 'binho'. Have fun!\n" .format(singleton_text)) # Create a new shell, and give it access to our created Binho object. shell = TerminalInteractiveShell() shell.push("binho") # Create nice aliases for our primary interfaces. # pylint: disable=unused-variable i2c = binho.i2c spi = binho.spi dac = binho.dac adc = binho.adc oneWire = binho.oneWire # uart = binho.uart gpio = binho.gpio # shell.push(('i2c', 'spi', 'adc', 'uart', 'gpio',)) shell.push(("i2c", "spi", "gpio", "dac", "adc", "oneWire")) # pylint: enable=unused-variable # Make the autoreload extension available. shell.extension_manager.load_extension("autoreload") # Add our magic commands, to make execution more 'fun'. shell.register_magics(binhoShellMagics) # If the user has requested automagic, let them have their automagic. if args.automagic: shell.automagic = True # If we're in avoid parenthesis mode if args.avoidparens: shell.autocall = 2 # If we're using autoreload, enable that. if args.autoreload: shell.run_cell("%autoreload 2") print( "Heads up: you've enabled autoreload. Things make break in unexpected ways as your code changes." ) print( "You can fix this by adjusting your expectations regarding breakage.\n" ) # Handle any inline execution requested. if args.code or args.prelude: # Replace any ;'s with newlines, so we can execute more than one # statement. code = args.code or args.prelude code = re.sub(r";\s*", "\n", code) lines = code.split("\n") # If we're in execute-and-quit mode, do so. for line in lines: shell.run_cell(line, shell_futures=True) # If we're to exit after running the relevant code, do so. if args.code: sys.exit(0) # If we have a file to execute, execute it. if args.file: shell.safe_execfile_ipy(args.file, shell_futures=True, raise_exceptions=True) # Run the shell itself. shell.connect_function = connect_function shell.mainloop() # close the connection to the device binho.close()
def main(): # Set up a simple argument parser. parser = GreatFETArgumentParser( description="Convenience shell for working with GreatFET devices.") parser.add_argument( '-e', '--exec', metavar="code", type=str, help="Executes the provided code as though it were passed " + "to a greatfet shell, and then terminates.", dest="code") parser.add_argument( '-E', '--pre-exec', metavar="code", type=str, help="Executes the provided code as though it were passed " + "to a greatfet shell, but does not explicitly terminate.", dest="prelude") parser.add_argument( '-f', '--file', metavar="file", type=str, help="Executes the relevant file before starting the given shell.") parser.add_argument( '-M', '--automagic', dest="automagic", action='store_true', help="Enable automagic, so lazy developers don't have to type %%.") parser.add_argument( '-P', '--avoid-parens', dest="avoidparens", action='store_true', help= "Enable full autocall, so bare methods are executed, rather than printed." ) parser.add_argument( '-A', '--autoreload', dest="autoreload", action='store_true', help= "Attempts to reload python modules automatically as they change; so current objects import new functionality. This may sometimes break your shell." ) parser.add_argument( '-S', '--singleton', dest="singleton", action='store_true', help= "Connect via a singleton that persists across device reconnects. Note: device state is not preserved." ) args = parser.parse_args() if args.singleton: connect_function = parser.get_singleton_for_specified_device else: connect_function = parser.find_specified_device gf = connect_function() # Break into IPython for the shell. if not args.code: print( "Spawning an IPython shell for easy access to your GreatFET board." ) print( "Like normal python, you can use help(object) to get help for that object.\n" ) print( "Try help(gf.gpio) to see the documentation for the GreatFET GPIO;" ) print( "try dir(gf) to see a list of properties on the GreatFET object, and" ) print( "try gf.available_interfaces() and gf.available_programmers() to see" ) print( "the interfaces you can work with, and the programmers you can create.\n" ) print( "This GreatFET shell is *magical*. Try some of our IPython magics:\n" ) print("\t %dmesg -- prints the GreatFET's debug ring (log)") print("\t %reconnect -- tries to reconnect to the current GreatFET") print( "\t %makeflash -- when run from a firmware build dir, builds and flashes your GreatFET" ) print( "\t %reload -- tries to reload your host python code; useful with $PYTHONPATH" ) print( "\t %refet -- (not %reset) resets and reconnects to the current GreatFET" ) print("\t [hey, %reset was taken!]\n\n") singleton_text = "singleton " if args.singleton else "" print( "A GreatFET {}object has been created for you as 'gf'. Have fun!\n" .format(singleton_text)) # Create a new shell, and give it access to our created GreatFET object. shell = TerminalInteractiveShell() shell.push('gf') # Create nice aliases for our primary interfaces. i2c = gf.i2c spi = gf.spi adc = gf.adc uart = gf.uart gpio = gf.gpio shell.push(( 'i2c', 'spi', 'adc', 'uart', 'gpio', )) # Make the autoreload extension available. shell.extension_manager.load_extension('autoreload') # Add our magic commands, to make execution more 'fun'. shell.register_magics(GreatFETShellMagics) # If the user has requested automagic, let them have their automagic. if args.automagic: shell.automagic = True # If we're in avoid parenthesis mode if args.avoidparens: shell.autocall = 2 # If we're using autoreload, enable that. if args.autoreload: shell.run_cell('%autoreload 2') print( "Heads up: you've enabled autoreload. Things make break in unexpected ways as your code changes." ) print( "You can fix this by adjusting your expectations regarding breakage.\n" ) # Handle any inline execution requested. if args.code or args.prelude: # Replace any ;'s with newlines, so we can execute more than one statement. code = args.code or args.prelude code = re.sub(r";\s*", "\n", code) lines = code.split("\n") # If we're in execute-and-quit mode, do so. for line in lines: shell.run_cell(line, shell_futures=True) # If we're to exit after running the relevant code, do so. if args.code: sys.exit(0) # If we have a file to execute, execute it. if args.file: shell.safe_execfile_ipy(args.file, shell_futures=True, raise_exceptions=True) # Run the shell itself. shell.connect_function = connect_function shell.mainloop()