Exemple #1
0
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.frontend.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)
Exemple #2
0
def ipython(user_ns=None):
    try:
        import IPython
        from IPython.config.loader import Config
    except ImportError:
        return simple_repl(user_ns=user_ns)
    defns = {'os': os, 're': re, 'sys': sys}
    if not user_ns:
        user_ns = defns
    else:
        defns.update(user_ns)
        user_ns = defns

    c = Config()
    c.InteractiveShellApp.exec_lines = [
        'from __future__ import division, absolute_import, unicode_literals, print_function',
    ]
    c.TerminalInteractiveShell.confirm_exit = False
    c.PromptManager.in_template = (
        r'{color.LightGreen}calibre '
        '{color.LightBlue}[{color.LightCyan}%s{color.LightBlue}]'
        r'{color.Green}|\#> ' % get_version())
    c.PromptManager.in2_template = r'{color.Green}|{color.LightGreen}\D{color.Green}> '
    c.PromptManager.out_template = r'<\#> '
    c.TerminalInteractiveShell.banner1 = BANNER
    c.PromptManager.justify = True
    c.TerminalIPythonApp.ipython_dir = ipydir
    os.environ['IPYTHONDIR'] = ipydir

    c.InteractiveShell.separate_in = ''
    c.InteractiveShell.separate_out = ''
    c.InteractiveShell.separate_out2 = ''

    c.PrefilterManager.multi_line_specials = True

    IPython.embed(config=c, user_ns=user_ns)
Exemple #3
0
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,
                                      banner2=banner)
        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)
Exemple #4
0
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    settings = get_appsettings(config_uri)
    engine = create_engine('pyvac', settings, scoped=False)

    config = Configurator(settings=settings)
    config.end()

    from pyvac.models import (
        Base,
        Permission,
        Group,
        User,
        Request,  # noqa
        Countries,
        VacationType,
        PasswordRecovery,
        Sudoer,
        CPVacation,
        RTTVacation,
        RequestHistory,
        Pool,
        UserPool)

    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 pyvac shell.")
    except ImportError:
        import code
        code.interact("pyvac shell", local=locals())
Exemple #5
0
def setup_shell():
    banner  = "+----------------------------------------------------------------------+\n"
    banner += " PML Shell - built on IPython.\n"
    banner += "+----------------------------------------------------------------------+\n"
    banner += "Commands: \n"
    banner += "\t'tutorial' will begin the interactive tutorial.\n"
    banner += "\t'tutorial list' will display individual lessons in the tutorial.\n"
    banner += "\t'docs' will open up the online documentation in a web browser.\n"
    banner += "\t'exit', 'quit' or press 'CTRL + D' to exit the shell.\n"

    exit_message = "\n* Exiting PML shell, good bye! *\n"
    
    # XXX: this currently only supports IPython version 0.11 or higher!
    config = Config()
    config.PromptManager.in_template = "pml:\\#> "
    config.PromptManager.out_template = "pml:\\#: "
    
    pml_shell = InteractiveShellEmbed(config=config, banner1=banner, 
                                      exit_msg=exit_message)
    
    pml_shell.define_magic("tutorial", magic_tutorial)
    pml_shell.define_magic("docs", magic_docs)
    
    return pml_shell
 def restart_kernel(self, kernel_id):
     ports = self.kernels[kernel_id][1]
     self.kill_kernel(kernel_id)
     return self.start_kernel(kernel_id, Config({"IPKernelApp": ports}))
Exemple #7
0
try:
    import IPython
    IPYTHON = True
except ImportError:
    IPYTHON = False

if not IPYTHON:
    # for virtualenvs w/o IPython
    import code
    code.InteractiveConsole().interact()

from IPython.config.loader import Config

editor = "subl -w"

cfg = Config()
cfg.InteractiveShell.use_readline = False
cfg.InteractiveShell.autoindent = False
cfg.InteractiveShell.colors = "NoColor"
cfg.InteractiveShell.editor = os.environ.get("SUBLIMEREPL_EDITOR", editor)

try:
    # IPython 1.0.0
    from IPython.terminal.console.app import ZMQTerminalIPythonApp

    def kernel_client(zmq_shell):
        return zmq_shell.kernel_client
except ImportError:
    # Older IPythons
    from IPython.frontend.terminal.console.app import ZMQTerminalIPythonApp
