def _write_entries(po_files, languages, msgid, msgstrs, metadata, comment):
    """
    Write msgstr for every language with all needed metadata and comment.
    Metadata are parser from string into dict, so read them only from gdocs.
    """
    start = re.compile(r'^[\s]+')
    end = re.compile(r'[\s]+$')
    for i, lang in enumerate(languages):
        meta = ast.literal_eval(metadata)
        entry = polib.POEntry(**meta)
        entry.tcomment = comment
        entry.msgid = msgid
        if msgstrs[i]:
            start_ws = start.search(msgid)
            end_ws = end.search(msgid)
            entry.msgstr = str(start_ws.group() if start_ws else '') + \
                unicode(msgstrs[i].strip()) + \
                str(end_ws.group() if end_ws else '')
        else:
            entry.msgstr = ''
        po_files[lang].append(entry)

def _write_entries ( po_files , languages , msgid , msgstrs , metadata , comment ) : start = re . compile ( r'^[\s]+' ) end = re . compile ( r'[\s]+$' ) for i , lang in enumerate ( languages ) : meta = ast . literal_eval ( metadata ) entry = polib . POEntry ( * * meta ) entry . tcomment = comment entry . msgid = msgid if msgstrs [ i ] : start_ws = start . search ( msgid ) end_ws = end . search ( msgid ) entry . msgstr = str ( start_ws . group ( ) if start_ws else '' ) + unicode ( msgstrs [ i ] . strip ( ) ) + str ( end_ws . group ( ) if end_ws else '' ) else : entry . msgstr = '' po_files [ lang ] . append ( entry )

Write msgstr for every language with all needed metadata and comment . Metadata are parser from string into dict so read them only from gdocs .


def _write_header(po_path, lang, header):
    """
    Write header into po file for specific lang.
    Metadata are read from settings file.
    """
    po_file = open(po_path, 'w')
    po_file.write(header + '\n')
    po_file.write(
        'msgid ""' +
        '\nmsgstr ""' +
        '\n"MIME-Version: ' + settings.METADATA['MIME-Version'] + r'\n"'
        '\n"Content-Type: ' + settings.METADATA['Content-Type'] + r'\n"'
        '\n"Content-Transfer-Encoding: ' +
        settings.METADATA['Content-Transfer-Encoding'] + r'\n"'
        '\n"Language: ' + lang + r'\n"' + '\n')
    po_file.close()

def
_write_header
(
po_path
,
lang
,
header
)
:
po_file
=
open
(
po_path
,
'w'
)
po_file
.
write
(
header
+
'\n'
)
po_file
.
write
(
'msgid ""'
+
'\nmsgstr ""'
+
'\n"MIME-Version: '
+
settings
.
METADATA
[
'MIME-Version'
]
+
r'\n"'
'\n"Content-Type: '
+
settings
.
METADATA
[
'Content-Type'
]
+
r'\n"'
'\n"Content-Transfer-Encoding: '
+
settings
.
METADATA
[
'Content-Transfer-Encoding'
]
+
r'\n"'
'\n"Language: '
+
lang
+
r'\n"'
+
'\n'
)
po_file
.
close
(
)

Write header into po file for specific lang . Metadata are read from settings file .


def subscribe_user(self, user):
        """ method to subscribe a user to a service
        """
        url = self.root_url + "subscribe_user"
        values = {}
        values["username"] = user
        return self._query(url, values)

def subscribe_user ( self , user ) : url = self . root_url + "subscribe_user" values = { } values [ "username" ] = user return self . _query ( url , values )


def init_parser():
    """ function to init option parser """
    usage = "usage: %prog -u user -s secret -n name [-l label] \
[-t title] [-c callback] [TEXT]"

    parser = OptionParser(usage, version="%prog " + notifo.__version__)
    parser.add_option("-u", "--user", action="store", dest="user",
                      help="your notifo username")
    parser.add_option("-s", "--secret", action="store", dest="secret",
                      help="your notifo API secret")
    parser.add_option("-n", "--name", action="store", dest="name",
                      help="recipient for the notification")
    parser.add_option("-l", "--label", action="store", dest="label",
                      help="label for the notification")
    parser.add_option("-t", "--title", action="store", dest="title",
                      help="title of the notification")
    parser.add_option("-c", "--callback", action="store", dest="callback",
                      help="callback URL to call")
    parser.add_option("-m", "--message", action="store_true", dest="message",
                      default=False, help="send message instead of notification")

    (options, args) = parser.parse_args()
    return (parser, options, args)


def run_python_module(modulename, args):
    """Run a python module, as though with ``python -m name args...``.

    `modulename` is the name of the module, possibly a dot-separated name.
    `args` is the argument array to present as sys.argv, including the first
    element naming the module being executed.

    """
    openfile = None
    glo, loc = globals(), locals()
    try:
        try:
            # Search for the module - inside its parent package, if any - using
            # standard import mechanics.
            if '.' in modulename:
                packagename, name = rsplit1(modulename, '.')
                package = __import__(packagename, glo, loc, ['__path__'])
                searchpath = package.__path__
            else:
                packagename, name = None, modulename
                searchpath = None  # "top-level search" in imp.find_module()
            openfile, pathname, _ = imp.find_module(name, searchpath)

            # Complain if this is a magic non-file module.
            if openfile is None and pathname is None:
                raise NoSource(
                    "module does not live in a file: %r" % modulename
                    )

            # If `modulename` is actually a package, not a mere module, then we
            # pretend to be Python 2.7 and try running its __main__.py script.
            if openfile is None:
                packagename = modulename
                name = '__main__'
                package = __import__(packagename, glo, loc, ['__path__'])
                searchpath = package.__path__
                openfile, pathname, _ = imp.find_module(name, searchpath)
        except ImportError:
            _, err, _ = sys.exc_info()
            raise NoSource(str(err))
    finally:
        if openfile:
            openfile.close()

    # Finally, hand the file off to run_python_file for execution.
    pathname = os.path.abspath(pathname)
    args[0] = pathname
    run_python_file(pathname, args, package=packagename)

Run a python module as though with python - m name args ... .


def run_python_file(filename, args, package=None):
    """Run a python file as if it were the main program on the command line.

    `filename` is the path to the file to execute, it need not be a .py file.
    `args` is the argument array to present as sys.argv, including the first
    element naming the file being executed.  `package` is the name of the
    enclosing package, if any.

    """
    # Create a module to serve as __main__
    old_main_mod = sys.modules['__main__']
    main_mod = imp.new_module('__main__')
    sys.modules['__main__'] = main_mod
    main_mod.__file__ = filename
    if package:
        main_mod.__package__ = package
    main_mod.__builtins__ = BUILTINS

    # Set sys.argv properly.
    old_argv = sys.argv
    sys.argv = args

    try:
        # Make a code object somehow.
        if filename.endswith(".pyc") or filename.endswith(".pyo"):
            code = make_code_from_pyc(filename)
        else:
            code = make_code_from_py(filename)

        # Execute the code object.
        try:
            exec_code_object(code, main_mod.__dict__)
        except SystemExit:
            # The user called sys.exit().  Just pass it along to the upper
            # layers, where it will be handled.
            raise
        except:
            # Something went wrong while executing the user code.
            # Get the exc_info, and pack them into an exception that we can
            # throw up to the outer loop.  We peel two layers off the traceback
            # so that the coverage.py code doesn't appear in the final printed
            # traceback.
            typ, err, tb = sys.exc_info()
            raise ExceptionDuringRun(typ, err, tb.tb_next.tb_next)
    finally:
        # Restore the old __main__
        sys.modules['__main__'] = old_main_mod

        # Restore the old argv and path
        sys.argv = old_argv

Run a python file as if it were the main program on the command line .


def make_code_from_py(filename):
    """Get source from `filename` and make a code object of it."""
    # Open the source file.
    try:
        source_file = open_source(filename)
    except IOError:
        raise NoSource("No file to run: %r" % filename)

    try:
        source = source_file.read()
    finally:
        source_file.close()

    # We have the source.  `compile` still needs the last line to be clean,
    # so make sure it is, then compile a code object from it.
    if not source or source[-1] != '\n':
        source += '\n'
    code = compile(source, filename, "exec")

    return code

Get source from filename and make a code object of it .


def make_code_from_pyc(filename):
    """Get a code object from a .pyc file."""
    try:
        fpyc = open(filename, "rb")
    except IOError:
        raise NoCode("No file to run: %r" % filename)

    try:
        # First four bytes are a version-specific magic number.  It has to
        # match or we won't run the file.
        magic = fpyc.read(4)
        if magic != imp.get_magic():
            raise NoCode("Bad magic number in .pyc file")

        # Skip the junk in the header that we don't need.
        fpyc.read(4)            # Skip the moddate.
        if sys.version_info >= (3, 3):
            # 3.3 added another long to the header (size), skip it.
            fpyc.read(4)

        # The rest of the file is the code object we want.
        code = marshal.load(fpyc)
    finally:
        fpyc.close()

    return code


def html_tableify(item_matrix, select=None, header=None , footer=None) :
    """ returnr a string for an html table"""
    if not item_matrix :
        return ''
    html_cols = []
    tds = lambda text : u''+text+u'  	'
    trs = lambda text : u'
