def text(self, etype, value, tb, tb_offset=None, context=5):
        """Return formatted traceback.

        Subclasses may override this if they add extra arguments.
        """
        tb_list = self.structured_traceback(etype, value, tb,
                                            tb_offset, context)
        return self.stb2text(tb_list)

def text ( self , etype , value , tb , tb_offset = None , context = 5 ) : tb_list = self . structured_traceback ( etype , value , tb , tb_offset , context ) return self . stb2text ( tb_list )


def structured_traceback(self, etype, value, elist, tb_offset=None,
                             context=5):
        """Return a color formatted string with the traceback info.

        Parameters
        ----------
        etype : exception type
          Type of the exception raised.

        value : object
          Data stored in the exception

        elist : list
          List of frames, see class docstring for details.

        tb_offset : int, optional
          Number of frames in the traceback to skip.  If not given, the
          instance value is used (set in constructor).

        context : int, optional
          Number of lines of context information to print.

        Returns
        -------
        String with formatted exception.
        """
        tb_offset = self.tb_offset if tb_offset is None else tb_offset
        Colors = self.Colors
        out_list = []
        if elist:

            if tb_offset and len(elist) > tb_offset:
                elist = elist[tb_offset:]

            out_list.append('Traceback %s(most recent call last)%s:' %
                                (Colors.normalEm, Colors.Normal) + '\n')
            out_list.extend(self._format_list(elist))
        # The exception info should be a single entry in the list.
        lines = ''.join(self._format_exception_only(etype, value))
        out_list.append(lines)

        # Note: this code originally read:

        ## for line in lines[:-1]:
        ##     out_list.append(" "+line)
        ## out_list.append(lines[-1])

        # This means it was indenting everything but the last line by a little
        # bit.  I've disabled this for now, but if we see ugliness somewhre we
        # can restore it.

        return out_list

Return a color formatted string with the traceback info .


def _format_list(self, extracted_list):
        """Format a list of traceback entry tuples for printing.

        Given a list of tuples as returned by extract_tb() or
        extract_stack(), return a list of strings ready for printing.
        Each string in the resulting list corresponds to the item with the
        same index in the argument list.  Each string ends in a newline;
        the strings may contain internal newlines as well, for those items
        whose source text line is not None.

        Lifted almost verbatim from traceback.py
        """

        Colors = self.Colors
        list = []
        for filename, lineno, name, line in extracted_list[:-1]:
            item = '  File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
                    (Colors.filename, filename, Colors.Normal,
                     Colors.lineno, lineno, Colors.Normal,
                     Colors.name, name, Colors.Normal)
            if line:
                item += '    %s\n' % line.strip()
            list.append(item)
        # Emphasize the last entry
        filename, lineno, name, line = extracted_list[-1]
        item = '%s  File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
                (Colors.normalEm,
                 Colors.filenameEm, filename, Colors.normalEm,
                 Colors.linenoEm, lineno, Colors.normalEm,
                 Colors.nameEm, name, Colors.normalEm,
                 Colors.Normal)
        if line:
            item += '%s    %s%s\n' % (Colors.line, line.strip(),
                                            Colors.Normal)
        list.append(item)
        #from pprint import pformat; print 'LISTTB', pformat(list) # dbg
        return list

Format a list of traceback entry tuples for printing .


def _format_exception_only(self, etype, value):
        """Format the exception part of a traceback.

        The arguments are the exception type and value such as given by
        sys.exc_info()[:2]. The return value is a list of strings, each ending
        in a newline.  Normally, the list contains a single string; however,
        for SyntaxError exceptions, it contains several lines that (when
        printed) display detailed information about where the syntax error
        occurred.  The message indicating which exception occurred is the
        always last string in the list.

        Also lifted nearly verbatim from traceback.py
        """

        have_filedata = False
        Colors = self.Colors
        list = []
        stype = Colors.excName + etype.__name__ + Colors.Normal
        if value is None:
            # Not sure if this can still happen in Python 2.6 and above
            list.append( str(stype) + '\n')
        else:
            if etype is SyntaxError:
                have_filedata = True
                #print 'filename is',filename  # dbg
                if not value.filename: value.filename = "<string>"
                list.append('%s  File %s"%s"%s, line %s%d%s\n' % \
                        (Colors.normalEm,
                         Colors.filenameEm, value.filename, Colors.normalEm,
                         Colors.linenoEm, value.lineno, Colors.Normal  ))
                if value.text is not None:
                    i = 0
                    while i < len(value.text) and value.text[i].isspace():
                        i += 1
                    list.append('%s    %s%s\n' % (Colors.line,
                                                  value.text.strip(),
                                                  Colors.Normal))
                    if value.offset is not None:
                        s = '    '
                        for c in value.text[i:value.offset-1]:
                            if c.isspace():
                                s += c
                            else:
                                s += ' '
                        list.append('%s%s^%s\n' % (Colors.caret, s,
                                                   Colors.Normal) )

            try:
                s = value.msg
            except Exception:
                s = self._some_str(value)
            if s:
                list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
                                              Colors.Normal, s))
            else:
                list.append('%s\n' % str(stype))

        # sync with user hooks
        if have_filedata:
            ipinst = ipapi.get()
            if ipinst is not None:
                ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)

        return list

Format the exception part of a traceback .


def show_exception_only(self, etype, evalue):
        """Only print the exception type and message, without a traceback.

        Parameters
        ----------
        etype : exception type
        value : exception value
        """
        # This method needs to use __call__ from *this* class, not the one from
        # a subclass whose signature or behavior may be different
        ostream = self.ostream
        ostream.flush()
        ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
        ostream.flush()

Only print the exception type and message without a traceback .


def debugger(self,force=False):
        """Call up the pdb debugger if desired, always clean up the tb
        reference.

        Keywords:

          - force(False): by default, this routine checks the instance call_pdb
          flag and does not actually invoke the debugger if the flag is false.
          The 'force' option forces the debugger to activate even if the flag
          is false.

        If the call_pdb flag is set, the pdb interactive debugger is
        invoked. In all cases, the self.tb reference to the current traceback
        is deleted to prevent lingering references which hamper memory
        management.

        Note that each call to pdb() does an 'import readline', so if your app
        requires a special setup for the readline completers, you'll have to
        fix that by hand after invoking the exception handler."""

        if force or self.call_pdb:
            if self.pdb is None:
                self.pdb = debugger.Pdb(
                    self.color_scheme_table.active_scheme_name)
            # the system displayhook may have changed, restore the original
            # for pdb
            display_trap = DisplayTrap(hook=sys.__displayhook__)
            with display_trap:
                self.pdb.reset()
                # Find the right frame so we don't pop up inside ipython itself
                if hasattr(self,'tb') and self.tb is not None:
                    etb = self.tb
                else:
                    etb = self.tb = sys.last_traceback
                while self.tb is not None and self.tb.tb_next is not None:
                    self.tb = self.tb.tb_next
                if etb and etb.tb_next:
                    etb = etb.tb_next
                self.pdb.botframe = etb.tb_frame
                self.pdb.interaction(self.tb.tb_frame, self.tb)

        if hasattr(self,'tb'):
            del self.tb

Call up the pdb debugger if desired always clean up the tb reference .


def set_mode(self,mode=None):
        """Switch to the desired mode.

        If mode is not specified, cycles through the available modes."""

        if not mode:
            new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
                      len(self.valid_modes)
            self.mode = self.valid_modes[new_idx]
        elif mode not in self.valid_modes:
            raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
                  'Valid modes: '+str(self.valid_modes)
        else:
            self.mode = mode
        # include variable details only in 'Verbose' mode
        self.include_vars = (self.mode == self.valid_modes[2])
        # Set the join character for generating text tracebacks
        self.tb_join_char = self._join_chars[self.mode]


def group_required(group,
                   login_url=None,
                   redirect_field_name=REDIRECT_FIELD_NAME,
                   skip_superuser=True):
    """
    View decorator for requiring a user group.
    """
    def decorator(view_func):
        @login_required(redirect_field_name=redirect_field_name,
                        login_url=login_url)
        def _wrapped_view(request, *args, **kwargs):

            if not (request.user.is_superuser and skip_superuser):
                if request.user.groups.filter(name=group).count() == 0:
                    raise PermissionDenied

            return view_func(request, *args, **kwargs)
        return _wrapped_view

    return decorator

def group_required ( group , login_url = None , redirect_field_name = REDIRECT_FIELD_NAME , skip_superuser = True ) : def decorator ( view_func ) : @ login_required ( redirect_field_name = redirect_field_name , login_url = login_url ) def _wrapped_view ( request , * args , * * kwargs ) : if not ( request . user . is_superuser and skip_superuser ) : if request . user . groups . filter ( name = group ) . count ( ) == 0 : raise PermissionDenied return view_func ( request , * args , * * kwargs ) return _wrapped_view return decorator

View decorator for requiring a user group .


def ensure_fromlist(mod, fromlist, buf, recursive):
    """Handle 'from module import a, b, c' imports."""
    if not hasattr(mod, '__path__'):
        return
    for item in fromlist:
        if not hasattr(item, 'rindex'):
            raise TypeError("Item in ``from list'' not a string")
        if item == '*':
            if recursive:
                continue # avoid endless recursion
            try:
                all = mod.__all__
            except AttributeError:
                pass
            else:
                ret = ensure_fromlist(mod, all, buf, 1)
                if not ret:
                    return 0
        elif not hasattr(mod, item):
            import_submodule(mod, item, buf + '.' + item)

def
ensure_fromlist
(
mod
,
fromlist
,
buf
,
recursive
)
:
if
not
hasattr
(
mod
,
'__path__'
)
:
return
for
item
in
fromlist
:
if
not
hasattr
(
item
,
'rindex'
)
:
raise
TypeError
(
"Item in ``from list'' not a string"
)
if
item
==
'*'
:
if
recursive
:
continue
# avoid endless recursion
try
:
all
=
mod
.
__all__
except
AttributeError
:
pass
else
:
ret
=
ensure_fromlist
(
mod
,
all
,
buf
,
1
)
if
not
ret
:
return
0
elif
not
hasattr
(
mod
,
item
)
:
import_submodule
(
mod
,
item
,
buf
+
'.'
+
item
)

Handle from module import a b c imports .


def add_line(self, line):
        """Add a line of source to the code.

        Don't include indentations or newlines.

        """
        self.code.append(" " * self.indent_amount)
        self.code.append(line)
        self.code.append("\n")

def
add_line
(
self
,
line
)
:
self
.
code
.
append
(
" "
*
self
.
indent_amount
)
self
.
code
.
append
(
line
)
self
.
code
.
append
(
"\n"
)


def add_section(self):
        """Add a section, a sub-CodeBuilder."""
        sect = CodeBuilder(self.indent_amount)
        self.code.append(sect)
        return sect

def add_section ( self ) : sect = CodeBuilder ( self . indent_amount ) self . code . append ( sect ) return sect