Exemple #8
0
    def config(self, s):
        """configure IPython

            %config Class[.trait=value]

        This magic exposes most of the IPython config system. Any
        Configurable class should be able to be configured with the simple
        line::

            %config Class.trait=value

        Where `value` will be resolved in the user's namespace, if it is an
        expression or variable name.

        Examples
        --------

        To see what classes are available for config, pass no arguments::

            In [1]: %config
            Available objects for config:
                TerminalInteractiveShell
                HistoryManager
                PrefilterManager
                AliasManager
                IPCompleter
                PromptManager
                DisplayFormatter

        To view what is configurable on a given class, just pass the class
        name::

            In [2]: %config IPCompleter
            IPCompleter options
            -----------------
            IPCompleter.omit__names=<Enum>
                Current: 2
                Choices: (0, 1, 2)
                Instruct the completer to omit private method names
                Specifically, when completing on ``object.<tab>``.
                When 2 [default]: all names that start with '_' will be excluded.
                When 1: all 'magic' names (``__foo__``) will be excluded.
                When 0: nothing will be excluded.
            IPCompleter.merge_completions=<CBool>
                Current: True
                Whether to merge completion results into a single list
                If False, only the completion results from the first non-empty
                completer will be returned.
            IPCompleter.limit_to__all__=<CBool>
                Current: False
                Instruct the completer to use __all__ for the completion
                Specifically, when completing on ``object.<tab>``.
                When True: only those names in obj.__all__ will be included.
                When False [default]: the __all__ attribute is ignored
            IPCompleter.greedy=<CBool>
                Current: False
                Activate greedy completion
                This will enable completion on elements of lists, results of
                function calls, etc., but can be unsafe because the code is
                actually evaluated on TAB.

        but the real use is in setting values::

            In [3]: %config IPCompleter.greedy = True

        and these values are read from the user_ns if they are variables::

            In [4]: feeling_greedy=False

            In [5]: %config IPCompleter.greedy = feeling_greedy

        """
        from IPython.config.loader import Config
        # some IPython objects are Configurable, but do not yet have
        # any configurable traits.  Exclude them from the effects of
        # this magic, as their presence is just noise:
        configurables = [
            c for c in self.shell.configurables
            if c.__class__.class_traits(config=True)
        ]
        classnames = [c.__class__.__name__ for c in configurables]

        line = s.strip()
        if not line:
            # print available configurable names
            print("Available objects for config:")
            for name in classnames:
                print("    ", name)
            return
        elif line in classnames:
            # `%config TerminalInteractiveShell` will print trait info for
            # TerminalInteractiveShell
            c = configurables[classnames.index(line)]
            cls = c.__class__
            help = cls.class_get_help(c)
            # strip leading '--' from cl-args:
            help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
            print(help)
            return
        elif '=' not in line:
            raise UsageError("Invalid config statement: %r, "
                             "should be Class.trait = value" % line)

        # otherwise, assume we are setting configurables.
        # leave quotes on args when splitting, because we want
        # unquoted args to eval in user_ns
        cfg = Config()
        exec("cfg." + line, locals(), self.shell.user_ns)

        for configurable in configurables:
            try:
                configurable.update_config(cfg)
            except Exception as e:
                error(e)
Exemple #9
0
 def test_set_update(self):
     c = Config()
     c.Containers.s.update({0, 1, 2})
     c.Containers.s.update({3})
     obj = Containers(config=c)
     self.assertEqual(obj.s, {'a', 0, 1, 2, 3})
Exemple #10
0
 def test_extend_append(self):
     c = Config()
     c.Containers.lis.extend([2, 3])
     c.Containers.lis.append([1, 2])
     obj = Containers(config=c)
     self.assertEqual(obj.lis, [-1, 2, 3, [1, 2]])
Exemple #11
0
 def test_insert(self):
     c = Config()
     c.Containers.lis.insert(0, 'a')
     c.Containers.lis.insert(1, 'b')
     obj = Containers(config=c)
     self.assertEqual(obj.lis, ['a', 'b', -1])
Exemple #12
0
 def test_fromdict(self):
     c1 = Config({'Foo': {'bar': 1}})
     self.assertEqual(c1.Foo.__class__, Config)
     self.assertEqual(c1.Foo.bar, 1)
