def wait(self, jobs=None, timeout=-1):
        """waits on one or more `jobs`, for up to `timeout` seconds.

        Parameters
        ----------

        jobs : int, str, or list of ints and/or strs, or one or more AsyncResult objects
                ints are indices to self.history
                strs are msg_ids
                default: wait on all outstanding messages
        timeout : float
                a time in seconds, after which to give up.
                default is -1, which means no timeout

        Returns
        -------

        True : when all msg_ids are done
        False : timeout reached, some msg_ids still outstanding
        """
        tic = time.time()
        if jobs is None:
            theids = self.outstanding
        else:
            if isinstance(jobs, (int, basestring, AsyncResult)):
                jobs = [jobs]
            theids = set()
            for job in jobs:
                if isinstance(job, int):
                    # index access
                    job = self.history[job]
                elif isinstance(job, AsyncResult):
                    map(theids.add, job.msg_ids)
                    continue
                theids.add(job)
        if not theids.intersection(self.outstanding):
            return True
        self.spin()
        while theids.intersection(self.outstanding):
            if timeout >= 0 and ( time.time()-tic ) > timeout:
                break
            time.sleep(1e-3)
            self.spin()
        return len(theids.intersection(self.outstanding)) == 0

def wait ( self , jobs = None , timeout = - 1 ) : tic = time . time ( ) if jobs is None : theids = self . outstanding else : if isinstance ( jobs , ( int , basestring , AsyncResult ) ) : jobs = [ jobs ] theids = set ( ) for job in jobs : if isinstance ( job , int ) : job = self . history [ job ] elif isinstance ( job , AsyncResult ) : map ( theids . add , job . msg_ids ) continue theids . add ( job ) if not theids . intersection ( self . outstanding ) : return True self . spin ( ) while theids . intersection ( self . outstanding ) : if timeout >= 0 and ( time . time ( ) - tic ) > timeout : break time . sleep ( 1e-3 ) self . spin ( ) return len ( theids . intersection ( self . outstanding ) ) == 0

waits on one or more jobs for up to timeout seconds .


def send_apply_request(self, socket, f, args=None, kwargs=None, subheader=None, track=False,
                            ident=None):
        """construct and send an apply message via a socket.

        This is the principal method with which all engine execution is performed by views.
        """

        if self._closed:
            raise RuntimeError("Client cannot be used after its sockets have been closed")

        # defaults:
        args = args if args is not None else []
        kwargs = kwargs if kwargs is not None else {}
        subheader = subheader if subheader is not None else {}

        # validate arguments
        if not callable(f) and not isinstance(f, Reference):
            raise TypeError("f must be callable, not %s"%type(f))
        if not isinstance(args, (tuple, list)):
            raise TypeError("args must be tuple or list, not %s"%type(args))
        if not isinstance(kwargs, dict):
            raise TypeError("kwargs must be dict, not %s"%type(kwargs))
        if not isinstance(subheader, dict):
            raise TypeError("subheader must be dict, not %s"%type(subheader))

        bufs = util.pack_apply_message(f,args,kwargs)

        msg = self.session.send(socket, "apply_request", buffers=bufs, ident=ident,
                            subheader=subheader, track=track)

        msg_id = msg['header']['msg_id']
        self.outstanding.add(msg_id)
        if ident:
            # possibly routed to a specific engine
            if isinstance(ident, list):
                ident = ident[-1]
            if ident in self._engines.values():
                # save for later, in case of engine death
                self._outstanding_dict[ident].add(msg_id)
        self.history.append(msg_id)
        self.metadata[msg_id]['submitted'] = datetime.now()

        return msg

construct and send an apply message via a socket .


def send_execute_request(self, socket, code, silent=True, subheader=None, ident=None):
        """construct and send an execute request via a socket.

        """

        if self._closed:
            raise RuntimeError("Client cannot be used after its sockets have been closed")

        # defaults:
        subheader = subheader if subheader is not None else {}

        # validate arguments
        if not isinstance(code, basestring):
            raise TypeError("code must be text, not %s" % type(code))
        if not isinstance(subheader, dict):
            raise TypeError("subheader must be dict, not %s" % type(subheader))

        content = dict(code=code, silent=bool(silent), user_variables=[], user_expressions={})

        msg = self.session.send(socket, "execute_request", content=content, ident=ident,
                            subheader=subheader)

        msg_id = msg['header']['msg_id']
        self.outstanding.add(msg_id)
        if ident:
            # possibly routed to a specific engine
            if isinstance(ident, list):
                ident = ident[-1]
            if ident in self._engines.values():
                # save for later, in case of engine death
                self._outstanding_dict[ident].add(msg_id)
        self.history.append(msg_id)
        self.metadata[msg_id]['submitted'] = datetime.now()

        return msg

construct and send an execute request via a socket .


def get_result(self, indices_or_msg_ids=None, block=None):
        """Retrieve a result by msg_id or history index, wrapped in an AsyncResult object.

        If the client already has the results, no request to the Hub will be made.

        This is a convenient way to construct AsyncResult objects, which are wrappers
        that include metadata about execution, and allow for awaiting results that
        were not submitted by this Client.

        It can also be a convenient way to retrieve the metadata associated with
        blocking execution, since it always retrieves

        Examples
        --------
        ::

            In [10]: r = client.apply()

        Parameters
        ----------

        indices_or_msg_ids : integer history index, str msg_id, or list of either
            The indices or msg_ids of indices to be retrieved

        block : bool
            Whether to wait for the result to be done

        Returns
        -------

        AsyncResult
            A single AsyncResult object will always be returned.

        AsyncHubResult
            A subclass of AsyncResult that retrieves results from the Hub

        """
        block = self.block if block is None else block
        if indices_or_msg_ids is None:
            indices_or_msg_ids = -1

        if not isinstance(indices_or_msg_ids, (list,tuple)):
            indices_or_msg_ids = [indices_or_msg_ids]

        theids = []
        for id in indices_or_msg_ids:
            if isinstance(id, int):
                id = self.history[id]
            if not isinstance(id, basestring):
                raise TypeError("indices must be str or int, not %r"%id)
            theids.append(id)

        local_ids = filter(lambda msg_id: msg_id in self.history or msg_id in self.results, theids)
        remote_ids = filter(lambda msg_id: msg_id not in local_ids, theids)

        if remote_ids:
            ar = AsyncHubResult(self, msg_ids=theids)
        else:
            ar = AsyncResult(self, msg_ids=theids)

        if block:
            ar.wait()

        return ar

def
get_result
(
self
,
indices_or_msg_ids
=
,
block
=
)
:
block
=
self
.
block
if
block
is
else
block
if
indices_or_msg_ids
is
:
indices_or_msg_ids
=
1
if
not
isinstance
(
indices_or_msg_ids
,
(
list
,
tuple
)
)
:
indices_or_msg_ids
=
[
indices_or_msg_ids
]
theids
=
[
]
for
id
in
indices_or_msg_ids
:
if
isinstance
(
id
,
int
)
:
id
=
self
.
history
[
id
]
if
not
isinstance
(
id
,
basestring
)
:
raise
TypeError
(
"indices must be str or int, not %r"
%
id
)
theids
.
append
(
id
)
local_ids
=
filter
(
lambda
msg_id
:
msg_id
in
self
.
history
or
msg_id
in
self
.
results
,
theids
)
remote_ids
=
filter
(
lambda
msg_id
:
msg_id
not
in
local_ids
,
theids
)
if
remote_ids
:
ar
=
AsyncHubResult
(
self
,
msg_ids
=
theids
)
else
:
ar
=
AsyncResult
(
self
,
msg_ids
=
theids
)
if
block
:
ar
.
wait
(
)
return
ar

Retrieve a result by msg_id or history index wrapped in an AsyncResult object .


def queue_status(self, targets='all', verbose=False):
        """Fetch the status of engine queues.

        Parameters
        ----------

        targets : int/str/list of ints/strs
                the engines whose states are to be queried.
                default : all
        verbose : bool
                Whether to return lengths only, or lists of ids for each element
        """
        if targets == 'all':
            # allow 'all' to be evaluated on the engine
            engine_ids = None
        else:
            engine_ids = self._build_targets(targets)[1]
        content = dict(targets=engine_ids, verbose=verbose)
        self.session.send(self._query_socket, "queue_request", content=content)
        idents,msg = self.session.recv(self._query_socket, 0)
        if self.debug:
            pprint(msg)
        content = msg['content']
        status = content.pop('status')
        if status != 'ok':
            raise self._unwrap_exception(content)
        content = rekey(content)
        if isinstance(targets, int):
            return content[targets]
        else:
            return content

def queue_status ( self , targets = 'all' , verbose = False ) : if targets == 'all' : engine_ids = None else : engine_ids = self . _build_targets ( targets ) [ 1 ] content = dict ( targets = engine_ids , verbose = verbose ) self . session . send ( self . _query_socket , "queue_request" , content = content ) idents , msg = self . session . recv ( self . _query_socket , 0 ) if self . debug : pprint ( msg ) content = msg [ 'content' ] status = content . pop ( 'status' ) if status != 'ok' : raise self . _unwrap_exception ( content ) content = rekey ( content ) if isinstance ( targets , int ) : return content [ targets ] else : return content


def purge_results(self, jobs=[], targets=[]):
        """Tell the Hub to forget results.

        Individual results can be purged by msg_id, or the entire
        history of specific targets can be purged.

        Use `purge_results('all')` to scrub everything from the Hub's db.

        Parameters
        ----------

        jobs : str or list of str or AsyncResult objects
                the msg_ids whose results should be forgotten.
        targets : int/str/list of ints/strs
                The targets, by int_id, whose entire history is to be purged.

                default : None
        """
        if not targets and not jobs:
            raise ValueError("Must specify at least one of `targets` and `jobs`")
        if targets:
            targets = self._build_targets(targets)[1]

        # construct msg_ids from jobs
        if jobs == 'all':
            msg_ids = jobs
        else:
            msg_ids = []
            if isinstance(jobs, (basestring,AsyncResult)):
                jobs = [jobs]
            bad_ids = filter(lambda obj: not isinstance(obj, (basestring, AsyncResult)), jobs)
            if bad_ids:
                raise TypeError("Invalid msg_id type %r, expected str or AsyncResult"%bad_ids[0])
            for j in jobs:
                if isinstance(j, AsyncResult):
                    msg_ids.extend(j.msg_ids)
                else:
                    msg_ids.append(j)

        content = dict(engine_ids=targets, msg_ids=msg_ids)
        self.session.send(self._query_socket, "purge_request", content=content)
        idents, msg = self.session.recv(self._query_socket, 0)
        if self.debug:
            pprint(msg)
        content = msg['content']
        if content['status'] != 'ok':
            raise self._unwrap_exception(content)


def hub_history(self):
        """Get the Hub's history

        Just like the Client, the Hub has a history, which is a list of msg_ids.
        This will contain the history of all clients, and, depending on configuration,
        may contain history across multiple cluster sessions.

        Any msg_id returned here is a valid argument to `get_result`.

        Returns
        -------

        msg_ids : list of strs
                list of all msg_ids, ordered by task submission time.
        """

        self.session.send(self._query_socket, "history_request", content={})
        idents, msg = self.session.recv(self._query_socket, 0)

        if self.debug:
            pprint(msg)
        content = msg['content']
        if content['status'] != 'ok':
            raise self._unwrap_exception(content)
        else:
            return content['history']

