def parse_filename(fname):
    """Parse a notebook filename.

    This function takes a notebook filename and returns the notebook
    format (json/py) and the notebook name. This logic can be
    summarized as follows:

    * notebook.ipynb -> (notebook.ipynb, notebook, json)
    * notebook.json  -> (notebook.json, notebook, json)
    * notebook.py    -> (notebook.py, notebook, py)
    * notebook       -> (notebook.ipynb, notebook, json)

    Parameters
    ----------
    fname : unicode
        The notebook filename. The filename can use a specific filename
        extention (.ipynb, .json, .py) or none, in which case .ipynb will
        be assumed.

    Returns
    -------
    (fname, name, format) : (unicode, unicode, unicode)
        The filename, notebook name and format.
    """
    if fname.endswith(u'.ipynb'):
        format = u'json'
    elif fname.endswith(u'.json'):
        format = u'json'
    elif fname.endswith(u'.py'):
        format = u'py'
    else:
        fname = fname + u'.ipynb'
        format = u'json'
    name = fname.split('.')[0]
    return fname, name, format

def parse_filename ( fname ) : if fname . endswith ( u'.ipynb' ) : format = u'json' elif fname . endswith ( u'.json' ) : format = u'json' elif fname . endswith ( u'.py' ) : format = u'py' else : fname = fname + u'.ipynb' format = u'json' name = fname . split ( '.' ) [ 0 ] return fname , name , format


def _collapse_leading_ws(header, txt):
    """
    ``Description`` header must preserve newlines; all others need not
    """
    if header.lower() == 'description':  # preserve newlines
        return '\n'.join([x[8:] if x.startswith(' ' * 8) else x
                          for x in txt.strip().splitlines()])
    else:
        return ' '.join([x.strip() for x in txt.splitlines()])

def
_collapse_leading_ws
(
header
,
txt
)
:
if
header
.
lower
(
)
==
'description'
:
# preserve newlines
return
'\n'
.
join
(
[
x
[
8
:
]
if
x
.
startswith
(
' '
*
8
)
else
x
for
x
in
txt
.
strip
(
)
.
splitlines
(
)
]
)
else
:
return
' '
.
join
(
[
x
.
strip
(
)
for
x
in
txt
.
splitlines
(
)
]
)

Description header must preserve newlines ; all others need not


def hideEvent(self, event):
        """ Reimplemented to disconnect signal handlers and event filter.
        """
        super(CompletionWidget, self).hideEvent(event)
        self._text_edit.cursorPositionChanged.disconnect(self._update_current)
        self._text_edit.removeEventFilter(self)

def hideEvent ( self , event ) : super ( CompletionWidget , self ) . hideEvent ( event ) self . _text_edit . cursorPositionChanged . disconnect ( self . _update_current ) self . _text_edit . removeEventFilter ( self )

Reimplemented to disconnect signal handlers and event filter .


def showEvent(self, event):
        """ Reimplemented to connect signal handlers and event filter.
        """
        super(CompletionWidget, self).showEvent(event)
        self._text_edit.cursorPositionChanged.connect(self._update_current)
        self._text_edit.installEventFilter(self)

def showEvent ( self , event ) : super ( CompletionWidget , self ) . showEvent ( event ) self . _text_edit . cursorPositionChanged . connect ( self . _update_current ) self . _text_edit . installEventFilter ( self )

Reimplemented to connect signal handlers and event filter .


def _current_text_cursor(self):
        """ Returns a cursor with text between the start position and the
            current position selected.
        """
        cursor = self._text_edit.textCursor()
        if cursor.position() >= self._start_position:
            cursor.setPosition(self._start_position,
                               QtGui.QTextCursor.KeepAnchor)
        return cursor

def _current_text_cursor ( self ) : cursor = self . _text_edit . textCursor ( ) if cursor . position ( ) >= self . _start_position : cursor . setPosition ( self . _start_position , QtGui . QTextCursor . KeepAnchor ) return cursor

Returns a cursor with text between the start position and the current position selected .


def _update_current(self):
        """ Updates the current item based on the current text.
        """
        prefix = self._current_text_cursor().selection().toPlainText()
        if prefix:
            items = self.findItems(prefix, (QtCore.Qt.MatchStartsWith |
                                            QtCore.Qt.MatchCaseSensitive))
            if items:
                self.setCurrentItem(items[0])
            else:
                self.hide()
        else:
            self.hide()

def _update_current ( self ) : prefix = self . _current_text_cursor ( ) . selection ( ) . toPlainText ( ) if prefix : items = self . findItems ( prefix , ( QtCore . Qt . MatchStartsWith | QtCore . Qt . MatchCaseSensitive ) ) if items : self . setCurrentItem ( items [ 0 ] ) else : self . hide ( ) else : self . hide ( )

Updates the current item based on the current text .


def registerAdminSite(appName, excludeModels=[]):
    """Registers the models of the app with the given "appName" for the admin site"""
    for model in apps.get_app_config(appName).get_models():
        if model not in excludeModels:
            admin.site.register(model)

def registerAdminSite ( appName , excludeModels = [ ] ) : for model in apps . get_app_config ( appName ) . get_models ( ) : if model not in excludeModels : admin . site . register ( model )

Registers the models of the app with the given appName for the admin site


def disk_partitions(all):
    """Return disk partitions."""
    rawlist = _psutil_mswindows.get_disk_partitions(all)
    return [nt_partition(*x) for x in rawlist]

def disk_partitions ( all ) : rawlist = _psutil_mswindows . get_disk_partitions ( all ) return [ nt_partition ( * x ) for x in rawlist ]


def get_system_cpu_times():
    """Return system CPU times as a named tuple."""
    user, system, idle = 0, 0, 0
    # computes system global times summing each processor value
    for cpu_time in _psutil_mswindows.get_system_cpu_times():
        user += cpu_time[0]
        system += cpu_time[1]
        idle += cpu_time[2]
    return _cputimes_ntuple(user, system, idle)

Return system CPU times as a named tuple .


def get_system_per_cpu_times():
    """Return system per-CPU times as a list of named tuples."""
    ret = []
    for cpu_t in _psutil_mswindows.get_system_cpu_times():
        user, system, idle = cpu_t
        item = _cputimes_ntuple(user, system, idle)
        ret.append(item)
    return ret

def get_system_per_cpu_times ( ) : ret = [ ] for cpu_t in _psutil_mswindows . get_system_cpu_times ( ) : user , system , idle = cpu_t item = _cputimes_ntuple ( user , system , idle ) ret . append ( item ) return ret

Return system per - CPU times as a list of named tuples .


def _stdin_raw_nonblock(self):
        """Use the raw Win32 handle of sys.stdin to do non-blocking reads"""
        # WARNING: This is experimental, and produces inconsistent results.
        #          It's possible for the handle not to be appropriate for use
        #          with WaitForSingleObject, among other things.
        handle = msvcrt.get_osfhandle(sys.stdin.fileno())
        result = WaitForSingleObject(handle, 100)
        if result == WAIT_FAILED:
            raise ctypes.WinError()
        elif result == WAIT_TIMEOUT:
            print(".", end='')
            return None
        else:
            data = ctypes.create_string_buffer(256)
            bytesRead = DWORD(0)
            print('?', end='')

            if not ReadFile(handle, data, 256,
                        ctypes.byref(bytesRead), None):
                raise ctypes.WinError()
            # This ensures the non-blocking works with an actual console
            # Not checking the error, so the processing will still work with
            # other handle types
            FlushConsoleInputBuffer(handle)

            data = data.value
            data = data.replace('\r\n', '\n')
            data = data.replace('\r', '\n')
            print(repr(data) + " ", end='')
            return data

Use the raw Win32 handle of sys . stdin to do non - blocking reads


def _stdin_raw_block(self):
        """Use a blocking stdin read"""
        # The big problem with the blocking read is that it doesn't
        # exit when it's supposed to in all contexts. An extra
        # key-press may be required to trigger the exit.
        try:
            data = sys.stdin.read(1)
            data = data.replace('\r', '\n')
            return data
        except WindowsError as we:
            if we.winerror == ERROR_NO_DATA:
                # This error occurs when the pipe is closed
                return None
            else:
                # Otherwise let the error propagate
                raise we


def update_tab_bar_visibility(self):
        """ update visibility of the tabBar depending of the number of tab

        0 or 1 tab, tabBar hidden
        2+ tabs, tabBar visible

        send a self.close if number of tab ==0

        need to be called explicitly, or be connected to tabInserted/tabRemoved
        """
        if self.tab_widget.count() <= 1:
            self.tab_widget.tabBar().setVisible(False)
        else:
            self.tab_widget.tabBar().setVisible(True)
        if self.tab_widget.count()==0 :
            self.close()

def update_tab_bar_visibility ( self ) : if self . tab_widget . count ( ) <= 1 : self . tab_widget . tabBar ( ) . setVisible ( False ) else : self . tab_widget . tabBar ( ) . setVisible ( True ) if self . tab_widget . count ( ) == 0 : self . close ( )


def create_tab_with_current_kernel(self):
        """create a new frontend attached to the same kernel as the current tab"""
        current_widget = self.tab_widget.currentWidget()
        current_widget_index = self.tab_widget.indexOf(current_widget)
        current_widget_name = self.tab_widget.tabText(current_widget_index)
        widget = self.slave_frontend_factory(current_widget)
        if 'slave' in current_widget_name:
            # don't keep stacking slaves
            name = current_widget_name
        else:
            name = '(%s) slave' % current_widget_name
        self.add_tab_with_frontend(widget,name=name)

def
create_tab_with_current_kernel
(
self
)
:
current_widget
=
self
.
tab_widget
.
currentWidget
(
)
current_widget_index
=
self
.
tab_widget
.
indexOf
(
current_widget
)
current_widget_name
=
self
.
tab_widget
.
tabText
(
current_widget_index
)
widget
=
self
.
slave_frontend_factory
(
current_widget
)
if
'slave'
in
current_widget_name
:
# don't keep stacking slaves
name
=
current_widget_name
else
:
name
=
'(%s) slave'
%
current_widget_name
self
.
add_tab_with_frontend
(
widget
,
name
=
name
)


def add_tab_with_frontend(self,frontend,name=None):
        """ insert a tab with a given frontend in the tab bar, and give it a name

        """
        if not name:
            name = 'kernel %i' % self.next_kernel_id
        self.tab_widget.addTab(frontend,name)
        self.update_tab_bar_visibility()
        self.make_frontend_visible(frontend)
        frontend.exit_requested.connect(self.close_tab)

def
add_tab_with_frontend
(
self
,
frontend
,
name
=
)
:
if
not
name
:
name
=
'kernel %i'
%
self
.
next_kernel_id
self
.
tab_widget
.
addTab
(
frontend
,
name
)
self
.
update_tab_bar_visibility
(
)
self
.
make_frontend_visible
(
frontend
)
frontend
.
exit_requested
.
connect
(
self
.
close_tab
)