Exemple #13
0
 def test_builtin(self):
     c1 = Config()
     c1.format = "json"
Exemple #14
0
 def test_setget(self):
     c = Config()
     c.a = 10
     self.assertEqual(c.a, 10)
     self.assertEqual('b' in c, False)
def process_argv(argv):
    """
    Process command-line arguments (minus argv[0]!), rearrange and execute.
    """
    # Initial preparation
    import __main__
    for (k, v) in global_constants.items():
        exec '%s = %s' % (k, v) in __main__.__dict__

    # Allow param.normalize_path.prefix to be overridden in the
    # startup files, but otherwise force it to exist before doing
    # anything else
    param.normalize_path.prefix = default_output_path()
    exec_startup_files()
    set_output_path(param.normalize_path.prefix)

    # Tell the user how many cores are in use, if available
    openmp_main = Parameterized(
        name="OpenMP")  # Dummy object just for messages
    try:
        import os, multiprocessing
        total_cores = multiprocessing.cpu_count()
        num_threads = int(os.environ.get('OMP_NUM_THREADS', total_cores))
        openmp_main.verbose(
            "Using %d threads on a machine with %d detected CPUs", num_threads,
            total_cores)
    except:
        pass

    # Repeatedly process options, if any, followed by filenames, if any, until nothing is left
    topo_parser.disable_interspersed_args()
    args = argv
    option = None
    global something_executed
    while True:
        # Process options up until the first filename
        (option, args) = topo_parser.parse_args(args, option)

        # Handle filename
        if args:
            filename = args.pop(0)
            #print "Executing %s" % (filename)
            filedir = os.path.dirname(os.path.abspath(filename))
            sys.path.insert(
                0, filedir)  # Allow imports relative to this file's path
            sim_name_from_filename(filename)  # Default value of topo.sim.name

            execfile(filename, __main__.__dict__)
            something_executed = True

        if not args:
            break

    global_params.check_for_unused_names()

    # If no scripts and no commands were given, pretend -i was given.
    if not something_executed: interactive()

    if option.gui: topo.guimain.title(topo.sim.name)

    ## INTERACTIVE SESSION BEGINS HERE (i.e. can't have anything but
    ## some kind of cleanup code afterwards)
    if os.environ.get('PYTHONINSPECT'):
        print "Output path: %s" % param.normalize_path.prefix
        print BANNER
        # CBALERT: should probably allow a way for users to pass
        # things to IPython? Or at least set up some kind of
        # topographica ipython config file. Right now, a topo_parser
        # option has to be added for every ipython option we want to
        # support (e.g. see --pdb)

        if ipython_shell_interface == "IPython.Shell":
            # IPython 0.10 and earlier

            # Stop IPython namespace hack?
            # http://www.nabble.com/__main__-vs-__main__-td14606612.html
            __main__.__name__ = "__mynamespace__"

            ipython_args = [
                '-noconfirm_exit', '-nobanner', '-pi1',
                CommandPrompt.get_format(), '-pi2',
                CommandPrompt2.get_format(), '-po',
                OutputPrompt.get_format()
            ]
            if option.pdb:
                ipython_args.append('-pdb')

            ipshell = IPShell(ipython_args, user_ns=__main__.__dict__)
            ipshell.mainloop(sys_exit=1)

        elif ipython_shell_interface == "InteractiveShellEmbed":
            # IPython 0.11 and later

            config = Config()

            if ipython_prompt_interface == "PromptManager":
                config.PromptManager.in_template = CommandPrompt.get_format()
                config.PromptManager.in2_template = CommandPrompt2.get_format()
                config.PromptManager.out_template = OutputPrompt.get_format()
            else:
                config.InteractiveShell.prompt_in1 = CommandPrompt.get_format()
                config.InteractiveShell.prompt_in2 = CommandPrompt2.get_format(
                )
                config.InteractiveShell.prompt_out = OutputPrompt.get_format()
            config.InteractiveShell.confirm_exit = False
            ipshell = IPShell(config=config,
                              user_ns=__main__.__dict__,
                              banner1="",
                              exit_msg="")
            if option.pdb:
                ipshell.call_pdb = True

            # Load Topographica IPython extension in embedded shell
            try:
                ipshell.extension_manager.load_extension('topo.misc.ipython')
            except:
                cmdline_main.warning(
                    "Could not load IPython extension 'topo.misc.ipython'; ignored error was:\n%s"
                    % traceback.format_exc())

            ipshell()

    global return_code
    if return_code != 0:
        cmdline_main.warning(
            "Errors encountered; exiting with return code %d" % return_code)

    sys.exit(return_code)