def hub_history ( self ) : self . session . send ( self . _query_socket , "history_request" , content = { } ) idents , msg = self . session . recv ( self . _query_socket , 0 ) if self . debug : pprint ( msg ) content = msg [ 'content' ] if content [ 'status' ] != 'ok' : raise self . _unwrap_exception ( content ) else : return content [ 'history' ]


def db_query(self, query, keys=None):
        """Query the Hub's TaskRecord database

        This will return a list of task record dicts that match `query`

        Parameters
        ----------

        query : mongodb query dict
            The search dict. See mongodb query docs for details.
        keys : list of strs [optional]
            The subset of keys to be returned.  The default is to fetch everything but buffers.
            'msg_id' will *always* be included.
        """
        if isinstance(keys, basestring):
            keys = [keys]
        content = dict(query=query, keys=keys)
        self.session.send(self._query_socket, "db_request", content=content)
        idents, msg = self.session.recv(self._query_socket, 0)
        if self.debug:
            pprint(msg)
        content = msg['content']
        if content['status'] != 'ok':
            raise self._unwrap_exception(content)

        records = content['records']

        buffer_lens = content['buffer_lens']
        result_buffer_lens = content['result_buffer_lens']
        buffers = msg['buffers']
        has_bufs = buffer_lens is not None
        has_rbufs = result_buffer_lens is not None
        for i,rec in enumerate(records):
            # relink buffers
            if has_bufs:
                blen = buffer_lens[i]
                rec['buffers'], buffers = buffers[:blen],buffers[blen:]
            if has_rbufs:
                blen = result_buffer_lens[i]
                rec['result_buffers'], buffers = buffers[:blen],buffers[blen:]

        return records

def db_query ( self , query , keys = None ) : if isinstance ( keys , basestring ) : keys = [ keys ] content = dict ( query = query , keys = keys ) self . session . send ( self . _query_socket , "db_request" , content = content ) idents , msg = self . session . recv ( self . _query_socket , 0 ) if self . debug : pprint ( msg ) content = msg [ 'content' ] if content [ 'status' ] != 'ok' : raise self . _unwrap_exception ( content ) records = content [ 'records' ] buffer_lens = content [ 'buffer_lens' ] result_buffer_lens = content [ 'result_buffer_lens' ] buffers = msg [ 'buffers' ] has_bufs = buffer_lens is not None has_rbufs = result_buffer_lens is not None for i , rec in enumerate ( records ) : if has_bufs : blen = buffer_lens [ i ] rec [ 'buffers' ] , buffers = buffers [ : blen ] , buffers [ blen : ] if has_rbufs : blen = result_buffer_lens [ i ] rec [ 'result_buffers' ] , buffers = buffers [ : blen ] , buffers [ blen : ] return records


def _opcode_set(*names):
    """Return a set of opcodes by the names in `names`."""
    s = set()
    for name in names:
        try:
            s.add(_opcode(name))
        except KeyError:
            pass
    return s

def _opcode_set ( * names ) : s = set ( ) for name in names : try : s . add ( _opcode ( name ) ) except KeyError : pass return s

Return a set of opcodes by the names in names .


def _get_byte_parser(self):
        """Create a ByteParser on demand."""
        if not self._byte_parser:
            self._byte_parser = \
                            ByteParser(text=self.text, filename=self.filename)
        return self._byte_parser

def _get_byte_parser ( self ) : if not self . _byte_parser : self . _byte_parser = ByteParser ( text = self . text , filename = self . filename ) return self . _byte_parser


def lines_matching(self, *regexes):
        """Find the lines matching one of a list of regexes.

        Returns a set of line numbers, the lines that contain a match for one
        of the regexes in `regexes`.  The entire line needn't match, just a
        part of it.

        """
        regex_c = re.compile(join_regex(regexes))
        matches = set()
        for i, ltext in enumerate(self.lines):
            if regex_c.search(ltext):
                matches.add(i+1)
        return matches

def lines_matching ( self , * regexes ) : regex_c = re . compile ( join_regex ( regexes ) ) matches = set ( ) for i , ltext in enumerate ( self . lines ) : if regex_c . search ( ltext ) : matches . add ( i + 1 ) return matches

Find the lines matching one of a list of regexes .


def _raw_parse(self):
        """Parse the source to find the interesting facts about its lines.

        A handful of member fields are updated.

        """
        # Find lines which match an exclusion pattern.
        if self.exclude:
            self.excluded = self.lines_matching(self.exclude)

        # Tokenize, to find excluded suites, to find docstrings, and to find
        # multi-line statements.
        indent = 0
        exclude_indent = 0
        excluding = False
        prev_toktype = token.INDENT
        first_line = None
        empty = True

        tokgen = generate_tokens(self.text)
        for toktype, ttext, (slineno, _), (elineno, _), ltext in tokgen:
            if self.show_tokens:                # pragma: not covered
                print("%10s %5s %-20r %r" % (
                    tokenize.tok_name.get(toktype, toktype),
                    nice_pair((slineno, elineno)), ttext, ltext
                    ))
            if toktype == token.INDENT:
                indent += 1
            elif toktype == token.DEDENT:
                indent -= 1
            elif toktype == token.NAME and ttext == 'class':
                # Class definitions look like branches in the byte code, so
                # we need to exclude them.  The simplest way is to note the
                # lines with the 'class' keyword.
                self.classdefs.add(slineno)
            elif toktype == token.OP and ttext == ':':
                if not excluding and elineno in self.excluded:
                    # Start excluding a suite.  We trigger off of the colon
                    # token so that the #pragma comment will be recognized on
                    # the same line as the colon.
                    exclude_indent = indent
                    excluding = True
            elif toktype == token.STRING and prev_toktype == token.INDENT:
                # Strings that are first on an indented line are docstrings.
                # (a trick from trace.py in the stdlib.) This works for
                # 99.9999% of cases.  For the rest (!) see:
                # http://stackoverflow.com/questions/1769332/x/1769794#1769794
                self.docstrings.update(range(slineno, elineno+1))
            elif toktype == token.NEWLINE:
                if first_line is not None and elineno != first_line:
                    # We're at the end of a line, and we've ended on a
                    # different line than the first line of the statement,
                    # so record a multi-line range.
                    rng = (first_line, elineno)
                    for l in range(first_line, elineno+1):
                        self.multiline[l] = rng
                first_line = None

            if ttext.strip() and toktype != tokenize.COMMENT:
                # A non-whitespace token.
                empty = False
                if first_line is None:
                    # The token is not whitespace, and is the first in a
                    # statement.
                    first_line = slineno
                    # Check whether to end an excluded suite.
                    if excluding and indent <= exclude_indent:
                        excluding = False
                    if excluding:
                        self.excluded.add(elineno)

            prev_toktype = toktype

        # Find the starts of the executable statements.
        if not empty:
            self.statement_starts.update(self.byte_parser._find_statements())

def
_raw_parse
(
self
)
:
if
self
.
exclude
:
self
.
excluded
=
self
.
lines_matching
(
self
.
exclude
)
indent
=
0
exclude_indent
=
0
excluding
=
False
prev_toktype
=
token
.
INDENT
first_line
=
empty
=
True
tokgen
=
generate_tokens
(
self
.
text
)
for
toktype
,
ttext
,
(
slineno
,
_
)
,
(
elineno
,
_
)
,
ltext
in
tokgen
:
if
self
.
show_tokens
:
print
(
"%10s %5s %-20r %r"
%
(
tokenize
.
tok_name
.
get
(
toktype
,
toktype
)
,
nice_pair
(
(
slineno
,
elineno
)
)
,
ttext
,
ltext
)
)
if
toktype
==
token
.
INDENT
:
indent
+=
1
elif
toktype
==
token
.
DEDENT
:
indent
-=
1
elif
toktype
==
token
.
NAME
and
ttext
==
'class'
:
self
.
classdefs
.
add
(
slineno
)
elif
toktype
==
token
.
OP
and
ttext
==
':'
:
if
not
excluding
and
elineno
in
self
.
excluded
:
exclude_indent
=
indent
excluding
=
True
elif
toktype
==
token
.
STRING
and
prev_toktype
==
token
.
INDENT
:
self
.
docstrings
.
update
(
range
(
slineno
,
elineno
+
1
)
)
elif
toktype
==
token
.
NEWLINE
:
if
first_line
is
not
and
elineno
!=
first_line
:
rng
=
(
first_line
,
elineno
)
for
l
in
range
(
first_line
,
elineno
+
1
)
:
self
.
multiline
[
l
]
=
rng
first_line
=
if
ttext
.
strip
(
)
and
toktype
!=
tokenize
.
COMMENT
:
empty
=
False
if
first_line
is
:
first_line
=
slineno
if
excluding
and
indent
<=
exclude_indent
:
excluding
=
False
if
excluding
:
self
.
excluded
.
add
(
elineno
)
prev_toktype
=
toktype
if
not
empty
:
self
.
statement_starts
.
update
(
self
.
byte_parser
.
_find_statements
(
)
)

Parse the source to find the interesting facts about its lines .


def first_line(self, line):
        """Return the first line number of the statement including `line`."""
        rng = self.multiline.get(line)
        if rng:
            first_line = rng[0]
        else:
            first_line = line
        return first_line

def first_line ( self , line ) : rng = self . multiline . get ( line ) if rng : first_line = rng [ 0 ] else : first_line = line return first_line

Return the first line number of the statement including line .


def first_lines(self, lines, *ignores):
        """Map the line numbers in `lines` to the correct first line of the
        statement.

        Skip any line mentioned in any of the sequences in `ignores`.

        Returns a set of the first lines.

        """
        ignore = set()
        for ign in ignores:
            ignore.update(ign)
        lset = set()
        for l in lines:
            if l in ignore:
                continue
            new_l = self.first_line(l)
            if new_l not in ignore:
                lset.add(new_l)
        return lset

def first_lines ( self , lines , * ignores ) : ignore = set ( ) for ign in ignores : ignore . update ( ign ) lset = set ( ) for l in lines : if l in ignore : continue new_l = self . first_line ( l ) if new_l not in ignore : lset . add ( new_l ) return lset

Map the line numbers in lines to the correct first line of the statement .


def parse_source(self):
        """Parse source text to find executable lines, excluded lines, etc.

        Return values are 1) a set of executable line numbers, and 2) a set of
        excluded line numbers.

        Reported line numbers are normalized to the first line of multi-line
        statements.

        """
        try:
            self._raw_parse()
        except (tokenize.TokenError, IndentationError):
            _, tokerr, _ = sys.exc_info()
            msg, lineno = tokerr.args
            raise NotPython(
                "Couldn't parse '%s' as Python source: '%s' at %s" %
                    (self.filename, msg, lineno)
                )

        excluded_lines = self.first_lines(self.excluded)
        lines = self.first_lines(
            self.statement_starts,
            excluded_lines,
            self.docstrings
        )

        return lines, excluded_lines

Parse source text to find executable lines excluded lines etc .


def arcs(self):
        """Get information about the arcs available in the code.

        Returns a sorted list of line number pairs.  Line numbers have been
        normalized to the first line of multiline statements.

        """
        all_arcs = []
        for l1, l2 in self.byte_parser._all_arcs():
            fl1 = self.first_line(l1)
            fl2 = self.first_line(l2)
            if fl1 != fl2:
                all_arcs.append((fl1, fl2))
        return sorted(all_arcs)