def get_function(self, fn_name):
        """Compile the code, and return the function `fn_name`."""
        assert self.indent_amount == 0
        g = {}
        code_text = str(self)
        exec(code_text, g)
        return g[fn_name]

def get_function ( self , fn_name ) : assert self . indent_amount == 0 g = { } code_text = str ( self ) exec ( code_text , g ) return g [ fn_name ]

Compile the code and return the function fn_name .


def expr_code(self, expr):
        """Generate a Python expression for `expr`."""
        if "|" in expr:
            pipes = expr.split("|")
            code = self.expr_code(pipes[0])
            for func in pipes[1:]:
                self.all_vars.add(func)
                code = "c_%s(%s)" % (func, code)
        elif "." in expr:
            dots = expr.split(".")
            code = self.expr_code(dots[0])
            args = [repr(d) for d in dots[1:]]
            code = "dot(%s, %s)" % (code, ", ".join(args))
        else:
            self.all_vars.add(expr)
            code = "c_%s" % expr
        return code

def
expr_code
(
self
,
expr
)
:
if
"|"
in
expr
:
pipes
=
expr
.
split
(
"|"
)
code
=
self
.
expr_code
(
pipes
[
0
]
)
for
func
in
pipes
[
1
:
]
:
self
.
all_vars
.
add
(
func
)
code
=
"c_%s(%s)"
%
(
func
,
code
)
elif
"."
in
expr
:
dots
=
expr
.
split
(
"."
)
code
=
self
.
expr_code
(
dots
[
0
]
)
args
=
[
repr
(
d
)
for
d
in
dots
[
1
:
]
]
code
=
"dot(%s, %s)"
%
(
code
,
", "
.
join
(
args
)
)
else
:
self
.
all_vars
.
add
(
expr
)
code
=
"c_%s"
%
expr
return
code


def render(self, context=None):
        """Render this template by applying it to `context`.

        `context` is a dictionary of values to use in this rendering.

        """
        # Make the complete context we'll use.
        ctx = dict(self.context)
        if context:
            ctx.update(context)
        return self.render_function(ctx, self.do_dots)

def
render
(
self
,
context
=
)
:
# Make the complete context we'll use.
ctx
=
dict
(
self
.
context
)
if
context
:
ctx
.
update
(
context
)
return
self
.
render_function
(
ctx
,
self
.
do_dots
)

Render this template by applying it to context .


def do_dots(self, value, *dots):
        """Evaluate dotted expressions at runtime."""
        for dot in dots:
            try:
                value = getattr(value, dot)
            except AttributeError:
                value = value[dot]
            if hasattr(value, '__call__'):
                value = value()
        return value

def do_dots ( self , value , * dots ) : for dot in dots : try : value = getattr ( value , dot ) except AttributeError : value = value [ dot ] if hasattr ( value , '__call__' ) : value = value ( ) return value

Evaluate dotted expressions at runtime .


def render_template(tpl, context):
    '''
    A shortcut function to render a partial template with context and return
    the output.
    '''

    templates = [tpl] if type(tpl) != list else tpl
    tpl_instance = None

    for tpl in templates:
        try:
            tpl_instance = template.loader.get_template(tpl)
            break
        except template.TemplateDoesNotExist:
            pass

    if not tpl_instance:
        raise Exception('Template does not exist: ' + templates[-1])

    return tpl_instance.render(template.Context(context))

def
render_template
(
tpl
,
context
)
:
templates
=
[
tpl
]
if
type
(
tpl
)
!=
list
else
tpl
tpl_instance
=
for
tpl
in
templates
:
try
:
tpl_instance
=
template
.
loader
.
get_template
(
tpl
)
break
except
template
.
TemplateDoesNotExist
:
pass
if
not
tpl_instance
:
raise
Exception
(
'Template does not exist: '
+
templates
[
1
]
)
return
tpl_instance
.
render
(
template
.
Context
(
context
)
)

A shortcut function to render a partial template with context and return the output .


def _formatters_default(self):
        """Activate the default formatters."""
        formatter_classes = [
            PlainTextFormatter,
            HTMLFormatter,
            SVGFormatter,
            PNGFormatter,
            JPEGFormatter,
            LatexFormatter,
            JSONFormatter,
            JavascriptFormatter
        ]
        d = {}
        for cls in formatter_classes:
            f = cls(config=self.config)
            d[f.format_type] = f
        return d

def _formatters_default ( self ) : formatter_classes = [ PlainTextFormatter , HTMLFormatter , SVGFormatter , PNGFormatter , JPEGFormatter , LatexFormatter , JSONFormatter , JavascriptFormatter ] d = { } for cls in formatter_classes : f = cls ( config = self . config ) d [ f . format_type ] = f return d


def for_type(self, typ, func):
        """Add a format function for a given type.

        Parameters
        -----------
        typ : class
            The class of the object that will be formatted using `func`.
        func : callable
            The callable that will be called to compute the format data. The
            call signature of this function is simple, it must take the
            object to be formatted and return the raw data for the given
            format. Subclasses may use a different call signature for the
            `func` argument.
        """
        oldfunc = self.type_printers.get(typ, None)
        if func is not None:
            # To support easy restoration of old printers, we need to ignore
            # Nones.
            self.type_printers[typ] = func
        return oldfunc

Add a format function for a given type .


def for_type_by_name(self, type_module, type_name, func):
        """Add a format function for a type specified by the full dotted
        module and name of the type, rather than the type of the object.

        Parameters
        ----------
        type_module : str
            The full dotted name of the module the type is defined in, like
            ``numpy``.
        type_name : str
            The name of the type (the class name), like ``dtype``
        func : callable
            The callable that will be called to compute the format data. The
            call signature of this function is simple, it must take the
            object to be formatted and return the raw data for the given
            format. Subclasses may use a different call signature for the
            `func` argument.
        """
        key = (type_module, type_name)
        oldfunc = self.deferred_printers.get(key, None)
        if func is not None:
            # To support easy restoration of old printers, we need to ignore
            # Nones.
            self.deferred_printers[key] = func
        return oldfunc

Add a format function for a type specified by the full dotted module and name of the type rather than the type of the object .


def _float_precision_changed(self, name, old, new):
        """float_precision changed, set float_format accordingly.

        float_precision can be set by int or str.
        This will set float_format, after interpreting input.
        If numpy has been imported, numpy print precision will also be set.

        integer `n` sets format to '%.nf', otherwise, format set directly.

        An empty string returns to defaults (repr for float, 8 for numpy).

        This parameter can be set via the '%precision' magic.
        """

        if '%' in new:
            # got explicit format string
            fmt = new
            try:
                fmt%3.14159
            except Exception:
                raise ValueError("Precision must be int or format string, not %r"%new)
        elif new:
            # otherwise, should be an int
            try:
                i = int(new)
                assert i >= 0
            except ValueError:
                raise ValueError("Precision must be int or format string, not %r"%new)
            except AssertionError:
                raise ValueError("int precision must be non-negative, not %r"%i)

            fmt = '%%.%if'%i
            if 'numpy' in sys.modules:
                # set numpy precision if it has been imported
                import numpy
                numpy.set_printoptions(precision=i)
        else:
            # default back to repr
            fmt = '%r'
            if 'numpy' in sys.modules:
                import numpy
                # numpy default is 8
                numpy.set_printoptions(precision=8)
        self.float_format = fmt

float_precision changed set float_format accordingly .


def user_config_files():
    """Return path to any existing user config files
    """
    return filter(os.path.exists,
                  map(os.path.expanduser, config_files))

def user_config_files ( ) : return filter ( os . path . exists , map ( os . path . expanduser , config_files ) )


def configure(self, argv=None, doc=None):
        """Configure the nose running environment. Execute configure before
        collecting tests with nose.TestCollector to enable output capture and
        other features.
        """
        env = self.env
        if argv is None:
            argv = sys.argv

        cfg_files = getattr(self, 'files', [])
        options, args = self._parseArgs(argv, cfg_files)
        # If -c --config has been specified on command line,
        # load those config files and reparse
        if getattr(options, 'files', []):
            options, args = self._parseArgs(argv, options.files)

        self.options = options
        if args:
            self.testNames = args
        if options.testNames is not None:
            self.testNames.extend(tolist(options.testNames))

        if options.py3where is not None:
            if sys.version_info >= (3,):
                options.where = options.py3where

        # `where` is an append action, so it can't have a default value
        # in the parser, or that default will always be in the list
        if not options.where:
            options.where = env.get('NOSE_WHERE', None)

        # include and exclude also
        if not options.ignoreFiles:
            options.ignoreFiles = env.get('NOSE_IGNORE_FILES', [])
        if not options.include:
            options.include = env.get('NOSE_INCLUDE', [])
        if not options.exclude:
            options.exclude = env.get('NOSE_EXCLUDE', [])

        self.addPaths = options.addPaths
        self.stopOnError = options.stopOnError
        self.verbosity = options.verbosity
        self.includeExe = options.includeExe
        self.traverseNamespace = options.traverseNamespace
        self.debug = options.debug
        self.debugLog = options.debugLog
        self.loggingConfig = options.loggingConfig
        self.firstPackageWins = options.firstPackageWins
        self.configureLogging()

        if options.where is not None:
            self.configureWhere(options.where)

        if options.testMatch:
            self.testMatch = re.compile(options.testMatch)

        if options.ignoreFiles:
            self.ignoreFiles = map(re.compile, tolist(options.ignoreFiles))
            log.info("Ignoring files matching %s", options.ignoreFiles)
        else:
            log.info("Ignoring files matching %s", self.ignoreFilesDefaultStrings)

        if options.include:
            self.include = map(re.compile, tolist(options.include))
            log.info("Including tests matching %s", options.include)

        if options.exclude:
            self.exclude = map(re.compile, tolist(options.exclude))
            log.info("Excluding tests matching %s", options.exclude)

        # When listing plugins we don't want to run them
        if not options.showPlugins:
            self.plugins.configure(options, self)
            self.plugins.begin()

Configure the nose running environment . Execute configure before collecting tests with nose . TestCollector to enable output capture and other features .


def configureLogging(self):
        """Configure logging for nose, or optionally other packages. Any logger
        name may be set with the debug option, and that logger will be set to
        debug level and be assigned the same handler as the nose loggers, unless
        it already has a handler.
        """
        if self.loggingConfig:
            from logging.config import fileConfig
            fileConfig(self.loggingConfig)
            return

        format = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
        if self.debugLog:
            handler = logging.FileHandler(self.debugLog)
        else:
            handler = logging.StreamHandler(self.logStream)
        handler.setFormatter(format)

        logger = logging.getLogger('nose')
        logger.propagate = 0

        # only add our default handler if there isn't already one there
        # this avoids annoying duplicate log messages.
        if handler not in logger.handlers:
            logger.addHandler(handler)

        # default level
        lvl = logging.WARNING
        if self.verbosity >= 5:
            lvl = 0
        elif self.verbosity >= 4:
            lvl = logging.DEBUG
        elif self.verbosity >= 3:
            lvl = logging.INFO
        logger.setLevel(lvl)

        # individual overrides
        if self.debug:
            # no blanks
            debug_loggers = [ name for name in self.debug.split(',')
                              if name ]
            for logger_name in debug_loggers:
                l = logging.getLogger(logger_name)
                l.setLevel(logging.DEBUG)
                if not l.handlers and not logger_name.startswith('nose'):
                    l.addHandler(handler)

