Show More
@@ -0,0 +1,90 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | """ IPython extension: add %rehashdir magic | |||
|
3 | ||||
|
4 | Usage: | |||
|
5 | ||||
|
6 | %rehash_dir c:/bin c:/tools | |||
|
7 | - Add all executables under c:/bin and c:/tools to alias table, in | |||
|
8 | order to make them directly executable from any directory. | |||
|
9 | ||||
|
10 | This also serves as an example on how to extend ipython | |||
|
11 | with new magic functions. | |||
|
12 | ||||
|
13 | Unlike rest of ipython, this requires Python 2.4 (optional | |||
|
14 | extensions are allowed to do that). | |||
|
15 | ||||
|
16 | To install, add | |||
|
17 | ||||
|
18 | "import_mod rehash_dir" | |||
|
19 | ||||
|
20 | To your ipythonrc or just execute "import rehash_dir" in ipython | |||
|
21 | prompt. | |||
|
22 | ||||
|
23 | ||||
|
24 | ||||
|
25 | ||||
|
26 | $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ | |||
|
27 | """ | |||
|
28 | ||||
|
29 | import IPython.ipapi as ip | |||
|
30 | ||||
|
31 | ||||
|
32 | import os,re | |||
|
33 | ||||
|
34 | @ip.asmagic("rehashdir") | |||
|
35 | def rehashdir_f(self,arg): | |||
|
36 | """ Add executables in all specified dirs to alias table | |||
|
37 | ||||
|
38 | Usage: | |||
|
39 | ||||
|
40 | %rehash_dir c:/bin c:/tools | |||
|
41 | - Add all executables under c:/bin and c:/tools to alias table, in | |||
|
42 | order to make them directly executable from any directory. | |||
|
43 | """ | |||
|
44 | ||||
|
45 | # most of the code copied from Magic.magic_rehashx | |||
|
46 | if not arg: | |||
|
47 | arg = '.' | |||
|
48 | path = arg.split() | |||
|
49 | alias_table = self.shell.alias_table | |||
|
50 | ||||
|
51 | if os.name == 'posix': | |||
|
52 | isexec = lambda fname:os.path.isfile(fname) and \ | |||
|
53 | os.access(fname,os.X_OK) | |||
|
54 | else: | |||
|
55 | ||||
|
56 | try: | |||
|
57 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |||
|
58 | except KeyError: | |||
|
59 | winext = 'exe|com|bat' | |||
|
60 | ||||
|
61 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |||
|
62 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | |||
|
63 | savedir = os.getcwd() | |||
|
64 | try: | |||
|
65 | # write the whole loop for posix/Windows so we don't have an if in | |||
|
66 | # the innermost part | |||
|
67 | if os.name == 'posix': | |||
|
68 | for pdir in path: | |||
|
69 | os.chdir(pdir) | |||
|
70 | for ff in os.listdir(pdir): | |||
|
71 | if isexec(ff): | |||
|
72 | # each entry in the alias table must be (N,name), | |||
|
73 | # where N is the number of positional arguments of the | |||
|
74 | # alias. | |||
|
75 | print "Aliasing",ff | |||
|
76 | alias_table[ff] = (0,os.path.abspath(ff)) | |||
|
77 | else: | |||
|
78 | for pdir in path: | |||
|
79 | os.chdir(pdir) | |||
|
80 | for ff in os.listdir(pdir): | |||
|
81 | if isexec(ff): | |||
|
82 | print "Aliasing",ff | |||
|
83 | alias_table[execre.sub(r'\1',ff)] = (0,os.path.abspath(ff)) | |||
|
84 | # Make sure the alias table doesn't contain keywords or builtins | |||
|
85 | self.shell.alias_table_validate() | |||
|
86 | # Call again init_auto_alias() so we get 'rm -i' and other | |||
|
87 | # modified aliases since %rehashx will probably clobber them | |||
|
88 | self.shell.init_auto_alias() | |||
|
89 | finally: | |||
|
90 | os.chdir(savedir) No newline at end of file |
@@ -6,7 +6,7 b' Requires Python 2.1 or better.' | |||||
6 |
|
6 | |||
7 | This file contains the main make_IPython() starter function. |
|
7 | This file contains the main make_IPython() starter function. | |
8 |
|
8 | |||
9 |
$Id: ipmaker.py 10 |
|
9 | $Id: ipmaker.py 1033 2006-01-20 10:41:20Z vivainio $""" | |
10 |
|
10 | |||
11 | #***************************************************************************** |
|
11 | #***************************************************************************** | |
12 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
12 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
@@ -478,6 +478,7 b" object? -> Details about 'object'. ?object also works, ?? prints more." | |||||
478 | # add personal .ipython dir to sys.path so that users can put things in |
|
478 | # add personal .ipython dir to sys.path so that users can put things in | |
479 | # there for customization |
|
479 | # there for customization | |
480 | sys.path.append(IP_rc.ipythondir) |
|
480 | sys.path.append(IP_rc.ipythondir) | |
|
481 | ||||
481 | sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran |
|
482 | sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran | |
482 |
|
483 | |||
483 | # update IP_rc with some special things that need manual |
|
484 | # update IP_rc with some special things that need manual | |
@@ -560,6 +561,8 b" object? -> Details about 'object'. ?object also works, ?? prints more." | |||||
560 | print 'loaded first).\n' |
|
561 | print 'loaded first).\n' | |
561 | pprint(IP_rc.__dict__) |
|
562 | pprint(IP_rc.__dict__) | |
562 |
|
563 | |||
|
564 | # Make it easy to import extensions | |||
|
565 | sys.path.append(os.path.join(IPython_dir,"Extensions")) | |||
563 | for mod in IP_rc.import_mod: |
|
566 | for mod in IP_rc.import_mod: | |
564 | try: |
|
567 | try: | |
565 | exec 'import '+mod in IP.user_ns |
|
568 | exec 'import '+mod in IP.user_ns |
@@ -1,3 +1,9 b'' | |||||
|
1 | 2006-01-20 Ville Vainio <vivainio@gmail.com> | |||
|
2 | ||||
|
3 | * Ipython/Extensions/rehash_dir.py: Created a usable example | |||
|
4 | of how to extend ipython with new magics. Also added Extensions | |||
|
5 | dir to pythonpath to make executing extensions easy. | |||
|
6 | ||||
1 | 2006-01-20 Fernando Perez <Fernando.Perez@colorado.edu> |
|
7 | 2006-01-20 Fernando Perez <Fernando.Perez@colorado.edu> | |
2 |
|
8 | |||
3 | * IPython/iplib.py (raw_input): I _think_ I got the pasting of |
|
9 | * IPython/iplib.py (raw_input): I _think_ I got the pasting of |
General Comments 0
You need to be logged in to leave comments.
Login now