Exemple #16
0
_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
Exemple #17
0
 def test_extend(self):
     c = Config()
     c.Containers.lis.extend(range(5))
     obj = Containers(config=c)
     self.assertEqual(obj.lis, range(-1, 5))
Exemple #18
0
 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)
Exemple #19
0
 def test_prepend(self):
     c = Config()
     c.Containers.lis.prepend([1, 2])
     c.Containers.lis.prepend([2, 3])
     obj = Containers(config=c)
     self.assertEqual(obj.lis, [2, 3, 1, 2, -1])
Exemple #20
0
 def test_pickle_config(self):
     cfg = Config()
     cfg.Foo.bar = 1
     pcfg = pickle.dumps(cfg)
     cfg2 = pickle.loads(pcfg)
     self.assertEqual(cfg2, cfg)
Exemple #21
0
 def test_insert_extend(self):
     c = Config()
     c.Containers.lis.insert(0, 1)
     c.Containers.lis.extend([2, 3])
     obj = Containers(config=c)
     self.assertEqual(obj.lis, [1, -1, 2, 3])
Exemple #22
0
 def test_getattr_section(self):
     cfg = Config()
     self.assertNotIn('Foo', cfg)
     Foo = cfg.Foo
     assert isinstance(Foo, Config)
     self.assertIn('Foo', cfg)
Exemple #23
0
 def test_dict_update(self):
     c = Config()
     c.Containers.d.update({'c': 'd'})
     c.Containers.d.update({'e': 'f'})
     obj = Containers(config=c)
     self.assertEqual(obj.d, {'a': 'b', 'c': 'd', 'e': 'f'})
Exemple #24
0
 def test_getitem_section(self):
     cfg = Config()
     self.assertNotIn('Foo', cfg)
     Foo = cfg['Foo']
     assert isinstance(Foo, Config)
     self.assertIn('Foo', cfg)
Exemple #25
0
addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax',
        'Turn on auto editing of files with syntax errors.',
        'Turn off auto editing of files with syntax errors.')
addflag('banner', 'TerminalIPythonApp.display_banner',
        "Display a banner upon starting IPython.",
        "Don't display a banner upon starting IPython.")
addflag(
    'confirm-exit', 'TerminalInteractiveShell.confirm_exit',
    """Set to confirm when you try to exit IPython with an EOF (Control-D
    in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
    you can force a direct exit without any confirmation.""",
    "Don't prompt the user when exiting.")
addflag('term-title', 'TerminalInteractiveShell.term_title',
        "Enable auto setting the terminal title.",
        "Disable auto setting the terminal title.")
classic_config = Config()
classic_config.InteractiveShell.cache_size = 0
classic_config.PlainTextFormatter.pprint = False
classic_config.PromptManager.in_template = '>>> '
classic_config.PromptManager.in2_template = '... '
classic_config.PromptManager.out_template = ''
classic_config.InteractiveShell.separate_in = ''
classic_config.InteractiveShell.separate_out = ''
classic_config.InteractiveShell.separate_out2 = ''
classic_config.InteractiveShell.colors = 'NoColor'
classic_config.InteractiveShell.xmode = 'Plain'

frontend_flags['classic'] = (
    classic_config,
    "Gives IPython a similar feel to the classic Python prompt.")
# # log doesn't make so much sense this way anymore
Exemple #26
0
 def test_getitem_not_section(self):
     cfg = Config()
     self.assertNotIn('foo', cfg)
     foo = cfg['foo']
     assert isinstance(foo, LazyConfigValue)
     self.assertIn('foo', cfg)
