-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathipythonmagic.py
79 lines (58 loc) · 2.01 KB
/
ipythonmagic.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
import diffcmd.ipython
from IPython.core.magic import register_line_magic
from diffcmd.ipython import parse_line
command_map = {}
_DEFAULT_HELP = """
For help with diffcalc's orientation phase try:
>>> help ub
For help with moving in reciprocal lattice space try:
>>> help hkl
For more detailed help try for example:
>>> help newub
For help with driving axes or scanning:
>>> help pos
>>> help scan
For help with regular python try for example:
>>> help list
For more detailed help with diffcalc go to:
https://diffcalc.readthedocs.io
"""
# This function should be called with parameter globals()
def define_commands(dictionary):
print "Ipython detected - magicing commands"
magiced_names = []
commands = hkl_commands_for_help + ub_commands_for_help # @UndefinedVariable
commands += [pos, scan] # @UndefinedVariable
ipython.GLOBAL_NAMESPACE_DICT = dictionary
for f in commands:
# Skip section headers like 'Motion'
if not hasattr(f, '__call__'):
continue
# magic the function and remove from namespace (otherwise it would
# shadow the magiced command)
register_line_magic(parse_line(f))
del dictionary[f.__name__]
command_map[f.__name__] = f
magiced_names.append(f.__name__)
print "Magiced commands: " + ' '.join(magiced_names)
# because the functions have gone from namespace we need to override
pythons_help = __builtins__.help
del __builtins__.help
register_line_magic(help)
del help
def help(s):
"""Diffcalc help for iPython
"""
if s == '':
print _DEFAULT_HELP
elif s == 'hkl':
# Use help injected into hkl object
print hkl.__doc__
elif s == 'ub':
# Use help injected into ub command
print command_map['ub'].__doc__
elif s in command_map:
print "%s (diffcalc command):" %s
print command_map[s].__doc__
else:
exec('pythons_help(%s)' %s)