'+text+u'
'
    tds_items = [map(tds, row) for row in item_matrix]
    if select :
        row, col = select
        tds_items[row][col] = u''\
                +item_matrix[row][col]\
                +u'  	'
    #select the right item
    html_cols = map(trs, (u''.join(row) for row in tds_items))
    head = ''
    foot = ''
    if header :
        head = (u'
'\
            +''.join((u''+header+u'	')*len(item_matrix[0]))\
            +'
')

    if footer :
        foot = (u'
'\
            +''.join((u''+footer+u'	')*len(item_matrix[0]))\
            +'
')
    html = (u'
'+head+(u''.join(html_cols))+foot+u'
')
    return html


def current(self, value):
        """set current cursor position"""
        current = min(max(self._min, value), self._max)

        self._current = current

        if current > self._stop :
            self._stop = current
            self._start = current-self._width
        elif current < self._start :
            self._start = current
            self._stop = current + self._width

        if abs(self._start - self._min) <= self._sticky_lenght :
            self._start = self._min

        if abs(self._stop - self._max) <= self._sticky_lenght :
            self._stop = self._max

def current ( self , value ) : current = min ( max ( self . _min , value ) , self . _max ) self . _current = current if current > self . _stop : self . _stop = current self . _start = current - self . _width elif current < self . _start : self . _start = current self . _stop = current + self . _width if abs ( self . _start - self . _min ) <= self . _sticky_lenght : self . _start = self . _min if abs ( self . _stop - self . _max ) <= self . _sticky_lenght : self . _stop = self . _max


def cancel_completion(self):
        """Cancel the completion

        should be called when the completer have to be dismissed

        This reset internal variable, clearing the temporary buffer
        of the console where the completion are shown.
        """
        self._consecutive_tab = 0
        self._slice_start = 0
        self._console_widget._clear_temporary_buffer()
        self._index = (0, 0)
        if(self._sliding_interval):
            self._sliding_interval = None

def cancel_completion ( self ) : self . _consecutive_tab = 0 self . _slice_start = 0 self . _console_widget . _clear_temporary_buffer ( ) self . _index = ( 0 , 0 ) if ( self . _sliding_interval ) : self . _sliding_interval = None


def _select_index(self, row, col):
        """Change the selection index, and make sure it stays in the right range

        A little more complicated than just dooing modulo the number of row columns
        to be sure to cycle through all element.

        horizontaly, the element are maped like this :
        to r <-- a b c d e f --> to g
        to f <-- g h i j k l --> to m
        to l <-- m n o p q r --> to a

        and vertically
        a d g j m p
        b e h k n q
        c f i l o r
        """

        nr, nc = self._size
        nr = nr-1
        nc = nc-1

        # case 1
        if (row > nr and col >= nc) or (row >= nr and col > nc):
            self._select_index(0, 0)
        # case 2
        elif (row <= 0 and col < 0) or  (row < 0 and col <= 0):
            self._select_index(nr, nc)
        # case 3
        elif row > nr :
            self._select_index(0, col+1)
        # case 4
        elif row < 0 :
            self._select_index(nr, col-1)
        # case 5
        elif col > nc :
            self._select_index(row+1, 0)
        # case 6
        elif col < 0 :
            self._select_index(row-1, nc)
        elif 0 <= row and row <= nr and 0 <= col and col <= nc :
            self._index = (row, col)
        else :
            raise NotImplementedError("you'r trying to go where no completion\
                           have gone before : %d:%d (%d:%d)"%(row, col, nr, nc) )

Change the selection index and make sure it stays in the right range


def select_up(self):
        """move cursor up"""
        r, c = self._index
        self._select_index(r-1, c)

def select_up ( self ) : r , c = self . _index self . _select_index ( r - 1 , c )


def select_down(self):
        """move cursor down"""
        r, c = self._index
        self._select_index(r+1, c)

def select_down ( self ) : r , c = self . _index self . _select_index ( r + 1 , c )


def select_left(self):
        """move cursor left"""
        r, c = self._index
        self._select_index(r, c-1)

def select_left ( self ) : r , c = self . _index self . _select_index ( r , c - 1 )


def select_right(self):
        """move cursor right"""
        r, c = self._index
        self._select_index(r, c+1)

def select_right ( self ) : r , c = self . _index self . _select_index ( r , c + 1 )


def _update_list(self, hilight=True):
        """ update the list of completion and hilight the currently selected completion """
        self._sliding_interval.current = self._index[0]
        head = None
        foot = None
        if self._sliding_interval.start > 0 :
            head = '...'

        if self._sliding_interval.stop < self._sliding_interval._max:
            foot = '...'
        items_m = self._justified_items[\
                       self._sliding_interval.start:\
                       self._sliding_interval.stop+1\
                                       ]

        self._console_widget._clear_temporary_buffer()
        if(hilight):
            sel = (self._sliding_interval.nth, self._index[1])
        else :
            sel = None

        strng = html_tableify(items_m, select=sel, header=head, footer=foot)
        self._console_widget._fill_temporary_buffer(self._old_cursor, strng, html=True)

def _update_list ( self , hilight = True ) : self . _sliding_interval . current = self . _index [ 0 ] head = None foot = None if self . _sliding_interval . start > 0 : head = '...' if self . _sliding_interval . stop < self . _sliding_interval . _max : foot = '...' items_m = self . _justified_items [ self . _sliding_interval . start : self . _sliding_interval . stop + 1 ] self . _console_widget . _clear_temporary_buffer ( ) if ( hilight ) : sel = ( self . _sliding_interval . nth , self . _index [ 1 ] ) else : sel = None strng = html_tableify ( items_m , select = sel , header = head , footer = foot ) self . _console_widget . _fill_temporary_buffer ( self . _old_cursor , strng , html = True )


def wordfreq(text, is_filename=False):
    """Return a dictionary of words and word counts in a string."""
    if is_filename:
        with open(text) as f:
            text = f.read()
    freqs = {}
    for word in text.split():
        lword = word.lower()
        freqs[lword] = freqs.get(lword, 0) + 1
    return freqs

def wordfreq ( text , is_filename = False ) : if is_filename : with open ( text ) as f : text = f . read ( ) freqs = { } for word in text . split ( ) : lword = word . lower ( ) freqs [ lword ] = freqs . get ( lword , 0 ) + 1 return freqs

Return a dictionary of words and word counts in a string .


def print_wordfreq(freqs, n=10):
    """Print the n most common words and counts in the freqs dict."""

    words, counts = freqs.keys(), freqs.values()
    items = zip(counts, words)
    items.sort(reverse=True)
    for (count, word) in items[:n]:
        print(word, count)

def print_wordfreq ( freqs , n = 10 ) : words , counts = freqs . keys ( ) , freqs . values ( ) items = zip ( counts , words ) items . sort ( reverse = True ) for ( count , word ) in items [ : n ] : print ( word , count )

Print the n most common words and counts in the freqs dict .


def tostring(self):
        """Return the string representation of the job description XML."""
        root = self.as_element()
        indent(root)
        txt = ET.tostring(root, encoding="utf-8")
        # Now remove the tokens used to order the attributes.
        txt = re.sub(r'_[A-Z]_','',txt)
        txt = '<?xml version="1.0" encoding="utf-8"?>\n' + txt
        return txt

Return the string representation of the job description XML .


def write(self, filename):
        """Write the XML job description to a file."""
        txt = self.tostring()
        with open(filename, 'w') as f:
            f.write(txt)

def write ( self , filename ) : txt = self . tostring ( ) with open ( filename , 'w' ) as f : f . write ( txt )

Write the XML job description to a file .


def validate_pin(pin):
    """ Validate the given pin against the schema.

    :param dict pin: The pin to validate:
    :raises pypebbleapi.schemas.DocumentError: If the pin is not valid.
    """
    v = _Validator(schemas.pin)
    if v.validate(pin):
        return
    else:
        raise schemas.DocumentError(errors=v.errors)

def validate_pin ( pin ) : v = _Validator ( schemas . pin ) if v . validate ( pin ) : return else : raise schemas . DocumentError ( errors = v . errors )

Validate the given pin against the schema .


def send_shared_pin(self, topics, pin, skip_validation=False):
        """
        Send a shared pin for the given topics.

        :param list topics: The list of topics.
        :param dict pin: The pin.
        :param bool skip_validation: Whether to skip the validation.
        :raises pypebbleapi.schemas.DocumentError: If the validation process failed.
        :raises `requests.exceptions.HTTPError`: If an HTTP error occurred.
        """
        if not self.api_key:
            raise ValueError("You need to specify an api_key.")
        if not skip_validation:
            validate_pin(pin)

        response = _request('PUT',
            url=self.url_v1('/shared/pins/' + pin['id']),
            user_agent=self.user_agent,
            api_key=self.api_key,
            topics_list=topics,
            json=pin,
        )
        _raise_for_status(response)

def
send_shared_pin
(
self
,
topics
,
pin
,
skip_validation
=
False
)
:
if
not
self
.
api_key
:
raise
ValueError
(
"You need to specify an api_key."
)
if
not
skip_validation
:
validate_pin
(
pin
)
response
=
_request
(
'PUT'
,
url
=
self
.
url_v1
(
'/shared/pins/'
+
pin
[
'id'
]
)
,
user_agent
=
self
.
user_agent
,
api_key
=
self
.
api_key
,
topics_list
=
topics
,
json
=
pin
,
)
_raise_for_status
(
response
)

Send a shared pin for the given topics .


def delete_shared_pin(self, pin_id):
        """
        Delete a shared pin.

        :param str pin_id: The id of the pin to delete.
        :raises `requests.exceptions.HTTPError`: If an HTTP error occurred.
        """
        if not self.api_key:
            raise ValueError("You need to specify an api_key.")

        response = _request('DELETE',
            url=self.url_v1('/shared/pins/' + pin_id),
            user_agent=self.user_agent,
            api_key=self.api_key,
        )
        _raise_for_status(response)

def
delete_shared_pin
(
self
,
pin_id
)
:
if
not
self
.
api_key
:
raise
ValueError
(
"You need to specify an api_key."
)
response
=
_request
(
'DELETE'
,
url
=
self
.
url_v1
(
'/shared/pins/'
+
pin_id
)
,
user_agent
=
self
.
user_agent
,
api_key
=
self
.
api_key
,
)
_raise_for_status
(
response
)


def send_user_pin(self, user_token, pin, skip_validation=False):
        """
        Send a user pin.

        :param str user_token: The token of the user.
        :param dict pin: The pin.
        :param bool skip_validation: Whether to skip the validation.
        :raises pypebbleapi.schemas.DocumentError: If the validation process failed.
        :raises `requests.exceptions.HTTPError`: If an HTTP error occurred.
        """
        if not skip_validation:
            validate_pin(pin)

        response = _request('PUT',
            url=self.url_v1('/user/pins/' + pin['id']),
            user_agent=self.user_agent,
            user_token=user_token,
            json=pin,
        )
        _raise_for_status(response)

def send_user_pin ( self , user_token , pin , skip_validation = False ) : if not skip_validation : validate_pin ( pin ) response = _request ( 'PUT' , url = self . url_v1 ( '/user/pins/' + pin [ 'id' ] ) , user_agent = self . user_agent , user_token = user_token , json = pin , ) _raise_for_status ( response )


def delete_user_pin(self, user_token, pin_id):
        """
        Delete a user pin.

        :param str user_token: The token of the user.
        :param str pin_id: The id of the pin to delete.
        :raises `requests.exceptions.HTTPError`: If an HTTP error occurred.
        """

        response = _request('DELETE',
            url=self.url_v1('/user/pins/' + pin_id),
            user_agent=self.user_agent,
            user_token=user_token,
        )
        _raise_for_status(response)

def delete_user_pin ( self , user_token , pin_id ) : response = _request ( 'DELETE' , url = self . url_v1 ( '/user/pins/' + pin_id ) , user_agent = self . user_agent , user_token = user_token , ) _raise_for_status ( response )


def subscribe(self, user_token, topic):
        """
        Subscribe a user to the given topic.

        :param str user_token: The token of the user.
        :param str topic: The topic.
        :raises `requests.exceptions.HTTPError`: If an HTTP error occurred.
        """
        response = _request('POST',
            url=self.url_v1('/user/subscriptions/' + topic),
            user_agent=self.user_agent,
            user_token=user_token,
        )
        _raise_for_status(response)

def subscribe ( self , user_token , topic ) : response = _request ( 'POST' , url = self . url_v1 ( '/user/subscriptions/' + topic ) , user_agent = self . user_agent , user_token = user_token , ) _raise_for_status ( response )


def list_subscriptions(self, user_token):
        """
        Get the list of the topics which a user is subscribed to.

        :param str user_token: The token of the user.
        :return: The list of the topics.
        :rtype: list
        :raises `requests.exceptions.HTTPError`: If an HTTP error occurred.
        """
        response = _request('GET',
            url=self.url_v1('/user/subscriptions'),
            user_agent=self.user_agent,
            user_token=user_token,
        )
        _raise_for_status(response)

        return response.json()['topics']

def list_subscriptions ( self , user_token ) : response = _request ( 'GET' , url = self . url_v1 ( '/user/subscriptions' ) , user_agent = self . user_agent , user_token = user_token , ) _raise_for_status ( response ) return response . json ( ) [ 'topics' ]

Get the list of the topics which a user is subscribed to .


def monitored(total: int, name=None, message=None):
    """
    Decorate a function to automatically begin and end a task on the progressmonitor.
    The function must have a parameter called 'monitor'
    """
    def decorator(f):
        nonlocal name
        monitor_index = list(inspect.signature(f).parameters.keys()).index('monitor')
        if name is None:
            name=f.__name__
        @wraps(f)
        def wrapper(*args, **kargs):
            if len(args) > monitor_index:
                monitor = args[monitor_index]
            elif 'monitor' in kargs:
                monitor = kargs['monitor']
            else:
                monitor = kargs['monitor'] = NullMonitor()
            with monitor.task(total, name, message):
                f(*args, **kargs)
        return wrapper
    return decorator

def monitored ( total : int , name = None , message = None ) : def decorator ( f ) : nonlocal name monitor_index = list ( inspect . signature ( f ) . parameters . keys ( ) ) . index ( 'monitor' ) if name is None : name = f . __name__ @ wraps ( f ) def wrapper ( * args , * * kargs ) : if len ( args ) > monitor_index : monitor = args [ monitor_index ] elif 'monitor' in kargs : monitor = kargs [ 'monitor' ] else : monitor = kargs [ 'monitor' ] = NullMonitor ( ) with monitor . task ( total , name , message ) : f ( * args , * * kargs ) return wrapper return decorator

Decorate a function to automatically begin and end a task on the progressmonitor . The function must have a parameter called monitor


def begin(self, total: int, name=None, message=None):
        """Call before starting work on a monitor, specifying name and amount of work"""
        self.total = total
        message = message or name or "Working..."
        self.name = name or "ProgressMonitor"
        self.update(0, message)

def begin ( self , total : int , name = None , message = None ) : self . total = total message = message or name or "Working..." self . name = name or "ProgressMonitor" self . update ( 0 , message )

Call before starting work on a monitor specifying name and amount of work


def task(self, total: int, name=None, message=None):
        """Wrap code into a begin and end call on this monitor"""
        self.begin(total, name, message)
        try:
            yield self
        finally:
            self.done()

def task ( self , total : int , name = None , message = None ) : self . begin ( total , name , message ) try : yield self finally : self . done ( )


def subtask(self, units: int):
        """Create a submonitor with the given units"""
        sm = self.submonitor(units)
        try:
            yield sm
        finally:
            if sm.total is None:
                # begin was never called, so the subtask cannot be closed
                self.update(units)
            else:
                sm.done()


def update(self, units: int=1, message: str=None):
        """Increment the monitor with N units worked and an optional message"""
        if self.total is None:
            raise Exception("Cannot call progressmonitor.update before calling begin")
        self.worked = min(self.total, self.worked+units)
        if message:
            self.message = message
        for listener in self.listeners:
            listener(self)


def submonitor(self, units: int, *args, **kargs) -> 'ProgressMonitor':
        """
        Create a sub monitor that stands for N units of work in this monitor
        The sub task should call .begin (or use @monitored / with .task) before calling updates
        """
        submonitor = ProgressMonitor(*args, **kargs)
        self.sub_monitors[submonitor] = units
        submonitor.add_listener(self._submonitor_update)
        return submonitor

def submonitor ( self , units : int , * args , * * kargs ) -> 'ProgressMonitor' : submonitor = ProgressMonitor ( * args , * * kargs ) self . sub_monitors [ submonitor ] = units submonitor . add_listener ( self . _submonitor_update ) return submonitor

Create a sub monitor that stands for N units of work in this monitor The sub task should call . begin ( or use


def done(self, message: str=None):
        """
        Signal that this task is done.
        This is completely optional and will just call .update with the remaining work.
        """
        if message is None:
            message = "{self.name} done".format(**locals()) if self.name else "Done"
        self.update(units=self.total - self.worked, message=message)

def
done
(
self
,
message
:
str
=
)
:
if
message
is
:
message
=
"{self.name} done"
.
format
(
*
*
locals
(
)
)
if
self
.
name
else
"Done"
self
.
update
(
units
=
self
.
total
self
.
worked
,
message
=
message
)

Signal that this task is done . This is completely optional and will just call . update with the remaining work .


def page(strng, start=0, screen_lines=0, pager_cmd=None,
         html=None, auto_html=False):
    """Print a string, piping through a pager.

    This version ignores the screen_lines and pager_cmd arguments and uses
    IPython's payload system instead.

    Parameters
    ----------
    strng : str
      Text to page.

    start : int
      Starting line at which to place the display.

    html : str, optional
      If given, an html string to send as well.

    auto_html : bool, optional
      If true, the input string is assumed to be valid reStructuredText and is
      converted to HTML with docutils.  Note that if docutils is not found,
      this option is silently ignored.

    Note
    ----

    Only one of the ``html`` and ``auto_html`` options can be given, not
    both.
    """

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

    if auto_html:
        try:
            # These defaults ensure user configuration variables for docutils
            # are not loaded, only our config is used here.
            defaults = {'file_insertion_enabled': 0,
                        'raw_enabled': 0,
                        '_disable_config': 1}
            html = publish_string(strng, writer_name='html',
                                  settings_overrides=defaults)
        except:
            pass

    payload = dict(
        source='IPython.zmq.page.page',
        text=strng,
        html=html,
        start_line_number=start
        )
    shell.payload_manager.write_payload(payload)


def correct_build_location(self):
        """If the build location was a temporary directory, this will move it
        to a new more permanent location"""
        if self.source_dir is not None:
            return
        assert self.req is not None
        assert self._temp_build_dir
        old_location = self._temp_build_dir
        new_build_dir = self._ideal_build_dir
        del self._ideal_build_dir
        if self.editable:
            name = self.name.lower()
        else:
            name = self.name
        new_location = os.path.join(new_build_dir, name)
        if not os.path.exists(new_build_dir):
            logger.debug('Creating directory %s', new_build_dir)
            _make_build_dir(new_build_dir)
        if os.path.exists(new_location):
            raise InstallationError(
                'A package already exists in %s; please remove it to continue'
                % display_path(new_location))
        logger.debug(
            'Moving package %s from %s to new location %s',
            self, display_path(old_location), display_path(new_location),
        )
        shutil.move(old_location, new_location)
        self._temp_build_dir = new_location
        self.source_dir = new_location
        self._egg_info_path = None

If the build location was a temporary directory this will move it to a new more permanent location


def load_pyconfig_files(config_files, path):
    """Load multiple Python config files, merging each of them in turn.

    Parameters
    ==========
    config_files : list of str
        List of config files names to load and merge into the config.
    path : unicode
        The full path to the location of the config files.
    """
    config = Config()
    for cf in config_files:
        loader = PyFileConfigLoader(cf, path=path)
        try:
            next_config = loader.load_config()
        except ConfigFileNotFound:
            pass
        except:
            raise
        else:
            config._merge(next_config)
    return config

def load_pyconfig_files ( config_files , path ) : config = Config ( ) for cf in config_files : loader = PyFileConfigLoader ( cf , path = path ) try : next_config = loader . load_config ( ) except ConfigFileNotFound : pass except : raise else : config . _merge ( next_config ) return config

Load multiple Python config files merging each of them in turn .


def load_config(self):
        """Load the config from a file and return it as a Struct."""
        self.clear()
        try:
            self._find_file()
        except IOError as e:
            raise ConfigFileNotFound(str(e))
        self._read_file_as_dict()
        self._convert_to_config()
        return self.config

def load_config ( self ) : self . clear ( ) try : self . _find_file ( ) except IOError as e : raise ConfigFileNotFound ( str ( e ) ) self . _read_file_as_dict ( ) self . _convert_to_config ( ) return self . config

Load the config from a file and return it as a Struct .


def _read_file_as_dict(self):
        """Load the config file into self.config, with recursive loading."""
        # This closure is made available in the namespace that is used
        # to exec the config file.  It allows users to call
        # load_subconfig('myconfig.py') to load config files recursively.
        # It needs to be a closure because it has references to self.path
        # and self.config.  The sub-config is loaded with the same path
        # as the parent, but it uses an empty config which is then merged
        # with the parents.

        # If a profile is specified, the config file will be loaded
        # from that profile

        def load_subconfig(fname, profile=None):
            # import here to prevent circular imports
            from IPython.core.profiledir import ProfileDir, ProfileDirError
            if profile is not None:
                try:
                    profile_dir = ProfileDir.find_profile_dir_by_name(
                            get_ipython_dir(),
                            profile,
                    )
                except ProfileDirError:
                    return
                path = profile_dir.location
            else:
                path = self.path
            loader = PyFileConfigLoader(fname, path)
            try:
                sub_config = loader.load_config()
            except ConfigFileNotFound:
                # Pass silently if the sub config is not there. This happens
                # when a user s using a profile, but not the default config.
                pass
            else:
                self.config._merge(sub_config)

        # Again, this needs to be a closure and should be used in config
        # files to get the config being loaded.
        def get_config():
            return self.config

        namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
        fs_encoding = sys.getfilesystemencoding() or 'ascii'
        conf_filename = self.full_filename.encode(fs_encoding)
        py3compat.execfile(conf_filename, namespace)

Load the config file into self . config with recursive loading .


def _load_flag(self, cfg):
        """update self.config from a flag, which can be a dict or Config"""
        if isinstance(cfg, (dict, Config)):
            # don't clobber whole config sections, update
            # each section from config:
            for sec,c in cfg.iteritems():
                self.config[sec].update(c)
        else:
            raise TypeError("Invalid flag: %r" % cfg)

update self . config from a flag which can be a dict or Config


def _decode_argv(self, argv, enc=None):
        """decode argv if bytes, using stin.encoding, falling back on default enc"""
        uargv = []
        if enc is None:
            enc = DEFAULT_ENCODING
        for arg in argv:
            if not isinstance(arg, unicode):
                # only decode if not already decoded
                arg = arg.decode(enc)
            uargv.append(arg)
        return uargv

def
_decode_argv
(
self
,
argv
,
enc
=
)
:
uargv
=
[
]
if
enc
is
:
enc
=
DEFAULT_ENCODING
for
arg
in
argv
:
if
not
isinstance
(
arg
,
unicode
)
:
# only decode if not already decoded
arg
=
arg
.
decode
(
enc
)
uargv
.
append
(
arg
)
return
uargv

decode argv if bytes using stin . encoding falling back on default enc


def load_config(self, argv=None, aliases=None, flags=None):
        """Parse the configuration and generate the Config object.

        After loading, any arguments that are not key-value or
        flags will be stored in self.extra_args - a list of
        unparsed command-line arguments.  This is used for
        arguments such as input files or subcommands.

        Parameters
        ----------
        argv : list, optional
            A list that has the form of sys.argv[1:] which has unicode
            elements of the form u"key=value". If this is None (default),
            then self.argv will be used.
        aliases : dict
            A dict of aliases for configurable traits.
            Keys are the short aliases, Values are the resolved trait.
            Of the form: `{'alias' : 'Configurable.trait'}`
        flags : dict
            A dict of flags, keyed by str name. Values can be Config objects
            or dicts.  When the flag is triggered, The config is loaded as
            `self.config.update(cfg)`.
        """
        from IPython.config.configurable import Configurable

        self.clear()
        if argv is None:
            argv = self.argv
        if aliases is None:
            aliases = self.aliases
        if flags is None:
            flags = self.flags

        # ensure argv is a list of unicode strings:
        uargv = self._decode_argv(argv)
        for idx,raw in enumerate(uargv):
            # strip leading '-'
            item = raw.lstrip('-')

            if raw == '--':
                # don't parse arguments after '--'
                # this is useful for relaying arguments to scripts, e.g.
                # ipython -i foo.py --pylab=qt -- args after '--' go-to-foo.py
                self.extra_args.extend(uargv[idx+1:])
                break

            if kv_pattern.match(raw):
                lhs,rhs = item.split('=',1)
                # Substitute longnames for aliases.
                if lhs in aliases:
                    lhs = aliases[lhs]
                if '.' not in lhs:
                    # probably a mistyped alias, but not technically illegal
                    warn.warn("Unrecognized alias: '%s', it will probably have no effect."%lhs)
                try:
                    self._exec_config_str(lhs, rhs)
                except Exception:
                    raise ArgumentError("Invalid argument: '%s'" % raw)

            elif flag_pattern.match(raw):
                if item in flags:
                    cfg,help = flags[item]
                    self._load_flag(cfg)
                else:
                    raise ArgumentError("Unrecognized flag: '%s'"%raw)
            elif raw.startswith('-'):
                kv = '--'+item
                if kv_pattern.match(kv):
                    raise ArgumentError("Invalid argument: '%s', did you mean '%s'?"%(raw, kv))
                else:
                    raise ArgumentError("Invalid argument: '%s'"%raw)
            else:
                # keep all args that aren't valid in a list,
                # in case our parent knows what to do with them.
                self.extra_args.append(item)
        return self.config

Parse the configuration and generate the Config object .


def load_config(self, argv=None, aliases=None, flags=None):
        """Parse command line arguments and return as a Config object.

        Parameters
        ----------

        args : optional, list
          If given, a list with the structure of sys.argv[1:] to parse
          arguments from. If not given, the instance's self.argv attribute
          (given at construction time) is used."""
        self.clear()
        if argv is None:
            argv = self.argv
        if aliases is None:
            aliases = self.aliases
        if flags is None:
            flags = self.flags
        self._create_parser(aliases, flags)
        self._parse_args(argv)
        self._convert_to_config()
        return self.config

def load_config ( self , argv = None , aliases = None , flags = None ) : self . clear ( ) if argv is None : argv = self . argv if aliases is None : aliases = self . aliases if flags is None : flags = self . flags self . _create_parser ( aliases , flags ) self . _parse_args ( argv ) self . _convert_to_config ( ) return self . config

Parse command line arguments and return as a Config object .


def _parse_args(self, args):
        """self.parser->self.parsed_data"""
        # decode sys.argv to support unicode command-line options
        enc = DEFAULT_ENCODING
        uargs = [py3compat.cast_unicode(a, enc) for a in args]
        self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)


def _convert_to_config(self):
        """self.parsed_data->self.config, parse unrecognized extra args via KVLoader."""
        # remove subconfigs list from namespace before transforming the Namespace
        if '_flags' in self.parsed_data:
            subcs = self.parsed_data._flags
            del self.parsed_data._flags
        else:
            subcs = []

        for k, v in vars(self.parsed_data).iteritems():
            if v is None:
                # it was a flag that shares the name of an alias
                subcs.append(self.alias_flags[k])
            else:
                # eval the KV assignment
                self._exec_config_str(k, v)

        for subc in subcs:
            self._load_flag(subc)

        if self.extra_args:
            sub_parser = KeyValueConfigLoader()
            sub_parser.load_config(self.extra_args)
            self.config._merge(sub_parser.config)
            self.extra_args = sub_parser.extra_args

self . parsed_data - > self . config parse unrecognized extra args via KVLoader .


def find_module(name, path=None):
    """imp.find_module variant that only return path of module.

    The `imp.find_module` returns a filehandle that we are not interested in.
    Also we ignore any bytecode files that `imp.find_module` finds.

    Parameters
    ----------
    name : str
        name of module to locate
    path : list of str
        list of paths to search for `name`. If path=None then search sys.path

    Returns
    -------
    filename : str
        Return full path of module or None if module is missing or does not have
        .py or .pyw extension
    """
    if name is None:
        return None
    try:
        file, filename, _ = imp.find_module(name, path)
    except ImportError:
        return None
    if file is None:
        return filename
    else:
        file.close()
    if os.path.splitext(filename)[1] in [".py", "pyc"]:
        return filename
    else:
        return None

def find_module ( name , path = None ) : if name is None : return None try : file , filename , _ = imp . find_module ( name , path ) except ImportError : return None if file is None : return filename else : file . close ( ) if os . path . splitext ( filename ) [ 1 ] in [ ".py" , "pyc" ] : return filename else : return None

imp . find_module variant that only return path of module . The imp . find_module returns a filehandle that we are not interested in . Also we ignore any bytecode files that imp . find_module finds .


def on_stop(self, f):
        """Register a callback to be called with this Launcher's stop_data
        when the process actually finishes.
        """
        if self.state=='after':
            return f(self.stop_data)
        else:
            self.stop_callbacks.append(f)

def on_stop ( self , f ) : if self . state == 'after' : return f ( self . stop_data ) else : self . stop_callbacks . append ( f )

Register a callback to be called with this Launcher s stop_data when the process actually finishes .


def notify_start(self, data):
        """Call this to trigger startup actions.

        This logs the process startup and sets the state to 'running'.  It is
        a pass-through so it can be used as a callback.
        """

        self.log.debug('Process %r started: %r', self.args[0], data)
        self.start_data = data
        self.state = 'running'
        return data

def
notify_start
(
self
,
data
)
:
self
.
log
.
debug
(
'Process %r started: %r'
,
self
.
args
[
0
]
,
data
)
self
.
start_data
=
data
self
.
state
=
'running'
return
data


def notify_stop(self, data):
        """Call this to trigger process stop actions.

        This logs the process stopping and sets the state to 'after'. Call
        this to trigger callbacks registered via :meth:`on_stop`."""

        self.log.debug('Process %r stopped: %r', self.args[0], data)
        self.stop_data = data
        self.state = 'after'
        for i in range(len(self.stop_callbacks)):
            d = self.stop_callbacks.pop()
            d(data)
        return data

def
notify_stop
(
self
,
data
)
:
self
.
log
.
debug
(
'Process %r stopped: %r'
,
self
.
args
[
0
]
,
data
)
self
.
stop_data
=
data
self
.
state
=
'after'
for
i
in
range
(
len
(
self
.
stop_callbacks
)
)
:
d
=
self
.
stop_callbacks
.
pop
(
)
d
(
data
)
return
data

Call this to trigger process stop actions .


def interrupt_then_kill(self, delay=2.0):
        """Send INT, wait a delay and then send KILL."""
        try:
            self.signal(SIGINT)
        except Exception:
            self.log.debug("interrupt failed")
            pass
        self.killer  = ioloop.DelayedCallback(lambda : self.signal(SIGKILL), delay*1000, self.loop)
        self.killer.start()

def
interrupt_then_kill
(
self
,
delay
=
2.0
)
:
try
:
self
.
signal
(
SIGINT
)
except
Exception
:
self
.
log
.
debug
(
"interrupt failed"
)
pass
self
.
killer
=
ioloop
.
DelayedCallback
(
lambda
:
self
.
signal
(
SIGKILL
)
,
delay
*
1000
,
self
.
loop
)
self
.
killer
.
start
(
)

Send INT wait a delay and then send KILL .


def find_args(self):
        """Build self.args using all the fields."""
        return self.mpi_cmd + ['-n', str(self.n)] + self.mpi_args + \
               self.program + self.program_args

def find_args ( self ) : return self . mpi_cmd + [ '-n' , str ( self . n ) ] + self . mpi_args + self . program + self . program_args

Build self . args using all the fields .


def start(self, n):
        """Start n instances of the program using mpiexec."""
        self.n = n
        return super(MPILauncher, self).start()

def start ( self , n ) : self . n = n return super ( MPILauncher , self ) . start ( )

Start n instances of the program using mpiexec .


def _send_file(self, local, remote):
        """send a single file"""
        remote = "%s:%s" % (self.location, remote)
        for i in range(10):
            if not os.path.exists(local):
                self.log.debug("waiting for %s" % local)
                time.sleep(1)
            else:
                break
        self.log.info("sending %s to %s", local, remote)
        check_output(self.scp_cmd + [local, remote])

def
_send_file
(
self
,
local
,
remote
)
:
remote
=
"%s:%s"
%
(
self
.
location
,
remote
)
for
i
in
range
(
10
)
:
if
not
os
.
path
.
exists
(
local
)
:
self
.
log
.
debug
(
"waiting for %s"
%
local
)
time
.
sleep
(
1
)
else
:
break
self
.
log
.
info
(
"sending %s to %s"
,
local
,
remote
)
check_output
(
self
.
scp_cmd
+
[
local
,
remote
]
)


def _fetch_file(self, remote, local):
        """fetch a single file"""
        full_remote = "%s:%s" % (self.location, remote)
        self.log.info("fetching %s from %s", local, full_remote)
        for i in range(10):
            # wait up to 10s for remote file to exist
            check = check_output(self.ssh_cmd + self.ssh_args + \
                [self.location, 'test -e', remote, "&& echo 'yes' || echo 'no'"])
            check = check.strip()
            if check == 'no':
                time.sleep(1)
            elif check == 'yes':
                break
        check_output(self.scp_cmd + [full_remote, local])


def engine_count(self):
        """determine engine count from `engines` dict"""
        count = 0
        for n in self.engines.itervalues():
            if isinstance(n, (tuple,list)):
                n,args = n
            count += n
        return count

def engine_count ( self ) : count = 0 for n in self . engines . itervalues ( ) : if isinstance ( n , ( tuple , list ) ) : n , args = n count += n return count

determine engine count from engines dict


def start(self, n):
        """Start engines by profile or profile_dir.
        `n` is ignored, and the `engines` config property is used instead.
        """

        dlist = []
        for host, n in self.engines.iteritems():
            if isinstance(n, (tuple, list)):
                n, args = n
            else:
                args = copy.deepcopy(self.engine_args)

            if '@' in host:
                user,host = host.split('@',1)
            else:
                user=None
            for i in range(n):
                if i > 0:
                    time.sleep(self.delay)
                el = self.launcher_class(work_dir=self.work_dir, config=self.config, log=self.log,
                                        profile_dir=self.profile_dir, cluster_id=self.cluster_id,
                )
                if i > 0:
                    # only send files for the first engine on each host
                    el.to_send = []

                # Copy the engine args over to each engine launcher.
                el.engine_cmd = self.engine_cmd
                el.engine_args = args
                el.on_stop(self._notice_engine_stopped)
                d = el.start(user=user, hostname=host)
                self.launchers[ "%s/%i" % (host,i) ] = el
                dlist.append(d)
        self.notify_start(dlist)
        return dlist

Start engines by profile or profile_dir . n is ignored and the engines config property is used instead .


def start(self, n):
        """Start n copies of the process using the Win HPC job scheduler."""
        self.write_job_file(n)
        args = [
            'submit',
            '/jobfile:%s' % self.job_file,
            '/scheduler:%s' % self.scheduler
        ]
        self.log.debug("Starting Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))

        output = check_output([self.job_cmd]+args,
            env=os.environ,
            cwd=self.work_dir,
            stderr=STDOUT
        )
        job_id = self.parse_job_id(output)
        self.notify_start(job_id)
        return job_id

def
start
(
self
,
n
)
:
self
.
write_job_file
(
n
)
args
=
[
'submit'
,
'/jobfile:%s'
%
self
.
job_file
,
'/scheduler:%s'
%
self
.
scheduler
]
self
.
log
.
debug
(
"Starting Win HPC Job: %s"
%
(
self
.
job_cmd
+
' '
+
' '
.
join
(
args
)
,
)
)
output
=
check_output
(
[
self
.
job_cmd
]
+
args
,
env
=
os
.
environ
,
cwd
=
self
.
work_dir
,
stderr
=
STDOUT
)
job_id
=
self
.
parse_job_id
(
output
)
self
.
notify_start
(
job_id
)
return
job_id

Start n copies of the process using the Win HPC job scheduler .


def _context_default(self):
        """load the default context with the default values for the basic keys

        because the _trait_changed methods only load the context if they
        are set to something other than the default value.
        """
        return dict(n=1, queue=u'', profile_dir=u'', cluster_id=u'')

def _context_default ( self ) : return dict ( n = 1 , queue = u'' , profile_dir = u'' , cluster_id = u'' )


def parse_job_id(self, output):
        """Take the output of the submit command and return the job id."""
        m = self.job_id_regexp.search(output)
        if m is not None:
            job_id = m.group()
        else:
            raise LauncherError("Job id couldn't be determined: %s" % output)
        self.job_id = job_id
        self.log.info('Job submitted with job id: %r', job_id)
        return job_id

def
parse_job_id
(
self
,
output
)
:
m
=
self
.
job_id_regexp
.
search
(
output
)
if
m
is
not
:
job_id
=
m
.
group
(
)
else
:
raise
LauncherError
(
"Job id couldn't be determined: %s"
%
output
)
self
.
job_id
=
job_id
self
.
log
.
info
(
'Job submitted with job id: %r'
,
job_id
)
return
job_id

Take the output of the submit command and return the job id .


def write_batch_script(self, n):
        """Instantiate and write the batch script to the work_dir."""
        self.n = n
        # first priority is batch_template if set
        if self.batch_template_file and not self.batch_template:
            # second priority is batch_template_file
            with open(self.batch_template_file) as f:
                self.batch_template = f.read()
        if not self.batch_template:
            # third (last) priority is default_template
            self.batch_template = self.default_template

            # add jobarray or queue lines to user-specified template
            # note that this is *only* when user did not specify a template.
            # print self.job_array_regexp.search(self.batch_template)
            if not self.job_array_regexp.search(self.batch_template):
                self.log.debug("adding job array settings to batch script")
                firstline, rest = self.batch_template.split('\n',1)
                self.batch_template = u'\n'.join([firstline, self.job_array_template, rest])

            # print self.queue_regexp.search(self.batch_template)
            if self.queue and not self.queue_regexp.search(self.batch_template):
                self.log.debug("adding PBS queue settings to batch script")
                firstline, rest = self.batch_template.split('\n',1)
                self.batch_template = u'\n'.join([firstline, self.queue_template, rest])

        script_as_string = self.formatter.format(self.batch_template, **self.context)
        self.log.debug('Writing batch script: %s', self.batch_file)

        with open(self.batch_file, 'w') as f:
            f.write(script_as_string)
        os.chmod(self.batch_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)

Instantiate and write the batch script to the work_dir .


def start(self, n):
        """Start n copies of the process using a batch system."""
        self.log.debug("Starting %s: %r", self.__class__.__name__, self.args)
        # Here we save profile_dir in the context so they
        # can be used in the batch script template as {profile_dir}
        self.write_batch_script(n)
        output = check_output(self.args, env=os.environ)

        job_id = self.parse_job_id(output)
        self.notify_start(job_id)
        return job_id

Start n copies of the process using a batch system .


def _context_menu_make(self, pos):
        """ Reimplemented to return a custom context menu for images.
        """
        format = self._control.cursorForPosition(pos).charFormat()
        name = format.stringProperty(QtGui.QTextFormat.ImageName)
        if name:
            menu = QtGui.QMenu()

            menu.addAction('Copy Image', lambda: self._copy_image(name))
            menu.addAction('Save Image As...', lambda: self._save_image(name))
            menu.addSeparator()

            svg = self._name_to_svg_map.get(name, None)
            if svg is not None:
                menu.addSeparator()
                menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
                menu.addAction('Save SVG As...',
                               lambda: save_svg(svg, self._control))
        else:
            menu = super(RichIPythonWidget, self)._context_menu_make(pos)
        return menu

def
_context_menu_make
(
self
,
pos
)
:
format
=
self
.
_control
.
cursorForPosition
(
pos
)
.
charFormat
(
)
name
=
format
.
stringProperty
(
QtGui
.
QTextFormat
.
ImageName
)
if
name
:
menu
=
QtGui
.
QMenu
(
)
menu
.
addAction
(
'Copy Image'
,
lambda
:
self
.
_copy_image
(
name
)
)
menu
.
addAction
(
'Save Image As...'
,
lambda
:
self
.
_save_image
(
name
)
)
menu
.
addSeparator
(
)
svg
=
self
.
_name_to_svg_map
.
get
(
name
,
)
if
svg
is
not
:
menu
.
addSeparator
(
)
menu
.
addAction
(
'Copy SVG'
,
lambda
:
svg_to_clipboard
(
svg
)
)
menu
.
addAction
(
'Save SVG As...'
,
lambda
:
save_svg
(
svg
,
self
.
_control
)
)
else
:
menu
=
super
(
RichIPythonWidget
,
self
)
.
_context_menu_make
(
pos
)
return
menu

Reimplemented to return a custom context menu for images .


def _append_jpg(self, jpg, before_prompt=False):
        """ Append raw JPG data to the widget."""
        self._append_custom(self._insert_jpg, jpg, before_prompt)

def _append_jpg ( self , jpg , before_prompt = False ) : self . _append_custom ( self . _insert_jpg , jpg , before_prompt )


def _append_png(self, png, before_prompt=False):
        """ Append raw PNG data to the widget.
        """
        self._append_custom(self._insert_png, png, before_prompt)

def _append_png ( self , png , before_prompt = False ) : self . _append_custom ( self . _insert_png , png , before_prompt )


def _append_svg(self, svg, before_prompt=False):
        """ Append raw SVG data to the widget.
        """
        self._append_custom(self._insert_svg, svg, before_prompt)

def _append_svg ( self , svg , before_prompt = False ) : self . _append_custom ( self . _insert_svg , svg , before_prompt )


def _add_image(self, image):
        """ Adds the specified QImage to the document and returns a
            QTextImageFormat that references it.
        """
        document = self._control.document()
        name = str(image.cacheKey())
        document.addResource(QtGui.QTextDocument.ImageResource,
                             QtCore.QUrl(name), image)
        format = QtGui.QTextImageFormat()
        format.setName(name)
        return format

def _add_image ( self , image ) : document = self . _control . document ( ) name = str ( image . cacheKey ( ) ) document . addResource ( QtGui . QTextDocument . ImageResource , QtCore . QUrl ( name ) , image ) format = QtGui . QTextImageFormat ( ) format . setName ( name ) return format

Adds the specified QImage to the document and returns a QTextImageFormat that references it .


def _copy_image(self, name):
        """ Copies the ImageResource with 'name' to the clipboard.
        """
        image = self._get_image(name)
        QtGui.QApplication.clipboard().setImage(image)

def _copy_image ( self , name ) : image = self . _get_image ( name ) QtGui . QApplication . clipboard ( ) . setImage ( image )

Copies the ImageResource with name to the clipboard .


def _get_image(self, name):
        """ Returns the QImage stored as the ImageResource with 'name'.
        """
        document = self._control.document()
        image = document.resource(QtGui.QTextDocument.ImageResource,
                                  QtCore.QUrl(name))
        return image

def _get_image ( self , name ) : document = self . _control . document ( ) image = document . resource ( QtGui . QTextDocument . ImageResource , QtCore . QUrl ( name ) ) return image

Returns the QImage stored as the ImageResource with name .


def _insert_img(self, cursor, img, fmt):
        """ insert a raw image, jpg or png """
        try:
            image = QtGui.QImage()
            image.loadFromData(img, fmt.upper())
        except ValueError:
            self._insert_plain_text(cursor, 'Received invalid %s data.'%fmt)
        else:
            format = self._add_image(image)
            cursor.insertBlock()
            cursor.insertImage(format)
            cursor.insertBlock()

def
_insert_img
(
self
,
cursor
,
img
,
fmt
)
:
try
:
image
=
QtGui
.
QImage
(
)
image
.
loadFromData
(
img
,
fmt
.
upper
(
)
)
except
ValueError
:
self
.
_insert_plain_text
(
cursor
,
'Received invalid %s data.'
%
fmt
)
else
:
format
=
self
.
_add_image
(
image
)
cursor
.
insertBlock
(
)
cursor
.
insertImage
(
format
)
cursor
.
insertBlock
(
)


def _insert_svg(self, cursor, svg):
        """ Insert raw SVG data into the widet.
        """
        try:
            image = svg_to_image(svg)
        except ValueError:
            self._insert_plain_text(cursor, 'Received invalid SVG data.')
        else:
            format = self._add_image(image)
            self._name_to_svg_map[format.name()] = svg
            cursor.insertBlock()
            cursor.insertImage(format)
            cursor.insertBlock()

def
_insert_svg
(
self
,
cursor
,
svg
)
:
try
:
image
=
svg_to_image
(
svg
)
except
ValueError
:
self
.
_insert_plain_text
(
cursor
,
'Received invalid SVG data.'
)
else
:
format
=
self
.
_add_image
(
image
)
self
.
_name_to_svg_map
[
format
.
name
(
)
]
=
svg
cursor
.
insertBlock
(
)
cursor
.
insertImage
(
format
)
cursor
.
insertBlock
(
)


def _save_image(self, name, format='PNG'):
        """ Shows a save dialog for the ImageResource with 'name'.
        """
        dialog = QtGui.QFileDialog(self._control, 'Save Image')
        dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        dialog.setDefaultSuffix(format.lower())
        dialog.setNameFilter('%s file (*.%s)' % (format, format.lower()))
        if dialog.exec_():
            filename = dialog.selectedFiles()[0]
            image = self._get_image(name)
            image.save(filename, format)

def
_save_image
(
self
,
name
,
format
=
'PNG'
)
:
dialog
=
QtGui
.
QFileDialog
(
self
.
_control
,
'Save Image'
)
dialog
.
setAcceptMode
(
QtGui
.
QFileDialog
.
AcceptSave
)
dialog
.
setDefaultSuffix
(
format
.
lower
(
)
)
dialog
.
setNameFilter
(
'%s file (*.%s)'
%
(
format
,
format
.
lower
(
)
)
)
if
dialog
.
exec_
(
)
:
filename
=
dialog
.
selectedFiles
(
)
[
0
]
image
=
self
.
_get_image
(
name
)
image
.
save
(
filename
,
format
)

Shows a save dialog for the ImageResource with name .


def _exit_now_changed(self, name, old, new):
        """stop eventloop when exit_now fires"""
        if new:
            loop = ioloop.IOLoop.instance()
            loop.add_timeout(time.time()+0.1, loop.stop)

def _exit_now_changed ( self , name , old , new ) : if new : loop = ioloop . IOLoop . instance ( ) loop . add_timeout ( time . time ( ) + 0.1 , loop . stop )


def init_environment(self):
        """Configure the user's environment.

        """
        env = os.environ
        # These two ensure 'ls' produces nice coloring on BSD-derived systems
        env['TERM'] = 'xterm-color'
        env['CLICOLOR'] = '1'
        # Since normal pagers don't work at all (over pexpect we don't have
        # single-key control of the subprocess), try to disable paging in
        # subprocesses as much as possible.
        env['PAGER'] = 'cat'
        env['GIT_PAGER'] = 'cat'

        # And install the payload version of page.
        install_payload_page()


def auto_rewrite_input(self, cmd):
        """Called to show the auto-rewritten input for autocall and friends.

        FIXME: this payload is currently not correctly processed by the
        frontend.
        """
        new = self.prompt_manager.render('rewrite') + cmd
        payload = dict(
            source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input',
            transformed_input=new,
            )
        self.payload_manager.write_payload(payload)

Called to show the auto - rewritten input for autocall and friends .


def ask_exit(self):
        """Engage the exit actions."""
        self.exit_now = True
        payload = dict(
            source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit',
            exit=True,
            keepkernel=self.keepkernel_on_exit,
            )
        self.payload_manager.write_payload(payload)


def set_next_input(self, text):
        """Send the specified text to the frontend to be presented at the next
        input cell."""
        payload = dict(
            source='IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input',
            text=text
        )
        self.payload_manager.write_payload(payload)

Send the specified text to the frontend to be presented at the next input cell .


def read(self, filename):
        """Read a filename as UTF-8 configuration data."""
        kwargs = {}
        if sys.version_info >= (3, 2):
            kwargs['encoding'] = "utf-8"
        return configparser.RawConfigParser.read(self, filename, **kwargs)

def read ( self , filename ) : kwargs = { } if sys . version_info >= ( 3 , 2 ) : kwargs [ 'encoding' ] = "utf-8" return configparser . RawConfigParser . read ( self , filename , * * kwargs )

Read a filename as UTF - 8 configuration data .


def getlist(self, section, option):
        """Read a list of strings.

        The value of `section` and `option` is treated as a comma- and newline-
        separated list of strings.  Each value is stripped of whitespace.

        Returns the list of strings.

        """
        value_list = self.get(section, option)
        values = []
        for value_line in value_list.split('\n'):
            for value in value_line.split(','):
                value = value.strip()
                if value:
                    values.append(value)
        return values

def getlist ( self , section , option ) : value_list = self . get ( section , option ) values = [ ] for value_line in value_list . split ( '\n' ) : for value in value_line . split ( ',' ) : value = value . strip ( ) if value : values . append ( value ) return values


def getlinelist(self, section, option):
        """Read a list of full-line strings.

        The value of `section` and `option` is treated as a newline-separated
        list of strings.  Each value is stripped of whitespace.

        Returns the list of strings.

        """
        value_list = self.get(section, option)
        return list(filter(None, value_list.split('\n')))

def getlinelist ( self , section , option ) : value_list = self . get ( section , option ) return list ( filter ( None , value_list . split ( '\n' ) ) )


def from_environment(self, env_var):
        """Read configuration from the `env_var` environment variable."""
        # Timidity: for nose users, read an environment variable.  This is a
        # cheap hack, since the rest of the command line arguments aren't
        # recognized, but it solves some users' problems.
        env = os.environ.get(env_var, '')
        if env:
            self.timid = ('--timid' in env)

Read configuration from the env_var environment variable .


def from_args(self, **kwargs):
        """Read config values from `kwargs`."""
        for k, v in iitems(kwargs):
            if v is not None:
                if k in self.MUST_BE_LIST and isinstance(v, string_class):
                    v = [v]
                setattr(self, k, v)

def from_args ( self , * * kwargs ) : for k , v in iitems ( kwargs ) : if v is not None : if k in self . MUST_BE_LIST and isinstance ( v , string_class ) : v = [ v ] setattr ( self , k , v )


def from_file(self, filename):
        """Read configuration from a .rc file.

        `filename` is a file name to read.

        """
        self.attempted_config_files.append(filename)

        cp = HandyConfigParser()
        files_read = cp.read(filename)
        if files_read is not None:  # return value changed in 2.4
            self.config_files.extend(files_read)

        for option_spec in self.CONFIG_FILE_OPTIONS:
            self.set_attr_from_config_option(cp, *option_spec)

        # [paths] is special
        if cp.has_section('paths'):
            for option in cp.options('paths'):
                self.paths[option] = cp.getlist('paths', option)

def
from_file
(
self
,
filename
)
:
self
.
attempted_config_files
.
append
(
filename
)
cp
=
HandyConfigParser
(
)
files_read
=
cp
.
read
(
filename
)
if
files_read
is
not
:
# return value changed in 2.4
self
.
config_files
.
extend
(
files_read
)
for
option_spec
in
self
.
CONFIG_FILE_OPTIONS
:
self
.
set_attr_from_config_option
(
cp
,
*
option_spec
)
# [paths] is special
if
cp
.
has_section
(
'paths'
)
:
for
option
in
cp
.
options
(
'paths'
)
:
self
.
paths
[
option
]
=
cp
.
getlist
(
'paths'
,
option
)


def set_attr_from_config_option(self, cp, attr, where, type_=''):
        """Set an attribute on self if it exists in the ConfigParser."""
        section, option = where.split(":")
        if cp.has_option(section, option):
            method = getattr(cp, 'get'+type_)
            setattr(self, attr, method(section, option))

def set_attr_from_config_option ( self , cp , attr , where , type_ = '' ) : section , option = where . split ( ":" ) if cp . has_option ( section , option ) : method = getattr ( cp , 'get' + type_ ) setattr ( self , attr , method ( section , option ) )

Set an attribute on self if it exists in the ConfigParser .


def expand_user(path):
    """Expand '~'-style usernames in strings.

    This is similar to :func:`os.path.expanduser`, but it computes and returns
    extra information that will be useful if the input was being used in
    computing completions, and you wish to return the completions with the
    original '~' instead of its expanded value.

    Parameters
    ----------
    path : str
      String to be expanded.  If no ~ is present, the output is the same as the
      input.

    Returns
    -------
    newpath : str
      Result of ~ expansion in the input path.
    tilde_expand : bool
      Whether any expansion was performed or not.
    tilde_val : str
      The value that ~ was replaced with.
    """
    # Default values
    tilde_expand = False
    tilde_val = ''
    newpath = path

    if path.startswith('~'):
        tilde_expand = True
        rest = len(path)-1
        newpath = os.path.expanduser(path)
        if rest:
            tilde_val = newpath[:-rest]
        else:
            tilde_val = newpath

    return newpath, tilde_expand, tilde_val

def
expand_user
(
path
)
:
# Default values
tilde_expand
=
False
tilde_val
=
''
newpath
=
path
if
path
.
startswith
(
'~'
)
:
tilde_expand
=
True
rest
=
len
(
path
)
1
newpath
=
os
.
path
.
expanduser
(
path
)
if
rest
:
tilde_val
=
newpath
[
:
rest
]
else
:
tilde_val
=
newpath
return
newpath
,
tilde_expand
,
tilde_val


def delims(self, delims):
        """Set the delimiters for line splitting."""
        expr = '[' + ''.join('\\'+ c for c in delims) + ']'
        self._delim_re = re.compile(expr)
        self._delims = delims
        self._delim_expr = expr

def delims ( self , delims ) : expr = '[' + '' . join ( '\\' + c for c in delims ) + ']' self . _delim_re = re . compile ( expr ) self . _delims = delims self . _delim_expr = expr


def split_line(self, line, cursor_pos=None):
        """Split a line of text with a cursor at the given position.
        """
        l = line if cursor_pos is None else line[:cursor_pos]
        return self._delim_re.split(l)[-1]

def split_line ( self , line , cursor_pos = None ) : l = line if cursor_pos is None else line [ : cursor_pos ] return self . _delim_re . split ( l ) [ - 1 ]

Split a line of text with a cursor at the given position .


def global_matches(self, text):
        """Compute matches when text is a simple name.

        Return a list of all keywords, built-in functions and names currently
        defined in self.namespace or self.global_namespace that match.

        """
        #print 'Completer->global_matches, txt=%r' % text # dbg
        matches = []
        match_append = matches.append
        n = len(text)
        for lst in [keyword.kwlist,
                    __builtin__.__dict__.keys(),
                    self.namespace.keys(),
                    self.global_namespace.keys()]:
            for word in lst:
                if word[:n] == text and word != "__builtins__":
                    match_append(word)
        return matches

Compute matches when text is a simple name .


def attr_matches(self, text):
        """Compute matches when text contains a dot.

        Assuming the text is of the form NAME.NAME....[NAME], and is
        evaluatable in self.namespace or self.global_namespace, it will be
        evaluated and its attributes (as revealed by dir()) are used as
        possible completions.  (For class instances, class members are are
        also considered.)

        WARNING: this can still invoke arbitrary C code, if an object
        with a __getattr__ hook is evaluated.

        """

        #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
        # Another option, seems to work great. Catches things like ''.<tab>
        m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)

        if m:
            expr, attr = m.group(1, 3)
        elif self.greedy:
            m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
            if not m2:
                return []
            expr, attr = m2.group(1,2)
        else:
            return []

        try:
            obj = eval(expr, self.namespace)
        except:
            try:
                obj = eval(expr, self.global_namespace)
            except:
                return []

        if self.limit_to__all__ and hasattr(obj, '__all__'):
            words = get__all__entries(obj)
        else:
            words = dir2(obj)

        try:
            words = generics.complete_object(obj, words)
        except TryNext:
            pass
        except Exception:
            # Silence errors from completion function
            #raise # dbg
            pass
        # Build match list to return
        n = len(attr)
        res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
        return res

Compute matches when text contains a dot .


def _greedy_changed(self, name, old, new):
        """update the splitter and readline delims when greedy is changed"""
        if new:
            self.splitter.delims = GREEDY_DELIMS
        else:
            self.splitter.delims = DELIMS

        if self.readline:
            self.readline.set_completer_delims(self.splitter.delims)

def _greedy_changed ( self , name , old , new ) : if new : self . splitter . delims = GREEDY_DELIMS else : self . splitter . delims = DELIMS if self . readline : self . readline . set_completer_delims ( self . splitter . delims )


def file_matches(self, text):
        """Match filenames, expanding ~USER type strings.

        Most of the seemingly convoluted logic in this completer is an
        attempt to handle filenames with spaces in them.  And yet it's not
        quite perfect, because Python's readline doesn't expose all of the
        GNU readline details needed for this to be done correctly.

        For a filename with a space in it, the printed completions will be
        only the parts after what's already been typed (instead of the
        full completions, as is normally done).  I don't think with the
        current (as of Python 2.3) Python readline it's possible to do
        better."""

        #io.rprint('Completer->file_matches: <%r>' % text) # dbg

        # chars that require escaping with backslash - i.e. chars
        # that readline treats incorrectly as delimiters, but we
        # don't want to treat as delimiters in filename matching
        # when escaped with backslash
        if text.startswith('!'):
            text = text[1:]
            text_prefix = '!'
        else:
            text_prefix = ''

        text_until_cursor = self.text_until_cursor
        # track strings with open quotes
        open_quotes = has_open_quotes(text_until_cursor)

        if '(' in text_until_cursor or '[' in text_until_cursor:
            lsplit = text
        else:
            try:
                # arg_split ~ shlex.split, but with unicode bugs fixed by us
                lsplit = arg_split(text_until_cursor)[-1]
            except ValueError:
                # typically an unmatched ", or backslash without escaped char.
                if open_quotes:
                    lsplit = text_until_cursor.split(open_quotes)[-1]
                else:
                    return []
            except IndexError:
                # tab pressed on empty line
                lsplit = ""

        if not open_quotes and lsplit != protect_filename(lsplit):
            # if protectables are found, do matching on the whole escaped name
            has_protectables = True
            text0,text = text,lsplit
        else:
            has_protectables = False
            text = os.path.expanduser(text)

        if text == "":
            return [text_prefix + protect_filename(f) for f in self.glob("*")]

        # Compute the matches from the filesystem
        m0 = self.clean_glob(text.replace('\\',''))

        if has_protectables:
            # If we had protectables, we need to revert our changes to the
            # beginning of filename so that we don't double-write the part
            # of the filename we have so far
            len_lsplit = len(lsplit)
            matches = [text_prefix + text0 +
                       protect_filename(f[len_lsplit:]) for f in m0]
        else:
            if open_quotes:
                # if we have a string with an open quote, we don't need to
                # protect the names at all (and we _shouldn't_, as it
                # would cause bugs when the filesystem call is made).
                matches = m0
            else:
                matches = [text_prefix +
                           protect_filename(f) for f in m0]

        #io.rprint('mm', matches)  # dbg

        # Mark directories in input list by appending '/' to their names.
        matches = [x+'/' if os.path.isdir(x) else x for x in matches]
        return matches

Match filenames expanding ~USER type strings .


def alias_matches(self, text):
        """Match internal system aliases"""
        #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg

        # if we are not in the first 'item', alias matching
        # doesn't make sense - unless we are starting with 'sudo' command.
        main_text = self.text_until_cursor.lstrip()
        if ' ' in main_text and not main_text.startswith('sudo'):
            return []
        text = os.path.expanduser(text)
        aliases =  self.alias_table.keys()
        if text == '':
            return aliases
        else:
            return [a for a in aliases if a.startswith(text)]


def python_matches(self,text):
        """Match attributes or global python names"""

        #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
        if "." in text:
            try:
                matches = self.attr_matches(text)
                if text.endswith('.') and self.omit__names:
                    if self.omit__names == 1:
                        # true if txt is _not_ a __ name, false otherwise:
                        no__name = (lambda txt:
                                    re.match(r'.*\.__.*?__',txt) is None)
                    else:
                        # true if txt is _not_ a _ name, false otherwise:
                        no__name = (lambda txt:
                                    re.match(r'.*\._.*?',txt) is None)
                    matches = filter(no__name, matches)
            except NameError:
                # catches <undefined attributes>.<tab>
                matches = []
        else:
            matches = self.global_matches(text)

        return matches


def _default_arguments(self, obj):
        """Return the list of default arguments of obj if it is callable,
        or empty list otherwise."""

        if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
            # for classes, check for __init__,__new__
            if inspect.isclass(obj):
                obj = (getattr(obj,'__init__',None) or
                       getattr(obj,'__new__',None))
            # for all others, check if they are __call__able
            elif hasattr(obj, '__call__'):
                obj = obj.__call__
            # XXX: is there a way to handle the builtins ?
        try:
            args,_,_1,defaults = inspect.getargspec(obj)
            if defaults:
                return args[-len(defaults):]
        except TypeError: pass
        return []

Return the list of default arguments of obj if it is callable or empty list otherwise .


def complete(self, text=None, line_buffer=None, cursor_pos=None):
        """Find completions for the given text and line context.

        This is called successively with state == 0, 1, 2, ... until it
        returns None.  The completion should begin with 'text'.

        Note that both the text and the line_buffer are optional, but at least
        one of them must be given.

        Parameters
        ----------
          text : string, optional
            Text to perform the completion on.  If not given, the line buffer
            is split using the instance's CompletionSplitter object.

          line_buffer : string, optional
            If not given, the completer attempts to obtain the current line
            buffer via readline.  This keyword allows clients which are
            requesting for text completions in non-readline contexts to inform
            the completer of the entire text.

          cursor_pos : int, optional
            Index of the cursor in the full line buffer.  Should be provided by
            remote frontends where kernel has no access to frontend state.

        Returns
        -------
        text : str
          Text that was actually used in the completion.

        matches : list
          A list of completion matches.
        """
        #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos))  # dbg

        # if the cursor position isn't given, the only sane assumption we can
        # make is that it's at the end of the line (the common case)
        if cursor_pos is None:
            cursor_pos = len(line_buffer) if text is None else len(text)

        # if text is either None or an empty string, rely on the line buffer
        if not text:
            text = self.splitter.split_line(line_buffer, cursor_pos)

        # If no line buffer is given, assume the input text is all there was
        if line_buffer is None:
            line_buffer = text

        self.line_buffer = line_buffer
        self.text_until_cursor = self.line_buffer[:cursor_pos]
        #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos))  # dbg

        # Start with a clean slate of completions
        self.matches[:] = []
        custom_res = self.dispatch_custom_completer(text)
        if custom_res is not None:
            # did custom completers produce something?
            self.matches = custom_res
        else:
            # Extend the list of completions with the results of each
            # matcher, so we return results to the user from all
            # namespaces.
            if self.merge_completions:
                self.matches = []
                for matcher in self.matchers:
                    try:
                        self.matches.extend(matcher(text))
                    except:
                        # Show the ugly traceback if the matcher causes an
                        # exception, but do NOT crash the kernel!
                        sys.excepthook(*sys.exc_info())
            else:
                for matcher in self.matchers:
                    self.matches = matcher(text)
                    if self.matches:
                        break
        # FIXME: we should extend our api to return a dict with completions for
        # different types of objects.  The rlcomplete() method could then
        # simply collapse the dict into a list for readline, but we'd have
        # richer completion semantics in other evironments.
        self.matches = sorted(set(self.matches))
        #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
        return text, self.matches

Find completions for the given text and line context .


def rlcomplete(self, text, state):
        """Return the state-th possible completion for 'text'.

        This is called successively with state == 0, 1, 2, ... until it
        returns None.  The completion should begin with 'text'.

        Parameters
        ----------
          text : string
            Text to perform the completion on.

          state : int
            Counter used by readline.
        """
        if state==0:

            self.line_buffer = line_buffer = self.readline.get_line_buffer()
            cursor_pos = self.readline.get_endidx()

            #io.rprint("\nRLCOMPLETE: %r %r %r" %
            #          (text, line_buffer, cursor_pos) ) # dbg

            # if there is only a tab on a line with only whitespace, instead of
            # the mostly useless 'do you want to see all million completions'
            # message, just do the right thing and give the user his tab!
            # Incidentally, this enables pasting of tabbed text from an editor
            # (as long as autoindent is off).

            # It should be noted that at least pyreadline still shows file
            # completions - is there a way around it?

            # don't apply this on 'dumb' terminals, such as emacs buffers, so
            # we don't interfere with their own tab-completion mechanism.
            if not (self.dumb_terminal or line_buffer.strip()):
                self.readline.insert_text('\t')
                sys.stdout.flush()
                return None

            # Note: debugging exceptions that may occur in completion is very
            # tricky, because readline unconditionally silences them.  So if
            # during development you suspect a bug in the completion code, turn
            # this flag on temporarily by uncommenting the second form (don't
            # flip the value in the first line, as the '# dbg' marker can be
            # automatically detected and is used elsewhere).
            DEBUG = False
            #DEBUG = True # dbg
            if DEBUG:
                try:
                    self.complete(text, line_buffer, cursor_pos)
                except:
                    import traceback; traceback.print_exc()
            else:
                # The normal production version is here

                # This method computes the self.matches array
                self.complete(text, line_buffer, cursor_pos)

        try:
            return self.matches[state]
        except IndexError:
            return None

Return the state - th possible completion for text .


def _match_one(self, rec, tests):
        """Check if a specific record matches tests."""
        for key,test in tests.iteritems():
            if not test(rec.get(key, None)):
                return False
        return True

def _match_one ( self , rec , tests ) : for key , test in tests . iteritems ( ) : if not test ( rec . get ( key , None ) ) : return False return True

Check if a specific record matches tests .


def _match(self, check):
        """Find all the matches for a check dict."""
        matches = []
        tests = {}
        for k,v in check.iteritems():
            if isinstance(v, dict):
                tests[k] = CompositeFilter(v)
            else:
                tests[k] = lambda o: o==v

        for rec in self._records.itervalues():
            if self._match_one(rec, tests):
                matches.append(copy(rec))
        return matches

def _match ( self , check ) : matches = [ ] tests = { } for k , v in check . iteritems ( ) : if isinstance ( v , dict ) : tests [ k ] = CompositeFilter ( v ) else : tests [ k ] = lambda o : o == v for rec in self . _records . itervalues ( ) : if self . _match_one ( rec , tests ) : matches . append ( copy ( rec ) ) return matches


def _extract_subdict(self, rec, keys):
        """extract subdict of keys"""
        d = {}
        d['msg_id'] = rec['msg_id']
        for key in keys:
            d[key] = rec[key]
        return copy(d)

def _extract_subdict ( self , rec , keys ) : d = { } d [ 'msg_id' ] = rec [ 'msg_id' ] for key in keys : d [ key ] = rec [ key ] return copy ( d )


def quiet(self):
        """Should we silence the display hook because of ';'?"""
        # do not print output if input ends in ';'
        try:
            cell = self.shell.history_manager.input_hist_parsed[self.prompt_count]
            if cell.rstrip().endswith(';'):
                return True
        except IndexError:
            # some uses of ipshellembed may fail here
            pass
        return False

Should we silence the display hook because of ; ?


def write_output_prompt(self):
        """Write the output prompt.

        The default implementation simply writes the prompt to
        ``io.stdout``.
        """
        # Use write, not print which adds an extra space.
        io.stdout.write(self.shell.separate_out)
        outprompt = self.shell.prompt_manager.render('out')
        if self.do_full_cache:
            io.stdout.write(outprompt)


def write_format_data(self, format_dict):
        """Write the format data dict to the frontend.

        This default version of this method simply writes the plain text
        representation of the object to ``io.stdout``. Subclasses should
        override this method to send the entire `format_dict` to the
        frontends.

        Parameters
        ----------
        format_dict : dict
            The format dict for the object passed to `sys.displayhook`.
        """
        # We want to print because we want to always make sure we have a
        # newline, even if all the prompt separators are ''. This is the
        # standard IPython behavior.
        result_repr = format_dict['text/plain']
        if '\n' in result_repr:
            # So that multi-line strings line up with the left column of
            # the screen, instead of having the output prompt mess up
            # their first line.
            # We use the prompt template instead of the expanded prompt
            # because the expansion may add ANSI escapes that will interfere
            # with our ability to determine whether or not we should add
            # a newline.
            prompt_template = self.shell.prompt_manager.out_template
            if prompt_template and not prompt_template.endswith('\n'):
                # But avoid extraneous empty lines.
                result_repr = '\n' + result_repr

        print >>io.stdout, result_repr

Write the format data dict to the frontend .


def log_output(self, format_dict):
        """Log the output."""
        if self.shell.logger.log_output:
            self.shell.logger.log_write(format_dict['text/plain'], 'output')
        self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
                                                    format_dict['text/plain']

def log_output ( self , format_dict ) : if self . shell . logger . log_output : self . shell . logger . log_write ( format_dict [ 'text/plain' ] , 'output' ) self . shell . history_manager . output_hist_reprs [ self . prompt_count ] = format_dict [ 'text/plain' ]


def raise_if_freezed(self):
        '''raise `InvalidOperationException` if is freezed.'''
        if self.is_freezed:
            name = type(self).__name__
            raise InvalidOperationException('obj {name} is freezed.'.format(name=name))

def
raise_if_freezed
(
self
)
:
if
self
.
is_freezed
:
name
=
type
(
self
)
.
__name__
raise
InvalidOperationException
(
'obj {name} is freezed.'
.
format
(
name
=
name
)
)

raise InvalidOperationException if is freezed .


def mysql_timestamp_converter(s):
    """Convert a MySQL TIMESTAMP to a Timestamp object."""
    # MySQL>4.1 returns TIMESTAMP in the same format as DATETIME
    if s[4] == '-': return DateTime_or_None(s)
    s = s + "0"*(14-len(s)) # padding
    parts = map(int, filter(None, (s[:4],s[4:6],s[6:8],
                                   s[8:10],s[10:12],s[12:14])))
    try:
        return Timestamp(*parts)
    except (SystemExit, KeyboardInterrupt):
        raise
    except:
        return None

Convert a MySQL TIMESTAMP to a Timestamp object .


def _eventloop_changed(self, name, old, new):
        """schedule call to eventloop from IOLoop"""
        loop = ioloop.IOLoop.instance()
        loop.add_timeout(time.time()+0.1, self.enter_eventloop)

def _eventloop_changed ( self , name , old , new ) : loop = ioloop . IOLoop . instance ( ) loop . add_timeout ( time . time ( ) + 0.1 , self . enter_eventloop )


def dispatch_control(self, msg):
        """dispatch control requests"""
        idents,msg = self.session.feed_identities(msg, copy=False)
        try:
            msg = self.session.unserialize(msg, content=True, copy=False)
        except:
            self.log.error("Invalid Control Message", exc_info=True)
            return

        self.log.debug("Control received: %s", msg)

        header = msg['header']
        msg_id = header['msg_id']
        msg_type = header['msg_type']

        handler = self.control_handlers.get(msg_type, None)
        if handler is None:
            self.log.error("UNKNOWN CONTROL MESSAGE TYPE: %r", msg_type)
        else:
            try:
                handler(self.control_stream, idents, msg)
            except Exception:
                self.log.error("Exception in control handler:", exc_info=True)

def
dispatch_control
(
self
,
msg
)
:
idents
,
msg
=
self
.
session
.
feed_identities
(
msg
,
copy
=
False
)
try
:
msg
=
self
.
session
.
unserialize
(
msg
,
content
=
True
,
copy
=
False
)
except
:
self
.
log
.
error
(
"Invalid Control Message"
,
exc_info
=
True
)
return
self
.
log
.
debug
(
"Control received: %s"
,
msg
)
header
=
msg
[
'header'
]
msg_id
=
header
[
'msg_id'
]
msg_type
=
header
[
'msg_type'
]
handler
=
self
.
control_handlers
.
get
(
msg_type
,
)
if
handler
is
:
self
.
log
.
error
(
"UNKNOWN CONTROL MESSAGE TYPE: %r"
,
msg_type
)
else
:
try
:
handler
(
self
.
control_stream
,
idents
,
msg
)
except
Exception
:
self
.
log
.
error
(
"Exception in control handler:"
,
exc_info
=
True
)


def dispatch_shell(self, stream, msg):
        """dispatch shell requests"""
        # flush control requests first
        if self.control_stream:
            self.control_stream.flush()

        idents,msg = self.session.feed_identities(msg, copy=False)
        try:
            msg = self.session.unserialize(msg, content=True, copy=False)
        except:
            self.log.error("Invalid Message", exc_info=True)
            return

        header = msg['header']
        msg_id = header['msg_id']
        msg_type = msg['header']['msg_type']

        # Print some info about this message and leave a '--->' marker, so it's
        # easier to trace visually the message chain when debugging.  Each
        # handler prints its message at the end.
        self.log.debug('\n*** MESSAGE TYPE:%s***', msg_type)
        self.log.debug('   Content: %s\n   --->\n   ', msg['content'])

        if msg_id in self.aborted:
            self.aborted.remove(msg_id)
            # is it safe to assume a msg_id will not be resubmitted?
            reply_type = msg_type.split('_')[0] + '_reply'
            status = {'status' : 'aborted'}
            sub = {'engine' : self.ident}
            sub.update(status)
            reply_msg = self.session.send(stream, reply_type, subheader=sub,
                        content=status, parent=msg, ident=idents)
            return

        handler = self.shell_handlers.get(msg_type, None)
        if handler is None:
            self.log.error("UNKNOWN MESSAGE TYPE: %r", msg_type)
        else:
            # ensure default_int_handler during handler call
            sig = signal(SIGINT, default_int_handler)
            try:
                handler(stream, idents, msg)
            except Exception:
                self.log.error("Exception in message handler:", exc_info=True)
            finally:
                signal(SIGINT, sig)


def start(self):
        """register dispatchers for streams"""
        self.shell.exit_now = False
        if self.control_stream:
            self.control_stream.on_recv(self.dispatch_control, copy=False)

        def make_dispatcher(stream):
            def dispatcher(msg):
                return self.dispatch_shell(stream, msg)
            return dispatcher

        for s in self.shell_streams:
            s.on_recv(make_dispatcher(s), copy=False)

def start ( self ) : self . shell . exit_now = False if self . control_stream : self . control_stream . on_recv ( self . dispatch_control , copy = False ) def make_dispatcher ( stream ) : def dispatcher ( msg ) : return self . dispatch_shell ( stream , msg ) return dispatcher for s in self . shell_streams : s . on_recv ( make_dispatcher ( s ) , copy = False )


def do_one_iteration(self):
        """step eventloop just once"""
        if self.control_stream:
            self.control_stream.flush()
        for stream in self.shell_streams:
            # handle at most one request per iteration
            stream.flush(zmq.POLLIN, 1)
            stream.flush(zmq.POLLOUT)


def _publish_pyin(self, code, parent, execution_count):
        """Publish the code request on the pyin stream."""

        self.session.send(self.iopub_socket, u'pyin',
                            {u'code':code, u'execution_count': execution_count},
                            parent=parent, ident=self._topic('pyin')
        )

def _publish_pyin ( self , code , parent , execution_count ) : self . session . send ( self . iopub_socket , u'pyin' , { u'code' : code , u'execution_count' : execution_count } , parent = parent , ident = self . _topic ( 'pyin' ) )

Publish the code request on the pyin stream .


def abort_request(self, stream, ident, parent):
        """abort a specifig msg by id"""
        msg_ids = parent['content'].get('msg_ids', None)
        if isinstance(msg_ids, basestring):
            msg_ids = [msg_ids]
        if not msg_ids:
            self.abort_queues()
        for mid in msg_ids:
            self.aborted.add(str(mid))

        content = dict(status='ok')
        reply_msg = self.session.send(stream, 'abort_reply', content=content,
                parent=parent, ident=ident)
        self.log.debug("%s", reply_msg)

def abort_request ( self , stream , ident , parent ) : msg_ids = parent [ 'content' ] . get ( 'msg_ids' , None ) if isinstance ( msg_ids , basestring ) : msg_ids = [ msg_ids ] if not msg_ids : self . abort_queues ( ) for mid in msg_ids : self . aborted . add ( str ( mid ) ) content = dict ( status = 'ok' ) reply_msg = self . session . send ( stream , 'abort_reply' , content = content , parent = parent , ident = ident ) self . log . debug ( "%s" , reply_msg )


def clear_request(self, stream, idents, parent):
        """Clear our namespace."""
        self.shell.reset(False)
        msg = self.session.send(stream, 'clear_reply', ident=idents, parent=parent,
                content = dict(status='ok'))

def clear_request ( self , stream , idents , parent ) : self . shell . reset ( False ) msg = self . session . send ( stream , 'clear_reply' , ident = idents , parent = parent , content = dict ( status = 'ok' ) )