/
shell.py
136 lines (108 loc) · 4.38 KB
/
shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida_core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Definition of modules that are to be automatically loaded for `verdi shell`."""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
DEFAULT_MODULES_LIST = [
('aiida.orm', 'Node', 'Node'),
('aiida.orm', 'Calculation', 'Calculation'),
('aiida.orm', 'load_workflow', 'load_workflow'),
('aiida.orm', 'JobCalculation', 'JobCalculation'),
('aiida.orm', 'Data', 'Data'),
('aiida.orm', 'CalculationFactory', 'CalculationFactory'),
('aiida.orm', 'DataFactory', 'DataFactory'),
('aiida.orm', 'WorkflowFactory', 'WorkflowFactory'),
('aiida.orm.code', 'Code', 'Code'),
('aiida.orm.computer', 'Computer', 'Computer'),
('aiida.orm.group', 'Group', 'Group'),
('aiida.orm.workflow', 'Workflow', 'Workflow'),
('aiida.orm.querybuilder', 'QueryBuilder', 'QueryBuilder'),
('aiida.orm.utils', 'load_node', 'load_node'),
]
def ipython():
"""Start any version of IPython"""
for ipy_version in (_ipython, _ipython_pre_100, _ipython_pre_011):
try:
ipy_version()
except ImportError:
pass
else:
return
raise ImportError('No IPython available')
def bpython():
"""Start a bpython shell."""
import bpython as bpy_shell # pylint: disable=import-error
user_ns = get_start_namespace()
if user_ns:
bpy_shell.embed(user_ns)
else:
bpy_shell.embed()
AVAILABLE_SHELLS = {'ipython': ipython, 'bpython': bpython}
def run_shell(interface=None):
"""Start the chosen external shell."""
available_shells = [AVAILABLE_SHELLS[interface]] if interface else AVAILABLE_SHELLS.values()
# Try the specified or the available shells one by one until you
# find one that is available. If the wanted shell is not available
# then an ImportError is raised leading the the loading of a generic
# shell.
for curr_shell in available_shells:
try:
curr_shell()
return
except ImportError:
pass
raise ImportError
def get_start_namespace():
"""Load all default and custom modules"""
from aiida.common.setup import get_property
user_ns = {}
# load default modules
for app_mod, model_name, alias in DEFAULT_MODULES_LIST:
user_ns[alias] = getattr(__import__(app_mod, {}, {}, model_name), model_name)
# load custom modules
custom_modules_list = [
(str(e[0]), str(e[2]))
for e in [p.rpartition('.') for p in get_property('verdishell.modules', default="").split(':')]
if e[1] == '.'
]
for app_mod, model_name in custom_modules_list:
try:
user_ns[model_name] = getattr(__import__(app_mod, {}, {}, model_name), model_name)
except AttributeError:
# if the module does not exist, we ignore it
pass
return user_ns
def _ipython_pre_011():
"""Start IPython pre-0.11"""
from IPython.Shell import IPShell # pylint: disable=import-error,no-name-in-module
user_ns = get_start_namespace()
if user_ns:
ipy_shell = IPShell(argv=[], user_ns=user_ns)
else:
ipy_shell = IPShell(argv=[])
ipy_shell.mainloop()
def _ipython_pre_100():
"""Start IPython pre-1.0.0"""
from IPython.frontend.terminal.ipapp import TerminalIPythonApp # pylint: disable=import-error,no-name-in-module
app = TerminalIPythonApp.instance()
app.initialize(argv=[])
user_ns = get_start_namespace()
if user_ns:
app.shell.user_ns.update(user_ns)
app.start()
def _ipython():
"""Start IPython >= 1.0"""
from IPython import start_ipython # pylint: disable=import-error,no-name-in-module
user_ns = get_start_namespace()
if user_ns:
start_ipython(argv=[], user_ns=user_ns)
else:
start_ipython(argv=[])