def arcs ( self ) : all_arcs = [ ] for l1 , l2 in self . byte_parser . _all_arcs ( ) : fl1 = self . first_line ( l1 ) fl2 = self . first_line ( l2 ) if fl1 != fl2 : all_arcs . append ( ( fl1 , fl2 ) ) return sorted ( all_arcs )

Get information about the arcs available in the code .


def exit_counts(self):
        """Get a mapping from line numbers to count of exits from that line.

        Excluded lines are excluded.

        """
        excluded_lines = self.first_lines(self.excluded)
        exit_counts = {}
        for l1, l2 in self.arcs():
            if l1 < 0:
                # Don't ever report -1 as a line number
                continue
            if l1 in excluded_lines:
                # Don't report excluded lines as line numbers.
                continue
            if l2 in excluded_lines:
                # Arcs to excluded lines shouldn't count.
                continue
            if l1 not in exit_counts:
                exit_counts[l1] = 0
            exit_counts[l1] += 1

        # Class definitions have one extra exit, so remove one for each:
        for l in self.classdefs:
            # Ensure key is there: classdefs can include excluded lines.
            if l in exit_counts:
                exit_counts[l] -= 1

        return exit_counts

def exit_counts ( self ) : excluded_lines = self . first_lines ( self . excluded ) exit_counts = { } for l1 , l2 in self . arcs ( ) : if l1 < 0 : continue if l1 in excluded_lines : continue if l2 in excluded_lines : continue if l1 not in exit_counts : exit_counts [ l1 ] = 0 exit_counts [ l1 ] += 1 for l in self . classdefs : if l in exit_counts : exit_counts [ l ] -= 1 return exit_counts

Get a mapping from line numbers to count of exits from that line .


def child_parsers(self):
        """Iterate over all the code objects nested within this one.

        The iteration includes `self` as its first value.

        """
        children = CodeObjects(self.code)
        return [ByteParser(code=c, text=self.text) for c in children]

def child_parsers ( self ) : children = CodeObjects ( self . code ) return [ ByteParser ( code = c , text = self . text ) for c in children ]

Iterate over all the code objects nested within this one .


def _bytes_lines(self):
        """Map byte offsets to line numbers in `code`.

        Uses co_lnotab described in Python/compile.c to map byte offsets to
        line numbers.  Produces a sequence: (b0, l0), (b1, l1), ...

        Only byte offsets that correspond to line numbers are included in the
        results.

        """
        # Adapted from dis.py in the standard library.
        byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
        line_increments = bytes_to_ints(self.code.co_lnotab[1::2])

        last_line_num = None
        line_num = self.code.co_firstlineno
        byte_num = 0
        for byte_incr, line_incr in zip(byte_increments, line_increments):
            if byte_incr:
                if line_num != last_line_num:
                    yield (byte_num, line_num)
                    last_line_num = line_num
                byte_num += byte_incr
            line_num += line_incr
        if line_num != last_line_num:
            yield (byte_num, line_num)

Map byte offsets to line numbers in code .


def _find_statements(self):
        """Find the statements in `self.code`.

        Produce a sequence of line numbers that start statements.  Recurses
        into all code objects reachable from `self.code`.

        """
        for bp in self.child_parsers():
            # Get all of the lineno information from this code.
            for _, l in bp._bytes_lines():
                yield l

def _find_statements ( self ) : for bp in self . child_parsers ( ) : for _ , l in bp . _bytes_lines ( ) : yield l


def _block_stack_repr(self, block_stack):
        """Get a string version of `block_stack`, for debugging."""
        blocks = ", ".join(
            ["(%s, %r)" % (dis.opname[b[0]], b[1]) for b in block_stack]
        )
        return "[" + blocks + "]"

def
_block_stack_repr
(
self
,
block_stack
)
:
blocks
=
", "
.
join
(
[
"(%s, %r)"
%
(
dis
.
opname
[
b
[
0
]
]
,
b
[
1
]
)
for
b
in
block_stack
]
)
return
"["
+
blocks
+
"]"

Get a string version of block_stack for debugging .


def _split_into_chunks(self):
        """Split the code object into a list of `Chunk` objects.

        Each chunk is only entered at its first instruction, though there can
        be many exits from a chunk.

        Returns a list of `Chunk` objects.

        """
        # The list of chunks so far, and the one we're working on.
        chunks = []
        chunk = None

        # A dict mapping byte offsets of line starts to the line numbers.
        bytes_lines_map = dict(self._bytes_lines())

        # The block stack: loops and try blocks get pushed here for the
        # implicit jumps that can occur.
        # Each entry is a tuple: (block type, destination)
        block_stack = []

        # Some op codes are followed by branches that should be ignored.  This
        # is a count of how many ignores are left.
        ignore_branch = 0

        # We have to handle the last two bytecodes specially.
        ult = penult = None

        # Get a set of all of the jump-to points.
        jump_to = set()
        bytecodes = list(ByteCodes(self.code.co_code))
        for bc in bytecodes:
            if bc.jump_to >= 0:
                jump_to.add(bc.jump_to)

        chunk_lineno = 0

        # Walk the byte codes building chunks.
        for bc in bytecodes:
            # Maybe have to start a new chunk
            start_new_chunk = False
            first_chunk = False
            if bc.offset in bytes_lines_map:
                # Start a new chunk for each source line number.
                start_new_chunk = True
                chunk_lineno = bytes_lines_map[bc.offset]
                first_chunk = True
            elif bc.offset in jump_to:
                # To make chunks have a single entrance, we have to make a new
                # chunk when we get to a place some bytecode jumps to.
                start_new_chunk = True
            elif bc.op in OPS_CHUNK_BEGIN:
                # Jumps deserve their own unnumbered chunk.  This fixes
                # problems with jumps to jumps getting confused.
                start_new_chunk = True

            if not chunk or start_new_chunk:
                if chunk:
                    chunk.exits.add(bc.offset)
                chunk = Chunk(bc.offset, chunk_lineno, first_chunk)
                chunks.append(chunk)

            # Look at the opcode
            if bc.jump_to >= 0 and bc.op not in OPS_NO_JUMP:
                if ignore_branch:
                    # Someone earlier wanted us to ignore this branch.
                    ignore_branch -= 1
                else:
                    # The opcode has a jump, it's an exit for this chunk.
                    chunk.exits.add(bc.jump_to)

            if bc.op in OPS_CODE_END:
                # The opcode can exit the code object.
                chunk.exits.add(-self.code.co_firstlineno)
            if bc.op in OPS_PUSH_BLOCK:
                # The opcode adds a block to the block_stack.
                block_stack.append((bc.op, bc.jump_to))
            if bc.op in OPS_POP_BLOCK:
                # The opcode pops a block from the block stack.
                block_stack.pop()
            if bc.op in OPS_CHUNK_END:
                # This opcode forces the end of the chunk.
                if bc.op == OP_BREAK_LOOP:
                    # A break is implicit: jump where the top of the
                    # block_stack points.
                    chunk.exits.add(block_stack[-1][1])
                chunk = None
            if bc.op == OP_END_FINALLY:
                # For the finally clause we need to find the closest exception
                # block, and use its jump target as an exit.
                for block in reversed(block_stack):
                    if block[0] in OPS_EXCEPT_BLOCKS:
                        chunk.exits.add(block[1])
                        break
            if bc.op == OP_COMPARE_OP and bc.arg == COMPARE_EXCEPTION:
                # This is an except clause.  We want to overlook the next
                # branch, so that except's don't count as branches.
                ignore_branch += 1

            penult = ult
            ult = bc

        if chunks:
            # The last two bytecodes could be a dummy "return None" that
            # shouldn't be counted as real code. Every Python code object seems
            # to end with a return, and a "return None" is inserted if there
            # isn't an explicit return in the source.
            if ult and penult:
                if penult.op == OP_LOAD_CONST and ult.op == OP_RETURN_VALUE:
                    if self.code.co_consts[penult.arg] is None:
                        # This is "return None", but is it dummy?  A real line
                        # would be a last chunk all by itself.
                        if chunks[-1].byte != penult.offset:
                            ex = -self.code.co_firstlineno
                            # Split the last chunk
                            last_chunk = chunks[-1]
                            last_chunk.exits.remove(ex)
                            last_chunk.exits.add(penult.offset)
                            chunk = Chunk(
                                penult.offset, last_chunk.line, False
                            )
                            chunk.exits.add(ex)
                            chunks.append(chunk)

            # Give all the chunks a length.
            chunks[-1].length = bc.next_offset - chunks[-1].byte # pylint: disable=W0631,C0301
            for i in range(len(chunks)-1):
                chunks[i].length = chunks[i+1].byte - chunks[i].byte

        #self.validate_chunks(chunks)
        return chunks

def _split_into_chunks ( self ) : chunks = [ ] chunk = None bytes_lines_map = dict ( self . _bytes_lines ( ) ) block_stack = [ ] ignore_branch = 0 ult = penult = None jump_to = set ( ) bytecodes = list ( ByteCodes ( self . code . co_code ) ) for bc in bytecodes : if bc . jump_to >= 0 : jump_to . add ( bc . jump_to ) chunk_lineno = 0 for bc in bytecodes : start_new_chunk = False first_chunk = False if bc . offset in bytes_lines_map : start_new_chunk = True chunk_lineno = bytes_lines_map [ bc . offset ] first_chunk = True elif bc . offset in jump_to : start_new_chunk = True elif bc . op in OPS_CHUNK_BEGIN : start_new_chunk = True if not chunk or start_new_chunk : if chunk : chunk . exits . add ( bc . offset ) chunk = Chunk ( bc . offset , chunk_lineno , first_chunk ) chunks . append ( chunk ) if bc . jump_to >= 0 and bc . op not in OPS_NO_JUMP : if ignore_branch : ignore_branch -= 1 else : chunk . exits . add ( bc . jump_to ) if bc . op in OPS_CODE_END : chunk . exits . add ( - self . code . co_firstlineno ) if bc . op in OPS_PUSH_BLOCK : block_stack . append ( ( bc . op , bc . jump_to ) ) if bc . op in OPS_POP_BLOCK : block_stack . pop ( ) if bc . op in OPS_CHUNK_END : if bc . op == OP_BREAK_LOOP : chunk . exits . add ( block_stack [ - 1 ] [ 1 ] ) chunk = None if bc . op == OP_END_FINALLY : for block in reversed ( block_stack ) : if block [ 0 ] in OPS_EXCEPT_BLOCKS : chunk . exits . add ( block [ 1 ] ) break if bc . op == OP_COMPARE_OP and bc . arg == COMPARE_EXCEPTION : ignore_branch += 1 penult = ult ult = bc if chunks : if ult and penult : if penult . op == OP_LOAD_CONST and ult . op == OP_RETURN_VALUE : if self . code . co_consts [ penult . arg ] is None : if chunks [ - 1 ] . byte != penult . offset : ex = - self . code . co_firstlineno last_chunk = chunks [ - 1 ] last_chunk . exits . remove ( ex ) last_chunk . exits . add ( penult . offset ) chunk = Chunk ( penult . offset , last_chunk . line , False ) chunk . exits . add ( ex ) chunks . append ( chunk ) chunks [ - 1 ] . length = bc . next_offset - chunks [ - 1 ] . byte for i in range ( len ( chunks ) - 1 ) : chunks [ i ] . length = chunks [ i + 1 ] . byte - chunks [ i ] . byte return chunks