Configure logging for nose or optionally other packages . Any logger name may be set with the debug option and that logger will be set to debug level and be assigned the same handler as the nose loggers unless it already has a handler .


def configureWhere(self, where):
        """Configure the working directory or directories for the test run.
        """
        from nose.importer import add_path
        self.workingDir = None
        where = tolist(where)
        warned = False
        for path in where:
            if not self.workingDir:
                abs_path = absdir(path)
                if abs_path is None:
                    raise ValueError("Working directory %s not found, or "
                                     "not a directory" % path)
                log.info("Set working dir to %s", abs_path)
                self.workingDir = abs_path
                if self.addPaths and \
                       os.path.exists(os.path.join(abs_path, '__init__.py')):
                    log.info("Working directory %s is a package; "
                             "adding to sys.path" % abs_path)
                    add_path(abs_path)
                continue
            if not warned:
                warn("Use of multiple -w arguments is deprecated and "
                     "support may be removed in a future release. You can "
                     "get the same behavior by passing directories without "
                     "the -w argument on the command line, or by using the "
                     "--tests argument in a configuration file.",
                     DeprecationWarning)
            self.testNames.append(path)

Configure the working directory or directories for the test run .


def page_dumb(strng, start=0, screen_lines=25):
    """Very dumb 'pager' in Python, for when nothing else works.

    Only moves forward, same interface as page(), except for pager_cmd and
    mode."""

    out_ln  = strng.splitlines()[start:]
    screens = chop(out_ln,screen_lines-1)
    if len(screens) == 1:
        print >>io.stdout, os.linesep.join(screens[0])
    else:
        last_escape = ""
        for scr in screens[0:-1]:
            hunk = os.linesep.join(scr)
            print >>io.stdout, last_escape + hunk
            if not page_more():
                return
            esc_list = esc_re.findall(hunk)
            if len(esc_list) > 0:
                last_escape = esc_list[-1]
        print >>io.stdout, last_escape + os.linesep.join(screens[-1])

def page_dumb ( strng , start = 0 , screen_lines = 25 ) : out_ln = strng . splitlines ( ) [ start : ] screens = chop ( out_ln , screen_lines - 1 ) if len ( screens ) == 1 : print >> io . stdout , os . linesep . join ( screens [ 0 ] ) else : last_escape = "" for scr in screens [ 0 : - 1 ] : hunk = os . linesep . join ( scr ) print >> io . stdout , last_escape + hunk if not page_more ( ) : return esc_list = esc_re . findall ( hunk ) if len ( esc_list ) > 0 : last_escape = esc_list [ - 1 ] print >> io . stdout , last_escape + os . linesep . join ( screens [ - 1 ] )

Very dumb pager in Python for when nothing else works .


def page(strng, start=0, screen_lines=0, pager_cmd=None):
    """Print a string, piping through a pager after a certain length.

    The screen_lines parameter specifies the number of *usable* lines of your
    terminal screen (total lines minus lines you need to reserve to show other
    information).

    If you set screen_lines to a number <=0, page() will try to auto-determine
    your screen size and will only use up to (screen_size+screen_lines) for
    printing, paging after that. That is, if you want auto-detection but need
    to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
    auto-detection without any lines reserved simply use screen_lines = 0.

    If a string won't fit in the allowed lines, it is sent through the
    specified pager command. If none given, look for PAGER in the environment,
    and ultimately default to less.

    If no system pager works, the string is sent through a 'dumb pager'
    written in python, very simplistic.
    """

    # Some routines may auto-compute start offsets incorrectly and pass a
    # negative value.  Offset to 0 for robustness.
    start = max(0, start)

    # first, try the hook
    ip = ipapi.get()
    if ip:
        try:
            ip.hooks.show_in_pager(strng)
            return
        except TryNext:
            pass

    # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
    TERM = os.environ.get('TERM','dumb')
    if TERM in ['dumb','emacs'] and os.name != 'nt':
        print strng
        return
    # chop off the topmost part of the string we don't want to see
    str_lines = strng.splitlines()[start:]
    str_toprint = os.linesep.join(str_lines)
    num_newlines = len(str_lines)
    len_str = len(str_toprint)

    # Dumb heuristics to guesstimate number of on-screen lines the string
    # takes.  Very basic, but good enough for docstrings in reasonable
    # terminals. If someone later feels like refining it, it's not hard.
    numlines = max(num_newlines,int(len_str/80)+1)

    screen_lines_def = get_terminal_size()[1]

    # auto-determine screen size
    if screen_lines <= 0:
        try:
            screen_lines += _detect_screen_size(use_curses, screen_lines_def)
        except (TypeError, UnsupportedOperation):
            print >>io.stdout, str_toprint
            return

    #print 'numlines',numlines,'screenlines',screen_lines  # dbg
    if numlines <= screen_lines :
        #print '*** normal print'  # dbg
        print >>io.stdout, str_toprint
    else:
        # Try to open pager and default to internal one if that fails.
        # All failure modes are tagged as 'retval=1', to match the return
        # value of a failed system command.  If any intermediate attempt
        # sets retval to 1, at the end we resort to our own page_dumb() pager.
        pager_cmd = get_pager_cmd(pager_cmd)
        pager_cmd += ' ' + get_pager_start(pager_cmd,start)
        if os.name == 'nt':
            if pager_cmd.startswith('type'):
                # The default WinXP 'type' command is failing on complex strings.
                retval = 1
            else:
                tmpname = tempfile.mktemp('.txt')
                tmpfile = open(tmpname,'wt')
                tmpfile.write(strng)
                tmpfile.close()
                cmd = "%s < %s" % (pager_cmd,tmpname)
                if os.system(cmd):
                  retval = 1
                else:
                  retval = None
                os.remove(tmpname)
        else:
            try:
                retval = None
                # if I use popen4, things hang. No idea why.
                #pager,shell_out = os.popen4(pager_cmd)
                pager = os.popen(pager_cmd,'w')
                pager.write(strng)
                pager.close()
                retval = pager.close()  # success returns None
            except IOError,msg:  # broken pipe when user quits
                if msg.args == (32,'Broken pipe'):
                    retval = None
                else:
                    retval = 1
            except OSError:
                # Other strange problems, sometimes seen in Win2k/cygwin
                retval = 1
        if retval is not None:
            page_dumb(strng,screen_lines=screen_lines)

Print a string piping through a pager after a certain length .


def page_file(fname, start=0, pager_cmd=None):
    """Page a file, using an optional pager command and starting line.
    """

    pager_cmd = get_pager_cmd(pager_cmd)
    pager_cmd += ' ' + get_pager_start(pager_cmd,start)

    try:
        if os.environ['TERM'] in ['emacs','dumb']:
            raise EnvironmentError
        system(pager_cmd + ' ' + fname)
    except:
        try:
            if start > 0:
                start -= 1
            page(open(fname).read(),start)
        except:
            print 'Unable to show file',`fname`

def
page_file
(
fname
,
start
=
0
,
pager_cmd
=
)
:
pager_cmd
=
get_pager_cmd
(
pager_cmd
)
pager_cmd
+=
' '
+
get_pager_start
(
pager_cmd
,
start
)
try
:
if
os
.
environ
[
'TERM'
]
in
[
'emacs'
,
'dumb'
]
:
raise
EnvironmentError
system
(
pager_cmd
+
' '
+
fname
)
except
:
try
:
if
start
>
0
:
start
-=
1
page
(
open
(
fname
)
.
read
(
)
,
start
)
except
:
print
'Unable to show file'
,
`fname`

Page a file using an optional pager command and starting line .


def get_pager_cmd(pager_cmd=None):
    """Return a pager command.

    Makes some attempts at finding an OS-correct one.
    """
    if os.name == 'posix':
        default_pager_cmd = 'less -r'  # -r for color control sequences
    elif os.name in ['nt','dos']:
        default_pager_cmd = 'type'

    if pager_cmd is None:
        try:
            pager_cmd = os.environ['PAGER']
        except:
            pager_cmd = default_pager_cmd
    return pager_cmd

def
get_pager_cmd
(
pager_cmd
=
)
:
if
os
.
name
==
'posix'
:
default_pager_cmd
=
'less -r'
# -r for color control sequences
elif
os
.
name
in
[
'nt'
,
'dos'
]
:
default_pager_cmd
=
'type'
if
pager_cmd
is
:
try
:
pager_cmd
=
os
.
environ
[
'PAGER'
]
except
:
pager_cmd
=
default_pager_cmd
return
pager_cmd


def get_pager_start(pager, start):
    """Return the string for paging files with an offset.

    This is the '+N' argument which less and more (under Unix) accept.
    """

    if pager in ['less','more']:
        if start:
            start_string = '+' + str(start)
        else:
            start_string = ''
    else:
        start_string = ''
    return start_string

def get_pager_start ( pager , start ) : if pager in [ 'less' , 'more' ] : if start : start_string = '+' + str ( start ) else : start_string = '' else : start_string = '' return start_string

Return the string for paging files with an offset .


def snip_print(str,width = 75,print_full = 0,header = ''):
    """Print a string snipping the midsection to fit in width.

    print_full: mode control:
      - 0: only snip long strings
      - 1: send to page() directly.
      - 2: snip long strings and ask for full length viewing with page()
    Return 1 if snipping was necessary, 0 otherwise."""

    if print_full == 1:
        page(header+str)
        return 0

    print header,
    if len(str) < width:
        print str
        snip = 0
    else:
        whalf = int((width -5)/2)
        print str[:whalf] + ' <...> ' + str[-whalf:]
        snip = 1
    if snip and print_full == 2:
        if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
            page(str)
    return snip

def
snip_print
(
str
,
width
=
75
,
print_full
=
0
,
header
=
''
)
:
if
print_full
==
1
:
page
(
header
+
str
)
return
0
print
header
,
if
len
(
str
)
<
width
:
print
str
snip
=
0
else
:
whalf
=
int
(
(
width
5
)
/
2
)
print
str
[
:
whalf
]
+
' <...> '
+
str
[
whalf
:
]
snip
=
1
if
snip
and
print_full
==
2
:
if
raw_input
(
header
+
' Snipped. View (y/n)? [N]'
)
.
lower
(
)
==
'y'
:
page
(
str
)
return
snip

Print a string snipping the midsection to fit in width .


def print_basic_unicode(o, p, cycle):
    """A function to pretty print sympy Basic objects."""
    if cycle:
        return p.text('Basic(...)')
    out = pretty(o, use_unicode=True)
    if '\n' in out:
        p.text(u'\n')
    p.text(out)