insert a tab with a given frontend in the tab bar and give it a name


def add_menu_action(self, menu, action, defer_shortcut=False):
        """Add action to menu as well as self

        So that when the menu bar is invisible, its actions are still available.

        If defer_shortcut is True, set the shortcut context to widget-only,
        where it will avoid conflict with shortcuts already bound to the
        widgets themselves.
        """
        menu.addAction(action)
        self.addAction(action)

        if defer_shortcut:
            action.setShortcutContext(QtCore.Qt.WidgetShortcut)

def add_menu_action ( self , menu , action , defer_shortcut = False ) : menu . addAction ( action ) self . addAction ( action ) if defer_shortcut : action . setShortcutContext ( QtCore . Qt . WidgetShortcut )

Add action to menu as well as self So that when the menu bar is invisible its actions are still available . If defer_shortcut is True set the shortcut context to widget - only where it will avoid conflict with shortcuts already bound to the widgets themselves .


def _make_dynamic_magic(self,magic):
        """Return a function `fun` that will execute `magic` on active frontend.

        Parameters
        ----------
        magic : string
            string that will be executed as is when the returned function is called

        Returns
        -------
        fun : function
            function with no parameters, when called will execute `magic` on the
            current active frontend at call time

        See Also
        --------
        populate_all_magic_menu : generate the "All Magics..." menu

        Notes
        -----
        `fun` executes `magic` in active frontend at the moment it is triggered,
        not the active frontend at the moment it was created.

        This function is mostly used to create the "All Magics..." Menu at run time.
        """
        # need two level nested function to be sure to pass magic
        # to active frontend **at run time**.
        def inner_dynamic_magic():
            self.active_frontend.execute(magic)
        inner_dynamic_magic.__name__ = "dynamics_magic_s"
        return inner_dynamic_magic

Return a function fun that will execute magic on active frontend .


def populate_all_magic_menu(self, listofmagic=None):
        """Clean "All Magics..." menu and repopulate it with `listofmagic`

        Parameters
        ----------
        listofmagic : string,
            repr() of a list of strings, send back by the kernel

        Notes
        -----
        `listofmagic`is a repr() of list because it is fed with the result of
        a 'user_expression'
        """
        for k,v in self._magic_menu_dict.items():
            v.clear()
        self.all_magic_menu.clear()

        protected_magic = set(["more","less","load_ext","pycat","loadpy","load","save","psource"])
        mlist=ast.literal_eval(listofmagic)
        for magic in mlist:
            cell = (magic['type'] == 'cell')
            name = magic['name']
            mclass = magic['class']
            if cell :
                prefix='%%'
            else :
                prefix='%'
            magic_menu = self._get_magic_menu(mclass)

            if name in protected_magic:
                suffix = '?'
            else :
                suffix = ''
            pmagic = '%s%s%s'%(prefix,name,suffix)

            xaction = QtGui.QAction(pmagic,
                self,
                triggered=self._make_dynamic_magic(pmagic)
                )
            magic_menu.addAction(xaction)
            self.all_magic_menu.addAction(xaction)

def populate_all_magic_menu ( self , listofmagic = None ) : for k , v in self . _magic_menu_dict . items ( ) : v . clear ( ) self . all_magic_menu . clear ( ) protected_magic = set ( [ "more" , "less" , "load_ext" , "pycat" , "loadpy" , "load" , "save" , "psource" ] ) mlist = ast . literal_eval ( listofmagic ) for magic in mlist : cell = ( magic [ 'type' ] == 'cell' ) name = magic [ 'name' ] mclass = magic [ 'class' ] if cell : prefix = '%%' else : prefix = '%' magic_menu = self . _get_magic_menu ( mclass ) if name in protected_magic : suffix = '?' else : suffix = '' pmagic = '%s%s%s' % ( prefix , name , suffix ) xaction = QtGui . QAction ( pmagic , self , triggered = self . _make_dynamic_magic ( pmagic ) ) magic_menu . addAction ( xaction ) self . all_magic_menu . addAction ( xaction )

Clean All Magics ... menu and repopulate it with listofmagic


def closeEvent(self, event):
        """ Forward the close event to every tabs contained by the windows
        """
        if self.tab_widget.count() == 0:
            # no tabs, just close
            event.accept()
            return
        # Do Not loop on the widget count as it change while closing
        title = self.window().windowTitle()
        cancel = QtGui.QMessageBox.Cancel
        okay = QtGui.QMessageBox.Ok

        if self.confirm_exit:
            if self.tab_widget.count() > 1:
                msg = "Close all tabs, stop all kernels, and Quit?"
            else:
                msg = "Close console, stop kernel, and Quit?"
            info = "Kernels not started here (e.g. notebooks) will be left alone."
            closeall = QtGui.QPushButton("&Quit", self)
            closeall.setShortcut('Q')
            box = QtGui.QMessageBox(QtGui.QMessageBox.Question,
                                    title, msg)
            box.setInformativeText(info)
            box.addButton(cancel)
            box.addButton(closeall, QtGui.QMessageBox.YesRole)
            box.setDefaultButton(closeall)
            box.setEscapeButton(cancel)
            pixmap = QtGui.QPixmap(self._app.icon.pixmap(QtCore.QSize(64,64)))
            box.setIconPixmap(pixmap)
            reply = box.exec_()
        else:
            reply = okay

        if reply == cancel:
            event.ignore()
            return
        if reply == okay:
            while self.tab_widget.count() >= 1:
                # prevent further confirmations:
                widget = self.active_frontend
                widget._confirm_exit = False
                self.close_tab(widget)
            event.accept()


def passwd(passphrase=None, algorithm='sha1'):
    """Generate hashed password and salt for use in notebook configuration.

    In the notebook configuration, set `c.NotebookApp.password` to
    the generated string.

    Parameters
    ----------
    passphrase : str
        Password to hash.  If unspecified, the user is asked to input
        and verify a password.
    algorithm : str
        Hashing algorithm to use (e.g, 'sha1' or any argument supported
        by :func:`hashlib.new`).

    Returns
    -------
    hashed_passphrase : str
        Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.

    Examples
    --------
    In [1]: passwd('mypassword')
    Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'

    """
    if passphrase is None:
        for i in range(3):
            p0 = getpass.getpass('Enter password: ')
            p1 = getpass.getpass('Verify password: ')
            if p0 == p1:
                passphrase = p0
                break
            else:
                print('Passwords do not match.')
        else:
            raise UsageError('No matching passwords found. Giving up.')

    h = hashlib.new(algorithm)
    salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
    h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))

    return ':'.join((algorithm, salt, h.hexdigest()))

Generate hashed password and salt for use in notebook configuration .


def passwd_check(hashed_passphrase, passphrase):
    """Verify that a given passphrase matches its hashed version.

    Parameters
    ----------
    hashed_passphrase : str
        Hashed password, in the format returned by `passwd`.
    passphrase : str
        Passphrase to validate.

    Returns
    -------
    valid : bool
        True if the passphrase matches the hash.

    Examples
    --------
    In [1]: from IPython.lib.security import passwd_check

    In [2]: passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
       ...:              'mypassword')
    Out[2]: True

    In [3]: passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
       ...:              'anotherpassword')
    Out[3]: False

    """
    try:
        algorithm, salt, pw_digest = hashed_passphrase.split(':', 2)
    except (ValueError, TypeError):
        return False

    try:
        h = hashlib.new(algorithm)
    except ValueError:
        return False

    if len(pw_digest) == 0:
        return False

    h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))

    return h.hexdigest() == pw_digest

def passwd_check ( hashed_passphrase , passphrase ) : try : algorithm , salt , pw_digest = hashed_passphrase . split ( ':' , 2 ) except ( ValueError , TypeError ) : return False try : h = hashlib . new ( algorithm ) except ValueError : return False if len ( pw_digest ) == 0 : return False h . update ( cast_bytes ( passphrase , 'utf-8' ) + str_to_bytes ( salt , 'ascii' ) ) return h . hexdigest ( ) == pw_digest

Verify that a given passphrase matches its hashed version .