Split the code object into a list of Chunk objects .


def validate_chunks(self, chunks):
        """Validate the rule that chunks have a single entrance."""
        # starts is the entrances to the chunks
        starts = set([ch.byte for ch in chunks])
        for ch in chunks:
            assert all([(ex in starts or ex < 0) for ex in ch.exits])

def validate_chunks ( self , chunks ) : starts = set ( [ ch . byte for ch in chunks ] ) for ch in chunks : assert all ( [ ( ex in starts or ex < 0 ) for ex in ch . exits ] )

Validate the rule that chunks have a single entrance .


def _arcs(self):
        """Find the executable arcs in the code.

        Yields pairs: (from,to).  From and to are integer line numbers.  If
        from is < 0, then the arc is an entrance into the code object.  If to
        is < 0, the arc is an exit from the code object.

        """
        chunks = self._split_into_chunks()

        # A map from byte offsets to chunks jumped into.
        byte_chunks = dict([(c.byte, c) for c in chunks])

        # There's always an entrance at the first chunk.
        yield (-1, byte_chunks[0].line)

        # Traverse from the first chunk in each line, and yield arcs where
        # the trace function will be invoked.
        for chunk in chunks:
            if not chunk.first:
                continue

            chunks_considered = set()
            chunks_to_consider = [chunk]
            while chunks_to_consider:
                # Get the chunk we're considering, and make sure we don't
                # consider it again
                this_chunk = chunks_to_consider.pop()
                chunks_considered.add(this_chunk)

                # For each exit, add the line number if the trace function
                # would be triggered, or add the chunk to those being
                # considered if not.
                for ex in this_chunk.exits:
                    if ex < 0:
                        yield (chunk.line, ex)
                    else:
                        next_chunk = byte_chunks[ex]
                        if next_chunk in chunks_considered:
                            continue

                        # The trace function is invoked if visiting the first
                        # bytecode in a line, or if the transition is a
                        # backward jump.
                        backward_jump = next_chunk.byte < this_chunk.byte
                        if next_chunk.first or backward_jump:
                            if next_chunk.line != chunk.line:
                                yield (chunk.line, next_chunk.line)
                        else:
                            chunks_to_consider.append(next_chunk)

def _arcs ( self ) : chunks = self . _split_into_chunks ( ) byte_chunks = dict ( [ ( c . byte , c ) for c in chunks ] ) yield ( - 1 , byte_chunks [ 0 ] . line ) for chunk in chunks : if not chunk . first : continue chunks_considered = set ( ) chunks_to_consider = [ chunk ] while chunks_to_consider : this_chunk = chunks_to_consider . pop ( ) chunks_considered . add ( this_chunk ) for ex in this_chunk . exits : if ex < 0 : yield ( chunk . line , ex ) else : next_chunk = byte_chunks [ ex ] if next_chunk in chunks_considered : continue backward_jump = next_chunk . byte < this_chunk . byte if next_chunk . first or backward_jump : if next_chunk . line != chunk . line : yield ( chunk . line , next_chunk . line ) else : chunks_to_consider . append ( next_chunk )


def _all_chunks(self):
        """Returns a list of `Chunk` objects for this code and its children.

        See `_split_into_chunks` for details.

        """
        chunks = []
        for bp in self.child_parsers():
            chunks.extend(bp._split_into_chunks())

        return chunks

def _all_chunks ( self ) : chunks = [ ] for bp in self . child_parsers ( ) : chunks . extend ( bp . _split_into_chunks ( ) ) return chunks

Returns a list of Chunk objects for this code and its children .


def _all_arcs(self):
        """Get the set of all arcs in this code object and its children.

        See `_arcs` for details.

        """
        arcs = set()
        for bp in self.child_parsers():
            arcs.update(bp._arcs())

        return arcs

def _all_arcs ( self ) : arcs = set ( ) for bp in self . child_parsers ( ) : arcs . update ( bp . _arcs ( ) ) return arcs

Get the set of all arcs in this code object and its children .


def options(self, parser, env):
        """
        Add options to command line.
        """
        super(Coverage, self).options(parser, env)
        parser.add_option("--cover-package", action="append",
                          default=env.get('NOSE_COVER_PACKAGE'),
                          metavar="PACKAGE",
                          dest="cover_packages",
                          help="Restrict coverage output to selected packages "
                          "[NOSE_COVER_PACKAGE]")
        parser.add_option("--cover-erase", action="store_true",
                          default=env.get('NOSE_COVER_ERASE'),
                          dest="cover_erase",
                          help="Erase previously collected coverage "
                          "statistics before run")
        parser.add_option("--cover-tests", action="store_true",
                          dest="cover_tests",
                          default=env.get('NOSE_COVER_TESTS'),
                          help="Include test modules in coverage report "
                          "[NOSE_COVER_TESTS]")
        parser.add_option("--cover-min-percentage", action="store",
                          dest="cover_min_percentage",
                          default=env.get('NOSE_COVER_MIN_PERCENTAGE'),
                          help="Minimum percentage of coverage for tests"
                          "to pass [NOSE_COVER_MIN_PERCENTAGE]")
        parser.add_option("--cover-inclusive", action="store_true",
                          dest="cover_inclusive",
                          default=env.get('NOSE_COVER_INCLUSIVE'),
                          help="Include all python files under working "
                          "directory in coverage report.  Useful for "
                          "discovering holes in test coverage if not all "
                          "files are imported by the test suite. "
                          "[NOSE_COVER_INCLUSIVE]")
        parser.add_option("--cover-html", action="store_true",
                          default=env.get('NOSE_COVER_HTML'),
                          dest='cover_html',
                          help="Produce HTML coverage information")
        parser.add_option('--cover-html-dir', action='store',
                          default=env.get('NOSE_COVER_HTML_DIR', 'cover'),
                          dest='cover_html_dir',
                          metavar='DIR',
                          help='Produce HTML coverage information in dir')
        parser.add_option("--cover-branches", action="store_true",
                          default=env.get('NOSE_COVER_BRANCHES'),
                          dest="cover_branches",
                          help="Include branch coverage in coverage report "
                          "[NOSE_COVER_BRANCHES]")
        parser.add_option("--cover-xml", action="store_true",
                          default=env.get('NOSE_COVER_XML'),
                          dest="cover_xml",
                          help="Produce XML coverage information")
        parser.add_option("--cover-xml-file", action="store",
                          default=env.get('NOSE_COVER_XML_FILE', 'coverage.xml'),
                          dest="cover_xml_file",
                          metavar="FILE",
                          help="Produce XML coverage information in file")


def begin(self):
        """
        Begin recording coverage information.
        """
        log.debug("Coverage begin")
        self.skipModules = sys.modules.keys()[:]
        if self.coverErase:
            log.debug("Clearing previously collected coverage statistics")
            self.coverInstance.combine()
            self.coverInstance.erase()
        self.coverInstance.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]')
        self.coverInstance.load()
        self.coverInstance.start()


def report(self, stream):
        """
        Output code coverage report.
        """
        log.debug("Coverage report")
        self.coverInstance.stop()
        self.coverInstance.combine()
        self.coverInstance.save()
        modules = [module
                    for name, module in sys.modules.items()
                    if self.wantModuleCoverage(name, module)]
        log.debug("Coverage report will cover modules: %s", modules)
        self.coverInstance.report(modules, file=stream)
        if self.coverHtmlDir:
            log.debug("Generating HTML coverage report")
            self.coverInstance.html_report(modules, self.coverHtmlDir)
        if self.coverXmlFile:
            log.debug("Generating XML coverage report")
            self.coverInstance.xml_report(modules, self.coverXmlFile)

        # make sure we have minimum required coverage
        if self.coverMinPercentage:
            f = StringIO.StringIO()
            self.coverInstance.report(modules, file=f)
            m = re.search(r'-------\s\w+\s+\d+\s+\d+\s+(\d+)%\s+\d*\s{0,1}$', f.getvalue())
            if m:
                percentage = int(m.groups()[0])
                if percentage < self.coverMinPercentage:
                    log.error('TOTAL Coverage did not reach minimum '
                              'required: %d%%' % self.coverMinPercentage)
                    sys.exit(1)
            else:
                log.error("No total percentage was found in coverage output, "
                          "something went wrong.")


def wantFile(self, file, package=None):
        """If inclusive coverage enabled, return true for all source files
        in wanted packages.
        """
        if self.coverInclusive:
            if file.endswith(".py"):
                if package and self.coverPackages:
                    for want in self.coverPackages:
                        if package.startswith(want):
                            return True
                else:
                    return True
        return None

def wantFile ( self , file , package = None ) : if self . coverInclusive : if file . endswith ( ".py" ) : if package and self . coverPackages : for want in self . coverPackages : if package . startswith ( want ) : return True else : return True return None

If inclusive coverage enabled return true for all source files in wanted packages .


def interpret_distro_name(location, basename, metadata,
    py_version=None, precedence=SOURCE_DIST, platform=None
):
    """Generate alternative interpretations of a source distro name

    Note: if `location` is a filesystem filename, you should call
    ``pkg_resources.normalize_path()`` on it before passing it to this
    routine!
    """
    # Generate alternative interpretations of a source distro name
    # Because some packages are ambiguous as to name/versions split
    # e.g. "adns-python-1.1.0", "egenix-mx-commercial", etc.
    # So, we generate each possible interepretation (e.g. "adns, python-1.1.0"
    # "adns-python, 1.1.0", and "adns-python-1.1.0, no version").  In practice,
    # the spurious interpretations should be ignored, because in the event
    # there's also an "adns" package, the spurious "python-1.1.0" version will
    # compare lower than any numeric version number, and is therefore unlikely
    # to match a request for it.  It's still a potential problem, though, and
    # in the long run PyPI and the distutils should go for "safe" names and
    # versions in distribution archive names (sdist and bdist).

    parts = basename.split('-')
    if not py_version:
        for i,p in enumerate(parts[2:]):
            if len(p)==5 and p.startswith('py2.'):
                return # It's a bdist_dumb, not an sdist -- bail out

    for p in range(1,len(parts)+1):
        yield Distribution(
            location, metadata, '-'.join(parts[:p]), '-'.join(parts[p:]),
            py_version=py_version, precedence = precedence,
            platform = platform
        )

def interpret_distro_name ( location , basename , metadata , py_version = None , precedence = SOURCE_DIST , platform = None ) : parts = basename . split ( '-' ) if not py_version : for i , p in enumerate ( parts [ 2 : ] ) : if len ( p ) == 5 and p . startswith ( 'py2.' ) : return for p in range ( 1 , len ( parts ) + 1 ) : yield Distribution ( location , metadata , '-' . join ( parts [ : p ] ) , '-' . join ( parts [ p : ] ) , py_version = py_version , precedence = precedence , platform = platform )