def print_basic_unicode ( o , p , cycle ) : if cycle : return p . text ( 'Basic(...)' ) out = pretty ( o , use_unicode = True ) if '\n' in out : p . text ( u'\n' ) p . text ( out )

A function to pretty print sympy Basic objects .


def print_png(o):
    """
    A function to display sympy expression using inline style LaTeX in PNG.
    """
    s = latex(o, mode='inline')
    # mathtext does not understand certain latex flags, so we try to replace
    # them with suitable subs.
    s = s.replace('\\operatorname','')
    s = s.replace('\\overline', '\\bar')
    png = latex_to_png(s)
    return png

A function to display sympy expression using inline style LaTeX in PNG .


def print_display_png(o):
    """
    A function to display sympy expression using display style LaTeX in PNG.
    """
    s = latex(o, mode='plain')
    s = s.strip('$')
    # As matplotlib does not support display style, dvipng backend is
    # used here.
    png = latex_to_png('$$%s$$' % s, backend='dvipng')
    return png

A function to display sympy expression using display style LaTeX in PNG .


def can_print_latex(o):
    """
    Return True if type o can be printed with LaTeX.

    If o is a container type, this is True if and only if every element of o
    can be printed with LaTeX.
    """
    import sympy
    if isinstance(o, (list, tuple, set, frozenset)):
        return all(can_print_latex(i) for i in o)
    elif isinstance(o, dict):
        return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
    elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):
        return True
    return False

def can_print_latex ( o ) : import sympy if isinstance ( o , ( list , tuple , set , frozenset ) ) : return all ( can_print_latex ( i ) for i in o ) elif isinstance ( o , dict ) : return all ( ( isinstance ( i , basestring ) or can_print_latex ( i ) ) and can_print_latex ( o [ i ] ) for i in o ) elif isinstance ( o , ( sympy . Basic , sympy . matrices . Matrix , int , long , float ) ) : return True return False

Return True if type o can be printed with LaTeX .


def print_latex(o):
    """A function to generate the latex representation of sympy
    expressions."""
    if can_print_latex(o):
        s = latex(o, mode='plain')
        s = s.replace('\\dag','\\dagger')
        s = s.strip('$')
        return '$$%s$$' % s
    # Fallback to the string printer
    return None

def
print_latex
(
o
)
:
if
can_print_latex
(
o
)
:
s
=
latex
(
o
,
mode
=
'plain'
)
s
=
s
.
replace
(
'\\dag'
,
'\\dagger'
)
s
=
s
.
strip
(
'$'
)
return
'$$%s$$'
%
s
# Fallback to the string printer
return

A function to generate the latex representation of sympy expressions .


def add_options(self, parser, env=None):
        """Non-camel-case version of func name for backwards compatibility.

        .. warning ::

           DEPRECATED: Do not use this method,
           use :meth:`options <nose.plugins.base.IPluginInterface.options>`
           instead.

        """
        # FIXME raise deprecation warning if wasn't called by wrapper
        if env is None:
            env = os.environ
        try:
            self.options(parser, env)
            self.can_configure = True
        except OptionConflictError, e:
            warn("Plugin %s has conflicting option string: %s and will "
                 "be disabled" % (self, e), RuntimeWarning)
            self.enabled = False
            self.can_configure = False

Non - camel - case version of func name for backwards compatibility .


def validate_string_list(lst):
    """Validate that the input is a list of strings.

    Raises ValueError if not."""
    if not isinstance(lst, list):
        raise ValueError('input %r must be a list' % lst)
    for x in lst:
        if not isinstance(x, basestring):
            raise ValueError('element %r in list must be a string' % x)

def
validate_string_list
(
lst
)
:
if
not
isinstance
(
lst
,
list
)
:
raise
ValueError
(
'input %r must be a list'
%
lst
)
for
x
in
lst
:
if
not
isinstance
(
x
,
basestring
)
:
raise
ValueError
(
'element %r in list must be a string'
%
x
)

Validate that the input is a list of strings .


def validate_string_dict(dct):
    """Validate that the input is a dict with string keys and values.

    Raises ValueError if not."""
    for k,v in dct.iteritems():
        if not isinstance(k, basestring):
            raise ValueError('key %r in dict must be a string' % k)
        if not isinstance(v, basestring):
            raise ValueError('value %r in dict must be a string' % v)

def
validate_string_dict
(
dct
)
:
for
k
,
v
in
dct
.
iteritems
(
)
:
if
not
isinstance
(
k
,
basestring
)
:
raise
ValueError
(
'key %r in dict must be a string'
%
k
)
if
not
isinstance
(
v
,
basestring
)
:
raise
ValueError
(
'value %r in dict must be a string'
%
v
)

Validate that the input is a dict with string keys and values .


def _run_loop(self):
        """Run my loop, ignoring EINTR events in the poller"""
        while True:
            try:
                self.ioloop.start()
            except ZMQError as e:
                if e.errno == errno.EINTR:
                    continue
                else:
                    raise
            except Exception:
                if self._exiting:
                    break
                else:
                    raise
            else:
                break

def _run_loop ( self ) : while True : try : self . ioloop . start ( ) except ZMQError as e : if e . errno == errno . EINTR : continue else : raise except Exception : if self . _exiting : break else : raise else : break

Run my loop ignoring EINTR events in the poller


def _handle_recv(self, msg):
        """callback for stream.on_recv

        unpacks message, and calls handlers with it.
        """
        ident,smsg = self.session.feed_identities(msg)
        self.call_handlers(self.session.unserialize(smsg))

def _handle_recv ( self , msg ) : ident , smsg = self . session . feed_identities ( msg ) self . call_handlers ( self . session . unserialize ( smsg ) )

callback for stream . on_recv unpacks message and calls handlers with it .


def execute(self, code, silent=False,
                user_variables=None, user_expressions=None, allow_stdin=None):
        """Execute code in the kernel.

        Parameters
        ----------
        code : str
            A string of Python code.

        silent : bool, optional (default False)
            If set, the kernel will execute the code as quietly possible.

        user_variables : list, optional
            A list of variable names to pull from the user's namespace.  They
            will come back as a dict with these names as keys and their
            :func:`repr` as values.

        user_expressions : dict, optional
            A dict with string keys and  to pull from the user's
            namespace.  They will come back as a dict with these names as keys
            and their :func:`repr` as values.

        allow_stdin : bool, optional
            Flag for
            A dict with string keys and  to pull from the user's
            namespace.  They will come back as a dict with these names as keys
            and their :func:`repr` as values.

        Returns
        -------
        The msg_id of the message sent.
        """
        if user_variables is None:
            user_variables = []
        if user_expressions is None:
            user_expressions = {}
        if allow_stdin is None:
            allow_stdin = self.allow_stdin

        # Don't waste network traffic if inputs are invalid
        if not isinstance(code, basestring):
            raise ValueError('code %r must be a string' % code)
        validate_string_list(user_variables)
        validate_string_dict(user_expressions)

        # Create class for content/msg creation. Related to, but possibly
        # not in Session.
        content = dict(code=code, silent=silent,
                       user_variables=user_variables,
                       user_expressions=user_expressions,
                       allow_stdin=allow_stdin,
                       )
        msg = self.session.msg('execute_request', content)
        self._queue_send(msg)
        return msg['header']['msg_id']


def complete(self, text, line, cursor_pos, block=None):
        """Tab complete text in the kernel's namespace.

        Parameters
        ----------
        text : str
            The text to complete.
        line : str
            The full line of text that is the surrounding context for the
            text to complete.
        cursor_pos : int
            The position of the cursor in the line where the completion was
            requested.
        block : str, optional
            The full block of code in which the completion is being requested.

        Returns
        -------
        The msg_id of the message sent.
        """
        content = dict(text=text, line=line, block=block, cursor_pos=cursor_pos)
        msg = self.session.msg('complete_request', content)
        self._queue_send(msg)
        return msg['header']['msg_id']

def complete ( self , text , line , cursor_pos , block = None ) : content = dict ( text = text , line = line , block = block , cursor_pos = cursor_pos ) msg = self . session . msg ( 'complete_request' , content ) self . _queue_send ( msg ) return msg [ 'header' ] [ 'msg_id' ]

Tab complete text in the kernel s namespace .


def object_info(self, oname, detail_level=0):
        """Get metadata information about an object.

        Parameters
        ----------
        oname : str
            A string specifying the object name.
        detail_level : int, optional
            The level of detail for the introspection (0-2)

        Returns
        -------
        The msg_id of the message sent.
        """
        content = dict(oname=oname, detail_level=detail_level)
        msg = self.session.msg('object_info_request', content)
        self._queue_send(msg)
        return msg['header']['msg_id']

def object_info ( self , oname , detail_level = 0 ) : content = dict ( oname = oname , detail_level = detail_level ) msg = self . session . msg ( 'object_info_request' , content ) self . _queue_send ( msg ) return msg [ 'header' ] [ 'msg_id' ]

Get metadata information about an object .


def history(self, raw=True, output=False, hist_access_type='range', **kwargs):
        """Get entries from the history list.

        Parameters
        ----------
        raw : bool
            If True, return the raw input.
        output : bool
            If True, then return the output as well.
        hist_access_type : str
            'range' (fill in session, start and stop params), 'tail' (fill in n)
             or 'search' (fill in pattern param).

        session : int
            For a range request, the session from which to get lines. Session
            numbers are positive integers; negative ones count back from the
            current session.
        start : int
            The first line number of a history range.
        stop : int
            The final (excluded) line number of a history range.

        n : int
            The number of lines of history to get for a tail request.

        pattern : str
            The glob-syntax pattern for a search request.

        Returns
        -------
        The msg_id of the message sent.
        """
        content = dict(raw=raw, output=output, hist_access_type=hist_access_type,
                                                                    **kwargs)
        msg = self.session.msg('history_request', content)
        self._queue_send(msg)
        return msg['header']['msg_id']

def history ( self , raw = True , output = False , hist_access_type = 'range' , * * kwargs ) : content = dict ( raw = raw , output = output , hist_access_type = hist_access_type , * * kwargs ) msg = self . session . msg ( 'history_request' , content ) self . _queue_send ( msg ) return msg [ 'header' ] [ 'msg_id' ]


def shutdown(self, restart=False):
        """Request an immediate kernel shutdown.

        Upon receipt of the (empty) reply, client code can safely assume that
        the kernel has shut down and it's safe to forcefully terminate it if
        it's still alive.

        The kernel will send the reply via a function registered with Python's
        atexit module, ensuring it's truly done as the kernel is done with all
        normal operation.
        """
        # Send quit message to kernel. Once we implement kernel-side setattr,
        # this should probably be done that way, but for now this will do.
        msg = self.session.msg('shutdown_request', {'restart':restart})
        self._queue_send(msg)
        return msg['header']['msg_id']


