Show More
@@ -1,102 +1,102 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ IPython extension: add %rehashdir magic |
|
2 | """ IPython extension: add %rehashdir magic | |
3 |
|
3 | |||
4 | Usage: |
|
4 | Usage: | |
5 |
|
5 | |||
6 | %rehashdir c:/bin c:/tools |
|
6 | %rehashdir c:/bin c:/tools | |
7 | - Add all executables under c:/bin and c:/tools to alias table, in |
|
7 | - Add all executables under c:/bin and c:/tools to alias table, in | |
8 | order to make them directly executable from any directory. |
|
8 | order to make them directly executable from any directory. | |
9 |
|
9 | |||
10 | This also serves as an example on how to extend ipython |
|
10 | This also serves as an example on how to extend ipython | |
11 | with new magic functions. |
|
11 | with new magic functions. | |
12 |
|
12 | |||
13 | Unlike rest of ipython, this requires Python 2.4 (optional |
|
13 | Unlike rest of ipython, this requires Python 2.4 (optional | |
14 | extensions are allowed to do that). |
|
14 | extensions are allowed to do that). | |
15 |
|
15 | |||
16 | To install, add |
|
16 | To install, add | |
17 |
|
17 | |||
18 | "import_mod ext_rehashdir" |
|
18 | "import_mod ext_rehashdir" | |
19 |
|
19 | |||
20 | To your ipythonrc or just execute "import rehash_dir" in ipython |
|
20 | To your ipythonrc or just execute "import rehash_dir" in ipython | |
21 | prompt. |
|
21 | prompt. | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ |
|
24 | $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ | |
25 | """ |
|
25 | """ | |
26 |
|
26 | |||
27 | import IPython.ipapi |
|
27 | import IPython.ipapi | |
28 | ip = IPython.ipapi.get() |
|
28 | ip = IPython.ipapi.get() | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | import os,re,fnmatch |
|
31 | import os,re,fnmatch | |
32 |
|
32 | |||
33 | def rehashdir_f(self,arg): |
|
33 | def rehashdir_f(self,arg): | |
34 | """ Add executables in all specified dirs to alias table |
|
34 | """ Add executables in all specified dirs to alias table | |
35 |
|
35 | |||
36 | Usage: |
|
36 | Usage: | |
37 |
|
37 | |||
38 | %rehashdir c:/bin;c:/tools |
|
38 | %rehashdir c:/bin;c:/tools | |
39 | - Add all executables under c:/bin and c:/tools to alias table, in |
|
39 | - Add all executables under c:/bin and c:/tools to alias table, in | |
40 | order to make them directly executable from any directory. |
|
40 | order to make them directly executable from any directory. | |
41 |
|
41 | |||
42 | Without arguments, add all executables in current directory. |
|
42 | Without arguments, add all executables in current directory. | |
43 |
|
43 | |||
44 | """ |
|
44 | """ | |
45 |
|
45 | |||
46 | # most of the code copied from Magic.magic_rehashx |
|
46 | # most of the code copied from Magic.magic_rehashx | |
47 |
|
47 | |||
48 | def isjunk(fname): |
|
48 | def isjunk(fname): | |
49 | junk = ['*~'] |
|
49 | junk = ['*~'] | |
50 | for j in junk: |
|
50 | for j in junk: | |
51 | if fnmatch.fnmatch(fname, j): |
|
51 | if fnmatch.fnmatch(fname, j): | |
52 | return True |
|
52 | return True | |
53 | return False |
|
53 | return False | |
54 |
|
54 | |||
55 | if not arg: |
|
55 | if not arg: | |
56 | arg = '.' |
|
56 | arg = '.' | |
57 | path = map(os.path.abspath,arg.split(';')) |
|
57 | path = map(os.path.abspath,arg.split(';')) | |
58 | alias_table = self.shell.alias_table |
|
58 | alias_table = self.shell.alias_table | |
59 |
|
59 | |||
60 | if os.name == 'posix': |
|
60 | if os.name == 'posix': | |
61 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
61 | isexec = lambda fname:os.path.isfile(fname) and \ | |
62 | os.access(fname,os.X_OK) |
|
62 | os.access(fname,os.X_OK) | |
63 | else: |
|
63 | else: | |
64 |
|
64 | |||
65 | try: |
|
65 | try: | |
66 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
66 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |
67 | except KeyError: |
|
67 | except KeyError: | |
68 | winext = 'exe|com|bat|py' |
|
68 | winext = 'exe|com|bat|py' | |
69 |
|
69 | |||
70 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
70 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |
71 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
71 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | |
72 | savedir = os.getcwd() |
|
72 | savedir = os.getcwd() | |
73 | try: |
|
73 | try: | |
74 | # write the whole loop for posix/Windows so we don't have an if in |
|
74 | # write the whole loop for posix/Windows so we don't have an if in | |
75 | # the innermost part |
|
75 | # the innermost part | |
76 | if os.name == 'posix': |
|
76 | if os.name == 'posix': | |
77 | for pdir in path: |
|
77 | for pdir in path: | |
78 | os.chdir(pdir) |
|
78 | os.chdir(pdir) | |
79 | for ff in os.listdir(pdir): |
|
79 | for ff in os.listdir(pdir): | |
80 | if isexec(ff) and not isjunk(ff): |
|
80 | if isexec(ff) and not isjunk(ff): | |
81 | # each entry in the alias table must be (N,name), |
|
81 | # each entry in the alias table must be (N,name), | |
82 | # where N is the number of positional arguments of the |
|
82 | # where N is the number of positional arguments of the | |
83 | # alias. |
|
83 | # alias. | |
84 | src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff) |
|
84 | src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff) | |
85 | print "Aliasing:",src,"->",tgt |
|
85 | print "Aliasing:",src,"->",tgt | |
86 | alias_table[src] = (0,tgt) |
|
86 | alias_table[src] = (0,tgt) | |
87 | else: |
|
87 | else: | |
88 | for pdir in path: |
|
88 | for pdir in path: | |
89 | os.chdir(pdir) |
|
89 | os.chdir(pdir) | |
90 | for ff in os.listdir(pdir): |
|
90 | for ff in os.listdir(pdir): | |
91 | if isexec(ff) and not isjunk(ff): |
|
91 | if isexec(ff) and not isjunk(ff): | |
92 | src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff) |
|
92 | src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff) | |
93 | print "Aliasing:",src,"->",tgt |
|
93 | print "Aliasing:",src,"->",tgt | |
94 | alias_table[src] = (0,tgt) |
|
94 | alias_table[src] = (0,tgt) | |
95 | # Make sure the alias table doesn't contain keywords or builtins |
|
95 | # Make sure the alias table doesn't contain keywords or builtins | |
96 | self.shell.alias_table_validate() |
|
96 | self.shell.alias_table_validate() | |
97 | # Call again init_auto_alias() so we get 'rm -i' and other |
|
97 | # Call again init_auto_alias() so we get 'rm -i' and other | |
98 | # modified aliases since %rehashx will probably clobber them |
|
98 | # modified aliases since %rehashx will probably clobber them | |
99 | self.shell.init_auto_alias() |
|
99 | self.shell.init_auto_alias() | |
100 | finally: |
|
100 | finally: | |
101 | os.chdir(savedir) |
|
101 | os.chdir(savedir) | |
102 | ip.expose_magic("rehashdir",rehashdir_f) |
|
102 | ip.expose_magic("rehashdir",rehashdir_f) |
@@ -1,60 +1,60 b'' | |||||
1 |
""" Set default options for |
|
1 | """ Set default options for IPython. | |
2 |
|
2 | |||
3 | Just import this module to get reasonable defaults for everything. |
|
3 | Just import this module to get reasonable defaults for everything. | |
4 |
|
4 | |||
5 | These configurations used to be performed in ipythonrc (or ipythonrc.ini). |
|
5 | These configurations used to be performed in ipythonrc (or ipythonrc.ini). | |
6 | Therefore importing this in your config files makes ipython basically |
|
6 | Therefore importing this in your config files makes ipython basically | |
7 | ignore your ipythonrc. This is *not* imported by default, you need to import |
|
7 | ignore your ipythonrc. This is *not* imported by default, you need to import | |
8 | this manually in one of your config files. |
|
8 | this manually in one of your config files. | |
9 |
|
9 | |||
10 | You can further override these defaults in e.g. your ipy_user_config.py, |
|
10 | You can further override these defaults in e.g. your ipy_user_config.py, | |
11 | ipy_profile_PROFILENAME etc. |
|
11 | ipy_profile_PROFILENAME etc. | |
12 |
|
12 | |||
13 | """ |
|
13 | """ | |
14 |
|
14 | |||
15 | import IPython.rlineimpl as readline |
|
15 | import IPython.rlineimpl as readline | |
16 | import IPython.ipapi |
|
16 | import IPython.ipapi | |
17 | ip = IPython.ipapi.get() |
|
17 | ip = IPython.ipapi.get() | |
18 |
|
18 | |||
19 | o = ip.options() |
|
19 | o = ip.options() | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | o.colors = "Linux" |
|
22 | o.colors = "Linux" | |
23 | o.color_info=1 |
|
23 | o.color_info=1 | |
24 | o.confirm_exit=1 |
|
24 | o.confirm_exit=1 | |
25 | o.pprint=1 |
|
25 | o.pprint=1 | |
26 | o.multi_line_specials=1 |
|
26 | o.multi_line_specials=1 | |
27 | o.xmode="Context" |
|
27 | o.xmode="Context" | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | o.prompt_in1='In [\#]: ' |
|
30 | o.prompt_in1='In [\#]: ' | |
31 | o.prompt_in2 =' .\D.: ' |
|
31 | o.prompt_in2 =' .\D.: ' | |
32 | o.prompt_out = 'Out[\#]: ' |
|
32 | o.prompt_out = 'Out[\#]: ' | |
33 | o.prompts_pad_left=1 |
|
33 | o.prompts_pad_left=1 | |
34 |
|
34 | |||
35 | o.readline_remove_delims="-/~" |
|
35 | o.readline_remove_delims="-/~" | |
36 | o.readline_merge_completions=1 |
|
36 | o.readline_merge_completions=1 | |
37 |
|
37 | |||
38 | o.readline = 1 |
|
38 | o.readline = 1 | |
39 |
|
39 | |||
40 | rlopts = """\ |
|
40 | rlopts = """\ | |
41 | tab: complete |
|
41 | tab: complete | |
42 | "\C-l": possible-completions |
|
42 | "\C-l": possible-completions | |
43 | set show-all-if-ambiguous on |
|
43 | set show-all-if-ambiguous on | |
44 | "\C-o": tab-insert |
|
44 | "\C-o": tab-insert | |
45 | "\M-i": " " |
|
45 | "\M-i": " " | |
46 | "\M-o": "\d\d\d\d" |
|
46 | "\M-o": "\d\d\d\d" | |
47 | "\M-I": "\d\d\d\d" |
|
47 | "\M-I": "\d\d\d\d" | |
48 | "\C-r": reverse-search-history |
|
48 | "\C-r": reverse-search-history | |
49 | "\C-s": forward-search-history |
|
49 | "\C-s": forward-search-history | |
50 | "\C-p": history-search-backward |
|
50 | "\C-p": history-search-backward | |
51 | "\C-n": history-search-forward |
|
51 | "\C-n": history-search-forward | |
52 | "\e[A": history-search-backward |
|
52 | "\e[A": history-search-backward | |
53 | "\e[B": history-search-forward |
|
53 | "\e[B": history-search-forward | |
54 | "\C-k": kill-line |
|
54 | "\C-k": kill-line | |
55 | "\C-u": unix-line-discard""" |
|
55 | "\C-u": unix-line-discard""" | |
56 |
|
56 | |||
57 | for cmd in rlopts.split('\n'): |
|
57 | for cmd in rlopts.split('\n'): | |
58 | readline.parse_and_bind(cmd) |
|
58 | readline.parse_and_bind(cmd) | |
59 |
|
59 | |||
60 | No newline at end of file |
|
60 |
@@ -1,45 +1,45 b'' | |||||
1 | import IPython.ipapi |
|
1 | import IPython.ipapi | |
2 |
|
2 | |||
3 | ip = IPython.ipapi.get() |
|
3 | ip = IPython.ipapi.get() | |
4 |
|
4 | |||
5 | def clip_f( self, parameter_s = '' ): |
|
5 | def clip_f( self, parameter_s = '' ): | |
6 | """Save a set of lines to the clipboard. |
|
6 | """Save a set of lines to the clipboard. | |
7 |
|
7 | |||
8 | Usage:\\ |
|
8 | Usage:\\ | |
9 | %clip n1-n2 n3-n4 ... n5 .. n6 ... |
|
9 | %clip n1-n2 n3-n4 ... n5 .. n6 ... | |
10 |
|
10 | |||
11 | This function uses the same syntax as %macro for line extraction, but |
|
11 | This function uses the same syntax as %macro for line extraction, but | |
12 | instead of creating a macro it saves the resulting string to the |
|
12 | instead of creating a macro it saves the resulting string to the | |
13 | clipboard. |
|
13 | clipboard. | |
14 |
|
14 | |||
15 | When used without arguments, this returns the text contents of the clipboard. |
|
15 | When used without arguments, this returns the text contents of the clipboard. | |
16 | E.g. |
|
16 | E.g. | |
17 |
|
17 | |||
18 | mytext = %clip |
|
18 | mytext = %clip | |
19 |
|
19 | |||
20 | """ |
|
20 | """ | |
21 |
|
21 | |||
22 | import win32clipboard as cl |
|
22 | import win32clipboard as cl | |
23 | import win32con |
|
23 | import win32con | |
24 | args = parameter_s.split() |
|
24 | args = parameter_s.split() | |
25 | cl.OpenClipboard() |
|
25 | cl.OpenClipboard() | |
26 | if len( args ) == 0: |
|
26 | if len( args ) == 0: | |
27 | data = cl.GetClipboardData( win32con.CF_TEXT ) |
|
27 | data = cl.GetClipboardData( win32con.CF_TEXT ) | |
28 | cl.CloseClipboard() |
|
28 | cl.CloseClipboard() | |
29 | return data |
|
29 | return data | |
30 | api = self.getapi() |
|
30 | api = self.getapi() | |
31 |
|
31 | |||
32 | if parameter_s.lstrip().startswith('='): |
|
32 | if parameter_s.lstrip().startswith('='): | |
33 | rest = parameter_s[parameter_s.index('=')+1:].strip() |
|
33 | rest = parameter_s[parameter_s.index('=')+1:].strip() | |
34 | val = str(api.ev(rest)) |
|
34 | val = str(api.ev(rest)) | |
35 | else: |
|
35 | else: | |
36 | ranges = args[0:] |
|
36 | ranges = args[0:] | |
37 | val = ''.join( self.extract_input_slices( ranges ) ) |
|
37 | val = ''.join( self.extract_input_slices( ranges ) ) | |
38 |
|
38 | |||
39 | cl.EmptyClipboard() |
|
39 | cl.EmptyClipboard() | |
40 | cl.SetClipboardText( val ) |
|
40 | cl.SetClipboardText( val ) | |
41 | cl.CloseClipboard() |
|
41 | cl.CloseClipboard() | |
42 | print 'The following text was written to the clipboard' |
|
42 | print 'The following text was written to the clipboard' | |
43 | print val |
|
43 | print val | |
44 |
|
44 | |||
45 | ip.expose_magic( "clip", clip_f ) |
|
45 | ip.expose_magic( "clip", clip_f ) |
General Comments 0
You need to be logged in to leave comments.
Login now