def test_fromdictmerge2(self): c1 = Config({'Foo' : {'baz' : 2}}) c2 = Config({'Foo' : {'bar' : 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1) self.assertEqual(c1.Foo.baz, 2) self.assertNotIn('baz', c2.Foo)
def test_fromdictmerge2(self): c1 = Config({'Foo' : {'baz' : 2}}) c2 = Config({'Foo' : {'bar' : 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1) self.assertEqual(c1.Foo.baz, 2) self.assertRaises(AttributeError, getattr, c2.Foo, 'baz')
def shellwidget_config(self): """ Generate a Config instance for shell widgets using our config system This lets us create each widget with its own config (as opposed to IPythonQtConsoleApp, where all widgets have the same config) """ # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), 'profile_default') full_ip_cfg = load_pyconfig_files(['ipython_qtconsole_config.py'], profile_path) # From the full config we only select the IPythonWidget section # because the others have no effect here. ip_cfg = Config({'IPythonWidget': full_ip_cfg.IPythonWidget}) except: ip_cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Make the pager widget a rich one (i.e a QTextEdit) spy_cfg.IPythonWidget.kind = 'rich' # Gui completion widget completion_type_o = CONF.get('ipython_console', 'completion_type') completions = {0: "droplist", 1: "ncurses", 2: "plain"} spy_cfg.IPythonWidget.gui_completion = completions[completion_type_o] # Pager pager_o = self.get_option('use_pager') if pager_o: spy_cfg.IPythonWidget.paging = 'inside' else: spy_cfg.IPythonWidget.paging = 'none' # Calltips calltips_o = self.get_option('show_calltips') spy_cfg.IPythonWidget.enable_calltips = calltips_o # Buffer size buffer_size_o = self.get_option('buffer_size') spy_cfg.IPythonWidget.buffer_size = buffer_size_o # Prompts in_prompt_o = self.get_option('in_prompt') out_prompt_o = self.get_option('out_prompt') if in_prompt_o: spy_cfg.IPythonWidget.in_prompt = in_prompt_o if out_prompt_o: spy_cfg.IPythonWidget.out_prompt = out_prompt_o # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones ip_cfg._merge(spy_cfg) return ip_cfg
def test_merge_copies(self): c = Config() c2 = Config() c2.Foo.trait = [] c.merge(c2) c2.Foo.trait.append(1) self.assertIsNot(c.Foo, c2.Foo) self.assertEqual(c.Foo.trait, []) self.assertEqual(c2.Foo.trait, [1])
def test_auto_section(self): c = Config() self.assertEqual("A" in c, True) self.assertEqual(c._has_section("A"), False) A = c.A A.foo = "hi there" self.assertEqual(c._has_section("A"), True) self.assertEqual(c.A.foo, "hi there") del c.A self.assertEqual(len(c.A.keys()), 0)
def test_auto_section(self): c = Config() self.assertEqual('A' in c, True) self.assertEqual(c._has_section('A'), False) A = c.A A.foo = 'hi there' self.assertEqual(c._has_section('A'), True) self.assertEqual(c.A.foo, 'hi there') del c.A self.assertEqual(len(c.A.keys()),0)
def test_deepcopy(self): c1 = Config() c1.Foo.bar = 10 c1.Foo.bam = 30 c1.a = 'asdf' c1.b = range(10) import copy c2 = copy.deepcopy(c1) self.assertEqual(c1, c2) self.assertTrue(c1 is not c2) self.assertTrue(c1.Foo is not c2.Foo)
def test_auto_section(self): c = Config() self.assertNotIn('A', c) assert not c._has_section('A') A = c.A A.foo = 'hi there' self.assertIn('A', c) assert c._has_section('A') self.assertEqual(c.A.foo, 'hi there') del c.A self.assertEqual(c.A, Config())
def test_merge_exists(self): c1 = Config() c2 = Config() c1.Foo.bar = 10 c1.Foo.bam = 30 c2.Foo.bar = 20 c2.Foo.wow = 40 c1.merge(c2) self.assertEqual(c1.Foo.bam, 30) self.assertEqual(c1.Foo.bar, 20) self.assertEqual(c1.Foo.wow, 40) c2.Foo.Bam.bam = 10 c1.merge(c2) self.assertEqual(c1.Foo.Bam.bam, 10)
def test_collision(self): a = Config() b = Config() self.assertEqual(a.collisions(b), {}) a.A.trait1 = 1 b.A.trait2 = 2 self.assertEqual(a.collisions(b), {}) b.A.trait1 = 1 self.assertEqual(a.collisions(b), {}) b.A.trait1 = 0 self.assertEqual(a.collisions(b), { 'A': { 'trait1': "1 ignored, using 0", } }) self.assertEqual(b.collisions(a), { 'A': { 'trait1': "0 ignored, using 1", } }) a.A.trait2 = 3 self.assertEqual(b.collisions(a), { 'A': { 'trait1': "0 ignored, using 1", 'trait2': "2 ignored, using 3", } })
def load_default_config(ipython_dir=None): """Load the default config file from the default ipython_dir. This is useful for embedded shells. """ if ipython_dir is None: ipython_dir = get_ipython_dir() profile_dir = os.path.join(ipython_dir, 'profile_default') config = Config() for cf in Application._load_config_files("ipython_config", path=profile_dir): config.update(cf) return config
def test_deepcopy(self): c1 = Config() c1.Foo.bar = 10 c1.Foo.bam = 30 c1.a = 'asdf' c1.b = range(10) c1.Test.logger = logging.Logger('test') c1.Test.get_logger = logging.getLogger('test') c2 = copy.deepcopy(c1) self.assertEqual(c1, c2) self.assertTrue(c1 is not c2) self.assertTrue(c1.Foo is not c2.Foo) self.assertTrue(c1.Test is not c2.Test) self.assertTrue(c1.Test.logger is c2.Test.logger) self.assertTrue(c1.Test.get_logger is c2.Test.get_logger)
def test_custom(self): config = Config() config.foo = 'foo' config.bar = 'bar' c1 = Configurable(config=config) c2 = Configurable(config=c1.config) c3 = Configurable(config=c2.config) self.assertEqual(c1.config, config) self.assertEqual(c2.config, config) self.assertEqual(c3.config, config) # Test that copies are not made self.assertTrue(c1.config is config) self.assertTrue(c2.config is config) self.assertTrue(c3.config is config) self.assertTrue(c1.config is c2.config) self.assertTrue(c2.config is c3.config)
def test_custom(self): config = Config() config.foo = 'foo' config.bar = 'bar' c1 = Component(None, config=config) c2 = Component(c1) c3 = Component(c2) self.assertEquals(c1.config, config) self.assertEquals(c2.config, config) self.assertEquals(c3.config, config) # Test that copies are not made self.assert_(c1.config is config) self.assert_(c2.config is config) self.assert_(c3.config is config) self.assert_(c1.config is c2.config) self.assert_(c2.config is c3.config)
def test_merge_doesnt_exist(self): c1 = Config() c2 = Config() c2.bar = 10 c2.Foo.bar = 10 c1.merge(c2) self.assertEqual(c1.Foo.bar, 10) self.assertEqual(c1.bar, 10) c2.Bar.bar = 10 c1.merge(c2) self.assertEqual(c1.Bar.bar, 10)
def merge_configs(self): """Merge the default, command line and file config objects.""" config = Config() config._merge(self.default_config) config._merge(self.file_config) config._merge(self.command_line_config) # XXX fperez - propose to Brian we rename master_config to simply # config, I think this is going to be heavily used in examples and # application code and the name is shorter/easier to find/remember. # For now, just alias it... self.master_config = config self.config = config
def load_default_config(ipython_dir=None): """Load the default config file from the default ipython_dir. This is useful for embedded shells. """ if ipython_dir is None: ipython_dir = get_ipython_dir() profile_dir = os.path.join(ipython_dir, 'profile_default') cl = PyFileConfigLoader(default_config_file_name, profile_dir) try: config = cl.load_config() except ConfigFileNotFound: # no config found config = Config() return config
def build_extra_config(self): self.extra_config = Config() self.extra_config.Exporter.preprocessors = [ 'nbgrader.preprocessors.IncludeHeaderFooter', 'nbgrader.preprocessors.CheckGradeIds', 'nbgrader.preprocessors.LockCells', 'nbgrader.preprocessors.ClearSolutions', 'IPython.nbconvert.preprocessors.ClearOutputPreprocessor', 'nbgrader.preprocessors.ComputeChecksums' ] if self.save_cells: self.extra_config.Exporter.preprocessors.append( 'nbgrader.preprocessors.SaveGradeCells' ) self.config.merge(self.extra_config)
def load_file_config(self): """Load the config file. This tries to load the config file from disk. If successful, the ``CONFIG_FILE`` config variable is set to the resolved config file location. If not successful, an empty config is used. """ self.log.debug("Attempting to load config file: %s" % self.config_file_name) loader = PyFileConfigLoader(self.config_file_name, path=self.config_file_paths) try: self.file_config = loader.load_config() self.file_config.Global.config_file = loader.full_filename except IOError: # Only warn if the default config file was NOT being used. if not self.config_file_name==self.default_config_file_name: self.log.warn("Config file not found, skipping: %s" % self.config_file_name, exc_info=True) self.file_config = Config() except: self.log.warn("Error loading config file: %s" % self.config_file_name, exc_info=True) self.file_config = Config()
def test_hist_file_config(): cfg = Config() tfile = tempfile.NamedTemporaryFile(delete=False) cfg.HistoryManager.hist_file = tfile.name try: hm = HistoryManager(shell=get_ipython(), config=cfg) nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file) finally: try: os.remove(tfile.name) except OSError: # same catch as in testing.tools.TempFileMixin # On Windows, even though we close the file, we still can't # delete it. I have no clue why pass
def handle(self, *args, **kwargs): imported_objects = { 'config': self.config, } try: # Try IPython imports from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell from IPython.config.loader import Config cfg = Config() shell = TerminalInteractiveShell(config=cfg, user_ns=imported_objects) shell.mainloop(display_banner='\n Samovar \n') except: import code code.InteractiveConsole(locals=imported_objects).interact()
def _launch_ipshell(pid, session): # type: (int, GdbWrapper) -> None from IPython.terminal.embed import InteractiveShellEmbed from IPython.config.loader import Config banner = """ === SHAMIKO SHELL === Opened a session to pid={}. You can access it from the variable `session`. ===================== """.format(pid) ipshell = InteractiveShellEmbed(config=Config(), banner1=banner, exit_msg="Bye.") ipshell()
def interact(self, vars=None): if vars is not None: self.ns.update(vars) configure_locale() banner = self.get_console_banner() # PyCharm doesn't support colors and tabs if USE_IPYTHON: config = Config() if 'PYCHARM_HOSTED' in os.environ: config.TerminalInteractiveShell.colors = 'NoColor' embed(config=config, user_ns=self.ns, banner1=banner) else: readline.parse_and_bind("tab: complete") code.interact(local=self.ns, banner=banner)
def shell(**kwargs): try: from IPython import embed from IPython.config.loader import Config cfg = Config() cfg.InteractiveShellEmbed.confirm_exit = False embed(config=cfg, banner1="Caliop Shell") except ImportError: # try ~IPython-0.10 API try: from IPython.Shell import IPShellEmbed as embed ipshell = embed(banner="Caliop Shell") ipshell() except ImportError: import code code.interact("Caliop Shell", local=locals())
def test_merge_exists(self): c1 = Config() c2 = Config() c1.Foo.bar = 10 c1.Foo.bam = 30 c2.Foo.bar = 20 c2.Foo.wow = 40 c1._merge(c2) self.assertEquals(c1.Foo.bam, 30) self.assertEquals(c1.Foo.bar, 20) self.assertEquals(c1.Foo.wow, 40) c2.Foo.Bam.bam = 10 c1._merge(c2) self.assertEquals(c1.Foo.Bam.bam, 10)
def main_shell(user_ns): try: from IPython import embed from IPython.config.loader import Config except ImportError: print("ERROR: The shell dosen't work on CCS RHEL " "hosts since IPython (EPEL) is missing.") return auth_url = user_ns['OS_AUTH_URL'] cloud_name = urlparse(auth_url).netloc.split('.', 1)[0] cfg = Config() cfg.PromptManager.in_template = '%s <\\#>: ' % cloud_name embed(config=cfg, user_ns=user_ns, banner1=('\n# Connected to %s' % auth_url) + INTRODUCTION)
def test_autorestore(): ip.user_ns['foo'] = 95 ip.magic('store foo') del ip.user_ns['foo'] c = Config() c.StoreMagics.autorestore = False orig_config = ip.config try: ip.config = c ip.extension_manager.reload_extension('storemagic') nt.assert_not_in('foo', ip.user_ns) c.StoreMagics.autorestore = True ip.extension_manager.reload_extension('storemagic') nt.assert_equal(ip.user_ns['foo'], 95) finally: ip.config = orig_config
def setup_shell(): banner = '+----------------------------------------------------------------+\n' banner += ' APSG ' banner += APSG_VERSION banner += ' [interactive shell] - http://ondrolexa.github.io/apsg\n' banner += '+----------------------------------------------------------------+\n' banner += '\n' banner += 'Commands: \n' banner += '\t"exit()" or press "Ctrl+ D" to exit the shell\n' banner += '\t"clear" to clear the shell screen\n' banner += '\n' banner += 'Documentation:\n' banner += '\thelp(Fol), ?Fol, Fol?, or Fol()? all do the same\n' banner += '\t"docs" will launch webbrowser showing documentation' banner += '\n' exit_msg = '\n... [Exiting the APSG interactive shell] ...\n' try: import IPython except: raise("ERROR: IPython Failed to load") try: from IPython.config.loader import Config from IPython.terminal.embed import InteractiveShellEmbed cfg = Config() cfg.PromptManager.in_template = "APSG:\\#> " cfg.PromptManager.out_template = "APSG:\\#: " apsgShell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg = exit_msg) apsgShell.define_magic("clear", magic_clear) #apsgShell.define_magic("docs", magic_docs) except ImportError: try: from IPython.Shell import IPShellEmbed argsv = ['-pi1','APSG:\\#>','-pi2',' .\\D.:','-po','APSG:\\#>','-nosep'] apsgShell = IPShellEmbed(argsv) apsgShell.set_banner(banner) apsgShell.set_exit_msg(exit_msg) apsgShell.IP.api.expose_magic("clear", magic_clear) except ImportError: raise return apsgShell
def launch_shell(self, argv=[]): # Configure prompts and messages in_template = 'L2A: In <\\#>: ' in_template2 = ' .\\D.:' out_template = 'L2A: Out<\\#>: ' banner = '*** Launcing L2 Analysis Shell ***\nAvailable analysis routines:\n%s' % self.formatted_routine_names( ) exit_msg = '*** Bye ***' # Set pylab environment as interactive pylab.interactive(True) # Try using an older version of IPython first try: argv += [ '-pi1', in_template, '-pi2', in_template2, '-po', out_template ] from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed(argv, banner=banner, exit_msg=exit_msg) ipshell(local_ns=self._local_namespace, global_ns=self._global_namespace) except ImportError as imp_err: # Newer version of IPython, 0.11 onward use this interface from IPython.config.loader import Config cfg = Config() prompt_config = cfg.PromptManager prompt_config.in_template = in_template prompt_config.in2_template = in_template2 prompt_config.out_template = out_template from IPython.frontend.terminal.embed import InteractiveShellEmbed ipshell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg=exit_msg) # There is no access to global namespace in this version of IPython # put everything into the local namespace namespace = {} namespace.update(self._local_namespace) namespace.update(self._global_namespace) ipshell(local_ns=namespace)
def start_kernel(self, kernel_id=None, config=None, resource_limits=None, logfile=None): """ A function for starting new kernels by forking. :arg str kernel_id: the id of the kernel to be started. if no id is passed, a uuid will be generated :arg Ipython.config.loader config: kernel configuration :arg dict resource_limits: a dict with keys resource.RLIMIT_* (see config_default documentation for explanation of valid options) and values of the limit for the given resource to be set in the kernel process :returns: kernel id and connection information which includes the kernel's ip, session key, and shell, heartbeat, stdin, and iopub port numbers :rtype: dict """ if kernel_id is None: kernel_id = str(uuid.uuid4()) if config is None: config = Config({"ip": self.ip}) if resource_limits is None: resource_limits = {} config.HistoryManager.enabled = False dir = os.path.join(self.dir, kernel_id) try: os.mkdir(dir) except OSError as e: # TODO: take care of race conditions and other problems with us # using an 'unclean' directory pass currdir = os.getcwd() os.chdir(dir) p, q = Pipe() proc = Process(target=self.fork_kernel, args=(config, q, resource_limits, logfile)) proc.start() os.chdir(currdir) # todo: yield back to the message processing while we wait if p.poll(2): connection = p.recv() p.close() self.kernels[kernel_id] = (proc, connection) return {"kernel_id": kernel_id, "connection": connection} else: p.close() self.kill_process(proc) raise KernelError("Could not start kernel")
def prepare_container(): import threading threading.current_thread().name = "CC-Main" # SIDE EFFECT: The import of pyon.public triggers many module initializers: # pyon.core.bootstrap (Config load, logging setup), etc. from pyon.public import Container, CFG from pyon.util.containers import dict_merge from pyon.util.config import Config # Check if user opted to override logging config # Requires re-initializing logging if opts.logcfg: from pyon.util.config import LOGGING_CFG, logging_conf_paths, read_logging_config, initialize_logging import ast # Dict of config values if '{' in opts.logcfg: try: eval_value = ast.literal_eval(opts.logcfg) except ValueError: raise Exception("Value error in logcfg arg '%s'" % opts.logcfg) dict_merge(LOGGING_CFG, eval_value) initialize_logging() # YAML file containing config values else: logging_conf_paths.append(opts.logcfg) read_logging_config() initialize_logging() # Set that system is not testing. We are running as standalone container dict_merge(CFG, {'system': {'testing': False}}, True) # Also set the immediate flag, but only if specified - it is an override if opts.immediate: dict_merge(CFG, {'system': {'immediate': True}}, True) # Load any additional config paths and merge them into main config if len(opts.config): ipython_cfg = Config(opts.config) dict_merge(CFG, ipython_cfg.data, True) # Create the container instance container = Container(*args, **kwargs) return container
def test_multi_parent(self): cfg = Config({ 'MyParent2' : { 'MyParent' : { 'MyConfigurable' : { 'b' : 2.0, } }, # this one shouldn't count 'MyConfigurable' : { 'b' : 3.0, }, } }) parent2 = MyParent2(config=cfg) parent = MyParent(parent=parent2) myc = MyConfigurable(parent=parent) self.assertEqual(myc.b, parent.config.MyParent2.MyParent.MyConfigurable.b)
def test_parent_priority(self): cfg = Config({ 'MyConfigurable': { 'b': 2.0, }, 'MyParent': { 'MyConfigurable': { 'b': 3.0, } }, 'MyParent2': { 'MyConfigurable': { 'b': 4.0, } } }) parent = MyParent2(config=cfg) myc = MyConfigurable(parent=parent) self.assertEqual(myc.b, parent.config.MyParent2.MyConfigurable.b)
def IPShell(argv=None, user_ns=None, banner=None): if argv is None: argv = [] try: from IPython.terminal.embed import InteractiveShellEmbed from IPython.config.loader import Config cfg = Config() cfg.InteractiveShellEmbed.autocall = 2 shell = InteractiveShellEmbed(config=cfg, user_ns=user_ns, banner2=banner) shell(local_ns=user_ns) except ImportError: from IPython import Shell # IPython < 0.11 Shell.IPShell(argv=argv, user_ns=user_ns).mainloop(banner=banner)
def Shell(user_session): # This should bring back the old autocall behaviour. e.g.: # In [1]: pslist cfg = Config() cfg.InteractiveShellEmbed.autocall = 2 cfg.PromptManager.in_template = ( r'{color.LightCyan}' r'{session.state.base_filename}' r'{color.LightBlue}{color.Green} \T> ') cfg.PromptManager.in2_template = ( r'{color.Green}|{color.LightGreen}\D{color.Green}> ') cfg.PromptManager.out_template = r'Out<\#> ' cfg.InteractiveShell.separate_in = '' cfg.InteractiveShell.separate_out = '' cfg.InteractiveShell.separate_out2 = '' shell = embed.InteractiveShellEmbed( config=cfg, user_ns=user_session._locals) shell.Completer.merge_completions = False shell.banner = constants.BANNER shell.exit_msg = constants.GetQuote() shell.set_custom_completer(RekallCompleter, 0) # Do we need to pre-run something? if user_session.run is not None: execfile(user_session.run, user_session._locals) # Workaround for completer bug. import IPython.core.completerlib IPython.core.completerlib.get_ipython = lambda: shell # Set known delimeters for the completer. This varies by OS so we need to # set it to ensure consistency. readline.set_completer_delims(' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?') shell(local_ns=user_session._locals) return True
def interact(self, vars=None): if vars is not None: self.ns.update(vars) # Set the default encoding to utf-8, pango/gtk normally does # this but we don't want to import that here. import sys reload(sys) sys.setdefaultencoding('utf-8') banner = self.get_console_banner() # PyCharm doesn't support colors and tabs if USE_IPYTHON: config = Config() if 'PYCHARM_HOSTED' in os.environ: config.TerminalInteractiveShell.colors = 'NoColor' embed(config=config, user_ns=self.ns, banner1=banner) else: readline.parse_and_bind("tab: complete") code.interact(local=self.ns, banner=banner)
def test_parent(self): class Foo(Component): a = Int(0, config=True) b = Str('nope', config=True) class Bar(Foo): b = Str('gotit', config=False) c = Float(config=True) config = Config() config.Foo.a = 10 config.Foo.b = "wow" config.Bar.b = 'later' config.Bar.c = 100.0 f = Foo(None, config=config) b = Bar(f) self.assertEquals(f.a, 10) self.assertEquals(f.b, 'wow') self.assertEquals(b.b, 'gotit') self.assertEquals(b.c, 100.0)
def setup_ipython(): try: import IPython from IPython.config.loader import Config from IPython.terminal.embed import InteractiveShellEmbed cfg = Config() cfg.PromptManager.in_template = "MyCV:\\#> " cfg.PromptManager.out_template = "MyCV:\\#: " #~ cfg.InteractiveShellEmbed.prompt_in1 = "MyCV:\\#> " #~ cfg.InteractiveShellEmbed.prompt_out="MyCV:\\#: " scvShell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg=exit_msg) scvShell.define_magic("tutorial", magic_tutorial) scvShell.define_magic("clear", magic_clear) scvShell.define_magic("example", magic_examples) scvShell.define_magic("forums", magic_forums) scvShell.define_magic("walkthrough", magic_walkthrough) scvShell.define_magic("docs", magic_docs) except ImportError: try: from IPython.Shell import IPShellEmbed argsv = [ '-pi1', 'MyCV:\\#>', '-pi2', ' .\\D.:', '-po', 'MyCV:\\#>', '-nosep' ] scvShell = IPShellEmbed(argsv) scvShell.set_banner(banner) scvShell.set_exit_msg(exit_msg) scvShell.IP.api.expose_magic("tutorial", magic_tutorial) scvShell.IP.api.expose_magic("clear", magic_clear) scvShell.IP.api.expose_magic("example", magic_examples) scvShell.IP.api.expose_magic("forums", magic_forums) scvShell.IP.api.expose_magic("walkthrough", magic_walkthrough) scvShell.IP.api.expose_magic("docs", magic_docs) except ImportError: raise return scvShell()
def command(self): #get SqlAlchemy session self._init_session() # imports, used in ipython shell import os import sys import time import shutil import datetime from rhodecode.model.db import * try: from IPython import embed from IPython.config.loader import Config cfg = Config() cfg.InteractiveShellEmbed.confirm_exit = False embed(config=cfg, banner1="RhodeCode IShell.") except ImportError: print 'ipython installation required for ishell' sys.exit(-1)
def interact(scope): banner = "gem5 Interactive Console" ipshell = None prompt_in1 = "gem5 \\#> " prompt_out = "gem5 \\#: " # Is IPython version 0.10 or earlier available? try: from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed( argv=["-prompt_in1", prompt_in1, "-prompt_out", prompt_out], banner=banner, user_ns=scope) except ImportError: pass # Is IPython version 0.11 or later available? if not ipshell: try: import IPython from IPython.config.loader import Config from IPython.terminal.embed import InteractiveShellEmbed cfg = Config() cfg.PromptManager.in_template = prompt_in1 cfg.PromptManager.out_template = prompt_out ipshell = InteractiveShellEmbed(config=cfg, user_ns=scope, banner1=banner) except ImportError: pass if ipshell: ipshell() else: # Use the Python shell in the standard library if IPython # isn't available. code.InteractiveConsole(scope).interact(banner)
def interact(self, vars=None): # Keep this imports here since they only work on linux import readline import code import rlcompleter rlcompleter # pylint: disable=W0104 if vars is not None: self.ns.update(vars) configure_locale() banner = self.get_console_banner() # PyCharm doesn't support colors and tabs if USE_IPYTHON: config = Config() if 'PYCHARM_HOSTED' in os.environ: config.TerminalInteractiveShell.colors = 'NoColor' embed(config=config, user_ns=self.ns, banner1=banner) else: readline.parse_and_bind("tab: complete") code.interact(local=self.ns, banner=banner)
def IPShell(argv=None, user_ns=None, banner=None): if argv is None: argv = [] try: # pylint: disable=g-import-not-at-top from IPython.terminal.embed import InteractiveShellEmbed from IPython.config.loader import Config # pylint: enable=g-import-not-at-top cfg = Config() cfg.InteractiveShellEmbed.autocall = 2 shell = InteractiveShellEmbed(config=cfg, user_ns=user_ns) shell(local_ns=user_ns) except ImportError: # pylint: disable=g-import-not-at-top from IPython import Shell # pylint: enable=g-import-not-at-top # IPython < 0.11 Shell.IPShell(argv=argv, user_ns=user_ns).mainloop(banner=banner)
def breakpoint(scope=None, global_scope=None): import traceback from IPython.config.loader import Config ipy_config = Config() ipy_config.PromptManager.in_template = '><> ' ipy_config.PromptManager.in2_template = '... ' ipy_config.PromptManager.out_template = '--> ' ipy_config.InteractiveShellEmbed.confirm_exit = False # First import the embeddable shell class from IPython.frontend.terminal.embed import InteractiveShellEmbed from mock import patch if scope is not None: locals().update(scope) if global_scope is not None: globals().update(global_scope) # Update namespace of interactive shell # TODO: Cleanup namespace even further # Now create an instance of the embeddable shell. The first argument is a # string with options exactly as you would type them if you were starting # IPython at the system command line. Any parameters you want to define for # configuration can thus be specified here. with patch( "IPython.core.interactiveshell.InteractiveShell.init_virtualenv"): ipshell = InteractiveShellEmbed(config=ipy_config, banner1="Entering Breakpoint Shell", exit_msg='Returning...') stack = traceback.extract_stack(limit=2) message = 'File %s, line %s, in %s' % stack[0][:-1] try: import growl growl.growl('breakpoint', 'Ready') except: pass ipshell('(%s) Breakpoint @ %s' % ('breakpoint', message))
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] settings = get_appsettings(config_uri) engine = create_engine('pyshop', settings, scoped=False) config = Configurator(settings=settings) config.end() from pyshop.models import (User, Group, Classifier, Package, Release, ReleaseFile) session = DBSession() try: from IPython import embed from IPython.config.loader import Config cfg = Config() cfg.InteractiveShellEmbed.confirm_exit = False embed(config=cfg, banner1="Welcome to PyShop shell.") except ImportError: import code code.interact("pyshop shell", local=locals())
def ipython(globals=None, locals=None): """Interactive python prompt (see :ref:`Example: IPython <example IPython>`).""" # NOTE: WE ASSUME IPython HAS BEEN ALREADY IMPORTED! # We use an embedded ipython session # (http://ipython.scipy.org/doc/manual/node9.html) # to inspect the current state. The calling_frame magic is necessary # to get the context of the place where this ipython() function is called # (and not where IPShellEmded([]) is invoked. calling_frame = sys._getframe(1) if globals == None: globals = calling_frame.f_globals if locals == None: locals = calling_frame.f_locals import IPython if hasattr(IPython, "InteractiveShell"): from IPython.config.loader import Config cfg = Config() IPython.embed(config=cfg) else: from IPython.Shell import IPShellEmbed IPShellEmbed([])(local_ns=locals, global_ns=globals)
def test_omit__names(): # also happens to test IPCompleter as a configurable ip = get_ipython() ip._hidden_attr = 1 c = ip.Completer ip.ex('ip=get_ipython()') cfg = Config() cfg.IPCompleter.omit__names = 0 c.update_config(cfg) s, matches = c.complete('ip.') nt.assert_true('ip.__str__' in matches) nt.assert_true('ip._hidden_attr' in matches) cfg.IPCompleter.omit__names = 1 c.update_config(cfg) s, matches = c.complete('ip.') nt.assert_false('ip.__str__' in matches) nt.assert_true('ip._hidden_attr' in matches) cfg.IPCompleter.omit__names = 2 c.update_config(cfg) s, matches = c.complete('ip.') nt.assert_false('ip.__str__' in matches) nt.assert_false('ip._hidden_attr' in matches) del ip._hidden_attr
def test_builtin(self): c1 = Config() exec 'foo = True' in c1 self.assertEqual(c1.foo, True) c1.format = "json"
def test_builtin(self): c1 = Config() c1.format = "json"
def kernel_config(): """Create a config object with IPython kernel options""" from IPython.config.loader import Config, load_pyconfig_files from IPython.core.application import get_ipython_dir from spyderlib.config.main import CONF from spyderlib.utils.programs import is_module_installed # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), "profile_default") ip_cfg = load_pyconfig_files(["ipython_config.py", "ipython_qtconsole_config.py"], profile_path) except: ip_cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Until we implement Issue 1052 spy_cfg.InteractiveShell.xmode = "Plain" # Run lines of code at startup run_lines_o = CONF.get("ipython_console", "startup/run_lines") if run_lines_o: spy_cfg.IPKernelApp.exec_lines = [x.strip() for x in run_lines_o.split(",")] else: spy_cfg.IPKernelApp.exec_lines = [] # Pylab configuration mpl_backend = None mpl_installed = is_module_installed("matplotlib") pylab_o = CONF.get("ipython_console", "pylab") if mpl_installed and pylab_o: # Get matplotlib backend backend_o = CONF.get("ipython_console", "pylab/backend", 0) backends = {0: "inline", 1: "auto", 2: "qt", 3: "osx", 4: "gtk", 5: "wx", 6: "tk"} mpl_backend = backends[backend_o] # Automatically load Pylab and Numpy, or only set Matplotlib # backend autoload_pylab_o = CONF.get("ipython_console", "pylab/autoload") if autoload_pylab_o: spy_cfg.IPKernelApp.exec_lines.append("%pylab {0}".format(mpl_backend)) else: spy_cfg.IPKernelApp.exec_lines.append("%matplotlib {0}".format(mpl_backend)) # Inline backend configuration if backends[backend_o] == "inline": # Figure format format_o = CONF.get("ipython_console", "pylab/inline/figure_format", 0) formats = {0: "png", 1: "svg"} spy_cfg.InlineBackend.figure_format = formats[format_o] # Resolution spy_cfg.InlineBackend.rc = { "figure.figsize": (6.0, 4.0), "savefig.dpi": 72, "font.size": 10, "figure.subplot.bottom": 0.125, "figure.facecolor": "white", "figure.edgecolor": "white", } resolution_o = CONF.get("ipython_console", "pylab/inline/resolution") spy_cfg.InlineBackend.rc["savefig.dpi"] = resolution_o # Figure size width_o = float(CONF.get("ipython_console", "pylab/inline/width")) height_o = float(CONF.get("ipython_console", "pylab/inline/height")) spy_cfg.InlineBackend.rc["figure.figsize"] = (width_o, height_o) # Run a file at startup use_file_o = CONF.get("ipython_console", "startup/use_run_file") run_file_o = CONF.get("ipython_console", "startup/run_file") if use_file_o and run_file_o: spy_cfg.IPKernelApp.file_to_run = run_file_o # Autocall autocall_o = CONF.get("ipython_console", "autocall") spy_cfg.ZMQInteractiveShell.autocall = autocall_o # To handle the banner by ourselves in IPython 3+ spy_cfg.ZMQInteractiveShell.banner1 = "" # Greedy completer greedy_o = CONF.get("ipython_console", "greedy_completer") spy_cfg.IPCompleter.greedy = greedy_o # Sympy loading sympy_o = CONF.get("ipython_console", "symbolic_math") if sympy_o: lines = sympy_config(mpl_backend) spy_cfg.IPKernelApp.exec_lines.append(lines) # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones ip_cfg._merge(spy_cfg) return ip_cfg
def test_fromdictmerge(self): c1 = Config() c2 = Config({'Foo' : {'bar' : 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1)
def kernel_config(): """Create a config object with IPython kernel options""" from IPython.config.loader import Config, load_pyconfig_files from IPython.core.application import get_ipython_dir from spyderlib.config import CONF from spyderlib.utils.programs import is_module_installed # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), 'profile_default') ip_cfg = load_pyconfig_files(['ipython_config.py', 'ipython_qtconsole_config.py'], profile_path) except: ip_cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Until we implement Issue 1052 spy_cfg.InteractiveShell.xmode = 'Plain' # Run lines of code at startup run_lines_o = CONF.get('ipython_console', 'startup/run_lines') if run_lines_o: spy_cfg.IPKernelApp.exec_lines = [x.strip() for x in run_lines_o.split(',')] else: spy_cfg.IPKernelApp.exec_lines = [] # Pylab configuration mpl_installed = is_module_installed('matplotlib') pylab_o = CONF.get('ipython_console', 'pylab') if mpl_installed and pylab_o: # Get matplotlib backend backend_o = CONF.get('ipython_console', 'pylab/backend', 0) backends = {0: 'inline', 1: 'auto', 2: 'qt', 3: 'osx', 4: 'gtk', 5: 'wx', 6: 'tk'} mpl_backend = backends[backend_o] # Automatically load Pylab and Numpy, or only set Matplotlib # backend autoload_pylab_o = CONF.get('ipython_console', 'pylab/autoload') if autoload_pylab_o: spy_cfg.IPKernelApp.exec_lines.append( "%pylab {0}".format(mpl_backend)) else: spy_cfg.IPKernelApp.exec_lines.append( "%matplotlib {0}".format(mpl_backend)) # Inline backend configuration if backends[backend_o] == 'inline': # Figure format format_o = CONF.get('ipython_console', 'pylab/inline/figure_format', 0) formats = {0: 'png', 1: 'svg'} spy_cfg.InlineBackend.figure_format = formats[format_o] # Resolution spy_cfg.InlineBackend.rc = {'figure.figsize': (6.0, 4.0), 'savefig.dpi': 72, 'font.size': 10, 'figure.subplot.bottom': .125, 'figure.facecolor': 'white', 'figure.edgecolor': 'white' } resolution_o = CONF.get('ipython_console', 'pylab/inline/resolution') spy_cfg.InlineBackend.rc['savefig.dpi'] = resolution_o # Figure size width_o = float(CONF.get('ipython_console', 'pylab/inline/width')) height_o = float(CONF.get('ipython_console', 'pylab/inline/height')) spy_cfg.InlineBackend.rc['figure.figsize'] = (width_o, height_o) # Run a file at startup use_file_o = CONF.get('ipython_console', 'startup/use_run_file') run_file_o = CONF.get('ipython_console', 'startup/run_file') if use_file_o and run_file_o: spy_cfg.IPKernelApp.file_to_run = run_file_o # Autocall autocall_o = CONF.get('ipython_console', 'autocall') spy_cfg.ZMQInteractiveShell.autocall = autocall_o # To handle the banner by ourselves in IPython 3+ spy_cfg.ZMQInteractiveShell.banner1 = '' # Greedy completer greedy_o = CONF.get('ipython_console', 'greedy_completer') spy_cfg.IPCompleter.greedy = greedy_o # Sympy loading sympy_o = CONF.get('ipython_console', 'symbolic_math') if sympy_o: lines = sympy_config(mpl_backend) spy_cfg.IPKernelApp.exec_lines.append(lines) # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones ip_cfg._merge(spy_cfg) return ip_cfg
def test_setget(self): c = Config() c.a = 10 self.assertEquals(c.a, 10) self.assertEquals(c.has_key('b'), False)
_description = """Start the IPython controller for parallel computing. The IPython controller provides a gateway between the IPython engines and clients. The controller needs to be started before the engines and can be configured using command line options or using a cluster directory. Cluster directories contain config, log and security files and are usually located in your .ipython directory and named as "cluster_<profile>". See the --profile and --cluster-dir options for details. """ #----------------------------------------------------------------------------- # Default interfaces #----------------------------------------------------------------------------- # The default client interfaces for FCClientServiceFactory.interfaces default_client_interfaces = Config() default_client_interfaces.Task.interface_chain = [ 'IPython.kernel.task.ITaskController', 'IPython.kernel.taskfc.IFCTaskController' ] default_client_interfaces.Task.furl_file = 'ipcontroller-tc.furl' default_client_interfaces.MultiEngine.interface_chain = [ 'IPython.kernel.multiengine.IMultiEngine', 'IPython.kernel.multienginefc.IFCSynchronousMultiEngine' ] default_client_interfaces.MultiEngine.furl_file = u'ipcontroller-mec.furl' # Make this a dict we can pass to Config.__init__ for the default
def test_setget(self): c = Config() c.a = 10 self.assertEqual(c.a, 10) self.assertEqual('b' in c, False)
The code in this file is deliberately extra-verbose, meant for learning.""" # The basics to get you going: # IPython sets the __IPYTHON__ variable so you can know if you have nested # copies running. # Try running this code both at the command line and from inside IPython (with # %run example-embed.py) from IPython.config.loader import Config try: get_ipython except NameError: nested = 0 cfg = Config() prompt_config = cfg.PromptManager prompt_config.in_template = 'In <\\#>: ' prompt_config.in2_template = ' .\\D.: ' prompt_config.out_template = 'Out<\\#>: ' else: print "Running nested copies of IPython." print "The prompts for the nested copy have been modified" cfg = Config() nested = 1 # First import the embeddable shell class from IPython.frontend.terminal.embed import InteractiveShellEmbed # Now create an instance of the embeddable shell. The first argument is a # string with options exactly as you would type them if you were starting
def kernel_config(): """Create a config object with IPython kernel options""" from IPython.config.loader import Config, load_pyconfig_files from IPython.core.application import get_ipython_dir from SMlib.config import CONF from SMlib.utils.programs import is_module_installed # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), 'profile_default') ip_cfg = load_pyconfig_files(['ipython_config.py', 'ipython_qtconsole_config.py'], profile_path) except: ip_cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Until we implement Issue 1052: # http://code.google.com/p/SMlib/issues/detail?id=1052 spy_cfg.InteractiveShell.xmode = 'Plain' # Pylab configuration mpl_installed = is_module_installed('matplotlib') pylab_o = CONF.get('ipython_console', 'pylab') if mpl_installed and pylab_o: backend_o = CONF.get('ipython_console', 'pylab/backend', 0) backends = {0: 'inline', 1: 'auto', 2: 'qt', 3: 'osx', 4: 'gtk', 5: 'wx', 6: 'tk'} spy_cfg.IPKernelApp.pylab = backends[backend_o] # Automatically load Pylab and Numpy autoload_pylab_o = CONF.get('ipython_console', 'pylab/autoload') spy_cfg.IPKernelApp.pylab_import_all = autoload_pylab_o # Inline backend configuration if backends[backend_o] == 'inline': # Figure format format_o = CONF.get('ipython_console', 'pylab/inline/figure_format', 0) formats = {0: 'png', 1: 'svg'} spy_cfg.InlineBackend.figure_format = formats[format_o] # Resolution spy_cfg.InlineBackend.rc = {'figure.figsize': (6.0, 4.0), 'savefig.dpi': 72, 'font.size': 10, 'figure.subplot.bottom': .125, 'figure.facecolor': 'white', 'figure.edgecolor': 'white' } resolution_o = CONF.get('ipython_console', 'pylab/inline/resolution') spy_cfg.InlineBackend.rc['savefig.dpi'] = resolution_o # Figure size width_o = float(CONF.get('ipython_console', 'pylab/inline/width')) height_o = float(CONF.get('ipython_console', 'pylab/inline/height')) spy_cfg.InlineBackend.rc['figure.figsize'] = (width_o, height_o) # Run lines of code at startup run_lines_o = CONF.get('ipython_console', 'startup/run_lines') if run_lines_o: spy_cfg.IPKernelApp.exec_lines = map(lambda x: x.strip(), run_lines_o.split(',')) # Run a file at startup use_file_o = CONF.get('ipython_console', 'startup/use_run_file') run_file_o = CONF.get('ipython_console', 'startup/run_file') if use_file_o and run_file_o: spy_cfg.IPKernelApp.file_to_run = run_file_o # Autocall autocall_o = CONF.get('ipython_console', 'autocall') spy_cfg.ZMQInteractiveShell.autocall = autocall_o # Greedy completer greedy_o = CONF.get('ipython_console', 'greedy_completer') spy_cfg.IPCompleter.greedy = greedy_o # Sympy loading sympy_o = CONF.get('ipython_console', 'symbolic_math') if sympy_o: lines, extension = sympy_config() if lines is not None: if run_lines_o: spy_cfg.IPKernelApp.exec_lines.append(lines) else: spy_cfg.IPKernelApp.exec_lines = [lines] if extension: spy_cfg.IPKernelApp.extra_extension = extension spy_cfg.LaTeXTool.backends = ['dvipng', 'matplotlib'] # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones ip_cfg._merge(spy_cfg) return ip_cfg