def open_with_auth(url):
    """Open a urllib2 request, handling HTTP authentication"""

    scheme, netloc, path, params, query, frag = urlparse.urlparse(url)

    # Double scheme does not raise on Mac OS X as revealed by a
    # failing test. We would expect "nonnumeric port". Refs #20.
    if netloc.endswith(':'):
        raise httplib.InvalidURL("nonnumeric port: ''")

    if scheme in ('http', 'https'):
        auth, host = urllib2.splituser(netloc)
    else:
        auth = None

    if auth:
        auth = "Basic " + _encode_auth(auth)
        new_url = urlparse.urlunparse((scheme,host,path,params,query,frag))
        request = urllib2.Request(new_url)
        request.add_header("Authorization", auth)
    else:
        request = urllib2.Request(url)

    request.add_header('User-Agent', user_agent)
    fp = urllib2.urlopen(request)

    if auth:
        # Put authentication info back into request URL if same host,
        # so that links found on the page will work
        s2, h2, path2, param2, query2, frag2 = urlparse.urlparse(fp.url)
        if s2==scheme and h2==host:
            fp.url = urlparse.urlunparse((s2,netloc,path2,param2,query2,frag2))

    return fp

def
open_with_auth
(
url
)
:
scheme
,
netloc
,
path
,
params
,
query
,
frag
=
urlparse
.
urlparse
(
url
)
if
netloc
.
endswith
(
':'
)
:
raise
httplib
.
InvalidURL
(
"nonnumeric port: ''"
)
if
scheme
in
(
'http'
,
'https'
)
:
auth
,
host
=
urllib2
.
splituser
(
netloc
)
else
:
auth
=
if
auth
:
auth
=
"Basic "
+
_encode_auth
(
auth
)
new_url
=
urlparse
.
urlunparse
(
(
scheme
,
host
,
path
,
params
,
query
,
frag
)
)
request
=
urllib2
.
Request
(
new_url
)
request
.
add_header
(
"Authorization"
,
auth
)
else
:
request
=
urllib2
.
Request
(
url
)
request
.
add_header
(
'User-Agent'
,
user_agent
)
fp
=
urllib2
.
urlopen
(
request
)
if
auth
:
s2
,
h2
,
path2
,
param2
,
query2
,
frag2
=
urlparse
.
urlparse
(
fp
.
url
)
if
s2
==
scheme
and
h2
==
host
:
fp
.
url
=
urlparse
.
urlunparse
(
(
s2
,
netloc
,
path2
,
param2
,
query2
,
frag2
)
)
return
fp

Open a urllib2 request handling HTTP authentication


def fetch_distribution(self,
        requirement, tmpdir, force_scan=False, source=False, develop_ok=False,
        local_index=None
    ):
        """Obtain a distribution suitable for fulfilling `requirement`

        `requirement` must be a ``pkg_resources.Requirement`` instance.
        If necessary, or if the `force_scan` flag is set, the requirement is
        searched for in the (online) package index as well as the locally
        installed packages.  If a distribution matching `requirement` is found,
        the returned distribution's ``location`` is the value you would have
        gotten from calling the ``download()`` method with the matching
        distribution's URL or filename.  If no matching distribution is found,
        ``None`` is returned.

        If the `source` flag is set, only source distributions and source
        checkout links will be considered.  Unless the `develop_ok` flag is
        set, development and system eggs (i.e., those using the ``.egg-info``
        format) will be ignored.
        """

        # process a Requirement
        self.info("Searching for %s", requirement)
        skipped = {}
        dist = None

        def find(req, env=None):
            if env is None:
                env = self
            # Find a matching distribution; may be called more than once

            for dist in env[req.key]:

                if dist.precedence==DEVELOP_DIST and not develop_ok:
                    if dist not in skipped:
                        self.warn("Skipping development or system egg: %s",dist)
                        skipped[dist] = 1
                    continue

                if dist in req and (dist.precedence<=SOURCE_DIST or not source):
                    self.info("Best match: %s", dist)
                    return dist.clone(
                        location=self.download(dist.location, tmpdir)
                    )

        if force_scan:
            self.prescan()
            self.find_packages(requirement)
            dist = find(requirement)

        if local_index is not None:
            dist = dist or find(requirement, local_index)

        if dist is None and self.to_scan is not None:
            self.prescan()
            dist = find(requirement)

        if dist is None and not force_scan:
            self.find_packages(requirement)
            dist = find(requirement)

        if dist is None:
            self.warn(
                "No local packages or download links found for %s%s",
                (source and "a source distribution of " or ""),
                requirement,
            )
        return dist

Obtain a distribution suitable for fulfilling requirement


def get_parent(obj):
    '''
    get parent from obj.
    '''
    names = obj.__qualname__.split('.')[:-1]
    if '<locals>' in names: # locals function
        raise ValueError('cannot get parent from locals object.')
    module = sys.modules[obj.__module__]
    parent = module
    while names:
        parent = getattr(parent, names.pop(0))
    return parent

def
get_parent
(
obj
)
:
names
=
obj
.
__qualname__
.
split
(
'.'
)
[
:
1
]
if
'<locals>'
in
names
:
raise
ValueError
(
'cannot get parent from locals object.'
)
module
=
sys
.
modules
[
obj
.
__module__
]
parent
=
module
while
names
:
parent
=
getattr
(
parent
,
names
.
pop
(
0
)
)
return
parent


def root_topic(self):
        """this is a property, in case the handler is created
        before the engine gets registered with an id"""
        if isinstance(getattr(self.engine, 'id', None), int):
            return "engine.%i"%self.engine.id
        else:
            return "engine"

def root_topic ( self ) : if isinstance ( getattr ( self . engine , 'id' , None ) , int ) : return "engine.%i" % self . engine . id else : return "engine"

this is a property in case the handler is created before the engine gets registered with an id


def render_template(content, context):
    """ renders context aware template """
    rendered = Template(content).render(Context(context))
    return rendered

def render_template ( content , context ) : rendered = Template ( content ) . render ( Context ( context ) ) return rendered


def configure(self, options, conf):
        """Configure plugin. Plugin is enabled by default.
        """
        self.conf = conf
        if not options.capture:
            self.enabled = False

def configure ( self , options , conf ) : self . conf = conf if not options . capture : self . enabled = False

Configure plugin . Plugin is enabled by default .


def formatError(self, test, err):
        """Add captured output to error report.
        """
        test.capturedOutput = output = self.buffer
        self._buf = None
        if not output:
            # Don't return None as that will prevent other
            # formatters from formatting and remove earlier formatters
            # formats, instead return the err we got
            return err
        ec, ev, tb = err
        return (ec, self.addCaptureToErr(ev, output), tb)

def formatError ( self , test , err ) : test . capturedOutput = output = self . buffer self . _buf = None if not output : return err ec , ev , tb = err return ( ec , self . addCaptureToErr ( ev , output ) , tb )


def splitBy(data, num):
    """ Turn a list to list of list """
    return [data[i:i + num] for i in range(0, len(data), num)]

def splitBy ( data , num ) : return [ data [ i : i + num ] for i in range ( 0 , len ( data ) , num ) ]


def convert_to_this_nbformat(nb, orig_version=2, orig_minor=0):
    """Convert a notebook to the v3 format.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    orig_version : int
        The original version of the notebook to convert.
    orig_minor : int
        The original minor version of the notebook to convert (only relevant for v >= 3).
    """
    if orig_version == 1:
        nb = v2.convert_to_this_nbformat(nb)
        orig_version = 2
    if orig_version == 2:
        # Mark the original nbformat so consumers know it has been converted.
        nb.nbformat = nbformat
        nb.nbformat_minor = nbformat_minor

        nb.orig_nbformat = 2
        return nb
    elif orig_version == 3:
        if orig_minor != nbformat_minor:
            nb.orig_nbformat_minor = orig_minor
        nb.nbformat_minor = nbformat_minor
        return nb
    else:
        raise ValueError('Cannot convert a notebook from v%s to v3' % orig_version)


def hex_to_rgb(color):
    """Convert a hex color to rgb integer tuple."""
    if color.startswith('#'):
        color = color[1:]
    if len(color) == 3:
        color = ''.join([c*2 for c in color])
    if len(color) != 6:
        return False
    try:
        r = int(color[:2],16)
        g = int(color[2:4],16)
        b = int(color[4:],16)
    except ValueError:
        return False
    else:
        return r,g,b

def hex_to_rgb ( color ) : if color . startswith ( '#' ) : color = color [ 1 : ] if len ( color ) == 3 : color = '' . join ( [ c * 2 for c in color ] ) if len ( color ) != 6 : return False try : r = int ( color [ : 2 ] , 16 ) g = int ( color [ 2 : 4 ] , 16 ) b = int ( color [ 4 : ] , 16 ) except ValueError : return False else : return r , g , b

Convert a hex color to rgb integer tuple .


def get_colors(stylename):
    """Construct the keys to be used building the base stylesheet
    from a templatee."""
    style = get_style_by_name(stylename)
    fgcolor = style.style_for_token(Token.Text)['color'] or ''
    if len(fgcolor) in (3,6):
        # could be 'abcdef' or 'ace' hex, which needs '#' prefix
        try:
            int(fgcolor, 16)
        except TypeError:
            pass
        else:
            fgcolor = "#"+fgcolor

    return dict(
        bgcolor = style.background_color,
        select = style.highlight_color,
        fgcolor = fgcolor
    )

def get_colors ( stylename ) : style = get_style_by_name ( stylename ) fgcolor = style . style_for_token ( Token . Text ) [ 'color' ] or '' if len ( fgcolor ) in ( 3 , 6 ) : try : int ( fgcolor , 16 ) except TypeError : pass else : fgcolor = "#" + fgcolor return dict ( bgcolor = style . background_color , select = style . highlight_color , fgcolor = fgcolor )

Construct the keys to be used building the base stylesheet from a templatee .


def get_font(family, fallback=None):
    """Return a font of the requested family, using fallback as alternative.

    If a fallback is provided, it is used in case the requested family isn't
    found.  If no fallback is given, no alternative is chosen and Qt's internal
    algorithms may automatically choose a fallback font.

    Parameters
    ----------
    family : str
      A font name.
    fallback : str
      A font name.

    Returns
    -------
    font : QFont object
    """
    font = QtGui.QFont(family)
    # Check whether we got what we wanted using QFontInfo, since exactMatch()
    # is overly strict and returns false in too many cases.
    font_info = QtGui.QFontInfo(font)
    if fallback is not None and font_info.family() != family:
        font = QtGui.QFont(fallback)
    return font

def get_font ( family , fallback = None ) : font = QtGui . QFont ( family ) font_info = QtGui . QFontInfo ( font ) if fallback is not None and font_info . family ( ) != family : font = QtGui . QFont ( fallback ) return font

Return a font of the requested family using fallback as alternative .


def _handle_execute_reply(self, msg):
        """ Reimplemented to support prompt requests.
        """
        msg_id = msg['parent_header'].get('msg_id')
        info = self._request_info['execute'].get(msg_id)
        if info and info.kind == 'prompt':
           number = msg['content']['execution_count'] + 1
           self._show_interpreter_prompt(number)
           self._request_info['execute'].pop(msg_id)
        else:
           super(IPythonWidget, self)._handle_execute_reply(msg)

def _handle_execute_reply ( self , msg ) : msg_id = msg [ 'parent_header' ] . get ( 'msg_id' ) info = self . _request_info [ 'execute' ] . get ( msg_id ) if info and info . kind == 'prompt' : number = msg [ 'content' ] [ 'execution_count' ] + 1 self . _show_interpreter_prompt ( number ) self . _request_info [ 'execute' ] . pop ( msg_id ) else : super ( IPythonWidget , self ) . _handle_execute_reply ( msg )

