Show More
@@ -1,116 +1,114 b'' | |||
|
1 | 1 | """ User configuration file for IPython |
|
2 | 2 | |
|
3 | 3 | This is a more flexible and safe way to configure ipython than *rc files |
|
4 | 4 | (ipythonrc, ipythonrc-pysh etc.) |
|
5 | 5 | |
|
6 | 6 | This file is always imported on ipython startup. You can import the |
|
7 | 7 | ipython extensions you need here (see IPython/Extensions directory). |
|
8 | 8 | |
|
9 | 9 | Feel free to edit this file to customize your ipython experience. |
|
10 | 10 | |
|
11 | 11 | Note that as such this file does nothing, for backwards compatibility. |
|
12 | 12 | Consult e.g. file 'ipy_profile_sh.py' for an example of the things |
|
13 | 13 | you can do here. |
|
14 | 14 | |
|
15 | 15 | See http://ipython.scipy.org/moin/IpythonExtensionApi for detailed |
|
16 | 16 | description on what you could do here. |
|
17 | 17 | """ |
|
18 | 18 | |
|
19 | 19 | # Most of your config files and extensions will probably start with this import |
|
20 | 20 | |
|
21 | 21 | import IPython.ipapi |
|
22 | 22 | ip = IPython.ipapi.get() |
|
23 | 23 | |
|
24 | 24 | # You probably want to uncomment this if you did %upgrade -nolegacy |
|
25 | 25 | # import ipy_defaults |
|
26 | 26 | |
|
27 | 27 | import os |
|
28 | 28 | |
|
29 | 29 | def main(): |
|
30 | 30 | |
|
31 | 31 | # uncomment if you want to get ipython -p sh behaviour |
|
32 | 32 | # without having to use command line switches |
|
33 | 33 | # import ipy_profile_sh |
|
34 | 34 | |
|
35 | 35 | # Configure your favourite editor? |
|
36 | 36 | # Good idea e.g. for %edit os.path.isfile |
|
37 | 37 | |
|
38 | 38 | #import ipy_editors |
|
39 | 39 | |
|
40 | 40 | # Choose one of these: |
|
41 | 41 | |
|
42 | 42 | #ipy_editors.scite() |
|
43 | 43 | #ipy_editors.scite('c:/opt/scite/scite.exe') |
|
44 | 44 | #ipy_editors.komodo() |
|
45 | 45 | #ipy_editors.idle() |
|
46 | 46 | # ... or many others, try 'ipy_editors??' after import to see them |
|
47 | 47 | |
|
48 | 48 | # Or roll your own: |
|
49 | 49 | #ipy_editors.install_editor("c:/opt/jed +$line $file") |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | o = ip.options |
|
53 | 53 | # An example on how to set options |
|
54 | 54 | #o.autocall = 1 |
|
55 | 55 | o.system_verbose = 0 |
|
56 | 56 | |
|
57 | 57 | #import_all("os sys") |
|
58 | 58 | #execf('~/_ipython/ns.py') |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | # -- prompt |
|
62 | 62 | # A different, more compact set of prompts from the default ones, that |
|
63 | 63 | # always show your current location in the filesystem: |
|
64 | 64 | |
|
65 | 65 | #o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>' |
|
66 | 66 | #o.prompt_in2 = r'.\D: ' |
|
67 | 67 | #o.prompt_out = r'[\#] ' |
|
68 | 68 | |
|
69 | 69 | # Try one of these color settings if you can't read the text easily |
|
70 | 70 | # autoexec is a list of IPython commands to execute on startup |
|
71 | 71 | #o.autoexec.append('%colors LightBG') |
|
72 | 72 | #o.autoexec.append('%colors NoColor') |
|
73 | 73 | #o.autoexec.append('%colors Linux') |
|
74 | 74 | |
|
75 | 75 | # for sane integer division that converts to float (1/2 == 0.5) |
|
76 | 76 | #o.autoexec.append('from __future__ import division') |
|
77 | 77 | |
|
78 | 78 | # For %tasks and %kill |
|
79 | 79 | #import jobctrl |
|
80 | 80 | |
|
81 | 81 | # For autoreloading of modules (%autoreload, %aimport) |
|
82 | 82 | #import ipy_autoreload |
|
83 | 83 | |
|
84 | 84 | # For winpdb support (%wdb) |
|
85 | 85 | #import ipy_winpdb |
|
86 | 86 | |
|
87 | 87 | # For bzr completer, requires bzrlib (the python installation of bzr) |
|
88 | 88 | #ip.load('ipy_bzr') |
|
89 | 89 | |
|
90 | 90 | # Tab completer that is not quite so picky (i.e. |
|
91 | 91 | # "foo".<TAB> and str(2).<TAB> will work). Complete |
|
92 | 92 | # at your own risk! |
|
93 | 93 | #import ipy_greedycompleter |
|
94 | 94 | |
|
95 | 95 | # If you are on Linux, you may be annoyed by |
|
96 | 96 | # "Display all N possibilities? (y or n)" on tab completion, |
|
97 | 97 | # as well as the paging through "more". Uncomment the following |
|
98 | 98 | # lines to disable that behaviour |
|
99 | 99 | #import readline |
|
100 | 100 | #readline.parse_and_bind('set completion-query-items 1000') |
|
101 | 101 | #readline.parse_and_bind('set page-completions no') |
|
102 | 102 | |
|
103 | ||
|
104 | ||
|
105 | ||
|
103 | ||
|
106 | 104 | # some config helper functions you can use |
|
107 | 105 | def import_all(modules): |
|
108 | 106 | """ Usage: import_all("os sys") """ |
|
109 | 107 | for m in modules.split(): |
|
110 | 108 | ip.ex("from %s import *" % m) |
|
111 | 109 | |
|
112 | 110 | def execf(fname): |
|
113 | 111 | """ Execute a file in user namespace """ |
|
114 | 112 | ip.ex('execfile("%s")' % os.path.expanduser(fname)) |
|
115 | 113 | |
|
116 | 114 | main() |
@@ -1,297 +1,306 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | |
|
3 | 3 | """Tests for genutils.py""" |
|
4 | 4 | |
|
5 | 5 | __docformat__ = "restructuredtext en" |
|
6 | 6 | |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (C) 2008 The IPython Development Team |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | # stdlib |
|
19 | 19 | import os |
|
20 | 20 | import shutil |
|
21 | 21 | import sys |
|
22 | 22 | import tempfile |
|
23 | 23 | |
|
24 | 24 | from os.path import join, abspath, split |
|
25 | 25 | |
|
26 | 26 | # third-party |
|
27 | 27 | import nose.tools as nt |
|
28 | 28 | |
|
29 | 29 | from nose import with_setup |
|
30 | 30 | from nose.tools import raises |
|
31 | 31 | |
|
32 | 32 | # Our own |
|
33 | 33 | import IPython |
|
34 | 34 | from IPython import genutils |
|
35 | 35 | from IPython.testing.decorators import skipif, skip_if_not_win32 |
|
36 | 36 | |
|
37 | 37 | # Platform-dependent imports |
|
38 | 38 | try: |
|
39 | 39 | import _winreg as wreg |
|
40 | 40 | except ImportError: |
|
41 | 41 | #Fake _winreg module on none windows platforms |
|
42 | 42 | import new |
|
43 | 43 | sys.modules["_winreg"] = new.module("_winreg") |
|
44 | 44 | import _winreg as wreg |
|
45 | 45 | #Add entries that needs to be stubbed by the testing code |
|
46 | 46 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
47 | 47 | |
|
48 | 48 | #----------------------------------------------------------------------------- |
|
49 | 49 | # Globals |
|
50 | 50 | #----------------------------------------------------------------------------- |
|
51 | 51 | env = os.environ |
|
52 | 52 | TEST_FILE_PATH = split(abspath(__file__))[0] |
|
53 | 53 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
54 | 54 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
55 | 55 | IP_TEST_DIR = join(HOME_TEST_DIR,'_ipython') |
|
56 | 56 | # |
|
57 | 57 | # Setup/teardown functions/decorators |
|
58 | 58 | # |
|
59 | 59 | |
|
60 | 60 | def setup(): |
|
61 | 61 | """Setup testenvironment for the module: |
|
62 | 62 | |
|
63 | 63 | - Adds dummy home dir tree |
|
64 | 64 | """ |
|
65 | 65 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
66 | 66 | # problem because that exception is only defined on Windows... |
|
67 | 67 | os.makedirs(IP_TEST_DIR) |
|
68 | 68 | |
|
69 | 69 | def teardown(): |
|
70 | 70 | """Teardown testenvironment for the module: |
|
71 | 71 | |
|
72 | 72 | - Remove dummy home dir tree |
|
73 | 73 | """ |
|
74 | 74 | # Note: we remove the parent test dir, which is the root of all test |
|
75 | 75 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
76 | 76 | # that non-empty directories are all recursively removed. |
|
77 | 77 | shutil.rmtree(TMP_TEST_DIR) |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | def setup_environment(): |
|
81 | 81 | """Setup testenvironment for some functions that are tested |
|
82 | 82 | in this module. In particular this functions stores attributes |
|
83 | 83 | and other things that we need to stub in some test functions. |
|
84 | 84 | This needs to be done on a function level and not module level because |
|
85 | 85 | each testfunction needs a pristine environment. |
|
86 | 86 | """ |
|
87 | 87 | global oldstuff, platformstuff |
|
88 | 88 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) |
|
89 | 89 | |
|
90 | 90 | if os.name == 'nt': |
|
91 | 91 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
92 | 92 | |
|
93 | 93 | if 'IPYTHONDIR' in env: |
|
94 | 94 | del env['IPYTHONDIR'] |
|
95 | 95 | |
|
96 | 96 | def teardown_environment(): |
|
97 | 97 | """Restore things that were remebered by the setup_environment function |
|
98 | 98 | """ |
|
99 | 99 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff |
|
100 | 100 | for key in env.keys(): |
|
101 | 101 | if key not in oldenv: |
|
102 | 102 | del env[key] |
|
103 | 103 | env.update(oldenv) |
|
104 | 104 | if hasattr(sys, 'frozen'): |
|
105 | 105 | del sys.frozen |
|
106 | 106 | if os.name == 'nt': |
|
107 | 107 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
108 | 108 | |
|
109 | 109 | # Build decorator that uses the setup_environment/setup_environment |
|
110 | 110 | with_enivronment = with_setup(setup_environment, teardown_environment) |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | # |
|
114 | 114 | # Tests for get_home_dir |
|
115 | 115 | # |
|
116 | 116 | |
|
117 | 117 | @skip_if_not_win32 |
|
118 | 118 | @with_enivronment |
|
119 | 119 | def test_get_home_dir_1(): |
|
120 | 120 | """Testcase for py2exe logic, un-compressed lib |
|
121 | 121 | """ |
|
122 | 122 | sys.frozen = True |
|
123 | 123 | |
|
124 | 124 | #fake filename for IPython.__init__ |
|
125 | 125 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) |
|
126 | 126 | |
|
127 | 127 | home_dir = genutils.get_home_dir() |
|
128 | 128 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
129 | 129 | |
|
130 | 130 | @skip_if_not_win32 |
|
131 | 131 | @with_enivronment |
|
132 | 132 | def test_get_home_dir_2(): |
|
133 | 133 | """Testcase for py2exe logic, compressed lib |
|
134 | 134 | """ |
|
135 | 135 | sys.frozen = True |
|
136 | 136 | #fake filename for IPython.__init__ |
|
137 | 137 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() |
|
138 | 138 | |
|
139 | 139 | home_dir = genutils.get_home_dir() |
|
140 | 140 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) |
|
141 | 141 | |
|
142 | 142 | @with_enivronment |
|
143 | 143 | def test_get_home_dir_3(): |
|
144 | 144 | """Testcase $HOME is set, then use its value as home directory.""" |
|
145 | 145 | env["HOME"] = HOME_TEST_DIR |
|
146 | 146 | home_dir = genutils.get_home_dir() |
|
147 | 147 | nt.assert_equal(home_dir, env["HOME"]) |
|
148 | 148 | |
|
149 | 149 | @with_enivronment |
|
150 | 150 | def test_get_home_dir_4(): |
|
151 | 151 | """Testcase $HOME is not set, os=='poix'. |
|
152 | 152 | This should fail with HomeDirError""" |
|
153 | 153 | |
|
154 | 154 | os.name = 'posix' |
|
155 | 155 | if 'HOME' in env: del env['HOME'] |
|
156 | 156 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) |
|
157 | 157 | |
|
158 | 158 | @skip_if_not_win32 |
|
159 | 159 | @with_enivronment |
|
160 | 160 | def test_get_home_dir_5(): |
|
161 | 161 | """Testcase $HOME is not set, os=='nt' |
|
162 | 162 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" |
|
163 | 163 | |
|
164 | 164 | os.name = 'nt' |
|
165 | 165 | if 'HOME' in env: del env['HOME'] |
|
166 | 166 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR) |
|
167 | 167 | |
|
168 | 168 | home_dir = genutils.get_home_dir() |
|
169 | 169 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
170 | 170 | |
|
171 | 171 | @skip_if_not_win32 |
|
172 | 172 | @with_enivronment |
|
173 | 173 | def test_get_home_dir_6(): |
|
174 | 174 | """Testcase $HOME is not set, os=='nt' |
|
175 | 175 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. |
|
176 | 176 | env['USERPROFILE'] points to path |
|
177 | 177 | """ |
|
178 | 178 | |
|
179 | 179 | os.name = 'nt' |
|
180 | 180 | if 'HOME' in env: del env['HOME'] |
|
181 | 181 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST" |
|
182 | 182 | env["USERPROFILE"] = abspath(HOME_TEST_DIR) |
|
183 | 183 | |
|
184 | 184 | home_dir = genutils.get_home_dir() |
|
185 | 185 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
186 | 186 | |
|
187 | 187 | # Should we stub wreg fully so we can run the test on all platforms? |
|
188 | 188 | @skip_if_not_win32 |
|
189 | 189 | @with_enivronment |
|
190 | 190 | def test_get_home_dir_7(): |
|
191 | 191 | """Testcase $HOME is not set, os=='nt' |
|
192 | 192 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing |
|
193 | 193 | """ |
|
194 | 194 | os.name = 'nt' |
|
195 | 195 | if 'HOME' in env: del env['HOME'] |
|
196 | 196 | if 'HOMEDRIVE' in env: del env['HOMEDRIVE'] |
|
197 | 197 | |
|
198 | 198 | #Stub windows registry functions |
|
199 | 199 | def OpenKey(x, y): |
|
200 | 200 | class key: |
|
201 | 201 | def Close(self): |
|
202 | 202 | pass |
|
203 | 203 | return key() |
|
204 | 204 | def QueryValueEx(x, y): |
|
205 | 205 | return [abspath(HOME_TEST_DIR)] |
|
206 | 206 | |
|
207 | 207 | wreg.OpenKey = OpenKey |
|
208 | 208 | wreg.QueryValueEx = QueryValueEx |
|
209 | 209 | |
|
210 | 210 | home_dir = genutils.get_home_dir() |
|
211 | 211 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
212 | 212 | |
|
213 | 213 | # |
|
214 | 214 | # Tests for get_ipython_dir |
|
215 | 215 | # |
|
216 | 216 | |
|
217 | 217 | @with_enivronment |
|
218 | 218 | def test_get_ipython_dir_1(): |
|
219 | 219 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
220 | 220 | env['IPYTHONDIR'] = "someplace/.ipython" |
|
221 | 221 | ipdir = genutils.get_ipython_dir() |
|
222 | 222 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) |
|
223 | 223 | |
|
224 | 224 | |
|
225 | 225 | @with_enivronment |
|
226 | 226 | def test_get_ipython_dir_2(): |
|
227 | 227 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
228 | 228 | genutils.get_home_dir = lambda : "someplace" |
|
229 | 229 | os.name = "posix" |
|
230 | 230 | ipdir = genutils.get_ipython_dir() |
|
231 | 231 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) |
|
232 | 232 | |
|
233 | 233 | @with_enivronment |
|
234 | 234 | def test_get_ipython_dir_3(): |
|
235 | 235 | """test_get_ipython_dir_3, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
236 | 236 | genutils.get_home_dir = lambda : "someplace" |
|
237 | 237 | os.name = "nt" |
|
238 | 238 | ipdir = genutils.get_ipython_dir() |
|
239 | 239 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) |
|
240 | 240 | |
|
241 | 241 | # |
|
242 | 242 | # Tests for get_security_dir |
|
243 | 243 | # |
|
244 | 244 | |
|
245 | 245 | @with_enivronment |
|
246 | 246 | def test_get_security_dir(): |
|
247 | 247 | """Testcase to see if we can call get_security_dir without Exceptions.""" |
|
248 | 248 | sdir = genutils.get_security_dir() |
|
249 | 249 | |
|
250 | 250 | # |
|
251 | 251 | # Tests for get_log_dir |
|
252 | 252 | # |
|
253 | 253 | |
|
254 | 254 | @with_enivronment |
|
255 | 255 | def test_get_log_dir(): |
|
256 | 256 | """Testcase to see if we can call get_log_dir without Exceptions.""" |
|
257 | 257 | sdir = genutils.get_log_dir() |
|
258 | 258 | |
|
259 | 259 | # |
|
260 | 260 | # Tests for popkey |
|
261 | 261 | # |
|
262 | 262 | |
|
263 | 263 | def test_popkey_1(): |
|
264 | 264 | """test_popkey_1, Basic usage test of popkey |
|
265 | 265 | """ |
|
266 | 266 | dct = dict(a=1, b=2, c=3) |
|
267 | 267 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
268 | 268 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
269 | 269 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
270 | 270 | nt.assert_equal(dct, dict(c=3)) |
|
271 | 271 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
272 | 272 | nt.assert_equal(dct, dict()) |
|
273 | 273 | |
|
274 | 274 | def test_popkey_2(): |
|
275 | 275 | """test_popkey_2, Test to see that popkey of non occuring keys |
|
276 | 276 | generates a KeyError exception |
|
277 | 277 | """ |
|
278 | 278 | dct = dict(a=1, b=2, c=3) |
|
279 | 279 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") |
|
280 | 280 | |
|
281 | 281 | def test_popkey_3(): |
|
282 | 282 | """test_popkey_3, Tests to see that popkey calls returns the correct value |
|
283 | 283 | and that the key/value was removed from the dict. |
|
284 | 284 | """ |
|
285 | 285 | dct = dict(a=1, b=2, c=3) |
|
286 | 286 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) |
|
287 | 287 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
288 | 288 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) |
|
289 | 289 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
290 | 290 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) |
|
291 | 291 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
292 | 292 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
293 | 293 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
294 | 294 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
295 | 295 | nt.assert_equal(dct, dict(c=3)) |
|
296 | 296 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
297 | 297 | nt.assert_equal(dct, dict()) |
|
298 | ||
|
299 | ||
|
300 | def test_filefind(): | |
|
301 | """Various tests for filefind""" | |
|
302 | f = tempfile.NamedTemporaryFile() | |
|
303 | print 'fname:',f.name | |
|
304 | alt_dirs = genutils.get_ipython_dir() | |
|
305 | t = genutils.filefind(f.name,alt_dirs) | |
|
306 | print 'found:',t |
General Comments 0
You need to be logged in to leave comments.
Login now