def ajax_editable_boolean_cell(item, attr, text='', override=None):
    """
    Generate a html snippet for showing a boolean value on the admin page.
    Item is an object, attr is the attribute name we should display. Text
    is an optional explanatory text to be included in the output.

    This function will emit code to produce a checkbox input with its state
    corresponding to the item.attr attribute if no override value is passed.
    This input is wired to run a JS ajax updater to toggle the value.

    If override is passed in, ignores the attr attribute and returns a
    static image for the override boolean with no user interaction possible
    (useful for "disabled and you can't change it" situations).
    """
    if text:
        text = ' (%s)' % unicode(text)

    if override is not None:
        a = [django_boolean_icon(override, text), text]
    else:
        value = getattr(item, attr)
        a = [
            '<input type="checkbox"',
            value and ' checked="checked"' or '',
            ' onclick="return inplace_toggle_boolean(%d, \'%s\')";' % (item.id, attr),
            ' />',
            text,
        ]

    a.insert(0, '
' % ( attr, item.id ))
    a.append('
')
    return unicode(''.join(a))

Generate a html snippet for showing a boolean value on the admin page . Item is an object attr is the attribute name we should display . Text is an optional explanatory text to be included in the output .


def indented_short_title(self, item):
        r = ""
        """
        Generate a short title for an object, indent it depending on
        the object's depth in the hierarchy.
        """
        if hasattr(item, 'get_absolute_url'):
            r = '<input type="hidden" class="medialibrary_file_path" value="%s" />' % item.get_absolute_url()

        editable_class = ''
        if not getattr(item, 'feincms_editable', True):
            editable_class = ' tree-item-not-editable'

        r += '  ' % (
            item.id, editable_class, 14 + item.level * 18)
        #        r += ''
        if hasattr(item, 'short_title'):
            r += item.short_title()
        else:
            r += unicode(item)
            #        r += ''
        return mark_safe(r)

Generate a short title for an object indent it depending on the object s depth in the hierarchy .


def _collect_editable_booleans(self):
        """
        Collect all fields marked as editable booleans. We do not
        want the user to be able to edit arbitrary fields by crafting
        an AJAX request by hand.
        """
        if hasattr(self, '_ajax_editable_booleans'):
            return

        self._ajax_editable_booleans = {}

        for field in self.list_display:
            # The ajax_editable_boolean return value has to be assigned
            # to the ModelAdmin class
            item = getattr(self.__class__, field, None)
            if not item:
                continue

            attr = getattr(item, 'editable_boolean_field', None)
            if attr:
                def _fn(self, instance):
                    return [ajax_editable_boolean_cell(instance, _fn.attr)]

                _fn.attr = attr
                result_func = getattr(item, 'editable_boolean_result', _fn)
                self._ajax_editable_booleans[attr] = result_func

Collect all fields marked as editable booleans . We do not want the user to be able to edit arbitrary fields by crafting an AJAX request by hand .


def _toggle_boolean(self, request):
        """
        Handle an AJAX toggle_boolean request
        """
        try:
            item_id = int(request.POST.get('item_id', None))
            attr = str(request.POST.get('attr', None))
        except:
            return HttpResponseBadRequest("Malformed request")

        if not request.user.is_staff:
            logging.warning("Denied AJAX request by non-staff %s to toggle boolean %s for object #%s", request.user,
                            attr, item_id)
            return HttpResponseForbidden("You do not have permission to access this object")

        self._collect_editable_booleans()

        if not self._ajax_editable_booleans.has_key(attr):
            return HttpResponseBadRequest("not a valid attribute %s" % attr)

        try:
            obj = self.model._default_manager.get(pk=item_id)
        except self.model.DoesNotExist:
            return HttpResponseNotFound("Object does not exist")

        can_change = False

        if hasattr(obj, "user_can") and obj.user_can(request.user, change_page=True):
            # Was added in c7f04dfb5d, but I've no idea what user_can is about.
            can_change = True
        else:
            can_change = self.has_change_permission(request, obj=obj)

        if not can_change:
            logging.warning("Denied AJAX request by %s to toggle boolean %s for object %s", request.user, attr, item_id)
            return HttpResponseForbidden("You do not have permission to access this object")

        logging.info("Processing request by %s to toggle %s on %s", request.user, attr, obj)

        try:
            before_data = self._ajax_editable_booleans[attr](self, obj)

            setattr(obj, attr, not getattr(obj, attr))
            obj.save()

            self._refresh_changelist_caches() # ???: Perhaps better a post_save signal?

            # Construct html snippets to send back to client for status update
            data = self._ajax_editable_booleans[attr](self, obj)

        except Exception:#, e:
            logging.exception("Unhandled exception while toggling %s on %s", attr, obj)
            return HttpResponseServerError("Unable to toggle %s on %s" % (attr, obj))

        # Weed out unchanged cells to keep the updates small. This assumes
        # that the order a possible get_descendents() returns does not change
        # before and after toggling this attribute. Unlikely, but still...
        d = []
        for a, b in zip(before_data, data):
            if a != b:
                d.append(b)

        # TODO: Shorter: [ y for x,y in zip(a,b) if x!=y ]
        return HttpResponse(json.dumps(d), mimetype="application/json")


def has_change_permission(self, request, obj=None):
        """
        Implement a lookup for object level permissions. Basically the same as
        ModelAdmin.has_change_permission, but also passes the obj parameter in.
        """
        if settings.TREE_EDITOR_OBJECT_PERMISSIONS:
            opts = self.opts
            r = request.user.has_perm(opts.app_label + '.' + opts.get_change_permission(), obj)
        else:
            r = True

        return r and super(TreeEditor, self).has_change_permission(request, obj)

def has_change_permission ( self , request , obj = None ) : if settings . TREE_EDITOR_OBJECT_PERMISSIONS : opts = self . opts r = request . user . has_perm ( opts . app_label + '.' + opts . get_change_permission ( ) , obj ) else : r = True return r and super ( TreeEditor , self ) . has_change_permission ( request , obj )

Implement a lookup for object level permissions . Basically the same as ModelAdmin . has_change_permission but also passes the obj parameter in .


def has_delete_permission(self, request, obj=None):
        """
        Implement a lookup for object level permissions. Basically the same as
        ModelAdmin.has_delete_permission, but also passes the obj parameter in.
        """
        if settings.TREE_EDITOR_OBJECT_PERMISSIONS:
            opts = self.opts
            r = request.user.has_perm(opts.app_label + '.' + opts.get_delete_permission(), obj)
        else:
            r = True

        return r and super(TreeEditor, self).has_delete_permission(request, obj)

def has_delete_permission ( self , request , obj = None ) : if settings . TREE_EDITOR_OBJECT_PERMISSIONS : opts = self . opts r = request . user . has_perm ( opts . app_label + '.' + opts . get_delete_permission ( ) , obj ) else : r = True return r and super ( TreeEditor , self ) . has_delete_permission ( request , obj )

Implement a lookup for object level permissions . Basically the same as ModelAdmin . has_delete_permission but also passes the obj parameter in .


def add_children(G, parent, level, n=2):
    """Add children recursively to a binary tree."""
    if level == 0:
        return
    for i in range(n):
        child = parent+str(i)
        G.add_node(child)
        G.add_edge(parent,child)
        add_children(G, child, level-1, n)

def add_children ( G , parent , level , n = 2 ) : if level == 0 : return for i in range ( n ) : child = parent + str ( i ) G . add_node ( child ) G . add_edge ( parent , child ) add_children ( G , child , level - 1 , n )

Add children recursively to a binary tree .


def make_bintree(levels):
    """Make a symmetrical binary tree with @levels"""
    G = nx.DiGraph()
    root = '0'
    G.add_node(root)
    add_children(G, root, levels, 2)
    return G

def make_bintree ( levels ) : G = nx . DiGraph ( ) root = '0' G . add_node ( root ) add_children ( G , root , levels , 2 ) return G


def submit_jobs(view, G, jobs):
    """Submit jobs via client where G describes the time dependencies."""
    results = {}
    for node in nx.topological_sort(G):
        with view.temp_flags(after=[ results[n] for n in G.predecessors(node) ]):
            results[node] = view.apply(jobs[node])
    return results

def submit_jobs ( view , G , jobs ) : results = { } for node in nx . topological_sort ( G ) : with view . temp_flags ( after = [ results [ n ] for n in G . predecessors ( node ) ] ) : results [ node ] = view . apply ( jobs [ node ] ) return results

Submit jobs via client where G describes the time dependencies .


def validate_tree(G, results):
    """Validate that jobs executed after their dependencies."""
    for node in G:
        started = results[node].metadata.started
        for parent in G.predecessors(node):
            finished = results[parent].metadata.completed
            assert started > finished, "%s should have happened after %s"%(node, parent)

def
validate_tree
(
G
,
results
)
:
for
node
in
G
:
started
=
results
[
node
]
.
metadata
.
started
for
parent
in
G
.
predecessors
(
node
)
:
finished
=
results
[
parent
]
.
metadata
.
completed
assert
started
>
finished
,
"%s should have happened after %s"
%
(
node
,
parent
)

Validate that jobs executed after their dependencies .


def make_color_table(in_class):
    """Build a set of color attributes in a class.

    Helper function for building the *TermColors classes."""

    for name,value in color_templates:
        setattr(in_class,name,in_class._base % value)

def make_color_table ( in_class ) : for name , value in color_templates : setattr ( in_class , name , in_class . _base % value )

Build a set of color attributes in a class .


def copy(self,name=None):
        """Return a full copy of the object, optionally renaming it."""
        if name is None:
            name = self.name
        return ColorScheme(name, self.colors.dict())

def copy ( self , name = None ) : if name is None : name = self . name return ColorScheme ( name , self . colors . dict ( ) )

Return a full copy of the object optionally renaming it .


def add_scheme(self,new_scheme):
        """Add a new color scheme to the table."""
        if not isinstance(new_scheme,ColorScheme):
            raise ValueError,'ColorSchemeTable only accepts ColorScheme instances'
        self[new_scheme.name] = new_scheme


def set_active_scheme(self,scheme,case_sensitive=0):
        """Set the currently active scheme.

        Names are by default compared in a case-insensitive way, but this can
        be changed by setting the parameter case_sensitive to true."""

        scheme_names = self.keys()
        if case_sensitive:
            valid_schemes = scheme_names
            scheme_test = scheme
        else:
            valid_schemes = [s.lower() for s in scheme_names]
            scheme_test = scheme.lower()
        try:
            scheme_idx = valid_schemes.index(scheme_test)
        except ValueError:
            raise ValueError,'Unrecognized color scheme: ' + scheme + \
                  '\nValid schemes: '+str(scheme_names).replace("'', ",'')
        else:
            active = scheme_names[scheme_idx]
            self.active_scheme_name = active
            self.active_colors = self[active].colors
            # Now allow using '' as an index for the current active scheme
            self[''] = self[active]


def home_lib(home):
    """Return the lib dir under the 'home' installation scheme"""
    if hasattr(sys, 'pypy_version_info'):
        lib = 'site-packages'
    else:
        lib = os.path.join('lib', 'python')
    return os.path.join(home, lib)

def home_lib ( home ) : if hasattr ( sys , 'pypy_version_info' ) : lib = 'site-packages' else : lib = os . path . join ( 'lib' , 'python' ) return os . path . join ( home , lib )

Return the lib dir under the home installation scheme


def handle_iopub(self):
        """ Method to procces subscribe channel's messages

           This method reads a message and processes the content in different
           outputs like stdout, stderr, pyout and status

           Arguments:
           sub_msg:  message receive from kernel in the sub socket channel
                     capture by kernel manager.
        """
        while self.km.sub_channel.msg_ready():
            sub_msg = self.km.sub_channel.get_msg()
            msg_type = sub_msg['header']['msg_type']
            parent = sub_msg["parent_header"]
            if (not parent) or self.session_id == parent['session']:
                if msg_type == 'status' :
                    if sub_msg["content"]["execution_state"] == "busy" :
                        pass

                elif msg_type == 'stream' :
                    if sub_msg["content"]["name"] == "stdout":
                        print(sub_msg["content"]["data"], file=io.stdout, end="")
                        io.stdout.flush()
                    elif sub_msg["content"]["name"] == "stderr" :
                        print(sub_msg["content"]["data"], file=io.stderr, end="")
                        io.stderr.flush()

                elif msg_type == 'pyout':
                    self.execution_count = int(sub_msg["content"]["execution_count"])
                    format_dict = sub_msg["content"]["data"]
                    # taken from DisplayHook.__call__:
                    hook = self.displayhook
                    hook.start_displayhook()
                    hook.write_output_prompt()
                    hook.write_format_data(format_dict)
                    hook.log_output(format_dict)
                    hook.finish_displayhook()

def
handle_iopub
(
self
)
:
while
self
.
km
.
sub_channel
.
msg_ready
(
)
:
sub_msg
=
self
.
km
.
sub_channel
.
get_msg
(
)
msg_type
=
sub_msg
[
'header'
]
[
'msg_type'
]
parent
=
sub_msg
[
"parent_header"
]
if
(
not
parent
)
or
self
.
session_id
==
parent
[
'session'
]
:
if
msg_type
==
'status'
:
if
sub_msg
[
"content"
]
[
"execution_state"
]
==
"busy"
:
pass
elif
msg_type
==
'stream'
:
if
sub_msg
[
"content"
]
[
"name"
]
==
"stdout"
:
print
(
sub_msg
[
"content"
]
[
"data"
]
,
file
=
io
.
stdout
,
end
=
""
)
io
.
stdout
.
flush
(
)
elif
sub_msg
[
"content"
]
[
"name"
]
==
"stderr"
:
print
(
sub_msg
[
"content"
]
[
"data"
]
,
file
=
io
.
stderr
,
end
=
""
)
io
.
stderr
.
flush
(
)
elif
msg_type
==
'pyout'
:
self
.
execution_count
=
int
(
sub_msg
[
"content"
]
[
"execution_count"
]
)
format_dict
=
sub_msg
[
"content"
]
[
"data"
]
# taken from DisplayHook.__call__:
hook
=
self
.
displayhook
hook
.
start_displayhook
(
)
hook
.
write_output_prompt
(
)
hook
.
write_format_data
(
format_dict
)
hook
.
log_output
(
format_dict
)
hook
.
finish_displayhook
(
)

Method to procces subscribe channel s messages


def handle_stdin_request(self, timeout=0.1):
        """ Method to capture raw_input
        """
        msg_rep = self.km.stdin_channel.get_msg(timeout=timeout)
        # in case any iopub came while we were waiting:
        self.handle_iopub()
        if self.session_id == msg_rep["parent_header"].get("session"):
            # wrap SIGINT handler
            real_handler = signal.getsignal(signal.SIGINT)
            def double_int(sig,frame):
                # call real handler (forwards sigint to kernel),
                # then raise local interrupt, stopping local raw_input
                real_handler(sig,frame)
                raise KeyboardInterrupt
            signal.signal(signal.SIGINT, double_int)

            try:
                raw_data = raw_input(msg_rep["content"]["prompt"])
            except EOFError:
                # turn EOFError into EOF character
                raw_data = '\x04'
            except KeyboardInterrupt:
                sys.stdout.write('\n')
                return
            finally:
                # restore SIGINT handler
                signal.signal(signal.SIGINT, real_handler)

            # only send stdin reply if there *was not* another request
            # or execution finished while we were reading.
            if not (self.km.stdin_channel.msg_ready() or self.km.shell_channel.msg_ready()):
                self.km.stdin_channel.input(raw_data)


def wait_for_kernel(self, timeout=None):
        """method to wait for a kernel to be ready"""
        tic = time.time()
        self.km.hb_channel.unpause()
        while True:
            self.run_cell('1', False)
            if self.km.hb_channel.is_beating():
                # heart failure was not the reason this returned
                break
            else:
                # heart failed
                if timeout is not None and (time.time() - tic) > timeout:
                    return False
        return True


def set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if isinstance(style, basestring):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches()

def set_style ( self , style ) : if isinstance ( style , basestring ) : style = get_style_by_name ( style ) self . _style = style self . _clear_caches ( )

Sets the style to the specified Pygments style .


def _get_format(self, token):
        """ Returns a QTextCharFormat for token or None.
        """
        if token in self._formats:
            return self._formats[token]

        if self._style is None:
            result = self._get_format_from_document(token, self._document)
        else:
            result = self._get_format_from_style(token, self._style)

        self._formats[token] = result
        return result

def _get_format ( self , token ) : if token in self . _formats : return self . _formats [ token ] if self . _style is None : result = self . _get_format_from_document ( token , self . _document ) else : result = self . _get_format_from_style ( token , self . _style ) self . _formats [ token ] = result return result

Returns a QTextCharFormat for token or None .


def _get_format_from_document(self, token, document):
        """ Returns a QTextCharFormat for token by
        """
        code, html = self._formatter._format_lines([(token, u'dummy')]).next()
        self._document.setHtml(html)
        return QtGui.QTextCursor(self._document).charFormat()

def _get_format_from_document ( self , token , document ) : code , html = self . _formatter . _format_lines ( [ ( token , u'dummy' ) ] ) . next ( ) self . _document . setHtml ( html ) return QtGui . QTextCursor ( self . _document ) . charFormat ( )


def _get_format_from_style(self, token, style):
        """ Returns a QTextCharFormat for token by reading a Pygments style.
        """
        result = QtGui.QTextCharFormat()
        for key, value in style.style_for_token(token).items():
            if value:
                if key == 'color':
                    result.setForeground(self._get_brush(value))
                elif key == 'bgcolor':
                    result.setBackground(self._get_brush(value))
                elif key == 'bold':
                    result.setFontWeight(QtGui.QFont.Bold)
                elif key == 'italic':
                    result.setFontItalic(True)
                elif key == 'underline':
                    result.setUnderlineStyle(
                        QtGui.QTextCharFormat.SingleUnderline)
                elif key == 'sans':
                    result.setFontStyleHint(QtGui.QFont.SansSerif)
                elif key == 'roman':
                    result.setFontStyleHint(QtGui.QFont.Times)
                elif key == 'mono':
                    result.setFontStyleHint(QtGui.QFont.TypeWriter)
        return result

def _get_format_from_style ( self , token , style ) : result = QtGui . QTextCharFormat ( ) for key , value in style . style_for_token ( token ) . items ( ) : if value : if key == 'color' : result . setForeground ( self . _get_brush ( value ) ) elif key == 'bgcolor' : result . setBackground ( self . _get_brush ( value ) ) elif key == 'bold' : result . setFontWeight ( QtGui . QFont . Bold ) elif key == 'italic' : result . setFontItalic ( True ) elif key == 'underline' : result . setUnderlineStyle ( QtGui . QTextCharFormat . SingleUnderline ) elif key == 'sans' : result . setFontStyleHint ( QtGui . QFont . SansSerif ) elif key == 'roman' : result . setFontStyleHint ( QtGui . QFont . Times ) elif key == 'mono' : result . setFontStyleHint ( QtGui . QFont . TypeWriter ) return result

Returns a QTextCharFormat for token by reading a Pygments style .


def find_command(cmd, paths=None, pathext=None):
    """Searches the PATH for the given command and returns its path"""
    if paths is None:
        paths = os.environ.get('PATH', '').split(os.pathsep)
    if isinstance(paths, six.string_types):
        paths = [paths]
    # check if there are funny path extensions for executables, e.g. Windows
    if pathext is None:
        pathext = get_pathext()
    pathext = [ext for ext in pathext.lower().split(os.pathsep) if len(ext)]
    # don't use extensions if the command ends with one of them
    if os.path.splitext(cmd)[1].lower() in pathext:
        pathext = ['']
    # check if we find the command on PATH
    for path in paths:
        # try without extension first
        cmd_path = os.path.join(path, cmd)
        for ext in pathext:
            # then including the extension
            cmd_path_ext = cmd_path + ext
            if os.path.isfile(cmd_path_ext):
                return cmd_path_ext
        if os.path.isfile(cmd_path):
            return cmd_path
    raise BadCommand('Cannot find command %r' % cmd)


def normalize_path(path):
    """
    Convert a path to its canonical, case-normalized, absolute version.

    """
    return os.path.normcase(os.path.realpath(os.path.expanduser(path)))

def normalize_path ( path ) : return os . path . normcase ( os . path . realpath ( os . path . expanduser ( path ) ) )

Convert a path to its canonical case - normalized absolute version .


def check_nsp(dist, attr, value):
    """Verify that namespace packages are valid"""
    assert_string_list(dist,attr,value)
    for nsp in value:
        if not dist.has_contents_for(nsp):
            raise DistutilsSetupError(
                "Distribution contains no modules or packages for " +
                "namespace package %r" % nsp
            )
        if '.' in nsp:
            parent = '.'.join(nsp.split('.')[:-1])
            if parent not in value:
                distutils.log.warn(
                    "%r is declared as a package namespace, but %r is not:"
                    " please correct this in setup.py", nsp, parent
                )


def check_entry_points(dist, attr, value):
    """Verify that entry_points map is parseable"""
    try:
        pkg_resources.EntryPoint.parse_map(value)
    except ValueError, e:
        raise DistutilsSetupError(e)

def check_entry_points ( dist , attr , value ) : try : pkg_resources . EntryPoint . parse_map ( value ) except ValueError , e : raise DistutilsSetupError ( e )


def last_blank(src):
    """Determine if the input source ends in a blank.

    A blank is either a newline or a line consisting of whitespace.

    Parameters
    ----------
    src : string
      A single or multiline string.
    """
    if not src: return False
    ll  = src.splitlines()[-1]
    return (ll == '') or ll.isspace()

def last_blank ( src ) : if not src : return False ll = src . splitlines ( ) [ - 1 ] return ( ll == '' ) or ll . isspace ( )

Determine if the input source ends in a blank .


def last_two_blanks(src):
    """Determine if the input source ends in two blanks.

    A blank is either a newline or a line consisting of whitespace.

    Parameters
    ----------
    src : string
      A single or multiline string.
    """
    if not src: return False
    # The logic here is tricky: I couldn't get a regexp to work and pass all
    # the tests, so I took a different approach: split the source by lines,
    # grab the last two and prepend '###\n' as a stand-in for whatever was in
    # the body before the last two lines.  Then, with that structure, it's
    # possible to analyze with two regexps.  Not the most elegant solution, but
    # it works.  If anyone tries to change this logic, make sure to validate
    # the whole test suite first!
    new_src = '\n'.join(['###\n'] + src.splitlines()[-2:])
    return (bool(last_two_blanks_re.match(new_src)) or
            bool(last_two_blanks_re2.match(new_src)) )

Determine if the input source ends in two blanks .


def transform_assign_system(line):
    """Handle the `files = !ls` syntax."""
    m = _assign_system_re.match(line)
    if m is not None:
        cmd = m.group('cmd')
        lhs = m.group('lhs')
        new_line = '%s = get_ipython().getoutput(%r)' % (lhs, cmd)
        return new_line
    return line

def
transform_assign_system
(
line
)
:
m
=
_assign_system_re
.
match
(
line
)
if
m
is
not
:
cmd
=
m
.
group
(
'cmd'
)
lhs
=
m
.
group
(
'lhs'
)
new_line
=
'%s = get_ipython().getoutput(%r)'
%
(
lhs
,
cmd
)
return
new_line
return
line


def transform_assign_magic(line):
    """Handle the `a = %who` syntax."""
    m = _assign_magic_re.match(line)
    if m is not None:
        cmd = m.group('cmd')
        lhs = m.group('lhs')
        new_line = '%s = get_ipython().magic(%r)' % (lhs, cmd)
        return new_line
    return line

def
transform_assign_magic
(
line
)
:
m
=
_assign_magic_re
.
match
(
line
)
if
m
is
not
:
cmd
=
m
.
group
(
'cmd'
)
lhs
=
m
.
group
(
'lhs'
)
new_line
=
'%s = get_ipython().magic(%r)'
%
(
lhs
,
cmd
)
return
new_line
return
line


def transform_classic_prompt(line):
    """Handle inputs that start with '>>> ' syntax."""

    if not line or line.isspace():
        return line
    m = _classic_prompt_re.match(line)
    if m:
        return line[len(m.group(0)):]
    else:
        return line

def transform_classic_prompt ( line ) : if not line or line . isspace ( ) : return line m = _classic_prompt_re . match ( line ) if m : return line [ len ( m . group ( 0 ) ) : ] else : return line

Handle inputs that start with >>> syntax .


def transform_ipy_prompt(line):
    """Handle inputs that start classic IPython prompt syntax."""

    if not line or line.isspace():
        return line
    #print 'LINE:  %r' % line # dbg
    m = _ipy_prompt_re.match(line)
    if m:
        #print 'MATCH! %r -> %r' % (line, line[len(m.group(0)):]) # dbg
        return line[len(m.group(0)):]
    else:
        return line

Handle inputs that start classic IPython prompt syntax .


def push(self, lines):
        """Push one or more lines of input.

        This stores the given lines and returns a status code indicating
        whether the code forms a complete Python block or not.

        Any exceptions generated in compilation are swallowed, but if an
        exception was produced, the method returns True.

        Parameters
        ----------
        lines : string
          One or more lines of Python input.

        Returns
        -------
        is_complete : boolean
          True if the current input source (the result of the current input
          plus prior inputs) forms a complete Python execution block.  Note that
          this value is also stored as a private attribute (``_is_complete``), so it
          can be queried at any time.
        """
        if self.input_mode == 'cell':
            self.reset()

        self._store(lines)
        source = self.source

        # Before calling _compile(), reset the code object to None so that if an
        # exception is raised in compilation, we don't mislead by having
        # inconsistent code/source attributes.
        self.code, self._is_complete = None, None

        # Honor termination lines properly
        if source.rstrip().endswith('\\'):
            return False

        self._update_indent(lines)
        try:
            self.code = self._compile(source, symbol="exec")
        # Invalid syntax can produce any of a number of different errors from
        # inside the compiler, so we have to catch them all.  Syntax errors
        # immediately produce a 'ready' block, so the invalid Python can be
        # sent to the kernel for evaluation with possible ipython
        # special-syntax conversion.
        except (SyntaxError, OverflowError, ValueError, TypeError,
                MemoryError):
            self._is_complete = True
        else:
            # Compilation didn't produce any exceptions (though it may not have
            # given a complete code object)
            self._is_complete = self.code is not None

        return self._is_complete


def push_accepts_more(self):
        """Return whether a block of interactive input can accept more input.

        This method is meant to be used by line-oriented frontends, who need to
        guess whether a block is complete or not based solely on prior and
        current input lines.  The InputSplitter considers it has a complete
        interactive block and will not accept more input only when either a
        SyntaxError is raised, or *all* of the following are true:

        1. The input compiles to a complete statement.

        2. The indentation level is flush-left (because if we are indented,
           like inside a function definition or for loop, we need to keep
           reading new input).

        3. There is one extra line consisting only of whitespace.

        Because of condition #3, this method should be used only by
        *line-oriented* frontends, since it means that intermediate blank lines
        are not allowed in function definitions (or any other indented block).

        If the current input produces a syntax error, this method immediately
        returns False but does *not* raise the syntax error exception, as
        typically clients will want to send invalid syntax to an execution
        backend which might convert the invalid syntax into valid Python via
        one of the dynamic IPython mechanisms.
        """

        # With incomplete input, unconditionally accept more
        if not self._is_complete:
            return True

        # If we already have complete input and we're flush left, the answer
        # depends.  In line mode, if there hasn't been any indentation,
        # that's it. If we've come back from some indentation, we need
        # the blank final line to finish.
        # In cell mode, we need to check how many blocks the input so far
        # compiles into, because if there's already more than one full
        # independent block of input, then the client has entered full
        # 'cell' mode and is feeding lines that each is complete.  In this
        # case we should then keep accepting. The Qt terminal-like console
        # does precisely this, to provide the convenience of terminal-like
        # input of single expressions, but allowing the user (with a
        # separate keystroke) to switch to 'cell' mode and type multiple
        # expressions in one shot.
        if self.indent_spaces==0:
            if self.input_mode=='line':
                if not self._full_dedent:
                    return False
            else:
                try:
                    code_ast = ast.parse(u''.join(self._buffer))
                except Exception:
                    return False
                else:
                    if len(code_ast.body) == 1:
                        return False

        # When input is complete, then termination is marked by an extra blank
        # line at the end.
        last_line = self.source.splitlines()[-1]
        return bool(last_line and not last_line.isspace())

Return whether a block of interactive input can accept more input .


def _find_indent(self, line):
        """Compute the new indentation level for a single line.

        Parameters
        ----------
        line : str
          A single new line of non-whitespace, non-comment Python input.

        Returns
        -------
        indent_spaces : int
          New value for the indent level (it may be equal to self.indent_spaces
        if indentation doesn't change.

        full_dedent : boolean
          Whether the new line causes a full flush-left dedent.
        """
        indent_spaces = self.indent_spaces
        full_dedent = self._full_dedent

        inisp = num_ini_spaces(line)
        if inisp < indent_spaces:
            indent_spaces = inisp
            if indent_spaces <= 0:
                #print 'Full dedent in text',self.source # dbg
                full_dedent = True

        if line.rstrip()[-1] == ':':
            indent_spaces += 4
        elif dedent_re.match(line):
            indent_spaces -= 4
            if indent_spaces <= 0:
                full_dedent = True

        # Safety
        if indent_spaces < 0:
            indent_spaces = 0
            #print 'safety' # dbg

        return indent_spaces, full_dedent

Compute the new indentation level for a single line .


def _store(self, lines, buffer=None, store='source'):
        """Store one or more lines of input.

        If input lines are not newline-terminated, a newline is automatically
        appended."""

        if buffer is None:
            buffer = self._buffer

        if lines.endswith('\n'):
            buffer.append(lines)
        else:
            buffer.append(lines+'\n')
        setattr(self, store, self._set_source(buffer))

def _store ( self , lines , buffer = None , store = 'source' ) : if buffer is None : buffer = self . _buffer if lines . endswith ( '\n' ) : buffer . append ( lines ) else : buffer . append ( lines + '\n' ) setattr ( self , store , self . _set_source ( buffer ) )


def source_raw_reset(self):
        """Return input and raw source and perform a full reset.
        """
        out = self.source
        out_r = self.source_raw
        self.reset()
        return out, out_r

def source_raw_reset ( self ) : out = self . source out_r = self . source_raw self . reset ( ) return out , out_r

Return input and raw source and perform a full reset .


def _handle_cell_magic(self, lines):
        """Process lines when they start with %%, which marks cell magics.
        """
        self.processing_cell_magic = True
        first, _, body = lines.partition('\n')
        magic_name, _, line = first.partition(' ')
        magic_name = magic_name.lstrip(ESC_MAGIC)
        # We store the body of the cell and create a call to a method that
        # will use this stored value. This is ugly, but it's a first cut to
        # get it all working, as right now changing the return API of our
        # methods would require major refactoring.
        self.cell_magic_parts = [body]
        tpl = 'get_ipython()._run_cached_cell_magic(%r, %r)'
        tlines = tpl % (magic_name, line)
        self._store(tlines)
        self._store(lines, self._buffer_raw, 'source_raw')
        # We can actually choose whether to allow for single blank lines here
        # during input for clients that use cell mode to decide when to stop
        # pushing input (currently only the Qt console).
        # My first implementation did that, and then I realized it wasn't
        # consistent with the terminal behavior, so I've reverted it to one
        # line.  But I'm leaving it here so we can easily test both behaviors,
        # I kind of liked having full blank lines allowed in the cell magics...
        #self._is_complete = last_two_blanks(lines)
        self._is_complete = last_blank(lines)
        return self._is_complete

Process lines when they start with %% which marks cell magics .


def _line_mode_cell_append(self, lines):
        """Append new content for a cell magic in line mode.
        """
        # Only store the raw input.  Lines beyond the first one are only only
        # stored for history purposes; for execution the caller will grab the
        # magic pieces from cell_magic_parts and will assemble the cell body
        self._store(lines, self._buffer_raw, 'source_raw')
        self.cell_magic_parts.append(lines)
        # Find out if the last stored block has a whitespace line as its
        # last line and also this line is whitespace, case in which we're
        # done (two contiguous blank lines signal termination).  Note that
        # the storage logic *enforces* that every stored block is
        # newline-terminated, so we grab everything but the last character
        # so we can have the body of the block alone.
        last_block = self.cell_magic_parts[-1]
        self._is_complete = last_blank(last_block) and lines.isspace()
        return self._is_complete

Append new content for a cell magic in line mode .


def transform_cell(self, cell):
        """Process and translate a cell of input.
        """
        self.reset()
        self.push(cell)
        return self.source_reset()

def transform_cell ( self , cell ) : self . reset ( ) self . push ( cell ) return self . source_reset ( )


def push(self, lines):
        """Push one or more lines of IPython input.

        This stores the given lines and returns a status code indicating
        whether the code forms a complete Python block or not, after processing
        all input lines for special IPython syntax.

        Any exceptions generated in compilation are swallowed, but if an
        exception was produced, the method returns True.

        Parameters
        ----------
        lines : string
          One or more lines of Python input.

        Returns
        -------
        is_complete : boolean
          True if the current input source (the result of the current input
        plus prior inputs) forms a complete Python execution block.  Note that
        this value is also stored as a private attribute (_is_complete), so it
        can be queried at any time.
        """
        if not lines:
            return super(IPythonInputSplitter, self).push(lines)

        # We must ensure all input is pure unicode
        lines = cast_unicode(lines, self.encoding)

        # If the entire input block is a cell magic, return after handling it
        # as the rest of the transformation logic should be skipped.
        if lines.startswith('%%') and not \
          (len(lines.splitlines()) == 1 and lines.strip().endswith('?')):
            return self._handle_cell_magic(lines)

        # In line mode, a cell magic can arrive in separate pieces
        if self.input_mode == 'line' and self.processing_cell_magic:
            return self._line_mode_cell_append(lines)

        # The rest of the processing is for 'normal' content, i.e. IPython
        # source that we process through our transformations pipeline.
        lines_list = lines.splitlines()

        transforms = [transform_ipy_prompt, transform_classic_prompt,
                      transform_help_end, transform_escaped,
                      transform_assign_system, transform_assign_magic]

        # Transform logic
        #
        # We only apply the line transformers to the input if we have either no
        # input yet, or complete input, or if the last line of the buffer ends
        # with ':' (opening an indented block).  This prevents the accidental
        # transformation of escapes inside multiline expressions like
        # triple-quoted strings or parenthesized expressions.
        #
        # The last heuristic, while ugly, ensures that the first line of an
        # indented block is correctly transformed.
        #
        # FIXME: try to find a cleaner approach for this last bit.

        # If we were in 'block' mode, since we're going to pump the parent
        # class by hand line by line, we need to temporarily switch out to
        # 'line' mode, do a single manual reset and then feed the lines one
        # by one.  Note that this only matters if the input has more than one
        # line.
        changed_input_mode = False

        if self.input_mode == 'cell':
            self.reset()
            changed_input_mode = True
            saved_input_mode = 'cell'
            self.input_mode = 'line'

        # Store raw source before applying any transformations to it.  Note
        # that this must be done *after* the reset() call that would otherwise
        # flush the buffer.
        self._store(lines, self._buffer_raw, 'source_raw')

        try:
            push = super(IPythonInputSplitter, self).push
            buf = self._buffer
            for line in lines_list:
                if self._is_complete or not buf or \
                       (buf and buf[-1].rstrip().endswith((':', ','))):
                    for f in transforms:
                        line = f(line)

                out = push(line)
        finally:
            if changed_input_mode:
                self.input_mode = saved_input_mode
        return out

Push one or more lines of IPython input .


def _init_observers(self):
        """Initialize observer storage"""

        self.registered_types = set() #set of types that are observed
        self.registered_senders = set() #set of senders that are observed
        self.observers = {}

def
_init_observers
(
self
)
:
self
.
registered_types
=
set
(
)
#set of types that are observed
self
.
registered_senders
=
set
(
)
#set of senders that are observed
self
.
observers
=
{
}


def post_notification(self, ntype, sender, *args, **kwargs):
        """Post notification to all registered observers.

        The registered callback will be called as::

            callback(ntype, sender, *args, **kwargs)

        Parameters
        ----------
        ntype : hashable
            The notification type.
        sender : hashable
            The object sending the notification.
        *args : tuple
            The positional arguments to be passed to the callback.
        **kwargs : dict
            The keyword argument to be passed to the callback.

        Notes
        -----
        * If no registered observers, performance is O(1).
        * Notificaiton order is undefined.
        * Notifications are posted synchronously.
        """

        if(ntype==None or sender==None):
            raise NotificationError(
                "Notification type and sender are required.")

        # If there are no registered observers for the type/sender pair
        if((ntype not in self.registered_types and
                None not in self.registered_types) or
            (sender not in self.registered_senders and
                None not in self.registered_senders)):
            return

        for o in self._observers_for_notification(ntype, sender):
            o(ntype, sender, *args, **kwargs)

Post notification to all registered observers .


def _observers_for_notification(self, ntype, sender):
        """Find all registered observers that should recieve notification"""

        keys = (
                   (ntype,sender),
                   (ntype, None),
                   (None, sender),
                   (None,None)
               )

        obs = set()
        for k in keys:
            obs.update(self.observers.get(k, set()))

        return obs

def _observers_for_notification ( self , ntype , sender ) : keys = ( ( ntype , sender ) , ( ntype , None ) , ( None , sender ) , ( None , None ) ) obs = set ( ) for k in keys : obs . update ( self . observers . get ( k , set ( ) ) ) return obs


def add_observer(self, callback, ntype, sender):
        """Add an observer callback to this notification center.

        The given callback will be called upon posting of notifications of
        the given type/sender and will receive any additional arguments passed
        to post_notification.

        Parameters
        ----------
        callback : callable
            The callable that will be called by :meth:`post_notification`
            as ``callback(ntype, sender, *args, **kwargs)
        ntype : hashable
            The notification type. If None, all notifications from sender
            will be posted.
        sender : hashable
            The notification sender. If None, all notifications of ntype
            will be posted.
        """
        assert(callback != None)
        self.registered_types.add(ntype)
        self.registered_senders.add(sender)
        self.observers.setdefault((ntype,sender), set()).add(callback)

def add_observer ( self , callback , ntype , sender ) : assert ( callback != None ) self . registered_types . add ( ntype ) self . registered_senders . add ( sender ) self . observers . setdefault ( ( ntype , sender ) , set ( ) ) . add ( callback )

Add an observer callback to this notification center .


def new(self, func_or_exp, *args, **kwargs):
        """Add a new background job and start it in a separate thread.

        There are two types of jobs which can be created:

        1. Jobs based on expressions which can be passed to an eval() call.
        The expression must be given as a string.  For example:

          job_manager.new('myfunc(x,y,z=1)'[,glob[,loc]])

        The given expression is passed to eval(), along with the optional
        global/local dicts provided.  If no dicts are given, they are
        extracted automatically from the caller's frame.

        A Python statement is NOT a valid eval() expression.  Basically, you
        can only use as an eval() argument something which can go on the right
        of an '=' sign and be assigned to a variable.

        For example,"print 'hello'" is not valid, but '2+3' is.

        2. Jobs given a function object, optionally passing additional
        positional arguments:

          job_manager.new(myfunc, x, y)

        The function is called with the given arguments.

        If you need to pass keyword arguments to your function, you must
        supply them as a dict named kw:

          job_manager.new(myfunc, x, y, kw=dict(z=1))

        The reason for this assymmetry is that the new() method needs to
        maintain access to its own keywords, and this prevents name collisions
        between arguments to new() and arguments to your own functions.

        In both cases, the result is stored in the job.result field of the
        background job object.

        You can set `daemon` attribute of the thread by giving the keyword
        argument `daemon`.

        Notes and caveats:

        1. All threads running share the same standard output.  Thus, if your
        background jobs generate output, it will come out on top of whatever
        you are currently writing.  For this reason, background jobs are best
        used with silent functions which simply return their output.

        2. Threads also all work within the same global namespace, and this
        system does not lock interactive variables.  So if you send job to the
        background which operates on a mutable object for a long time, and
        start modifying that same mutable object interactively (or in another
        backgrounded job), all sorts of bizarre behaviour will occur.

        3. If a background job is spending a lot of time inside a C extension
        module which does not release the Python Global Interpreter Lock
        (GIL), this will block the IPython prompt.  This is simply because the
        Python interpreter can only switch between threads at Python
        bytecodes.  While the execution is inside C code, the interpreter must
        simply wait unless the extension module releases the GIL.

        4. There is no way, due to limitations in the Python threads library,
        to kill a thread once it has started."""

        if callable(func_or_exp):
            kw  = kwargs.get('kw',{})
            job = BackgroundJobFunc(func_or_exp,*args,**kw)
        elif isinstance(func_or_exp, basestring):
            if not args:
                frame = sys._getframe(1)
                glob, loc = frame.f_globals, frame.f_locals
            elif len(args)==1:
                glob = loc = args[0]
            elif len(args)==2:
                glob,loc = args
            else:
                raise ValueError(
                      'Expression jobs take at most 2 args (globals,locals)')
            job = BackgroundJobExpr(func_or_exp, glob, loc)
        else:
            raise TypeError('invalid args for new job')

        if kwargs.get('daemon', False):
            job.daemon = True
        job.num = len(self.all)+1 if self.all else 0
        self.running.append(job)
        self.all[job.num] = job
        print 'Starting job # %s in a separate thread.' % job.num
        job.start()
        return job

Add a new background job and start it in a separate thread .


def _update_status(self):
        """Update the status of the job lists.

        This method moves finished jobs to one of two lists:
          - self.completed: jobs which completed successfully
          - self.dead: jobs which finished but died.

        It also copies those jobs to corresponding _report lists.  These lists
        are used to report jobs completed/dead since the last update, and are
        then cleared by the reporting function after each call."""

        # Status codes
        srun, scomp, sdead = self._s_running, self._s_completed, self._s_dead
        # State lists, use the actual lists b/c the public names are properties
        # that call this very function on access
        running, completed, dead = self._running, self._completed, self._dead

        # Now, update all state lists
        for num, job in enumerate(running):
            stat = job.stat_code
            if stat == srun:
                continue
            elif stat == scomp:
                completed.append(job)
                self._comp_report.append(job)
                running[num] = False
            elif stat == sdead:
                dead.append(job)
                self._dead_report.append(job)
                running[num] = False
        # Remove dead/completed jobs from running list
        running[:] = filter(None, running)


def _group_report(self,group,name):
        """Report summary for a given job group.

        Return True if the group had any elements."""

        if group:
            print '%s jobs:' % name
            for job in group:
                print '%s : %s' % (job.num,job)
            print
            return True

def
_group_report
(
self
,
group
,
name
)
:
if
group
:
print
'%s jobs:'
%
name
for
job
in
group
:
print
'%s : %s'
%
(
job
.
num
,
job
)
print
return
True


def _group_flush(self,group,name):
        """Flush a given job group

        Return True if the group had any elements."""

        njobs = len(group)
        if njobs:
            plural = {1:''}.setdefault(njobs,'s')
            print 'Flushing %s %s job%s.' % (njobs,name,plural)
            group[:] = []
            return True

def
_group_flush
(
self
,
group
,
name
)
:
njobs
=
len
(
group
)
if
njobs
:
plural
=
{
1
:
''
}
.
setdefault
(
njobs
,
's'
)
print
'Flushing %s %s job%s.'
%
(
njobs
,
name
,
plural
)
group
[
:
]
=
[
]
return
True


def _status_new(self):
        """Print the status of newly finished jobs.

        Return True if any new jobs are reported.

        This call resets its own state every time, so it only reports jobs
        which have finished since the last time it was called."""

        self._update_status()
        new_comp = self._group_report(self._comp_report, 'Completed')
        new_dead = self._group_report(self._dead_report,
                                      'Dead, call jobs.traceback() for details')
        self._comp_report[:] = []
        self._dead_report[:] = []
        return new_comp or new_dead

Print the status of newly finished jobs .


def status(self,verbose=0):
        """Print a status of all jobs currently being managed."""

        self._update_status()
        self._group_report(self.running,'Running')
        self._group_report(self.completed,'Completed')
        self._group_report(self.dead,'Dead')
        # Also flush the report queues
        self._comp_report[:] = []
        self._dead_report[:] = []

def
status
(
self
,
verbose
=
0
)
:
self
.
_update_status
(
)
self
.
_group_report
(
self
.
running
,
'Running'
)
self
.
_group_report
(
self
.
completed
,
'Completed'
)
self
.
_group_report
(
self
.
dead
,
'Dead'
)
# Also flush the report queues
self
.
_comp_report
[
:
]
=
[
]
self
.
_dead_report
[
:
]
=
[
]

Print a status of all jobs currently being managed .


def _init(self):
        """Common initialization for all BackgroundJob objects"""

        for attr in ['call','strform']:
            assert hasattr(self,attr), "Missing attribute <%s>" % attr

        # The num tag can be set by an external job manager
        self.num = None

        self.status    = BackgroundJobBase.stat_created
        self.stat_code = BackgroundJobBase.stat_created_c
        self.finished  = False
        self.result    = '<BackgroundJob has not completed>'

        # reuse the ipython traceback handler if we can get to it, otherwise
        # make a new one
        try:
            make_tb = get_ipython().InteractiveTB.text
        except:
            make_tb = AutoFormattedTB(mode = 'Context',
                                      color_scheme='NoColor',
                                      tb_offset = 1).text
        # Note that the actual API for text() requires the three args to be
        # passed in, so we wrap it in a simple lambda.
        self._make_tb = lambda : make_tb(None, None, None)

        # Hold a formatted traceback if one is generated.
        self._tb = None

        threading.Thread.__init__(self)


def insert(self, idx, value):
        """
        Inserts a value in the ``ListVariable`` at an appropriate index.

        :param idx: The index before which to insert the new value.
        :param value: The value to insert.
        """

        self._value.insert(idx, value)
        self._rebuild()

def insert ( self , idx , value ) : self . _value . insert ( idx , value ) self . _rebuild ( )

Inserts a value in the ListVariable at an appropriate index .


def copy(self):
        """
        Retrieve a copy of the Environment.  Note that this is a shallow
        copy.
        """

        return self.__class__(self._data.copy(), self._sensitive.copy(),
                              self._cwd)

def copy ( self ) : return self . __class__ ( self . _data . copy ( ) , self . _sensitive . copy ( ) , self . _cwd )

Retrieve a copy of the Environment . Note that this is a shallow copy .


def _declare_special(self, name, sep, klass):
        """
        Declare an environment variable as a special variable.  This can
        be used even if the environment variable is not present.

        :param name: The name of the environment variable that should
                     be considered special.
        :param sep: The separator to be used.
        :param klass: The subclass of ``SpecialVariable`` used to
                      represent the variable.
        """

        # First, has it already been declared?
        if name in self._special:
            special = self._special[name]
            if not isinstance(special, klass) or sep != special._sep:
                raise ValueError('variable %s already declared as %s '
                                 'with separator "%s"' %
                                 (name, special.__class__.__name__,
                                  special._sep))

        # OK, it's new; declare it
        else:
            self._special[name] = klass(self, name, sep)

def
_declare_special
(
self
,
name
,
sep
,
klass
)
:
# First, has it already been declared?
if
name
in
self
.
_special
:
special
=
self
.
_special
[
name
]
if
not
isinstance
(
special
,
klass
)
or
sep
!=
special
.
_sep
:
raise
ValueError
(
'variable %s already declared as %s '
'with separator "%s"'
%
(
name
,
special
.
__class__
.
__name__
,
special
.
_sep
)
)
# OK, it's new; declare it
else
:
self
.
_special
[
name
]
=
klass
(
self
,
name
,
sep
)

Declare an environment variable as a special variable . This can be used even if the environment variable is not present .


def declare_list(self, name, sep=os.pathsep):
        """
        Declare an environment variable as a list-like special variable.
        This can be used even if the environment variable is not
        present.

        :param name: The name of the environment variable that should
                     be considered list-like.
        :param sep: The separator to be used.  Defaults to the value
                    of ``os.pathsep``.
        """

        self._declare_special(name, sep, ListVariable)

def declare_list ( self , name , sep = os . pathsep ) : self . _declare_special ( name , sep , ListVariable )

Declare an environment variable as a list - like special variable . This can be used even if the environment variable is not present .


def declare_set(self, name, sep=os.pathsep):
        """
        Declare an environment variable as a set-like special variable.
        This can be used even if the environment variable is not
        present.

        :param name: The name of the environment variable that should
                     be considered set-like.
        :param sep: The separator to be used.  Defaults to the value
                    of ``os.pathsep``.
        """

        self._declare_special(name, sep, SetVariable)

def declare_set ( self , name , sep = os . pathsep ) : self . _declare_special ( name , sep , SetVariable )

Declare an environment variable as a set - like special variable . This can be used even if the environment variable is not present .


def cwd(self, value):
        """
        Change the working directory that processes should be executed in.

        :param value: The new path to change to.  If relative, will be
                      interpreted relative to the current working
                      directory.
        """

        self._cwd = utils.canonicalize_path(self._cwd, value)

def cwd ( self , value ) : self . _cwd = utils . canonicalize_path ( self . _cwd , value )

Change the working directory that processes should be executed in .


def move(self, state=None):
        """Swaps two cities in the route.

        :type state: TSPState
        """
        state = self.state if state is None else state
        route = state
        a = random.randint(self.locked_range, len(route) - 1)
        b = random.randint(self.locked_range, len(route) - 1)
        route[a], route[b] = route[b], route[a]

def move ( self , state = None ) : state = self . state if state is None else state route = state a = random . randint ( self . locked_range , len ( route ) - 1 ) b = random . randint ( self . locked_range , len ( route ) - 1 ) route [ a ] , route [ b ] = route [ b ] , route [ a ]


def energy(self, state=None):
        """Calculates the length of the route."""
        state = self.state if state is None else state
        route = state
        e = 0
        if self.distance_matrix:
            for i in range(len(route)):
                e += self.distance_matrix["{},{}".format(route[i-1], route[i])]
        else:
            for i in range(len(route)):
                e += distance(self.cities[route[i-1]], self.cities[route[i]])
        return e

def energy ( self , state = None ) : state = self . state if state is None else state route = state e = 0 if self . distance_matrix : for i in range ( len ( route ) ) : e += self . distance_matrix [ "{},{}" . format ( route [ i - 1 ] , route [ i ] ) ] else : for i in range ( len ( route ) ) : e += distance ( self . cities [ route [ i - 1 ] ] , self . cities [ route [ i ] ] ) return e


def _defaults(self, keys=None):
        """create an empty record"""
        d = {}
        keys = self._keys if keys is None else keys
        for key in keys:
            d[key] = None
        return d

def _defaults ( self , keys = None ) : d = { } keys = self . _keys if keys is None else keys for key in keys : d [ key ] = None return d


def _check_table(self):
        """Ensure that an incorrect table doesn't exist

        If a bad (old) table does exist, return False
        """
        cursor = self._db.execute("PRAGMA table_info(%s)"%self.table)
        lines = cursor.fetchall()
        if not lines:
            # table does not exist
            return True
        types = {}
        keys = []
        for line in lines:
            keys.append(line[1])
            types[line[1]] = line[2]
        if self._keys != keys:
            # key mismatch
            self.log.warn('keys mismatch')
            return False
        for key in self._keys:
            if types[key] != self._types[key]:
                self.log.warn(
                    'type mismatch: %s: %s != %s'%(key,types[key],self._types[key])
                )
                return False
        return True

def
_check_table
(
self
)
:
cursor
=
self
.
_db
.
execute
(
"PRAGMA table_info(%s)"
%
self
.
table
)
lines
=
cursor
.
fetchall
(
)
if
not
lines
:
# table does not exist
return
True
types
=
{
}
keys
=
[
]
for
line
in
lines
:
keys
.
append
(
line
[
1
]
)
types
[
line
[
1
]
]
=
line
[
2
]
if
self
.
_keys
!=
keys
:
# key mismatch
self
.
log
.
warn
(
'keys mismatch'
)
return
False
for
key
in
self
.
_keys
:
if
types
[
key
]
!=
self
.
_types
[
key
]
:
self
.
log
.
warn
(
'type mismatch: %s: %s != %s'
%
(
key
,
types
[
key
]
,
self
.
_types
[
key
]
)
)
return
False
return
True

Ensure that an incorrect table doesn t exist


def _list_to_dict(self, line, keys=None):
        """Inverse of dict_to_list"""
        keys = self._keys if keys is None else keys
        d = self._defaults(keys)
        for key,value in zip(keys, line):
            d[key] = value

        return d

def _list_to_dict ( self , line , keys = None ) : keys = self . _keys if keys is None else keys d = self . _defaults ( keys ) for key , value in zip ( keys , line ) : d [ key ] = value return d


def _render_expression(self, check):
        """Turn a mongodb-style search dict into an SQL query."""
        expressions = []
        args = []

        skeys = set(check.keys())
        skeys.difference_update(set(self._keys))
        skeys.difference_update(set(['buffers', 'result_buffers']))
        if skeys:
            raise KeyError("Illegal testing key(s): %s"%skeys)

        for name,sub_check in check.iteritems():
            if isinstance(sub_check, dict):
                for test,value in sub_check.iteritems():
                    try:
                        op = operators[test]
                    except KeyError:
                        raise KeyError("Unsupported operator: %r"%test)
                    if isinstance(op, tuple):
                        op, join = op

                    if value is None and op in null_operators:
                        expr = "%s %s" % (name, null_operators[op])
                    else:
                        expr = "%s %s ?"%(name, op)
                        if isinstance(value, (tuple,list)):
                            if op in null_operators and any([v is None for v in value]):
                                # equality tests don't work with NULL
                                raise ValueError("Cannot use %r test with NULL values on SQLite backend"%test)
                            expr = '( %s )'%( join.join([expr]*len(value)) )
                            args.extend(value)
                        else:
                            args.append(value)
                    expressions.append(expr)
            else:
                # it's an equality check
                if sub_check is None:
                    expressions.append("%s IS NULL" % name)
                else:
                    expressions.append("%s = ?"%name)
                    args.append(sub_check)

        expr = " AND ".join(expressions)
        return expr, args

Turn a mongodb - style search dict into an SQL query .


def warn(msg,level=2,exit_val=1):
    """Standard warning printer. Gives formatting consistency.

    Output is sent to io.stderr (sys.stderr by default).

    Options:

    -level(2): allows finer control:
      0 -> Do nothing, dummy function.
      1 -> Print message.
      2 -> Print 'WARNING:' + message. (Default level).
      3 -> Print 'ERROR:' + message.
      4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).

    -exit_val (1): exit value returned by sys.exit() for a level 4
    warning. Ignored for all other levels."""

    if level>0:
        header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
        io.stderr.write('%s%s' % (header[level],msg))
        if level == 4:
            print >> io.stderr,'Exiting.\n'
            sys.exit(exit_val)

def
warn
(
msg
,
level
=
2
,
exit_val
=
1
)
:
if
level
>
0
:
header
=
[
''
,
''
,
'WARNING: '
,
'ERROR: '
,
'FATAL ERROR: '
]
io
.
stderr
.
write
(
'%s%s'
%
(
header
[
level
]
,
msg
)
)
if
level
==
4
:
print
>>
io
.
stderr
,
'Exiting.\n'
sys
.
exit
(
exit_val
)

Standard warning printer . Gives formatting consistency .


def parse(self, config_file=None, specs=None, default_file=None):
        """Read a config_file, check the validity with a JSON Schema as specs
        and get default values from default_file if asked.

        All parameters are optionnal.

        If there is no config_file defined, read the venv base
        dir and try to get config/app.yml.

        If no specs, don't validate anything.

        If no default_file, don't merge with default values."""

        self._config_exists(config_file)
        self._specs_exists(specs)

        self.loaded_config = anyconfig.load(self.config_file, ac_parser='yaml')

        if default_file is not None:
            self._merge_default(default_file)

        if self.specs is None:
            return self.loaded_config

        self._validate()

        return self.loaded_config

def parse ( self , config_file = None , specs = None , default_file = None ) : self . _config_exists ( config_file ) self . _specs_exists ( specs ) self . loaded_config = anyconfig . load ( self . config_file , ac_parser = 'yaml' ) if default_file is not None : self . _merge_default ( default_file ) if self . specs is None : return self . loaded_config self . _validate ( ) return self . loaded_config

Read a config_file check the validity with a JSON Schema as specs and get default values from default_file if asked .


def table(rows):
    '''
    Output a simple table with several columns.
    '''

    output = '
'

    for row in rows:
        output += '
'
        for column in row:
            output += '{s}	'.format(s=column)
        output += '
'

    output += '
'

    return output

def table ( rows ) : output = '
' for row in rows : output += '
' for column in row : output += '{s}	' . format ( s = column ) output += '
' output += '
' return output

Output a simple table with several columns .


def link(url, text='', classes='', target='', get="", **kwargs):
    '''
    Output a link tag.
    '''

    if not (url.startswith('http') or url.startswith('/')):
        # Handle additional reverse args.
        urlargs = {}

        for arg, val in kwargs.items():
            if arg[:4] == "url_":
                urlargs[arg[4:]] = val

        url = reverse(url, kwargs=urlargs)
        if get:
            url += '?' + get

    return html.tag('a', text or url, {
        'class': classes, 'target': target, 'href': url})

def
link
(
url
,
text
=
''
,
classes
=
''
,
target
=
''
,
get
=
""
,
*
*
kwargs
)
:
if
not
(
url
.
startswith
(
'http'
)
or
url
.
startswith
(
'/'
)
)
:
# Handle additional reverse args.
urlargs
=
{
}
for
arg
,
val
in
kwargs
.
items
(
)
:
if
arg
[
:
4
]
==
"url_"
:
urlargs
[
arg
[
4
:
]
]
=
val
url
=
reverse
(
url
,
kwargs
=
urlargs
)
if
get
:
url
+=
'?'
+
get
return
html
.
tag
(
'a'
,
text
or
url
,
{
'class'
:
classes
,
'target'
:
target
,
'href'
:
url
}
)


def jsfile(url):
    '''
    Output a script tag to a js file.
    '''

    if not url.startswith('http://') and not url[:1] == '/':
        #add media_url for relative paths
        url = settings.STATIC_URL + url

    return '<script type="text/javascript" src="{src}"></script>'.format(
        src=url)


def cssfile(url):
    '''
    Output a link tag to a css stylesheet.
    '''

    if not url.startswith('http://') and not url[:1] == '/':
        #add media_url for relative paths
        url = settings.STATIC_URL + url

    return '<link href="{src}" rel="stylesheet">'.format(src=url)

def
cssfile
(
url
)
:
if
not
url
.
startswith
(
'http://'
)
and
not
url
[
:
1
]
==
'/'
:
#add media_url for relative paths
url
=
settings
.
STATIC_URL
+
url
return
'<link href="{src}" rel="stylesheet">'
.
format
(
src
=
url
)


def img(url, alt='', classes='', style=''):
    '''
    Image tag helper.
    '''

    if not url.startswith('http://') and not url[:1] == '/':
        #add media_url for relative paths
        url = settings.STATIC_URL + url

    attr = {
        'class': classes,
        'alt': alt,
        'style': style,
        'src': url
    }

    return html.tag('img', '', attr)

def
img
(
url
,
alt
=
''
,
classes
=
''
,
style
=
''
)
:
if
not
url
.
startswith
(
'http://'
)
and
not
url
[
:
1
]
==
'/'
:
#add media_url for relative paths
url
=
settings
.
STATIC_URL
+
url
attr
=
{
'class'
:
classes
,
'alt'
:
alt
,
'style'
:
style
,
'src'
:
url
}
return
html
.
tag
(
'img'
,
''
,
attr
)


def sub(value, arg):
    """Subtract the arg from the value."""
    try:
        return valid_numeric(value) - valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value - arg
        except Exception:
            return ''

def sub ( value , arg ) : try : return valid_numeric ( value ) - valid_numeric ( arg ) except ( ValueError , TypeError ) : try : return value - arg except Exception : return ''


def mul(value, arg):
    """Multiply the arg with the value."""
    try:
        return valid_numeric(value) * valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value * arg
        except Exception:
            return ''

def mul ( value , arg ) : try : return valid_numeric ( value ) * valid_numeric ( arg ) except ( ValueError , TypeError ) : try : return value * arg except Exception : return ''


def div(value, arg):
    """Divide the arg by the value."""
    try:
        return valid_numeric(value) / valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value / arg
        except Exception:
            return ''

def div ( value , arg ) : try : return valid_numeric ( value ) / valid_numeric ( arg ) except ( ValueError , TypeError ) : try : return value / arg except Exception : return ''


def mod(value, arg):
    """Return the modulo value."""
    try:
        return valid_numeric(value) % valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value % arg
        except Exception:
            return ''

def mod ( value , arg ) : try : return valid_numeric ( value ) % valid_numeric ( arg ) except ( ValueError , TypeError ) : try : return value % arg except Exception : return ''


def model_verbose(obj, capitalize=True):
    """
    Return the verbose name of a model.
    The obj argument can be either a Model instance, or a ModelForm instance.
    This allows to retrieve the verbose name of the model of a ModelForm
    easily, without adding extra context vars.
    """

    if isinstance(obj, ModelForm):
        name = obj._meta.model._meta.verbose_name
    elif isinstance(obj, Model):
        name = obj._meta.verbose_name
    else:
        raise Exception('Unhandled type: ' + type(obj))

    return name.capitalize() if capitalize else name

def
model_verbose
(
obj
,
capitalize
=
True
)
:
if
isinstance
(
obj
,
ModelForm
)
:
name
=
obj
.
_meta
.
model
.
_meta
.
verbose_name
elif
isinstance
(
obj
,
Model
)
:
name
=
obj
.
_meta
.
verbose_name
else
:
raise
Exception
(
'Unhandled type: '
+
type
(
obj
)
)
return
name
.
capitalize
(
)
if
capitalize
else
name

Return the verbose name of a model . The obj argument can be either a Model instance or a ModelForm instance . This allows to retrieve the verbose name of the model of a ModelForm easily without adding extra context vars .


def split_user_input(line, pattern=None):
    """Split user input into initial whitespace, escape character, function part
    and the rest.
    """
    # We need to ensure that the rest of this routine deals only with unicode
    encoding = get_stream_enc(sys.stdin, 'utf-8')
    line = py3compat.cast_unicode(line, encoding)

    if pattern is None:
        pattern = line_split
    match = pattern.match(line)
    if not match:
        # print "match failed for line '%s'" % line
        try:
            ifun, the_rest = line.split(None,1)
        except ValueError:
            # print "split failed for line '%s'" % line
            ifun, the_rest = line, u''
        pre = re.match('^(\s*)(.*)',line).groups()[0]
        esc = ""
    else:
        pre, esc, ifun, the_rest = match.groups()

    #print 'line:<%s>' % line # dbg
    #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
    return pre, esc or '', ifun.strip(), the_rest.lstrip()

Split user input into initial whitespace escape character function part and the rest .


def options(self, parser, env):
        """
        Register command-line options.
        """
        parser.add_option("--processes", action="store",
                          default=env.get('NOSE_PROCESSES', 0),
                          dest="multiprocess_workers",
                          metavar="NUM",
                          help="Spread test run among this many processes. "
                          "Set a number equal to the number of processors "
                          "or cores in your machine for best results. "
                          "[NOSE_PROCESSES]")
        parser.add_option("--process-timeout", action="store",
                          default=env.get('NOSE_PROCESS_TIMEOUT', 10),
                          dest="multiprocess_timeout",
                          metavar="SECONDS",
                          help="Set timeout for return of results from each "
                          "test runner process. [NOSE_PROCESS_TIMEOUT]")
        parser.add_option("--process-restartworker", action="store_true",
                          default=env.get('NOSE_PROCESS_RESTARTWORKER', False),
                          dest="multiprocess_restartworker",
                          help="If set, will restart each worker process once"
                          " their tests are done, this helps control memory "
                          "leaks from killing the system. "
                          "[NOSE_PROCESS_RESTARTWORKER]")


def add_builtin(self, key, value):
        """Add a builtin and save the original."""
        bdict = __builtin__.__dict__
        orig = bdict.get(key, BuiltinUndefined)
        if value is HideBuiltin:
            if orig is not BuiltinUndefined: #same as 'key in bdict'
                self._orig_builtins[key] = orig
                del bdict[key]
        else:
            self._orig_builtins[key] = orig
            bdict[key] = value

def
add_builtin
(
self
,
key
,
value
)
:
bdict
=
__builtin__
.
__dict__
orig
=
bdict
.
get
(
key
,
BuiltinUndefined
)
if
value
is
HideBuiltin
:
if
orig
is
not
BuiltinUndefined
:
#same as 'key in bdict'
self
.
_orig_builtins
[
key
]
=
orig
del
bdict
[
key
]
else
:
self
.
_orig_builtins
[
key
]
=
orig
bdict
[
key
]
=
value


def remove_builtin(self, key, orig):
        """Remove an added builtin and re-set the original."""
        if orig is BuiltinUndefined:
            del __builtin__.__dict__[key]
        else:
            __builtin__.__dict__[key] = orig

def remove_builtin ( self , key , orig ) : if orig is BuiltinUndefined : del __builtin__ . __dict__ [ key ] else : __builtin__ . __dict__ [ key ] = orig

Remove an added builtin and re - set the original .


def deactivate(self):
        """Remove any builtins which might have been added by add_builtins, or
        restore overwritten ones to their previous values."""
        remove_builtin = self.remove_builtin
        for key, val in self._orig_builtins.iteritems():
            remove_builtin(key, val)
        self._orig_builtins.clear()
        self._builtins_added = False

def deactivate ( self ) : remove_builtin = self . remove_builtin for key , val in self . _orig_builtins . iteritems ( ) : remove_builtin ( key , val ) self . _orig_builtins . clear ( ) self . _builtins_added = False

Remove any builtins which might have been added by add_builtins or restore overwritten ones to their previous values .


def _find_url_name(self, index_url, url_name, req):
        """
        Finds the true URL name of a package, when the given name isn't quite
        correct.
        This is usually used to implement case-insensitivity.
        """
        if not index_url.url.endswith('/'):
            # Vaguely part of the PyPI API... weird but true.
            # FIXME: bad to modify this?
            index_url.url += '/'
        page = self._get_page(index_url, req)
        if page is None:
            logger.critical('Cannot fetch index base URL %s', index_url)
            return
        norm_name = normalize_name(req.url_name)
        for link in page.links:
            base = posixpath.basename(link.path.rstrip('/'))
            if norm_name == normalize_name(base):
                logger.debug(
                    'Real name of requirement %s is %s', url_name, base,
                )
                return base
        return None

Finds the true URL name of a package when the given name isn t quite correct . This is usually used to implement case - insensitivity .