def flush(self, timeout=1.0):
        """Immediately processes all pending messages on the SUB channel.

        Callers should use this method to ensure that :method:`call_handlers`
        has been called for all messages that have been received on the
        0MQ SUB socket of this channel.

        This method is thread safe.

        Parameters
        ----------
        timeout : float, optional
            The maximum amount of time to spend flushing, in seconds. The
            default is one second.
        """
        # We do the IOLoop callback process twice to ensure that the IOLoop
        # gets to perform at least one full poll.
        stop_time = time.time() + timeout
        for i in xrange(2):
            self._flushed = False
            self.ioloop.add_callback(self._flush)
            while not self._flushed and time.time() < stop_time:
                time.sleep(0.01)

Immediately processes all pending messages on the SUB channel .


def input(self, string):
        """Send a string of raw input to the kernel."""
        content = dict(value=string)
        msg = self.session.msg('input_reply', content)
        self._queue_send(msg)

def input ( self , string ) : content = dict ( value = string ) msg = self . session . msg ( 'input_reply' , content ) self . _queue_send ( msg )

Send a string of raw input to the kernel .


def start_channels(self, shell=True, sub=True, stdin=True, hb=True):
        """Starts the channels for this kernel.

        This will create the channels if they do not exist and then start
        them. If port numbers of 0 are being used (random ports) then you
        must first call :method:`start_kernel`. If the channels have been
        stopped and you call this, :class:`RuntimeError` will be raised.
        """
        if shell:
            self.shell_channel.start()
        if sub:
            self.sub_channel.start()
        if stdin:
            self.stdin_channel.start()
            self.shell_channel.allow_stdin = True
        else:
            self.shell_channel.allow_stdin = False
        if hb:
            self.hb_channel.start()

def start_channels ( self , shell = True , sub = True , stdin = True , hb = True ) : if shell : self . shell_channel . start ( ) if sub : self . sub_channel . start ( ) if stdin : self . stdin_channel . start ( ) self . shell_channel . allow_stdin = True else : self . shell_channel . allow_stdin = False if hb : self . hb_channel . start ( )


def stop_channels(self):
        """Stops all the running channels for this kernel.
        """
        if self.shell_channel.is_alive():
            self.shell_channel.stop()
        if self.sub_channel.is_alive():
            self.sub_channel.stop()
        if self.stdin_channel.is_alive():
            self.stdin_channel.stop()
        if self.hb_channel.is_alive():
            self.hb_channel.stop()

def stop_channels ( self ) : if self . shell_channel . is_alive ( ) : self . shell_channel . stop ( ) if self . sub_channel . is_alive ( ) : self . sub_channel . stop ( ) if self . stdin_channel . is_alive ( ) : self . stdin_channel . stop ( ) if self . hb_channel . is_alive ( ) : self . hb_channel . stop ( )

Stops all the running channels for this kernel .


def channels_running(self):
        """Are any of the channels created and running?"""
        return (self.shell_channel.is_alive() or self.sub_channel.is_alive() or
                self.stdin_channel.is_alive() or self.hb_channel.is_alive())

def channels_running ( self ) : return ( self . shell_channel . is_alive ( ) or self . sub_channel . is_alive ( ) or self . stdin_channel . is_alive ( ) or self . hb_channel . is_alive ( ) )


def load_connection_file(self):
        """load connection info from JSON dict in self.connection_file"""
        with open(self.connection_file) as f:
            cfg = json.loads(f.read())

        self.ip = cfg['ip']
        self.shell_port = cfg['shell_port']
        self.stdin_port = cfg['stdin_port']
        self.iopub_port = cfg['iopub_port']
        self.hb_port = cfg['hb_port']
        self.session.key = str_to_bytes(cfg['key'])

def load_connection_file ( self ) : with open ( self . connection_file ) as f : cfg = json . loads ( f . read ( ) ) self . ip = cfg [ 'ip' ] self . shell_port = cfg [ 'shell_port' ] self . stdin_port = cfg [ 'stdin_port' ] self . iopub_port = cfg [ 'iopub_port' ] self . hb_port = cfg [ 'hb_port' ] self . session . key = str_to_bytes ( cfg [ 'key' ] )

load connection info from JSON dict in self . connection_file


def write_connection_file(self):
        """write connection info to JSON dict in self.connection_file"""
        if self._connection_file_written:
            return
        self.connection_file,cfg = write_connection_file(self.connection_file,
            ip=self.ip, key=self.session.key,
            stdin_port=self.stdin_port, iopub_port=self.iopub_port,
            shell_port=self.shell_port, hb_port=self.hb_port)
        # write_connection_file also sets default ports:
        self.shell_port = cfg['shell_port']
        self.stdin_port = cfg['stdin_port']
        self.iopub_port = cfg['iopub_port']
        self.hb_port = cfg['hb_port']

        self._connection_file_written = True

write connection info to JSON dict in self . connection_file


def start_kernel(self, **kw):
        """Starts a kernel process and configures the manager to use it.

        If random ports (port=0) are being used, this method must be called
        before the channels are created.

        Parameters:
        -----------
        launcher : callable, optional (default None)
             A custom function for launching the kernel process (generally a
             wrapper around ``entry_point.base_launch_kernel``). In most cases,
             it should not be necessary to use this parameter.

        **kw : optional
             See respective options for IPython and Python kernels.
        """
        if self.ip not in LOCAL_IPS:
            raise RuntimeError("Can only launch a kernel on a local interface. "
                               "Make sure that the '*_address' attributes are "
                               "configured properly. "
                               "Currently valid addresses are: %s"%LOCAL_IPS
                               )

        # write connection file / get default ports
        self.write_connection_file()

        self._launch_args = kw.copy()
        launch_kernel = kw.pop('launcher', None)
        if launch_kernel is None:
            from ipkernel import launch_kernel
        self.kernel = launch_kernel(fname=self.connection_file, **kw)

Starts a kernel process and configures the manager to use it .


def shutdown_kernel(self, restart=False):
        """ Attempts to the stop the kernel process cleanly. If the kernel
        cannot be stopped, it is killed, if possible.
        """
        # FIXME: Shutdown does not work on Windows due to ZMQ errors!
        if sys.platform == 'win32':
            self.kill_kernel()
            return

        # Pause the heart beat channel if it exists.
        if self._hb_channel is not None:
            self._hb_channel.pause()

        # Don't send any additional kernel kill messages immediately, to give
        # the kernel a chance to properly execute shutdown actions. Wait for at
        # most 1s, checking every 0.1s.
        self.shell_channel.shutdown(restart=restart)
        for i in range(10):
            if self.is_alive:
                time.sleep(0.1)
            else:
                break
        else:
            # OK, we've waited long enough.
            if self.has_kernel:
                self.kill_kernel()

        if not restart and self._connection_file_written:
            # cleanup connection files on full shutdown of kernel we started
            self._connection_file_written = False
            try:
                os.remove(self.connection_file)
            except IOError:
                pass

Attempts to the stop the kernel process cleanly . If the kernel cannot be stopped it is killed if possible .


def restart_kernel(self, now=False, **kw):
        """Restarts a kernel with the arguments that were used to launch it.

        If the old kernel was launched with random ports, the same ports will be
        used for the new kernel.

        Parameters
        ----------
        now : bool, optional
            If True, the kernel is forcefully restarted *immediately*, without
            having a chance to do any cleanup action.  Otherwise the kernel is
            given 1s to clean up before a forceful restart is issued.

            In all cases the kernel is restarted, the only difference is whether
            it is given a chance to perform a clean shutdown or not.

        **kw : optional
            Any options specified here will replace those used to launch the
            kernel.
        """
        if self._launch_args is None:
            raise RuntimeError("Cannot restart the kernel. "
                               "No previous call to 'start_kernel'.")
        else:
            # Stop currently running kernel.
            if self.has_kernel:
                if now:
                    self.kill_kernel()
                else:
                    self.shutdown_kernel(restart=True)

            # Start new kernel.
            self._launch_args.update(kw)
            self.start_kernel(**self._launch_args)

            # FIXME: Messages get dropped in Windows due to probable ZMQ bug
            # unless there is some delay here.
            if sys.platform == 'win32':
                time.sleep(0.2)

Restarts a kernel with the arguments that were used to launch it .


def kill_kernel(self):
        """ Kill the running kernel. """
        if self.has_kernel:
            # Pause the heart beat channel if it exists.
            if self._hb_channel is not None:
                self._hb_channel.pause()

            # Attempt to kill the kernel.
            try:
                self.kernel.kill()
            except OSError, e:
                # In Windows, we will get an Access Denied error if the process
                # has already terminated. Ignore it.
                if sys.platform == 'win32':
                    if e.winerror != 5:
                        raise
                # On Unix, we may get an ESRCH error if the process has already
                # terminated. Ignore it.
                else:
                    from errno import ESRCH
                    if e.errno != ESRCH:
                        raise
            self.kernel = None
        else:
            raise RuntimeError("Cannot kill kernel. No kernel is running!")


def interrupt_kernel(self):
        """ Interrupts the kernel. Unlike ``signal_kernel``, this operation is
        well supported on all platforms.
        """
        if self.has_kernel:
            if sys.platform == 'win32':
                from parentpoller import ParentPollerWindows as Poller
                Poller.send_interrupt(self.kernel.win32_interrupt_event)
            else:
                self.kernel.send_signal(signal.SIGINT)
        else:
            raise RuntimeError("Cannot interrupt kernel. No kernel is running!")

Interrupts the kernel . Unlike signal_kernel this operation is well supported on all platforms .


def signal_kernel(self, signum):
        """ Sends a signal to the kernel. Note that since only SIGTERM is
        supported on Windows, this function is only useful on Unix systems.
        """
        if self.has_kernel:
            self.kernel.send_signal(signum)
        else:
            raise RuntimeError("Cannot signal kernel. No kernel is running!")

Sends a signal to the kernel . Note that since only SIGTERM is supported on Windows this function is only useful on Unix systems .


def is_alive(self):
        """Is the kernel process still running?"""
        if self.has_kernel:
            if self.kernel.poll() is None:
                return True
            else:
                return False
        elif self._hb_channel is not None:
            # We didn't start the kernel with this KernelManager so we
            # use the heartbeat.
            return self._hb_channel.is_beating()
        else:
            # no heartbeat and not local, we can't tell if it's running,
            # so naively return True
            return True


def shell_channel(self):
        """Get the REQ socket channel object to make requests of the kernel."""
        if self._shell_channel is None:
            self._shell_channel = self.shell_channel_class(self.context,
                                                         self.session,
                                                         (self.ip, self.shell_port))
        return self._shell_channel

def shell_channel ( self ) : if self . _shell_channel is None : self . _shell_channel = self . shell_channel_class ( self . context , self . session , ( self . ip , self . shell_port ) ) return self . _shell_channel

Get the REQ socket channel object to make requests of the kernel .


def sub_channel(self):
        """Get the SUB socket channel object."""
        if self._sub_channel is None:
            self._sub_channel = self.sub_channel_class(self.context,
                                                       self.session,
                                                       (self.ip, self.iopub_port))
        return self._sub_channel

def sub_channel ( self ) : if self . _sub_channel is None : self . _sub_channel = self . sub_channel_class ( self . context , self . session , ( self . ip , self . iopub_port ) ) return self . _sub_channel


