##// END OF EJS Templates
-Added a unit testing framework...
vivainio -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -0,0 +1,31 b''
1 """ Run ipython unit tests
2
3 This should be launched from inside ipython by "%run runtests.py"
4 or through ipython command line "ipython runtests.py".
5
6 """
7
8 from path import path
9 import pprint,os
10 import IPython.ipapi
11 ip = IPython.ipapi.get()
12
13 def main():
14 all = path('.').files('test_*py')
15 results = {}
16 res_exc = [None]
17 def exchook(self,*e):
18 res_exc[0] = [e]
19 ip.IP.set_custom_exc((Exception,), exchook)
20 startdir = os.getcwd()
21 for test in all:
22 print test
23 res_exc[0] = 'ok'
24 os.chdir(startdir)
25 ip.runlines(test.text())
26 results[str(test)] = res_exc[0]
27
28 os.chdir(startdir)
29 pprint.pprint(results)
30
31 main() No newline at end of file
@@ -0,0 +1,6 b''
1 import os
2 cd /
3 assert os.getcwd() == '/'
4 %cd /tmp
5
6 assert os.getcwd() == '/tmp'
@@ -0,0 +1,3 b''
1 # this will fail w/ assertionerror
2 cd /
3 assert os.getcwd() == '/does/not/exist'
@@ -1,184 +1,197 b''
1 1 ''' IPython customization API
2 2
3 3 Your one-stop module for configuring & extending ipython
4 4
5 5 The API will probably break when ipython 1.0 is released, but so
6 6 will the other configuration method (rc files).
7 7
8 8 All names prefixed by underscores are for internal use, not part
9 9 of the public api.
10 10
11 11 Below is an example that you can just put to a module and import from ipython.
12 12
13 13 A good practice is to install the config script below as e.g.
14 14
15 15 ~/.ipython/my_private_conf.py
16 16
17 17 And do
18 18
19 19 import_mod my_private_conf
20 20
21 21 in ~/.ipython/ipythonrc
22 22
23 23 That way the module is imported at startup and you can have all your
24 24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
25 25 stuff) in there.
26 26
27 27 -----------------------------------------------
28 28 import IPython.ipapi as ip
29 29
30 30 def ankka_f(self, arg):
31 31 print "Ankka",self,"says uppercase:",arg.upper()
32 32
33 33 ip.expose_magic("ankka",ankka_f)
34 34
35 35 ip.magic('alias sayhi echo "Testing, hi ok"')
36 36 ip.magic('alias helloworld echo "Hello world"')
37 37 ip.system('pwd')
38 38
39 39 ip.ex('import re')
40 40 ip.ex("""
41 41 def funcci(a,b):
42 42 print a+b
43 43 print funcci(3,4)
44 44 """)
45 45 ip.ex("funcci(348,9)")
46 46
47 47 def jed_editor(self,filename, linenum=None):
48 48 print "Calling my own editor, jed ... via hook!"
49 49 import os
50 50 if linenum is None: linenum = 0
51 51 os.system('jed +%d %s' % (linenum, filename))
52 52 print "exiting jed"
53 53
54 54 ip.set_hook('editor',jed_editor)
55 55
56 56 o = ip.options()
57 57 o.autocall = 2 # FULL autocall mode
58 58
59 59 print "done!"
60 60
61 61 '''
62 62
63 63
64 64 class TryNext(Exception):
65 65 """ Try next hook exception.
66 66
67 67 Raise this in your hook function to indicate that the next
68 68 hook handler should be used to handle the operation.
69 69 """
70 70
71 71
72 72 # contains the most recently instantiated IPApi
73 73 _recent = None
74 74
75 75 def get():
76 76 """ Get an IPApi object, or None if not running under ipython
77 77
78 78 Running this should be the first thing you do when writing
79 79 extensions that can be imported as normal modules. You can then
80 80 direct all the configuration operations against the returned
81 81 object.
82 82
83 83 """
84 84
85 85 return _recent
86 86
87 87
88 88
89 89 class IPApi:
90 90 """ The actual API class for configuring IPython
91 91
92 92 You should do all of the IPython configuration by getting
93 93 an IPApi object with IPython.ipapi.get() and using the provided
94 94 methods.
95 95
96 96 """
97 97 def __init__(self,ip):
98 98
99 99 self.magic = ip.ipmagic
100 100
101 101 self.system = ip.ipsystem
102 102
103 103 self.set_hook = ip.set_hook
104 104
105 self.set_custom_exc = ip.set_custom_exc
106
105 107 self.IP = ip
106 108 global _recent
107 109 _recent = self
108 110
109 111
110 112
111 113 def options(self):
112 114 """ All configurable variables """
113 115 return self.IP.rc
114 116
115 117 def user_ns(self):
116 118 return self.IP.user_ns
117 119
118 120 def expose_magic(self,magicname, func):
119 121 ''' Expose own function as magic function for ipython
120 122
121 123 def foo_impl(self,parameter_s=''):
122 124 """My very own magic!. (Use docstrings, IPython reads them)."""
123 125 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
124 126 print 'The self object is:',self
125 127
126 128 ipapi.expose_magic("foo",foo_impl)
127 129 '''
128 130
129 131 import new
130 132 im = new.instancemethod(func,self.IP, self.IP.__class__)
131 133 setattr(self.IP, "magic_" + magicname, im)
132 134
133 135
134 136 def ex(self,cmd):
135 137 """ Execute a normal python statement in user namespace """
136 138 exec cmd in self.user_ns()
137 139
138 140 def ev(self,expr):
139 141 """ Evaluate python expression expr in user namespace
140 142
141 143 Returns the result of evaluation"""
142 144 return eval(expr,self.user_ns())
143 145
144 146 def meta(self):
145 147 """ Get a session-specific data store
146 148
147 149 Object returned by this method can be used to store
148 150 data that should persist through the ipython session.
149 151 """
150 152 return self.IP.meta
151 153
152 154 def getdb(self):
153 155 """ Return a handle to persistent dict-like database
154 156
155 157 Return a PickleShareDB object.
156 158 """
157 159 return self.IP.db
160 def runlines(self,lines):
161 """ Run the specified lines in interpreter, honoring ipython directives.
162
163 This allows %magic and !shell escape notations.
158 164
165 Takes either all lines in one string or list of lines.
166 """
167 if isinstance(lines,basestring):
168 self.IP.runlines(lines)
169 else:
170 self.IP.runlines('\n'.join(lines))
171
159 172
160 173 def launch_new_instance(user_ns = None):
161 174 """ Create and start a new ipython instance.
162 175
163 176 This can be called even without having an already initialized
164 177 ipython session running.
165 178
166 179 This is also used as the egg entry point for the 'ipython' script.
167 180
168 181 """
169 182 ses = create_session(user_ns)
170 183 ses.mainloop()
171 184
172 185
173 186 def create_session(user_ns = None):
174 187 """ Creates, but does not launch an IPython session.
175 188
176 189 Later on you can call obj.mainloop() on the returned object.
177 190
178 191 This should *not* be run when a session exists already.
179 192
180 193 """
181 194 if user_ns is not None:
182 195 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
183 196 import IPython
184 197 return IPython.Shell.start(user_ns = user_ns) No newline at end of file
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now