A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/ipython/ipython/commit/cdde5bba87d7ac8f06da0273182b5503043fd306 below:

add script magics · ipython/ipython@cdde5bb · GitHub

1 +

"""Magic functions for running cells in various scripts."""

2 +

#-----------------------------------------------------------------------------

3 +

# Copyright (c) 2012 The IPython Development Team.

4 +

#

5 +

# Distributed under the terms of the Modified BSD License.

6 +

#

7 +

# The full license is in the file COPYING.txt, distributed with this software.

8 +

#-----------------------------------------------------------------------------

9 + 10 +

#-----------------------------------------------------------------------------

11 +

# Imports

12 +

#-----------------------------------------------------------------------------

13 + 14 +

# Stdlib

15 +

import os

16 +

import re

17 +

import sys

18 +

from subprocess import Popen, PIPE

19 + 20 +

# Our own packages

21 +

from IPython.config.configurable import Configurable

22 +

from IPython.core.error import UsageError

23 +

from IPython.core.magic import (

24 +

Magics, magics_class, line_magic, cell_magic

25 +

)

26 +

from IPython.testing.skipdoctest import skip_doctest

27 +

from IPython.utils.process import find_cmd, FindCmdError

28 +

from IPython.utils.traitlets import List, Dict

29 + 30 +

#-----------------------------------------------------------------------------

31 +

# Magic implementation classes

32 +

#-----------------------------------------------------------------------------

33 + 34 +

@magics_class

35 +

class ScriptMagics(Magics, Configurable):

36 +

"""Magics for talking to scripts

37 + 38 +

This defines a base `%%script` cell magic for running a cell

39 +

with a program in a subprocess, and registers a few top-level

40 +

magics that call %%script with common interpreters.

41 +

"""

42 +

script_magics = List(config=True,

43 +

help="""Extra script cell magics to define

44 + 45 +

This generates simple wrappers of `%%script foo` as `%%foo`.

46 + 47 +

If you want to add script magics that aren't on your path,

48 +

specify them in script_paths

49 +

""",

50 +

)

51 +

def _script_magics_default(self):

52 +

"""default to a common list of programs if we find them"""

53 + 54 +

defaults = []

55 +

to_try = []

56 +

if os.name == 'nt':

57 +

defaults.append('cmd')

58 +

to_try.append('powershell')

59 +

to_try.extend([

60 +

'sh',

61 +

'bash',

62 +

'perl',

63 +

'ruby',

64 +

'python3',

65 +

'pypy',

66 +

])

67 + 68 +

for cmd in to_try:

69 +

if cmd in self.script_paths:

70 +

defaults.append(cmd)

71 +

else:

72 +

try:

73 +

find_cmd(cmd)

74 +

except FindCmdError:

75 +

# command not found, ignore it

76 +

pass

77 +

except ImportError:

78 +

# Windows without pywin32, find_cmd doesn't work

79 +

pass

80 +

else:

81 +

defaults.append(cmd)

82 +

return defaults

83 + 84 +

script_paths = Dict(config=True,

85 +

help="""Dict mapping short 'ruby' names to full paths, such as '/opt/secret/bin/ruby'

86 + 87 +

Only necessary for items in script_magics where the default path will not

88 +

find the right interpreter.

89 +

"""

90 +

)

91 + 92 +

def __init__(self, shell=None):

93 +

Configurable.__init__(self, config=shell.config)

94 +

self._generate_script_magics()

95 +

Magics.__init__(self, shell=shell)

96 + 97 +

def _generate_script_magics(self):

98 +

cell_magics = self.magics['cell']

99 +

for name in self.script_magics:

100 +

cell_magics[name] = self._make_script_magic(name)

101 + 102 +

def _make_script_magic(self, name):

103 +

"""make a named magic, that calls %%script with a particular program"""

104 +

# expand to explicit path if necessary:

105 +

script = self.script_paths.get(name, name)

106 + 107 +

def named_script_magic(line, cell):

108 +

# if line, add it as cl-flags

109 +

if line:

110 +

line = "%s %s" % (script, line)

111 +

else:

112 +

line = script

113 +

return self.shebang(line, cell)

114 + 115 +

# write a basic docstring:

116 +

named_script_magic.__doc__ = \

117 +

"""%%{name} script magic

118 + 119 +

Run cells with {script} in a subprocess.

120 + 121 +

This is a shortcut for `%%script {script}`

122 +

""".format(**locals())

123 + 124 +

return named_script_magic

125 + 126 +

@cell_magic("script")

127 +

def shebang(self, line, cell):

128 +

"""Run a cell via a shell command

129 + 130 +

The `%%script` line is like the #! line of script,

131 +

specifying a program (bash, perl, ruby, etc.) with which to run.

132 + 133 +

The rest of the cell is run by that program.

134 + 135 +

Examples

136 +

--------

137 +

::

138 + 139 +

In [1]: %%script bash

140 +

...: for i in 1 2 3; do

141 +

...: echo $i

142 +

...: done

143 +

1

144 +

2

145 +

3

146 +

"""

147 +

p = Popen(line, stdout=PIPE, stderr=PIPE, stdin=PIPE)

148 +

out,err = p.communicate(cell)

149 +

sys.stdout.write(out)

150 +

sys.stdout.flush()

151 +

sys.stderr.write(err)

152 +

sys.stderr.flush()

153 + 154 +

# expose %%script as %%!

155 +

cell_magic('!')(shebang)


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4