Exemple #27
0
    def __init__(self, user_ns=None, cin=None, cout=None, cerr=None, input_func=None):
        '''

        @param argv: Command line options for IPython
        @type argv: list
        @param user_ns: User namespace.
        @type user_ns: dictionary
        @param user_global_ns: User global namespace.
        @type user_global_ns: dictionary.
        @param cin: Console standard input.
        @type cin: IO stream
        @param cout: Console standard output.
        @type cout: IO stream
        @param cerr: Console standard error.
        @type cerr: IO stream
        @param input_func: Replacement for builtin raw_input()
        @type input_func: function
        '''

        self.prompt = None

        ip_io = IPython.utils.io
        if input_func:
            IPython.terminal.interactiveshell.raw_input_original = input_func
        if cin:
            ip_io.stdin = ip_io.IOStream(cin)
        if cout:
            ip_io.stdout = ip_io.IOStream(cout)
        if cerr:
            ip_io.stderr = ip_io.IOStream(cerr)

        # This is to get rid of the blockage that occurs during
        # IPython.Shell.InteractiveShell.user_setup()

        ip_io.raw_input = lambda x: None

        os.environ['TERM'] = 'dumb'
        excepthook = sys.excepthook

        from IPython.config.loader import Config

        cfg = Config()
        cfg.InteractiveShell.colors = "Linux"

        # InteractiveShell's __init__ overwrites ip_io.stdout,ip_io.stderr with
        # sys.stdout, sys.stderr, this makes sure they are right
        #
        old_stdout, old_stderr = sys.stdout, sys.stderr
        sys.stdout, sys.stderr = ip_io.stdout.stream, ip_io.stderr.stream

        # InteractiveShell inherits from SingletonConfigurable, so use instance()
        #
        self.IP = IPython.terminal.embed.InteractiveShellEmbed.instance(
            config=cfg, user_ns=user_ns
        )

        sys.stdout, sys.stderr = old_stdout, old_stderr

        self.IP.system = lambda cmd: self.shell(
            self.IP.var_expand(cmd), header='IPython system call: '
        )
        # local_ns=user_ns)
        # global_ns=user_global_ns)
        # verbose=self.IP.rc.system_verbose)

        self.IP.raw_input = input_func
        sys.excepthook = excepthook
        self.iter_more = 0
        self.history_level = 0
        self.complete_sep = re.compile(r'[\s\{\}\[\]\(\)]')
        self.updateNamespace({'exit': lambda: None})
        self.updateNamespace({'quit': lambda: None})
        if int(IPYTHON_VERSION) < 5:  # HACK
            self.IP.readline_startup_hook(self.IP.pre_readline)
        # Workaround for updating namespace with sys.modules
        #
        self.__update_namespace()

        # help() is blocking, which hangs GTK+.
        import pydoc

        self.updateNamespace({'help': pydoc.doc})
Exemple #28
0
 def test_setget(self):
     c = Config()
     c.a = 10
     self.assertEquals(c.a, 10)
     self.assertEquals(c.has_key('b'), False)
Exemple #29
0
    works for you.  Test it and turn it on permanently if it works with
    your system.  The magic function %%color_info allows you to toggle this
    interactively for testing.""",
    "Disable using colors for info related things.")
addflag(
    'deep-reload', 'InteractiveShell.deep_reload',
    """Enable deep (recursive) reloading by default. IPython can use the
    deep_reload module which reloads changes in modules recursively (it
    replaces the reload() function, so you don't need to change anything to
    use it). deep_reload() forces a full reload of modules whose code may
    have changed, which the default reload() function does not.  When
    deep_reload is off, IPython will use the normal reload(), but
    deep_reload will still be available as dreload(). This feature is off
    by default [which means that you have both normal reload() and
    dreload()].""", "Disable deep (recursive) reloading by default.")
nosep_config = Config()
nosep_config.InteractiveShell.separate_in = ''
nosep_config.InteractiveShell.separate_out = ''
nosep_config.InteractiveShell.separate_out2 = ''

shell_flags['nosep'] = (nosep_config, "Eliminate all spacing between prompts.")
shell_flags['pylab'] = ({
    'InteractiveShellApp': {
        'pylab': 'auto'
    }
}, """Pre-load matplotlib and numpy for interactive use with
    the default matplotlib backend.""")

# it's possible we don't want short aliases for *all* of these:
shell_aliases = dict(
    autocall='InteractiveShell.autocall',
Exemple #30
0
 def test_builtin(self):
     c1 = Config()
     exec 'foo = True' in c1
     self.assertEquals(c1.foo, True)
     self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)