Пример #1
0
    def aiida(self, line='', local_ns=None):
        """Load AiiDA in ipython (checking if it was already loaded), and
        inserts in the namespace the main AiiDA classes (the same that are
        loaded in ``verdi shell``.

        Usage::

            %aiida [optional parameters]

        .. todo:: implement parameters, e.g. for the profile to load.
        """
        # pylint: disable=unused-argument,attribute-defined-outside-init
        from aiida.manage.configuration import load_profile
        from aiida.cmdline.utils.shell import get_start_namespace

        self.is_warning = False
        lcontent = line.strip()
        if lcontent:
            profile = load_profile(lcontent)
        else:
            profile = load_profile()

        self.current_state = f'Loaded AiiDA DB environment - profile name: {profile.name}.'

        user_ns = get_start_namespace()
        for key, value in user_ns.items():
            add_to_ns(local_ns, key, value)

        return self
Пример #2
0
        def load_ipy(cmd2_app, py_bridge):
            """Embed an IPython shell in an environment that is restricted to only the variables in this function
            :param cmd2_app: instance of the cmd2 app
            :param py_bridge: a PyBridge
            """
            from aiida.cmdline.utils.shell import get_start_namespace
            from IPython import embed

            # Create a variable pointing to py_bridge
            exec("{} = py_bridge".format(cmd2_app.py_bridge_name))

            # Add node_shell variable pointing to this app
            exec("node_shell = cmd2_app")
            # Add current_node varible pointing to the current loaded node
            exec("current_node = cmd2_app._current_node")

            # Initialse the namespace - this is what used by `verdi shell`
            locals().update(get_start_namespace())
            _cnode_string = cmd2_app._get_node_string()

            # Delete these names from the environment so IPython can't use them
            del cmd2_app
            del py_bridge

            # Start ipy shell
            banner = (
                'Entering an embedded verdi shell. Type quit or <Ctrl>-d to exit.\n'
                'Run Python code from external files with: run filename.py\n')

            if _cnode_string:
                banner += 'The loaded node \'{}\' can be accessed with \'current_node\'\n'.format(
                    _cnode_string)

            embed(
                banner1=banner,
                exit_msg='Leaving verdi shell, back to the node shell',
                colors='Neutral',
            )