def hb_channel(self):
        """Get the heartbeat socket channel object to check that the
        kernel is alive."""
        if self._hb_channel is None:
            self._hb_channel = self.hb_channel_class(self.context,
                                                       self.session,
                                                       (self.ip, self.hb_port))
        return self._hb_channel

def hb_channel ( self ) : if self . _hb_channel is None : self . _hb_channel = self . hb_channel_class ( self . context , self . session , ( self . ip , self . hb_port ) ) return self . _hb_channel

Get the heartbeat socket channel object to check that the kernel is alive .


def bind_kernel(**kwargs):
    """Bind an Engine's Kernel to be used as a full IPython kernel.

    This allows a running Engine to be used simultaneously as a full IPython kernel
    with the QtConsole or other frontends.

    This function returns immediately.
    """
    from IPython.zmq.ipkernel import IPKernelApp
    from IPython.parallel.apps.ipengineapp import IPEngineApp

    # first check for IPKernelApp, in which case this should be a no-op
    # because there is already a bound kernel
    if IPKernelApp.initialized() and isinstance(IPKernelApp._instance, IPKernelApp):
        return

    if IPEngineApp.initialized():
        try:
            app = IPEngineApp.instance()
        except MultipleInstanceError:
            pass
        else:
            return app.bind_kernel(**kwargs)

    raise RuntimeError("bind_kernel be called from an IPEngineApp instance")

Bind an Engine s Kernel to be used as a full IPython kernel . This allows a running Engine to be used simultaneously as a full IPython kernel with the QtConsole or other frontends . This function returns immediately .


def debug(self, level, message):
        """
        Emit a debugging message depending on the debugging level.

        :param level: The debugging level.
        :param message: The message to emit.
        """

        if self._debug >= level:
            print(message, file=sys.stderr)

def debug ( self , level , message ) : if self . _debug >= level : print ( message , file = sys . stderr )

Emit a debugging message depending on the debugging level .


def _get_extension_classes(cls):
        """
        Retrieve the extension classes in priority order.

        :returns: A list of extension classes, in proper priority
                  order.
        """

        if cls._extension_classes is None:
            exts = {}

            # Iterate over the entrypoints
            for ext in entry.points[NAMESPACE_EXTENSIONS]:
                exts.setdefault(ext.priority, [])
                exts[ext.priority].append(ext)

            # Save the list of extension classes
            cls._extension_classes = list(utils.iter_prio_dict(exts))

        return cls._extension_classes

def
_get_extension_classes
(
cls
)
:
if
cls
.
_extension_classes
is
:
exts
=
{
}
# Iterate over the entrypoints
for
ext
in
entry
.
points
[
NAMESPACE_EXTENSIONS
]
:
exts
.
setdefault
(
ext
.
priority
,
[
]
)
exts
[
ext
.
priority
]
.
append
(
ext
)
# Save the list of extension classes
cls
.
_extension_classes
=
list
(
utils
.
iter_prio_dict
(
exts
)
)
return
cls
.
_extension_classes

Retrieve the extension classes in priority order .


def pre_step(self, ctxt, step, idx):
        """
        Called prior to executing a step.

        :param ctxt: An instance of ``timid.context.Context``.
        :param step: An instance of ``timid.steps.Step`` describing
                     the step to be executed.
        :param idx: The index of the step in the list of steps.

        :returns: A ``True`` value if the step is to be skipped,
                  ``False`` otherwise.
        """

        debugger = ExtensionDebugger('pre_step')

        for ext in self.exts:
            with debugger(ext):
                if ext.pre_step(ctxt, step, idx):
                    # Step must be skipped
                    debugger.debug(3, 'Skipping step %d' % idx)
                    return True

        return False

def
pre_step
(
self
,
ctxt
,
step
,
idx
)
:
debugger
=
ExtensionDebugger
(
'pre_step'
)
for
ext
in
self
.
exts
:
with
debugger
(
ext
)
:
if
ext
.
pre_step
(
ctxt
,
step
,
idx
)
:
# Step must be skipped
debugger
.
debug
(
3
,
'Skipping step %d'
%
idx
)
return
True
return
False


def post_step(self, ctxt, step, idx, result):
        """
        Called after executing a step.

        :param ctxt: An instance of ``timid.context.Context``.
        :param step: An instance of ``timid.steps.Step`` describing
                     the step that was executed.
        :param idx: The index of the step in the list of steps.
        :param result: An instance of ``timid.steps.StepResult``
                       describing the result of executing the step.
                       May be altered by the extension, e.g., to set
                       the ``ignore`` attribute.

        :returns: The ``result`` parameter, for convenience.
        """

        debugger = ExtensionDebugger('post_step')

        for ext in self.exts:
            with debugger(ext):
                ext.post_step(ctxt, step, idx, result)

        # Convenience return
        return result

def
post_step
(
self
,
ctxt
,
step
,
idx
,
result
)
:
debugger
=
ExtensionDebugger
(
'post_step'
)
for
ext
in
self
.
exts
:
with
debugger
(
ext
)
:
ext
.
post_step
(
ctxt
,
step
,
idx
,
result
)
# Convenience return
return
result


def finalize(self, ctxt, result):
        """
        Called at the end of processing.  This call allows extensions to
        emit any additional data, such as timing information, prior to
        ``timid``'s exit.  Extensions may also alter the return value.

        :param ctxt: An instance of ``timid.context.Context``.
        :param result: The return value of the basic ``timid`` call,
                       or an ``Exception`` instance if an exception
                       was raised.  Without the extension, this would
                       be passed directly to ``sys.exit()``.

        :returns: The final result.
        """

        debugger = ExtensionDebugger('finalize')

        for ext in self.exts:
            with debugger(ext):
                result = ext.finalize(ctxt, result)

        return result

def finalize ( self , ctxt , result ) : debugger = ExtensionDebugger ( 'finalize' ) for ext in self . exts : with debugger ( ext ) : result = ext . finalize ( ctxt , result ) return result

Called at the end of processing . This call allows extensions to emit any additional data such as timing information prior to timid s exit . Extensions may also alter the return value .


def walk_egg(egg_dir):
    """Walk an unpacked egg's contents, skipping the metadata directory"""
    walker = os.walk(egg_dir)
    base,dirs,files = walker.next()
    if 'EGG-INFO' in dirs:
        dirs.remove('EGG-INFO')
    yield base,dirs,files
    for bdf in walker:
        yield bdf

def walk_egg ( egg_dir ) : walker = os . walk ( egg_dir ) base , dirs , files = walker . next ( ) if 'EGG-INFO' in dirs : dirs . remove ( 'EGG-INFO' ) yield base , dirs , files for bdf in walker : yield bdf

Walk an unpacked egg s contents skipping the metadata directory


def scan_module(egg_dir, base, name, stubs):
    """Check whether module possibly uses unsafe-for-zipfile stuff"""

    filename = os.path.join(base,name)
    if filename[:-1] in stubs:
        return True     # Extension module
    pkg = base[len(egg_dir)+1:].replace(os.sep,'.')
    module = pkg+(pkg and '.' or '')+os.path.splitext(name)[0]
    if sys.version_info < (3, 3):
        skip = 8   # skip magic & date
    else:
        skip = 12  # skip magic & date & file size
    f = open(filename,'rb'); f.read(skip)
    code = marshal.load(f); f.close()
    safe = True
    symbols = dict.fromkeys(iter_symbols(code))
    for bad in ['__file__', '__path__']:
        if bad in symbols:
            log.warn("%s: module references %s", module, bad)
            safe = False
    if 'inspect' in symbols:
        for bad in [
            'getsource', 'getabsfile', 'getsourcefile', 'getfile'
            'getsourcelines', 'findsource', 'getcomments', 'getframeinfo',
            'getinnerframes', 'getouterframes', 'stack', 'trace'
        ]:
            if bad in symbols:
                log.warn("%s: module MAY be using inspect.%s", module, bad)
                safe = False
    if '__name__' in symbols and '__main__' in symbols and '.' not in module:
        if sys.version[:3]=="2.4":  # -m works w/zipfiles in 2.5
            log.warn("%s: top-level module may be 'python -m' script", module)
            safe = False
    return safe

Check whether module possibly uses unsafe - for - zipfile stuff


def launch_new_instance():
    """Create and run the IPython controller"""
    if sys.platform == 'win32':
        # make sure we don't get called from a multiprocessing subprocess
        # this can result in infinite Controllers being started on Windows
        # which doesn't have a proper fork, so multiprocessing is wonky

        # this only comes up when IPython has been installed using vanilla
        # setuptools, and *not* distribute.
        import multiprocessing
        p = multiprocessing.current_process()
        # the main process has name 'MainProcess'
        # subprocesses will have names like 'Process-1'
        if p.name != 'MainProcess':
            # we are a subprocess, don't start another Controller!
            return
    app = IPControllerApp.instance()
    app.initialize()
    app.start()


def save_connection_dict(self, fname, cdict):
        """save a connection dict to json file."""
        c = self.config
        url = cdict['url']
        location = cdict['location']
        if not location:
            try:
                proto,ip,port = split_url(url)
            except AssertionError:
                pass
            else:
                try:
                    location = socket.gethostbyname_ex(socket.gethostname())[2][-1]
                except (socket.gaierror, IndexError):
                    self.log.warn("Could not identify this machine's IP, assuming 127.0.0.1."
                    " You may need to specify '--location=<external_ip_address>' to help"
                    " IPython decide when to connect via loopback.")
                    location = '127.0.0.1'
            cdict['location'] = location
        fname = os.path.join(self.profile_dir.security_dir, fname)
        self.log.info("writing connection info to %s", fname)
        with open(fname, 'w') as f:
            f.write(json.dumps(cdict, indent=2))
        os.chmod(fname, stat.S_IRUSR|stat.S_IWUSR)


def load_config_from_json(self):
        """load config from existing json connector files."""
        c = self.config
        self.log.debug("loading config from JSON")
        # load from engine config
        fname = os.path.join(self.profile_dir.security_dir, self.engine_json_file)
        self.log.info("loading connection info from %s", fname)
        with open(fname) as f:
            cfg = json.loads(f.read())
        key = cfg['exec_key']
        # json gives unicode, Session.key wants bytes
        c.Session.key = key.encode('ascii')
        xport,addr = cfg['url'].split('://')
        c.HubFactory.engine_transport = xport
        ip,ports = addr.split(':')
        c.HubFactory.engine_ip = ip
        c.HubFactory.regport = int(ports)
        self.location = cfg['location']
        if not self.engine_ssh_server:
            self.engine_ssh_server = cfg['ssh']
        # load client config
        fname = os.path.join(self.profile_dir.security_dir, self.client_json_file)
        self.log.info("loading connection info from %s", fname)
        with open(fname) as f:
            cfg = json.loads(f.read())
        assert key == cfg['exec_key'], "exec_key mismatch between engine and client keys"
        xport,addr = cfg['url'].split('://')
        c.HubFactory.client_transport = xport
        ip,ports = addr.split(':')
        c.HubFactory.client_ip = ip
        if not self.ssh_server:
            self.ssh_server = cfg['ssh']
        assert int(ports) == c.HubFactory.regport, "regport mismatch"

