##// END OF EJS Templates
Adding -m option to %run, similar to -m for python interpreter....
Jörgen Stenarson -
Show More
@@ -91,6 +91,43 b' def needs_local_scope(func):'
91 91 func.needs_local_scope = True
92 92 return func
93 93
94 import imp, os
95
96 def find_module(name, path=None):
97 """imp.find_module variant that only return path of module
98 """
99 file, filename, _ = imp.find_module(name, path)
100 if file is None:
101 return filename
102 else:
103 file.close()
104 return filename
105
106 def get_init(dirname):
107 """Get __init__ file path for module with directory dirname
108 """
109 fbase = os.path.join(dirname, "__init__")
110 for ext in [".py", ".pyw", ".pyc", ".pyo"]:
111 fname = fbase + ext
112 if os.path.isfile(fname):
113 return fname
114
115
116 def find_mod(name):
117 """Find module *name* on sys.path
118 """
119 parts = name.split(".")
120 if len(parts) == 1:
121 basepath = find_module(parts[0])
122 else:
123 basepath = find_module(parts[0])
124 for submodname in parts[1:]:
125 basepath = find_module(submodname, [basepath])
126 if os.path.isdir(basepath):
127 basepath = get_init(basepath)
128 return basepath
129
130
94 131 # Used for exception handling in magic_edit
95 132 class MacroToEdit(ValueError): pass
96 133
@@ -1557,11 +1594,22 b' Currently the magic system has the following functions:\\n"""'
1557 1594 There is one special usage for which the text above doesn't apply:
1558 1595 if the filename ends with .ipy, the file is run as ipython script,
1559 1596 just as if the commands were written on IPython prompt.
1597
1598 -m: specify module name to load instead of script path. Similar to
1599 the -m option for the python interpreter. For example:
1600
1601 %run -m example
1602
1603 will run the example module.
1604
1560 1605 """
1561 1606
1562 1607 # get arguments and set sys.argv for program to be run.
1563 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1608 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:em:',
1564 1609 mode='list',list_all=1)
1610 if opts.has_key("m"):
1611 modulename = opts.get("m")[0]
1612 arg_lst = [find_mod(modulename)]
1565 1613
1566 1614 try:
1567 1615 filename = file_finder(arg_lst[0])
General Comments 0
You need to be logged in to leave comments. Login now