Reimplemented to support prompt requests .


def _handle_history_reply(self, msg):
        """ Implemented to handle history tail replies, which are only supported
            by the IPython kernel.
        """
        content = msg['content']
        if 'history' not in content:
            self.log.error("History request failed: %r"%content)
            if content.get('status', '') == 'aborted' and \
                                            not self._retrying_history_request:
                # a *different* action caused this request to be aborted, so
                # we should try again.
                self.log.error("Retrying aborted history request")
                # prevent multiple retries of aborted requests:
                self._retrying_history_request = True
                # wait out the kernel's queue flush, which is currently timed at 0.1s
                time.sleep(0.25)
                self.kernel_manager.shell_channel.history(hist_access_type='tail',n=1000)
            else:
                self._retrying_history_request = False
            return
        # reset retry flag
        self._retrying_history_request = False
        history_items = content['history']
        self.log.debug("Received history reply with %i entries", len(history_items))
        items = []
        last_cell = u""
        for _, _, cell in history_items:
            cell = cell.rstrip()
            if cell != last_cell:
                items.append(cell)
                last_cell = cell
        self._set_history(items)

Implemented to handle history tail replies which are only supported by the IPython kernel .


def _handle_pyout(self, msg):
        """ Reimplemented for IPython-style "display hook".
        """
        self.log.debug("pyout: %s", msg.get('content', ''))
        if not self._hidden and self._is_from_this_session(msg):
            content = msg['content']
            prompt_number = content.get('execution_count', 0)
            data = content['data']
            if data.has_key('text/html'):
                self._append_plain_text(self.output_sep, True)
                self._append_html(self._make_out_prompt(prompt_number), True)
                html = data['text/html']
                self._append_plain_text('\n', True)
                self._append_html(html + self.output_sep2, True)
            elif data.has_key('text/plain'):
                self._append_plain_text(self.output_sep, True)
                self._append_html(self._make_out_prompt(prompt_number), True)
                text = data['text/plain']
                # If the repr is multiline, make sure we start on a new line,
                # so that its lines are aligned.
                if "\n" in text and not self.output_sep.endswith("\n"):
                    self._append_plain_text('\n', True)
                self._append_plain_text(text + self.output_sep2, True)

def
_handle_pyout
(
self
,
msg
)
:
self
.
log
.
debug
(
"pyout: %s"
,
msg
.
get
(
'content'
,
''
)
)
if
not
self
.
_hidden
and
self
.
_is_from_this_session
(
msg
)
:
content
=
msg
[
'content'
]
prompt_number
=
content
.
get
(
'execution_count'
,
0
)
data
=
content
[
'data'
]
if
data
.
has_key
(
'text/html'
)
:
self
.
_append_plain_text
(
self
.
output_sep
,
True
)
self
.
_append_html
(
self
.
_make_out_prompt
(
prompt_number
)
,
True
)
html
=
data
[
'text/html'
]
self
.
_append_plain_text
(
'\n'
,
True
)
self
.
_append_html
(
html
+
self
.
output_sep2
,
True
)
elif
data
.
has_key
(
'text/plain'
)
:
self
.
_append_plain_text
(
self
.
output_sep
,
True
)
self
.
_append_html
(
self
.
_make_out_prompt
(
prompt_number
)
,
True
)
text
=
data
[
'text/plain'
]
if
"\n"
in
text
and
not
self
.
output_sep
.
endswith
(
"\n"
)
:
self
.
_append_plain_text
(
'\n'
,
True
)
self
.
_append_plain_text
(
text
+
self
.
output_sep2
,
True
)

Reimplemented for IPython - style display hook .


def _handle_display_data(self, msg):
        """ The base handler for the ``display_data`` message.
        """
        self.log.debug("display: %s", msg.get('content', ''))
        # For now, we don't display data from other frontends, but we
        # eventually will as this allows all frontends to monitor the display
        # data. But we need to figure out how to handle this in the GUI.
        if not self._hidden and self._is_from_this_session(msg):
            source = msg['content']['source']
            data = msg['content']['data']
            metadata = msg['content']['metadata']
            # In the regular IPythonWidget, we simply print the plain text
            # representation.
            if data.has_key('text/html'):
                html = data['text/html']
                self._append_html(html, True)
            elif data.has_key('text/plain'):
                text = data['text/plain']
                self._append_plain_text(text, True)
            # This newline seems to be needed for text and html output.
            self._append_plain_text(u'\n', True)

def
_handle_display_data
(
self
,
msg
)
:
self
.
log
.
debug
(
"display: %s"
,
msg
.
get
(
'content'
,
''
)
)
if
not
self
.
_hidden
and
self
.
_is_from_this_session
(
msg
)
:
source
=
msg
[
'content'
]
[
'source'
]
data
=
msg
[
'content'
]
[
'data'
]
metadata
=
msg
[
'content'
]
[
'metadata'
]
if
data
.
has_key
(
'text/html'
)
:
html
=
data
[
'text/html'
]
self
.
_append_html
(
html
,
True
)
elif
data
.
has_key
(
'text/plain'
)
:
text
=
data
[
'text/plain'
]
self
.
_append_plain_text
(
text
,
True
)
self
.
_append_plain_text
(
u'\n'
,
True
)

The base handler for the display_data message .


def _started_channels(self):
        """Reimplemented to make a history request and load %guiref."""
        super(IPythonWidget, self)._started_channels()
        self._load_guiref_magic()
        self.kernel_manager.shell_channel.history(hist_access_type='tail',
                                                  n=1000)

def _started_channels ( self ) : super ( IPythonWidget , self ) . _started_channels ( ) self . _load_guiref_magic ( ) self . kernel_manager . shell_channel . history ( hist_access_type = 'tail' , n = 1000 )

Reimplemented to make a history request and load %guiref .


def execute_file(self, path, hidden=False):
        """ Reimplemented to use the 'run' magic.
        """
        # Use forward slashes on Windows to avoid escaping each separator.
        if sys.platform == 'win32':
            path = os.path.normpath(path).replace('\\', '/')

        # Perhaps we should not be using %run directly, but while we
        # are, it is necessary to quote or escape filenames containing spaces
        # or quotes.

        # In earlier code here, to minimize escaping, we sometimes quoted the
        # filename with single quotes. But to do this, this code must be
        # platform-aware, because run uses shlex rather than python string
        # parsing, so that:
        # * In Win: single quotes can be used in the filename without quoting,
        #   and we cannot use single quotes to quote the filename.
        # * In *nix: we can escape double quotes in a double quoted filename,
        #   but can't escape single quotes in a single quoted filename.

        # So to keep this code non-platform-specific and simple, we now only
        # use double quotes to quote filenames, and escape when needed:
        if ' ' in path or "'" in path or '"' in path:
            path = '"%s"' % path.replace('"', '\\"')
        self.execute('%%run %s' % path, hidden=hidden)

def
execute_file
(
self
,
path
,
hidden
=
False
)
:
if
sys
.
platform
==
'win32'
:
path
=
os
.
path
.
normpath
(
path
)
.
replace
(
'\\'
,
'/'
)
if
' '
in
path
or
"'"
in
path
or
'"'
in
path
:
path
=
'"%s"'
%
path
.
replace
(
'"'
,
'\\"'
)
self
.
execute
(
'%%run %s'
%
path
,
hidden
=
hidden
)