load config from existing json connector files .


def load_secondary_config(self):
        """secondary config, loading from JSON and setting defaults"""
        if self.reuse_files:
            try:
                self.load_config_from_json()
            except (AssertionError,IOError) as e:
                self.log.error("Could not load config from JSON: %s" % e)
            else:
                # successfully loaded config from JSON, and reuse=True
                # no need to wite back the same file
                self.write_connection_files = False

        # switch Session.key default to secure
        default_secure(self.config)
        self.log.debug("Config changed")
        self.log.debug(repr(self.config))

secondary config loading from JSON and setting defaults


def parallel_execute(self, cell, block=None, groupby='type', save_name=None):
        """implementation used by %px and %%parallel"""

        # defaults:
        block = self.view.block if block is None else block

        base = "Parallel" if block else "Async parallel"

        targets = self.view.targets
        if isinstance(targets, list) and len(targets) > 10:
            str_targets = str(targets[:4])[:-1] + ', ..., ' + str(targets[-4:])[1:]
        else:
            str_targets = str(targets)
        if self.verbose:
            print base + " execution on engine(s): %s" % str_targets

        result = self.view.execute(cell, silent=False, block=False)
        self.last_result = result

        if save_name:
            self.shell.user_ns[save_name] = result

        if block:
            result.get()
            result.display_outputs(groupby)
        else:
            # return AsyncResult only on non-blocking submission
            return result


def _enable_autopx(self):
        """Enable %autopx mode by saving the original run_cell and installing
        pxrun_cell.
        """
        # override run_cell
        self._original_run_cell = self.shell.run_cell
        self.shell.run_cell = self.pxrun_cell

        self._autopx = True
        print "%autopx enabled"

def
_enable_autopx
(
self
)
:
# override run_cell
self
.
_original_run_cell
=
self
.
shell
.
run_cell
self
.
shell
.
run_cell
=
self
.
pxrun_cell
self
.
_autopx
=
True
print
"%autopx enabled"

Enable %autopx mode by saving the original run_cell and installing pxrun_cell .


def _disable_autopx(self):
        """Disable %autopx by restoring the original InteractiveShell.run_cell.
        """
        if self._autopx:
            self.shell.run_cell = self._original_run_cell
            self._autopx = False
            print "%autopx disabled"

def
_disable_autopx
(
self
)
:
if
self
.
_autopx
:
self
.
shell
.
run_cell
=
self
.
_original_run_cell
self
.
_autopx
=
False
print
"%autopx disabled"

Disable %autopx by restoring the original InteractiveShell . run_cell .


def pxrun_cell(self, raw_cell, store_history=False, silent=False):
        """drop-in replacement for InteractiveShell.run_cell.

        This executes code remotely, instead of in the local namespace.

        See InteractiveShell.run_cell for details.
        """

        if (not raw_cell) or raw_cell.isspace():
            return

        ipself = self.shell

        with ipself.builtin_trap:
            cell = ipself.prefilter_manager.prefilter_lines(raw_cell)

            # Store raw and processed history
            if store_history:
                ipself.history_manager.store_inputs(ipself.execution_count,
                                                  cell, raw_cell)

            # ipself.logger.log(cell, raw_cell)

            cell_name = ipself.compile.cache(cell, ipself.execution_count)

            try:
                ast.parse(cell, filename=cell_name)
            except (OverflowError, SyntaxError, ValueError, TypeError,
                    MemoryError):
                # Case 1
                ipself.showsyntaxerror()
                ipself.execution_count += 1
                return None
            except NameError:
                # ignore name errors, because we don't know the remote keys
                pass

        if store_history:
            # Write output to the database. Does nothing unless
            # history output logging is enabled.
            ipself.history_manager.store_output(ipself.execution_count)
            # Each cell is a *single* input, regardless of how many lines it has
            ipself.execution_count += 1
        if re.search(r'get_ipython\(\)\.magic\(u?["\']%?autopx', cell):
            self._disable_autopx()
            return False
        else:
            try:
                result = self.view.execute(cell, silent=False, block=False)
            except:
                ipself.showtraceback()
                return True
            else:
                if self.view.block:
                    try:
                        result.get()
                    except:
                        self.shell.showtraceback()
                        return True
                    else:
                        with ipself.builtin_trap:
                            result.display_outputs()
                return False

drop - in replacement for InteractiveShell . run_cell .


def run_heartbeat(message):
    """Internal ``CLOCK_CHANNEL`` consumer to process task runs"""
    then = arrow.get(message['time'])
    now = arrow.get()

    if (now - then) > timezone.timedelta(seconds=(TICK_FREQ+1)):
        pass # discard old ticks
    else:
        Task.run_tasks()

def
run_heartbeat
(
message
)
:
then
=
arrow
.
get
(
message
[
'time'
]
)
now
=
arrow
.
get
(
)
if
(
now
then
)
>
timezone
.
timedelta
(
seconds
=
(
TICK_FREQ
+
1
)
)
:
pass
# discard old ticks
else
:
Task
.
run_tasks
(
)

Internal CLOCK_CHANNEL consumer to process task runs


def run_task(message):
    """Internal ``RUN_TASK`` consumer to run the task's callable"""
    task = Task.objects.get(pk=message['id'])
    if task.allow_overlap:
        task.run(message)
    else:
        if not task.running:
            task.running = True
            task.save()
            try:
                task.run(message)
            finally:
                task.running = False
                task.save()

def run_task ( message ) : task = Task . objects . get ( pk = message [ 'id' ] ) if task . allow_overlap : task . run ( message ) else : if not task . running : task . running = True task . save ( ) try : task . run ( message ) finally : task . running = False task . save ( )

Internal RUN_TASK consumer to run the task s callable


def remove_task(message):
    """Internal ``KILL_TASK`` consumer to remove retired tasks"""
    task = Task.objects.get(pk=message['id'])
    task.delete()

def remove_task ( message ) : task = Task . objects . get ( pk = message [ 'id' ] ) task . delete ( )

Internal KILL_TASK consumer to remove retired tasks


def patch_protocol_for_agent(protocol):
    """
    Patch the protocol's makeConnection and connectionLost methods to make the
    protocol and its transport behave more like what `Agent` expects.

    While `Agent` is the driving force behind this, other clients and servers
    will no doubt have similar requirements.
    """
    old_makeConnection = protocol.makeConnection
    old_connectionLost = protocol.connectionLost

    def new_makeConnection(transport):
        patch_transport_fake_push_producer(transport)
        patch_transport_abortConnection(transport, protocol)
        return old_makeConnection(transport)

    def new_connectionLost(reason):
        # Replace ConnectionDone with ConnectionAborted if we aborted.
        if protocol._fake_connection_aborted and reason.check(ConnectionDone):
            reason = Failure(ConnectionAborted())
        return old_connectionLost(reason)

    protocol.makeConnection = new_makeConnection
    protocol.connectionLost = new_connectionLost
    protocol._fake_connection_aborted = False

Patch the protocol s makeConnection and connectionLost methods to make the protocol and its transport behave more like what Agent expects .


def patch_if_missing(obj, name, method):
    """
    Patch a method onto an object if it isn't already there.
    """
    setattr(obj, name, getattr(obj, name, method))

def patch_if_missing ( obj , name , method ) : setattr ( obj , name , getattr ( obj , name , method ) )

Patch a method onto an object if it isn t already there .


def accept_connection(self):
        """
        Accept a pending connection.
        """
        assert self.pending, "Connection is not pending."
        self.server_protocol = self.server.server_factory.buildProtocol(None)
        self._accept_d.callback(
            FakeServerProtocolWrapper(self, self.server_protocol))
        return self.await_connected()

def
accept_connection
(
self
)
:
assert
self
.
pending
,
"Connection is not pending."
self
.
server_protocol
=
self
.
server
.
server_factory
.
buildProtocol
(
)
self
.
_accept_d
.
callback
(
FakeServerProtocolWrapper
(
self
,
self
.
server_protocol
)
)
return
self
.
await_connected
(
)


def reject_connection(self, reason=None):
        """
        Reject a pending connection.
        """
        assert self.pending, "Connection is not pending."
        if reason is None:
            reason = ConnectionRefusedError()
        self._accept_d.errback(reason)

def
reject_connection
(
self
,
reason
=
)
:
assert
self
.
pending
,
"Connection is not pending."
if
reason
is
:
reason
=
ConnectionRefusedError
(
)
self
.
_accept_d
.
errback
(
reason
)


def get_agent(self, reactor=None, contextFactory=None):
        """
        Returns an IAgent that makes requests to this fake server.
        """
        return ProxyAgentWithContext(
            self.endpoint, reactor=reactor, contextFactory=contextFactory)

def get_agent ( self , reactor = None , contextFactory = None ) : return ProxyAgentWithContext ( self . endpoint , reactor = reactor , contextFactory = contextFactory )

Returns an IAgent that makes requests to this fake server .


def form_valid(self, form):
        """
        Calls pre and post save hooks.
        """
        self.object = form.save(commit=False)

        # Invoke pre_save hook, and allow it to abort the saving
        # process and do a redirect.
        response = self.pre_save(self.object)
        if response:
            return response

        self.object.save()
        form.save_m2m()
        self.post_save(self.object)

        return HttpResponseRedirect(self.get_success_url())


def delete(self, request, *args, **kwargs):
        """
        Calls pre and post delete hooks for DelteViews.
        """

        self.object = self.get_object()
        success_url = self.get_success_url()
        self.pre_delete(self.object)
        self.object.delete()
        self.post_delete(self.object)

        return HttpResponseRedirect(success_url)

def delete ( self , request , * args , * * kwargs ) : self . object = self . get_object ( ) success_url = self . get_success_url ( ) self . pre_delete ( self . object ) self . object . delete ( ) self . post_delete ( self . object ) return HttpResponseRedirect ( success_url )

Calls pre and post delete hooks for DelteViews .


def pre_save(self, instance):
        super(UserViewMixin, self).pre_save(instance)

        """
        Use SaveHookMixin pre_save to set the user.
        """
        if  self.request.user.is_authenticated():
            for field in self.user_field:
                setattr(instance, field, self.request.user)

def pre_save ( self , instance ) : super ( UserViewMixin , self ) . pre_save ( instance ) if self . request . user . is_authenticated ( ) : for field in self . user_field : setattr ( instance , field , self . request . user )

Use SaveHookMixin pre_save to set the user .


