diff --git a/IPython/Extensions/ext_rehashdir.py b/IPython/Extensions/ext_rehashdir.py index 657830b..78ece65 100644 --- a/IPython/Extensions/ext_rehashdir.py +++ b/IPython/Extensions/ext_rehashdir.py @@ -1,102 +1,102 @@ -# -*- coding: utf-8 -*- -""" IPython extension: add %rehashdir magic - -Usage: - -%rehashdir c:/bin c:/tools - - Add all executables under c:/bin and c:/tools to alias table, in - order to make them directly executable from any directory. - -This also serves as an example on how to extend ipython -with new magic functions. - -Unlike rest of ipython, this requires Python 2.4 (optional -extensions are allowed to do that). - -To install, add - -"import_mod ext_rehashdir" - -To your ipythonrc or just execute "import rehash_dir" in ipython -prompt. - - -$Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ -""" - -import IPython.ipapi -ip = IPython.ipapi.get() - - -import os,re,fnmatch - -def rehashdir_f(self,arg): - """ Add executables in all specified dirs to alias table - - Usage: - - %rehashdir c:/bin;c:/tools - - Add all executables under c:/bin and c:/tools to alias table, in - order to make them directly executable from any directory. - - Without arguments, add all executables in current directory. - - """ - - # most of the code copied from Magic.magic_rehashx - - def isjunk(fname): - junk = ['*~'] - for j in junk: - if fnmatch.fnmatch(fname, j): - return True - return False - - if not arg: - arg = '.' - path = map(os.path.abspath,arg.split(';')) - alias_table = self.shell.alias_table - - if os.name == 'posix': - isexec = lambda fname:os.path.isfile(fname) and \ - os.access(fname,os.X_OK) - else: - - try: - winext = os.environ['pathext'].replace(';','|').replace('.','') - except KeyError: - winext = 'exe|com|bat|py' - - execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) - isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) - savedir = os.getcwd() - try: - # write the whole loop for posix/Windows so we don't have an if in - # the innermost part - if os.name == 'posix': - for pdir in path: - os.chdir(pdir) - for ff in os.listdir(pdir): - if isexec(ff) and not isjunk(ff): - # each entry in the alias table must be (N,name), - # where N is the number of positional arguments of the - # alias. - src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff) - print "Aliasing:",src,"->",tgt - alias_table[src] = (0,tgt) - else: - for pdir in path: - os.chdir(pdir) - for ff in os.listdir(pdir): - if isexec(ff) and not isjunk(ff): - src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff) - print "Aliasing:",src,"->",tgt - alias_table[src] = (0,tgt) - # Make sure the alias table doesn't contain keywords or builtins - self.shell.alias_table_validate() - # Call again init_auto_alias() so we get 'rm -i' and other - # modified aliases since %rehashx will probably clobber them - self.shell.init_auto_alias() - finally: - os.chdir(savedir) -ip.expose_magic("rehashdir",rehashdir_f) +# -*- coding: utf-8 -*- +""" IPython extension: add %rehashdir magic + +Usage: + +%rehashdir c:/bin c:/tools + - Add all executables under c:/bin and c:/tools to alias table, in + order to make them directly executable from any directory. + +This also serves as an example on how to extend ipython +with new magic functions. + +Unlike rest of ipython, this requires Python 2.4 (optional +extensions are allowed to do that). + +To install, add + +"import_mod ext_rehashdir" + +To your ipythonrc or just execute "import rehash_dir" in ipython +prompt. + + +$Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ +""" + +import IPython.ipapi +ip = IPython.ipapi.get() + + +import os,re,fnmatch + +def rehashdir_f(self,arg): + """ Add executables in all specified dirs to alias table + + Usage: + + %rehashdir c:/bin;c:/tools + - Add all executables under c:/bin and c:/tools to alias table, in + order to make them directly executable from any directory. + + Without arguments, add all executables in current directory. + + """ + + # most of the code copied from Magic.magic_rehashx + + def isjunk(fname): + junk = ['*~'] + for j in junk: + if fnmatch.fnmatch(fname, j): + return True + return False + + if not arg: + arg = '.' + path = map(os.path.abspath,arg.split(';')) + alias_table = self.shell.alias_table + + if os.name == 'posix': + isexec = lambda fname:os.path.isfile(fname) and \ + os.access(fname,os.X_OK) + else: + + try: + winext = os.environ['pathext'].replace(';','|').replace('.','') + except KeyError: + winext = 'exe|com|bat|py' + + execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) + isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) + savedir = os.getcwd() + try: + # write the whole loop for posix/Windows so we don't have an if in + # the innermost part + if os.name == 'posix': + for pdir in path: + os.chdir(pdir) + for ff in os.listdir(pdir): + if isexec(ff) and not isjunk(ff): + # each entry in the alias table must be (N,name), + # where N is the number of positional arguments of the + # alias. + src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff) + print "Aliasing:",src,"->",tgt + alias_table[src] = (0,tgt) + else: + for pdir in path: + os.chdir(pdir) + for ff in os.listdir(pdir): + if isexec(ff) and not isjunk(ff): + src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff) + print "Aliasing:",src,"->",tgt + alias_table[src] = (0,tgt) + # Make sure the alias table doesn't contain keywords or builtins + self.shell.alias_table_validate() + # Call again init_auto_alias() so we get 'rm -i' and other + # modified aliases since %rehashx will probably clobber them + self.shell.init_auto_alias() + finally: + os.chdir(savedir) +ip.expose_magic("rehashdir",rehashdir_f) diff --git a/IPython/Extensions/ipy_sane_defaults.py b/IPython/Extensions/ipy_sane_defaults.py index 424a097..3319e5a 100644 --- a/IPython/Extensions/ipy_sane_defaults.py +++ b/IPython/Extensions/ipy_sane_defaults.py @@ -1,60 +1,60 @@ -""" Set default options for "reasonable use" - -Just import this module to get reasonable defaults for everything. - -These configurations used to be performed in ipythonrc (or ipythonrc.ini). -Therefore importing this in your config files makes ipython basically -ignore your ipythonrc. This is *not* imported by default, you need to import -this manually in one of your config files. - -You can further override these defaults in e.g. your ipy_user_config.py, -ipy_profile_PROFILENAME etc. - -""" - -import IPython.rlineimpl as readline -import IPython.ipapi -ip = IPython.ipapi.get() - -o = ip.options() - - -o.colors = "Linux" -o.color_info=1 -o.confirm_exit=1 -o.pprint=1 -o.multi_line_specials=1 -o.xmode="Context" - - -o.prompt_in1='In [\#]: ' -o.prompt_in2 =' .\D.: ' -o.prompt_out = 'Out[\#]: ' -o.prompts_pad_left=1 - -o.readline_remove_delims="-/~" -o.readline_merge_completions=1 - -o.readline = 1 - -rlopts = """\ -tab: complete -"\C-l": possible-completions -set show-all-if-ambiguous on -"\C-o": tab-insert -"\M-i": " " -"\M-o": "\d\d\d\d" -"\M-I": "\d\d\d\d" -"\C-r": reverse-search-history -"\C-s": forward-search-history -"\C-p": history-search-backward -"\C-n": history-search-forward -"\e[A": history-search-backward -"\e[B": history-search-forward -"\C-k": kill-line -"\C-u": unix-line-discard""" - -for cmd in rlopts.split('\n'): - readline.parse_and_bind(cmd) - - \ No newline at end of file +""" Set default options for IPython. + +Just import this module to get reasonable defaults for everything. + +These configurations used to be performed in ipythonrc (or ipythonrc.ini). +Therefore importing this in your config files makes ipython basically +ignore your ipythonrc. This is *not* imported by default, you need to import +this manually in one of your config files. + +You can further override these defaults in e.g. your ipy_user_config.py, +ipy_profile_PROFILENAME etc. + +""" + +import IPython.rlineimpl as readline +import IPython.ipapi +ip = IPython.ipapi.get() + +o = ip.options() + + +o.colors = "Linux" +o.color_info=1 +o.confirm_exit=1 +o.pprint=1 +o.multi_line_specials=1 +o.xmode="Context" + + +o.prompt_in1='In [\#]: ' +o.prompt_in2 =' .\D.: ' +o.prompt_out = 'Out[\#]: ' +o.prompts_pad_left=1 + +o.readline_remove_delims="-/~" +o.readline_merge_completions=1 + +o.readline = 1 + +rlopts = """\ +tab: complete +"\C-l": possible-completions +set show-all-if-ambiguous on +"\C-o": tab-insert +"\M-i": " " +"\M-o": "\d\d\d\d" +"\M-I": "\d\d\d\d" +"\C-r": reverse-search-history +"\C-s": forward-search-history +"\C-p": history-search-backward +"\C-n": history-search-forward +"\e[A": history-search-backward +"\e[B": history-search-forward +"\C-k": kill-line +"\C-u": unix-line-discard""" + +for cmd in rlopts.split('\n'): + readline.parse_and_bind(cmd) + + diff --git a/IPython/Extensions/win32clip.py b/IPython/Extensions/win32clip.py index fb41f42..f2030e0 100644 --- a/IPython/Extensions/win32clip.py +++ b/IPython/Extensions/win32clip.py @@ -1,45 +1,45 @@ -import IPython.ipapi - -ip = IPython.ipapi.get() - -def clip_f( self, parameter_s = '' ): - """Save a set of lines to the clipboard. - - Usage:\\ - %clip n1-n2 n3-n4 ... n5 .. n6 ... - - This function uses the same syntax as %macro for line extraction, but - instead of creating a macro it saves the resulting string to the - clipboard. - - When used without arguments, this returns the text contents of the clipboard. - E.g. - - mytext = %clip - - """ - - import win32clipboard as cl - import win32con - args = parameter_s.split() - cl.OpenClipboard() - if len( args ) == 0: - data = cl.GetClipboardData( win32con.CF_TEXT ) - cl.CloseClipboard() - return data - api = self.getapi() - - if parameter_s.lstrip().startswith('='): - rest = parameter_s[parameter_s.index('=')+1:].strip() - val = str(api.ev(rest)) - else: - ranges = args[0:] - val = ''.join( self.extract_input_slices( ranges ) ) - - cl.EmptyClipboard() - cl.SetClipboardText( val ) - cl.CloseClipboard() - print 'The following text was written to the clipboard' - print val - -ip.expose_magic( "clip", clip_f ) +import IPython.ipapi + +ip = IPython.ipapi.get() + +def clip_f( self, parameter_s = '' ): + """Save a set of lines to the clipboard. + + Usage:\\ + %clip n1-n2 n3-n4 ... n5 .. n6 ... + + This function uses the same syntax as %macro for line extraction, but + instead of creating a macro it saves the resulting string to the + clipboard. + + When used without arguments, this returns the text contents of the clipboard. + E.g. + + mytext = %clip + + """ + + import win32clipboard as cl + import win32con + args = parameter_s.split() + cl.OpenClipboard() + if len( args ) == 0: + data = cl.GetClipboardData( win32con.CF_TEXT ) + cl.CloseClipboard() + return data + api = self.getapi() + + if parameter_s.lstrip().startswith('='): + rest = parameter_s[parameter_s.index('=')+1:].strip() + val = str(api.ev(rest)) + else: + ranges = args[0:] + val = ''.join( self.extract_input_slices( ranges ) ) + + cl.EmptyClipboard() + cl.SetClipboardText( val ) + cl.CloseClipboard() + print 'The following text was written to the clipboard' + print val + +ip.expose_magic( "clip", clip_f )