def _process_execute_error(self, msg):
        """ Reimplemented for IPython-style traceback formatting.
        """
        content = msg['content']
        traceback = '\n'.join(content['traceback']) + '\n'
        if False:
            # FIXME: For now, tracebacks come as plain text, so we can't use
            # the html renderer yet.  Once we refactor ultratb to produce
            # properly styled tracebacks, this branch should be the default
            traceback = traceback.replace(' ', ' ')
            traceback = traceback.replace('\n', '
')

            ename = content['ename']
            ename_styled = '%s' % ename
            traceback = traceback.replace(ename, ename_styled)

            self._append_html(traceback)
        else:
            # This is the fallback for now, using plain text with ansi escapes
            self._append_plain_text(traceback)

def
_process_execute_error
(
self
,
msg
)
:
content
=
msg
[
'content'
]
traceback
=
'\n'
.
join
(
content
[
'traceback'
]
)
+
'\n'
if
False
:
traceback
=
traceback
.
replace
(
' '
,
' '
)
traceback
=
traceback
.
replace
(
'\n'
,
'
'
)
ename
=
content
[
'ename'
]
ename_styled
=
'%s'
%
ename
traceback
=
traceback
.
replace
(
ename
,
ename_styled
)
self
.
_append_html
(
traceback
)
else
:
self
.
_append_plain_text
(
traceback
)

Reimplemented for IPython - style traceback formatting .


def _process_execute_payload(self, item):
        """ Reimplemented to dispatch payloads to handler methods.
        """
        handler = self._payload_handlers.get(item['source'])
        if handler is None:
            # We have no handler for this type of payload, simply ignore it
            return False
        else:
            handler(item)
            return True

def _process_execute_payload ( self , item ) : handler = self . _payload_handlers . get ( item [ 'source' ] ) if handler is None : return False else : handler ( item ) return True

Reimplemented to dispatch payloads to handler methods .


def set_default_style(self, colors='lightbg'):
        """ Sets the widget style to the class defaults.

        Parameters:
        -----------
        colors : str, optional (default lightbg)
            Whether to use the default IPython light background or dark
            background or B&W style.
        """
        colors = colors.lower()
        if colors=='lightbg':
            self.style_sheet = styles.default_light_style_sheet
            self.syntax_style = styles.default_light_syntax_style
        elif colors=='linux':
            self.style_sheet = styles.default_dark_style_sheet
            self.syntax_style = styles.default_dark_syntax_style
        elif colors=='nocolor':
            self.style_sheet = styles.default_bw_style_sheet
            self.syntax_style = styles.default_bw_syntax_style
        else:
            raise KeyError("No such color scheme: %s"%colors)

def
set_default_style
(
self
,
colors
=
'lightbg'
)
:
colors
=
colors
.
lower
(
)
if
colors
==
'lightbg'
:
self
.
style_sheet
=
styles
.
default_light_style_sheet
self
.
syntax_style
=
styles
.
default_light_syntax_style
elif
colors
==
'linux'
:
self
.
style_sheet
=
styles
.
default_dark_style_sheet
self
.
syntax_style
=
styles
.
default_dark_syntax_style
elif
colors
==
'nocolor'
:
self
.
style_sheet
=
styles
.
default_bw_style_sheet
self
.
syntax_style
=
styles
.
default_bw_syntax_style
else
:
raise
KeyError
(
"No such color scheme: %s"
%
colors
)

Sets the widget style to the class defaults .


def _edit(self, filename, line=None):
        """ Opens a Python script for editing.

        Parameters:
        -----------
        filename : str
            A path to a local system file.

        line : int, optional
            A line of interest in the file.
        """
        if self.custom_edit:
            self.custom_edit_requested.emit(filename, line)
        elif not self.editor:
            self._append_plain_text('No default editor available.\n'
            'Specify a GUI text editor in the `IPythonWidget.editor` '
            'configurable to enable the %edit magic')
        else:
            try:
                filename = '"%s"' % filename
                if line and self.editor_line:
                    command = self.editor_line.format(filename=filename,
                                                      line=line)
                else:
                    try:
                        command = self.editor.format()
                    except KeyError:
                        command = self.editor.format(filename=filename)
                    else:
                        command += ' ' + filename
            except KeyError:
                self._append_plain_text('Invalid editor command.\n')
            else:
                try:
                    Popen(command, shell=True)
                except OSError:
                    msg = 'Opening editor with command "%s" failed.\n'
                    self._append_plain_text(msg % command)


def _make_in_prompt(self, number):
        """ Given a prompt number, returns an HTML In prompt.
        """
        try:
            body = self.in_prompt % number
        except TypeError:
            # allow in_prompt to leave out number, e.g. '>>> '
            body = self.in_prompt
        return '%s' % body

def
_make_in_prompt
(
self
,
number
)
:
try
:
body
=
self
.
in_prompt
%
number
except
TypeError
:
body
=
self
.
in_prompt
return
'%s'
%
body

Given a prompt number returns an HTML In prompt .


def _make_continuation_prompt(self, prompt):
        """ Given a plain text version of an In prompt, returns an HTML
            continuation prompt.
        """
        end_chars = '...: '
        space_count = len(prompt.lstrip('\n')) - len(end_chars)
        body = ' ' * space_count + end_chars
        return '%s' % body

def
_make_continuation_prompt
(
self
,
prompt
)
:
end_chars
=
'...: '
space_count
=
len
(
prompt
.
lstrip
(
'\n'
)
)
len
(
end_chars
)
body
=
' '
*
space_count
+
end_chars
return
'%s'
%
body

Given a plain text version of an In prompt returns an HTML continuation prompt .


def _style_sheet_changed(self):
        """ Set the style sheets of the underlying widgets.
        """
        self.setStyleSheet(self.style_sheet)
        if self._control is not None:
            self._control.document().setDefaultStyleSheet(self.style_sheet)
            bg_color = self._control.palette().window().color()
            self._ansi_processor.set_background_color(bg_color)

        if self._page_control is not None:
            self._page_control.document().setDefaultStyleSheet(self.style_sheet)

def _style_sheet_changed ( self ) : self . setStyleSheet ( self . style_sheet ) if self . _control is not None : self . _control . document ( ) . setDefaultStyleSheet ( self . style_sheet ) bg_color = self . _control . palette ( ) . window ( ) . color ( ) self . _ansi_processor . set_background_color ( bg_color ) if self . _page_control is not None : self . _page_control . document ( ) . setDefaultStyleSheet ( self . style_sheet )

Set the style sheets of the underlying widgets .


def _syntax_style_changed(self):
        """ Set the style for the syntax highlighter.
        """
        if self._highlighter is None:
            # ignore premature calls
            return
        if self.syntax_style:
            self._highlighter.set_style(self.syntax_style)
        else:
            self._highlighter.set_style_sheet(self.style_sheet)

def _syntax_style_changed ( self ) : if self . _highlighter is None : return if self . syntax_style : self . _highlighter . set_style ( self . syntax_style ) else : self . _highlighter . set_style_sheet ( self . style_sheet )

Set the style for the syntax highlighter .


async def _handle_response(self, response: aiohttp.client_reqrep.ClientResponse, await_final_result: bool) -> dict:
        """
        Handles the response returned from the CloudStack API. Some CloudStack API are implemented asynchronous, which
        means that the API call returns just a job id. The actually expected API response is postponed and a specific
        asyncJobResults API has to be polled using the job id to get the final result once the API call has been
        processed.

        :param response: The response returned by the aiohttp call.
        :type response: aiohttp.client_reqrep.ClientResponse
        :param await_final_result: Specifier that indicates whether the function should poll the asyncJobResult API
                                   until the asynchronous API call has been processed
        :type await_final_result: bool
        :return: Dictionary containing the JSON response of the API call
        :rtype: dict
        """
        try:
            data = await response.json()
        except aiohttp.client_exceptions.ContentTypeError:
            text = await response.text()
            logging.debug('Content returned by server not of type "application/json"\n Content: {}'.format(text))
            raise CloudStackClientException(message="Could not decode content. Server did not return json content!")
        else:
            data = self._transform_data(data)
            if response.status != 200:
                raise CloudStackClientException(message="Async CloudStack call failed!",
                                                error_code=data.get("errorcode", response.status),
                                                error_text=data.get("errortext"),
                                                response=data)

        while await_final_result and ('jobid' in data):
            await asyncio.sleep(self.async_poll_latency)
            data = await self.queryAsyncJobResult(jobid=data['jobid'])
            if data['jobstatus']:  # jobstatus is 0 for pending async CloudStack calls
                if not data['jobresultcode']:  # exit code is zero
                    try:
                        return data['jobresult']
                    except KeyError:
                        pass
                logging.debug("Async CloudStack call returned {}".format(str(data)))
                raise CloudStackClientException(message="Async CloudStack call failed!",
                                                error_code=data.get("errorcode"),
                                                error_text=data.get("errortext"),
                                                response=data)

        return data

Handles the response returned from the CloudStack API . Some CloudStack API are implemented asynchronous which means that the API call returns just a job id . The actually expected API response is postponed and a specific asyncJobResults API has to be polled using the job id to get the final result once the API call has been processed .


def _sign(self, url_parameters: dict) -> dict:
        """
        According to the CloudStack documentation, each request needs to be signed in order to authenticate the user
        account executing the API command. The signature is generated using a combination of the api secret and a SHA-1
        hash of the url parameters including the command string. In order to generate a unique identifier, the url
        parameters have to be transformed to lower case and ordered alphabetically.

        :param url_parameters: The url parameters of the API call including the command string
        :type url_parameters: dict
        :return: The url parameters including a new key, which contains the signature
        :rtype: dict
        """
        if url_parameters:
            url_parameters.pop('signature', None)  # remove potential existing signature from url parameters
            request_string = urlencode(sorted(url_parameters.items()), safe='.-*_', quote_via=quote).lower()
            digest = hmac.new(self.api_secret.encode('utf-8'), request_string.encode('utf-8'), hashlib.sha1).digest()
            url_parameters['signature'] = base64.b64encode(digest).decode('utf-8').strip()
        return url_parameters

def _sign ( self , url_parameters : dict ) -> dict : if url_parameters : url_parameters . pop ( 'signature' , None ) request_string = urlencode ( sorted ( url_parameters . items ( ) ) , safe = '.-*_' , quote_via = quote ) . lower ( ) digest = hmac . new ( self . api_secret . encode ( 'utf-8' ) , request_string . encode ( 'utf-8' ) , hashlib . sha1 ) . digest ( ) url_parameters [ 'signature' ] = base64 . b64encode ( digest ) . decode ( 'utf-8' ) . strip ( ) return url_parameters

According to the CloudStack documentation each request needs to be signed in order to authenticate the user account executing the API command . The signature is generated using a combination of the api secret and a SHA - 1 hash of the url parameters including the command string . In order to generate a unique identifier the url parameters have to be transformed to lower case and ordered alphabetically .


def _transform_data(data: dict) -> dict:
        """
        Each CloudStack API call returns a nested dictionary structure. The first level contains only one key indicating
        the API that originated the response. This function removes that first level from the data returned to the
        caller.

        :param data: Response of the API call
        :type data: dict
        :return: Simplified response without the information about the API that originated the response.
        :rtype: dict
        """
        for key in data.keys():
            return_value = data[key]
            if isinstance(return_value, dict):
                return return_value
        return data

def _transform_data ( data : dict ) -> dict : for key in data . keys ( ) : return_value = data [ key ] if isinstance ( return_value , dict ) : return return_value return data

Each CloudStack API call returns a nested dictionary structure . The first level contains only one key indicating the API that originated the response . This function removes that first level from the data returned to the caller .


def virtual_memory():
    """System virtual memory as a namedutple."""
    mem =  _psutil_bsd.get_virtual_mem()
    total, free, active, inactive, wired, cached, buffers, shared = mem
    avail = inactive + cached + free
    used =  active + wired + cached
    percent = usage_percent((total - avail), total, _round=1)
    return nt_virtmem_info(total, avail, percent, used, free,
                           active, inactive, buffers, cached, shared, wired)

def virtual_memory ( ) : mem = _psutil_bsd . get_virtual_mem ( ) total , free , active , inactive , wired , cached , buffers , shared = mem avail = inactive + cached + free used = active + wired + cached percent = usage_percent ( ( total - avail ) , total , _round = 1 ) return nt_virtmem_info ( total , avail , percent , used , free , active , inactive , buffers , cached , shared , wired )


def get_system_cpu_times():
    """Return system per-CPU times as a named tuple"""
    user, nice, system, idle, irq = _psutil_bsd.get_system_cpu_times()
    return _cputimes_ntuple(user, nice, system, idle, irq)

def get_system_cpu_times ( ) : user , nice , system , idle , irq = _psutil_bsd . get_system_cpu_times ( ) return _cputimes_ntuple ( user , nice , system , idle , irq )

Return system per - CPU times as a named tuple


def get_process_uids(self):
        """Return real, effective and saved user ids."""
        real, effective, saved = _psutil_bsd.get_process_uids(self.pid)
        return nt_uids(real, effective, saved)

def get_process_uids ( self ) : real , effective , saved = _psutil_bsd . get_process_uids ( self . pid ) return nt_uids ( real , effective , saved )

Return real effective and saved user ids .


def get_process_gids(self):
        """Return real, effective and saved group ids."""
        real, effective, saved = _psutil_bsd.get_process_gids(self.pid)
        return nt_gids(real, effective, saved)

def get_process_gids ( self ) : real , effective , saved = _psutil_bsd . get_process_gids ( self . pid ) return nt_gids ( real , effective , saved )

Return real effective and saved group ids .


def get_process_threads(self):
        """Return the number of threads belonging to the process."""
        rawlist = _psutil_bsd.get_process_threads(self.pid)
        retlist = []
        for thread_id, utime, stime in rawlist:
            ntuple = nt_thread(thread_id, utime, stime)
            retlist.append(ntuple)
        return retlist

def get_process_threads ( self ) : rawlist = _psutil_bsd . get_process_threads ( self . pid ) retlist = [ ] for thread_id , utime , stime in rawlist : ntuple = nt_thread ( thread_id , utime , stime ) retlist . append ( ntuple ) return retlist

Return the number of threads belonging to the process .


def get_open_files(self):
        """Return files opened by process as a list of namedtuples."""
        # XXX - C implementation available on FreeBSD >= 8 only
        # else fallback on lsof parser
        if hasattr(_psutil_bsd, "get_process_open_files"):
            rawlist = _psutil_bsd.get_process_open_files(self.pid)
            return [nt_openfile(path, fd) for path, fd in rawlist]
        else:
            lsof = _psposix.LsofParser(self.pid, self._process_name)
            return lsof.get_process_open_files()

def get_open_files ( self ) : if hasattr ( _psutil_bsd , "get_process_open_files" ) : rawlist = _psutil_bsd . get_process_open_files ( self . pid ) return [ nt_openfile ( path , fd ) for path , fd in rawlist ] else : lsof = _psposix . LsofParser ( self . pid , self . _process_name ) return lsof . get_process_open_files ( )

Return files opened by process as a list of namedtuples .


def pkg_commit_hash(pkg_path):
    """Get short form of commit hash given directory `pkg_path`

    We get the commit hash from (in order of preference):

    * IPython.utils._sysinfo.commit
    * git output, if we are in a git repository

    If these fail, we return a not-found placeholder tuple

    Parameters
    ----------
    pkg_path : str
       directory containing package
       only used for getting commit from active repo

    Returns
    -------
    hash_from : str
       Where we got the hash from - description
    hash_str : str
       short form of hash
    """
    # Try and get commit from written commit text file
    if _sysinfo.commit:
        return "installation", _sysinfo.commit

    # maybe we are in a repository
    proc = subprocess.Popen('git rev-parse --short HEAD',
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            cwd=pkg_path, shell=True)
    repo_commit, _ = proc.communicate()
    if repo_commit:
        return 'repository', repo_commit.strip()
    return '(none found)', '<not found>'

def
pkg_commit_hash
(
pkg_path
)
:
if
_sysinfo
.
commit
:
return
"installation"
,
_sysinfo
.
commit
proc
=
subprocess
.
Popen
(
'git rev-parse --short HEAD'
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
cwd
=
pkg_path
,
shell
=
True
)
repo_commit
,
_
=
proc
.
communicate
(
)
if
repo_commit
:
return
'repository'
,
repo_commit
.
strip
(
)
return
'(none found)'
,
'<not found>'

Get short form of commit hash given directory pkg_path


def pkg_info(pkg_path):
    """Return dict describing the context of this package

    Parameters
    ----------
    pkg_path : str
       path containing __init__.py for package

    Returns
    -------
    context : dict
       with named parameters of interest
    """
    src, hsh = pkg_commit_hash(pkg_path)
    return dict(
        ipython_version=release.version,
        ipython_path=pkg_path,
        commit_source=src,
        commit_hash=hsh,
        sys_version=sys.version,
        sys_executable=sys.executable,
        sys_platform=sys.platform,
        platform=platform.platform(),
        os_name=os.name,
        default_encoding=encoding.DEFAULT_ENCODING,
        )

def pkg_info ( pkg_path ) : src , hsh = pkg_commit_hash ( pkg_path ) return dict ( ipython_version = release . version , ipython_path = pkg_path , commit_source = src , commit_hash = hsh , sys_version = sys . version , sys_executable = sys . executable , sys_platform = sys . platform , platform = platform . platform ( ) , os_name = os . name , default_encoding = encoding . DEFAULT_ENCODING , )


def sys_info():
    """Return useful information about IPython and the system, as a string.

    Example
    -------
    In [2]: print sys_info()
    {'commit_hash': '144fdae',      # random
     'commit_source': 'repository',
     'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
     'ipython_version': '0.11.dev',
     'os_name': 'posix',
     'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
     'sys_executable': '/usr/bin/python',
     'sys_platform': 'linux2',
     'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
   """
    p = os.path
    path = p.dirname(p.abspath(p.join(__file__, '..')))
    return pprint.pformat(pkg_info(path))

def sys_info ( ) : p = os . path path = p . dirname ( p . abspath ( p . join ( __file__ , '..' ) ) ) return pprint . pformat ( pkg_info ( path ) )

Return useful information about IPython and the system as a string .


def _num_cpus_darwin():
    """Return the number of active CPUs on a Darwin system."""
    p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
    return p.stdout.read()

def _num_cpus_darwin ( ) : p = subprocess . Popen ( [ 'sysctl' , '-n' , 'hw.ncpu' ] , stdout = subprocess . PIPE ) return p . stdout . read ( )

Return the number of active CPUs on a Darwin system .


def num_cpus():
   """Return the effective number of CPUs in the system as an integer.

   This cross-platform function makes an attempt at finding the total number of
   available CPUs in the system, as returned by various underlying system and
   python calls.

   If it can't find a sensible answer, it returns 1 (though an error *may* make
   it return a large positive number that's actually incorrect).
   """

   # Many thanks to the Parallel Python project (http://www.parallelpython.com)
   # for the names of the keys we needed to look up for this function.  This
   # code was inspired by their equivalent function.

   ncpufuncs = {'Linux':_num_cpus_unix,
                'Darwin':_num_cpus_darwin,
                'Windows':_num_cpus_windows,
                # On Vista, python < 2.5.2 has a bug and returns 'Microsoft'
                # See http://bugs.python.org/issue1082 for details.
                'Microsoft':_num_cpus_windows,
                }

   ncpufunc = ncpufuncs.get(platform.system(),
                            # default to unix version (Solaris, AIX, etc)
                            _num_cpus_unix)

   try:
       ncpus = max(1,int(ncpufunc()))
   except:
       ncpus = 1
   return ncpus

def num_cpus ( ) : ncpufuncs = { 'Linux' : _num_cpus_unix , 'Darwin' : _num_cpus_darwin , 'Windows' : _num_cpus_windows , 'Microsoft' : _num_cpus_windows , } ncpufunc = ncpufuncs . get ( platform . system ( ) , _num_cpus_unix ) try : ncpus = max ( 1 , int ( ncpufunc ( ) ) ) except : ncpus = 1 return ncpus

Return the effective number of CPUs in the system as an integer .


def nextset(self):
        """Advance to the next result set.

        Returns None if there are no more result sets.
        """
        if self._executed:
            self.fetchall()
        del self.messages[:]

        db = self._get_db()
        nr = db.next_result()
        if nr == -1:
            return None
        self._do_get_result()
        self._post_get_result()
        self._warning_check()
        return 1

def nextset ( self ) : if self . _executed : self . fetchall ( ) del self . messages [ : ] db = self . _get_db ( ) nr = db . next_result ( ) if nr == - 1 : return None self . _do_get_result ( ) self . _post_get_result ( ) self . _warning_check ( ) return 1


def fetchone(self):
        """Fetches a single row from the cursor."""
        self._check_executed()
        r = self._fetch_row(1)
        if not r:
            self._warning_check()
            return None
        self.rownumber = self.rownumber + 1
        return r[0]

def fetchone ( self ) : self . _check_executed ( ) r = self . _fetch_row ( 1 ) if not r : self . _warning_check ( ) return None self . rownumber = self . rownumber + 1 return r [ 0 ]


def fetchmany(self, size=None):
        """Fetch up to size rows from the cursor. Result set may be smaller
        than size. If size is not defined, cursor.arraysize is used."""
        self._check_executed()
        r = self._fetch_row(size or self.arraysize)
        self.rownumber = self.rownumber + len(r)
        if not r:
            self._warning_check()
        return r

def fetchmany ( self , size = None ) : self . _check_executed ( ) r = self . _fetch_row ( size or self . arraysize ) self . rownumber = self . rownumber + len ( r ) if not r : self . _warning_check ( ) return r

Fetch up to size rows from the cursor . Result set may be smaller than size . If size is not defined cursor . arraysize is used .


def fetchall(self):
        """Fetchs all available rows from the cursor."""
        self._check_executed()
        r = self._fetch_row(0)
        self.rownumber = self.rownumber + len(r)
        self._warning_check()
        return r

def fetchall ( self ) : self . _check_executed ( ) r = self . _fetch_row ( 0 ) self . rownumber = self . rownumber + len ( r ) self . _warning_check ( ) return r

Fetchs all available rows from the cursor .


def connect(com, peers, tree, pub_url, root_id):
    """this function will be called on the engines"""
    com.connect(peers, tree, pub_url, root_id)

def connect ( com , peers , tree , pub_url , root_id ) : com . connect ( peers , tree , pub_url , root_id )


def reads_json(s, **kwargs):
    """Read a JSON notebook from a string and return the NotebookNode object."""
    nbf, minor, d = parse_json(s, **kwargs)
    if nbf == 1:
        nb = v1.to_notebook_json(d, **kwargs)
        nb = v3.convert_to_this_nbformat(nb, orig_version=1)
    elif nbf == 2:
        nb = v2.to_notebook_json(d, **kwargs)
        nb = v3.convert_to_this_nbformat(nb, orig_version=2)
    elif nbf == 3:
        nb = v3.to_notebook_json(d, **kwargs)
        nb = v3.convert_to_this_nbformat(nb, orig_version=3, orig_minor=minor)
    else:
        raise NBFormatError('Unsupported JSON nbformat version: %i' % nbf)
    return nb

def
reads_json
(
s
,
**
kwargs
)
:
nbf
,
minor
,
d
=
parse_json
(
s
,
**
kwargs
)
if
nbf
==
1
:
nb
=
v1
.
to_notebook_json
(
d
,
**
kwargs
)
nb
=
v3
.
convert_to_this_nbformat
(
nb
,
orig_version
=
1
)
elif
nbf
==
2
:
nb
=
v2
.
to_notebook_json
(
d
,
**
kwargs
)
nb
=
v3
.
convert_to_this_nbformat
(
nb
,
orig_version
=
2
)
elif
nbf
==
3
:
nb
=
v3
.
to_notebook_json
(
d
,
**
kwargs
)
nb
=
v3
.
convert_to_this_nbformat
(
nb
,
orig_version
=
3
,
orig_minor
=
minor
)
else
:
raise
NBFormatError
(
'Unsupported JSON nbformat version: %i'
%
nbf
)
return
nb

Read a JSON notebook from a string and return the NotebookNode object .


def reads_py(s, **kwargs):
    """Read a .py notebook from a string and return the NotebookNode object."""
    nbf, nbm, s = parse_py(s, **kwargs)
    if nbf == 2:
        nb = v2.to_notebook_py(s, **kwargs)
    elif nbf == 3:
        nb = v3.to_notebook_py(s, **kwargs)
    else:
        raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
    return nb

def
reads_py
(
s
,
**
kwargs
)
:
nbf
,
nbm
,
s
=
parse_py
(
s
,
**
kwargs
)
if
nbf
==
2
:
nb
=
v2
.
to_notebook_py
(
s
,
**
kwargs
)
elif
nbf
==
3
:
nb
=
v3
.
to_notebook_py
(
s
,
**
kwargs
)
else
:
raise
NBFormatError
(
'Unsupported PY nbformat version: %i'
%
nbf
)
return
nb

Read a . py notebook from a string and return the NotebookNode object .


def reads(s, format, **kwargs):
    """Read a notebook from a string and return the NotebookNode object.

    This function properly handles notebooks of any version. The notebook
    returned will always be in the current version's format.

    Parameters
    ----------
    s : unicode
        The raw unicode string to read the notebook from.
    format : (u'json', u'ipynb', u'py')
        The format that the string is in.

    Returns
    -------
    nb : NotebookNode
        The notebook that was read.
    """
    format = unicode(format)
    if format == u'json' or format == u'ipynb':
        return reads_json(s, **kwargs)
    elif format == u'py':
        return reads_py(s, **kwargs)
    else:
        raise NBFormatError('Unsupported format: %s' % format)

def
reads
(
s
,
format
,
**
kwargs
)
:
format
=
unicode
(
format
)
if
format
==
u'json'
or
format
==
u'ipynb'
:
return
reads_json
(
s
,
**
kwargs
)
elif
format
==
u'py'
:
return
reads_py
(
s
,
**
kwargs
)
else
:
raise
NBFormatError
(
'Unsupported format: %s'
%
format
)

Read a notebook from a string and return the NotebookNode object .