def report(self, morfs, outfile=None):
        """Writes a report summarizing coverage statistics per module.

        `outfile` is a file object to write the summary to.

        """
        self.find_code_units(morfs)

        # Prepare the formatting strings
        max_name = max([len(cu.name) for cu in self.code_units] + [5])
        fmt_name = "%%- %ds  " % max_name
        fmt_err = "%s   %s: %s\n"
        header = (fmt_name % "Name") + " Stmts   Miss"
        fmt_coverage = fmt_name + "%6d %6d"
        if self.branches:
            header += " Branch BrMiss"
            fmt_coverage += " %6d %6d"
        width100 = Numbers.pc_str_width()
        header += "%*s" % (width100+4, "Cover")
        fmt_coverage += "%%%ds%%%%" % (width100+3,)
        if self.config.show_missing:
            header += "   Missing"
            fmt_coverage += "   %s"
        rule = "-" * len(header) + "\n"
        header += "\n"
        fmt_coverage += "\n"

        if not outfile:
            outfile = sys.stdout

        # Write the header
        outfile.write(header)
        outfile.write(rule)

        total = Numbers()

        for cu in self.code_units:
            try:
                analysis = self.coverage._analyze(cu)
                nums = analysis.numbers
                args = (cu.name, nums.n_statements, nums.n_missing)
                if self.branches:
                    args += (nums.n_branches, nums.n_missing_branches)
                args += (nums.pc_covered_str,)
                if self.config.show_missing:
                    args += (analysis.missing_formatted(),)
                outfile.write(fmt_coverage % args)
                total += nums
            except KeyboardInterrupt:                   # pragma: not covered
                raise
            except:
                report_it = not self.config.ignore_errors
                if report_it:
                    typ, msg = sys.exc_info()[:2]
                    if typ is NotPython and not cu.should_be_python():
                        report_it = False
                if report_it:
                    outfile.write(fmt_err % (cu.name, typ.__name__, msg))

        if total.n_files > 1:
            outfile.write(rule)
            args = ("TOTAL", total.n_statements, total.n_missing)
            if self.branches:
                args += (total.n_branches, total.n_missing_branches)
            args += (total.pc_covered_str,)
            if self.config.show_missing:
                args += ("",)
            outfile.write(fmt_coverage % args)

        return total.pc_covered

def
report
(
self
,
morfs
,
outfile
=
)
:
self
.
find_code_units
(
morfs
)
# Prepare the formatting strings
max_name
=
max
(
[
len
(
cu
.
name
)
for
cu
in
self
.
code_units
]
+
[
5
]
)
fmt_name
=
"%%- %ds  "
%
max_name
fmt_err
=
"%s   %s: %s\n"
header
=
(
fmt_name
%
"Name"
)
+
" Stmts   Miss"
fmt_coverage
=
fmt_name
+
"%6d %6d"
if
self
.
branches
:
header
+=
" Branch BrMiss"
fmt_coverage
+=
" %6d %6d"
width100
=
Numbers
.
pc_str_width
(
)
header
+=
"%*s"
%
(
width100
+
4
,
"Cover"
)
fmt_coverage
+=
"%%%ds%%%%"
%
(
width100
+
3
,
)
if
self
.
config
.
show_missing
:
header
+=
"   Missing"
fmt_coverage
+=
"   %s"
rule
=
"-"
*
len
(
header
)
+
"\n"
header
+=
"\n"
fmt_coverage
+=
"\n"
if
not
outfile
:
outfile
=
sys
.
stdout
# Write the header
outfile
.
write
(
header
)
outfile
.
write
(
rule
)
total
=
Numbers
(
)
for
cu
in
self
.
code_units
:
try
:
analysis
=
self
.
coverage
.
_analyze
(
cu
)
nums
=
analysis
.
numbers
args
=
(
cu
.
name
,
nums
.
n_statements
,
nums
.
n_missing
)
if
self
.
branches
:
args
+=
(
nums
.
n_branches
,
nums
.
n_missing_branches
)
args
+=
(
nums
.
pc_covered_str
,
)
if
self
.
config
.
show_missing
:
args
+=
(
analysis
.
missing_formatted
(
)
,
)
outfile
.
write
(
fmt_coverage
%
args
)
total
+=
nums
except
KeyboardInterrupt
:
# pragma: not covered
raise
except
:
report_it
=
not
self
.
config
.
ignore_errors
if
report_it
:
typ
,
msg
=
sys
.
exc_info
(
)
[
:
2
]
if
typ
is
NotPython
and
not
cu
.
should_be_python
(
)
:
report_it
=
False
if
report_it
:
outfile
.
write
(
fmt_err
%
(
cu
.
name
,
typ
.
__name__
,
msg
)
)
if
total
.
n_files
>
1
:
outfile
.
write
(
rule
)
args
=
(
"TOTAL"
,
total
.
n_statements
,
total
.
n_missing
)
if
self
.
branches
:
args
+=
(
total
.
n_branches
,
total
.
n_missing_branches
)
args
+=
(
total
.
pc_covered_str
,
)
if
self
.
config
.
show_missing
:
args
+=
(
""
,
)
outfile
.
write
(
fmt_coverage
%
args
)
return
total
.
pc_covered

Writes a report summarizing coverage statistics per module .


def check(self, check_all=False):
        """Check whether some modules need to be reloaded."""

        if not self.enabled and not check_all:
            return

        if check_all or self.check_all:
            modules = sys.modules.keys()
        else:
            modules = self.modules.keys()

        for modname in modules:
            m = sys.modules.get(modname, None)

            if modname in self.skip_modules:
                continue

            if not hasattr(m, '__file__'):
                continue

            if m.__name__ == '__main__':
                # we cannot reload(__main__)
                continue

            filename = m.__file__
            path, ext = os.path.splitext(filename)

            if ext.lower() == '.py':
                ext = PY_COMPILED_EXT
                pyc_filename = pyfile.cache_from_source(filename)
                py_filename = filename
            else:
                pyc_filename = filename
                try:
                    py_filename = pyfile.source_from_cache(filename)
                except ValueError:
                    continue

            try:
                pymtime = os.stat(py_filename).st_mtime
                if pymtime <= os.stat(pyc_filename).st_mtime:
                    continue
                if self.failed.get(py_filename, None) == pymtime:
                    continue
            except OSError:
                continue

            try:
                superreload(m, reload, self.old_objects)
                if py_filename in self.failed:
                    del self.failed[py_filename]
            except:
                print >> sys.stderr, "[autoreload of %s failed: %s]" % (
                        modname, traceback.format_exc(1))
                self.failed[py_filename] = pymtime

def
check
(
self
,
check_all
=
False
)
:
if
not
self
.
enabled
and
not
check_all
:
return
if
check_all
or
self
.
check_all
:
modules
=
sys
.
modules
.
keys
(
)
else
:
modules
=
self
.
modules
.
keys
(
)
for
modname
in
modules
:
m
=
sys
.
modules
.
get
(
modname
,
)
if
modname
in
self
.
skip_modules
:
continue
if
not
hasattr
(
m
,
'__file__'
)
:
continue
if
m
.
__name__
==
'__main__'
:
# we cannot reload(__main__)
continue
filename
=
m
.
__file__
path
,
ext
=
os
.
path
.
splitext
(
filename
)
if
ext
.
lower
(
)
==
'.py'
:
ext
=
PY_COMPILED_EXT
pyc_filename
=
pyfile
.
cache_from_source
(
filename
)
py_filename
=
filename
else
:
pyc_filename
=
filename
try
:
py_filename
=
pyfile
.
source_from_cache
(
filename
)
except
ValueError
:
continue
try
:
pymtime
=
os
.
stat
(
py_filename
)
.
st_mtime
if
pymtime
<=
os
.
stat
(
pyc_filename
)
.
st_mtime
:
continue
if
self
.
failed
.
get
(
py_filename
,
)
==
pymtime
:
continue
except
OSError
:
continue
try
:
superreload
(
m
,
reload
,
self
.
old_objects
)
if
py_filename
in
self
.
failed
:
del
self
.
failed
[
py_filename
]
except
:
print
>>
sys
.
stderr
,
"[autoreload of %s failed: %s]"
%
(
modname
,
traceback
.
format_exc
(
1
)
)
self
.
failed
[
py_filename
]
=
pymtime

Check whether some modules need to be reloaded .


def editor(self, filename, linenum=None, wait=True):
    """Open the default editor at the given filename and linenumber.

    This is IPython's default editor hook, you can use it as an example to
    write your own modified one.  To set your own editor function as the
    new editor hook, call ip.set_hook('editor',yourfunc)."""

    # IPython configures a default editor at startup by reading $EDITOR from
    # the environment, and falling back on vi (unix) or notepad (win32).
    editor = self.editor

    # marker for at which line to open the file (for existing objects)
    if linenum is None or editor=='notepad':
        linemark = ''
    else:
        linemark = '+%d' % int(linenum)

    # Enclose in quotes if necessary and legal
    if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
        editor = '"%s"' % editor

    # Call the actual editor
    proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename),
                            shell=True)
    if wait and proc.wait() != 0:
        raise TryNext()

Open the default editor at the given filename and linenumber .


def fix_error_editor(self,filename,linenum,column,msg):
    """Open the editor at the given filename, linenumber, column and
    show an error message. This is used for correcting syntax errors.
    The current implementation only has special support for the VIM editor,
    and falls back on the 'editor' hook if VIM is not used.

    Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
    """
    def vim_quickfix_file():
        t = tempfile.NamedTemporaryFile()
        t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
        t.flush()
        return t
    if os.path.basename(self.editor) != 'vim':
        self.hooks.editor(filename,linenum)
        return
    t = vim_quickfix_file()
    try:
        if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
            raise TryNext()
    finally:
        t.close()

Open the editor at the given filename linenumber column and show an error message . This is used for correcting syntax errors . The current implementation only has special support for the VIM editor and falls back on the editor hook if VIM is not used .


def clipboard_get(self):
    """ Get text from the clipboard.
    """
    from IPython.lib.clipboard import (
        osx_clipboard_get, tkinter_clipboard_get,
        win32_clipboard_get
    )
    if sys.platform == 'win32':
        chain = [win32_clipboard_get, tkinter_clipboard_get]
    elif sys.platform == 'darwin':
        chain = [osx_clipboard_get, tkinter_clipboard_get]
    else:
        chain = [tkinter_clipboard_get]
    dispatcher = CommandChainDispatcher()
    for func in chain:
        dispatcher.add(func)
    text = dispatcher()
    return text

def clipboard_get ( self ) : from IPython . lib . clipboard import ( osx_clipboard_get , tkinter_clipboard_get , win32_clipboard_get ) if sys . platform == 'win32' : chain = [ win32_clipboard_get , tkinter_clipboard_get ] elif sys . platform == 'darwin' : chain = [ osx_clipboard_get , tkinter_clipboard_get ] else : chain = [ tkinter_clipboard_get ] dispatcher = CommandChainDispatcher ( ) for func in chain : dispatcher . add ( func ) text = dispatcher ( ) return text


def add(self, func, priority=0):
        """ Add a func to the cmd chain with given priority """
        self.chain.append((priority, func))
        self.chain.sort(key=lambda x: x[0])

def add ( self , func , priority = 0 ) : self . chain . append ( ( priority , func ) ) self . chain . sort ( key = lambda x : x [ 0 ] )