Exemplo n.º 1
0
def source_code(s, globs, system='sage'):
    r"""
    Format an object's source code to process and display in the
    Sage notebook.
    
    INPUT:

    - ``s`` - a string; a name of an object

    - ``globs`` - a string:object dictionary; a context in which to
      evaluate ``s``

    - ``system`` - a string (default: 'sage'); the system to which to
      confine the search

    OUTPUT:

    - a string containing the object's file, starting line number, and
      source code

    AUTHORS:

    - William Stein: partly taken from IPython for use in Sage

    - Nick Alexander: extensions
    """
    if system not in ['sage', 'python']:
        s = system + '.' + s

    try:
        obj = eval(s, globs)
    except NameError:
        return html_markup("No object %s"%s)
    
    try:
        try:
            return html_markup(obj._sage_src_())
        except:
            pass
        newline = "\n\n"  # blank line to start new paragraph
        indent = "    "   # indent source code to mark it as a code block

        filename = sageinspect.sage_getfile(obj)
        try:
            lines, lineno = sageinspect.sage_getsourcelines(obj)
        except IOError as msg:
            return html_markup(str(msg))
        src = indent.join(lines)
        src = indent + format_src(src)
        if not lineno is None:
            output = "**File:** %s"%filename
            output += newline
            output += "**Source Code** (starting at line %s)::"%lineno
            output += newline
            output += src
        return html_markup(output)
    
    except (TypeError, IndexError):
        return html_markup("Source code for {} is not available.".format(s) +
                           "\nUse {}? to see the documentation.".format(s))
Exemplo n.º 2
0
def source_code(s, globs, system='sage'):
    r"""
    Format an object's source code to process and display in the
    Sage notebook.
    
    INPUT:

    - ``s`` - a string; a name of an object

    - ``globs`` - a string:object dictionary; a context in which to
      evaluate ``s``

    - ``system`` - a string (default: 'sage'); the system to which to
      confine the search

    OUTPUT:

    - a string containing the object's file, starting line number, and
      source code

    AUTHORS:

    - William Stein: partly taken from IPython for use in Sage

    - Nick Alexander: extensions
    """
    if system not in ['sage', 'python']:
        s = system + '.' + s

    try:
        obj = eval(s, globs)
    except NameError:
        return html_markup("No object %s" % s)

    try:
        try:
            return html_markup(obj._sage_src_())
        except:
            pass
        newline = "\n\n"  # blank line to start new paragraph
        indent = "    "  # indent source code to mark it as a code block

        filename = sageinspect.sage_getfile(obj)
        try:
            lines, lineno = sageinspect.sage_getsourcelines(obj)
        except IOError as msg:
            return html_markup(str(msg))
        src = indent.join(lines)
        src = indent + format_src(src)
        if not lineno is None:
            output = "**File:** %s" % filename
            output += newline
            output += "**Source Code** (starting at line %s)::" % lineno
            output += newline
            output += src
        return html_markup(output)

    except (TypeError, IndexError):
        return html_markup("Source code for {} is not available.".format(s) +
                           "\nUse {}? to see the documentation.".format(s))
Exemplo n.º 3
0
def docstring(obj_name, globs, system='sage'):
    r"""
    Format an object's docstring to process and display in the FEMhub
    notebook.
    
    INPUT:

    - ``obj_name`` - a string; a name of an object

    - ``globs`` - a string:object dictionary; a context in which to
      evaluate ``obj_name``

    - ``system`` - a string (default: 'sage'); the system to which to
      confine the search

    OUTPUT:

    - a string containing the object's file, type, definition, and
      docstring or a message stating the object is not defined

    AUTHORS:

    - William Stein: partly taken from IPython for use in FEMhub

    - Nick Alexander: extensions
    """
    if system not in ['sage', 'python']:
        obj_name = system + '.' + obj_name
    try:
        obj = eval(obj_name, globs)
    except (AttributeError, NameError, SyntaxError):
        return "No object '%s' currently defined."%obj_name
    s  = ''
    newline = "\n\n"  # blank line to start new paragraph
    try:
        filename = sageinspect.sage_getfile(obj)
        #i = filename.find('site-packages/sage/')
        #if i == -1:
        s += '**File:** %s'%filename
        s += newline
        #else:
        #    file = filename[i+len('site-packages/sage/'):]
        #    s += 'File:        <html><a href="src_browser?%s">%s</a></html>\n'%(file,file)
    except TypeError:
        pass
    s += '**Type:** %s'%type(obj)
    s += newline
    s += '**Definition:** %s'%sageinspect.sage_getdef(obj, obj_name)
    s += newline
    s += '**Docstring:**'
    s += newline
    s += sageinspect.sage_getdoc(obj, obj_name)
    s = s.rstrip()
    return html_markup(s)
Exemplo n.º 4
0
def docstring(obj_name, globs, system='sage'):
    r"""
    Format an object's docstring to process and display in the Sage
    notebook.
    
    INPUT:

    - ``obj_name`` - a string; a name of an object

    - ``globs`` - a string:object dictionary; a context in which to
      evaluate ``obj_name``

    - ``system`` - a string (default: 'sage'); the system to which to
      confine the search

    OUTPUT:

    - a string containing the object's file, type, definition, and
      docstring or a message stating the object is not defined

    AUTHORS:

    - William Stein: partly taken from IPython for use in Sage

    - Nick Alexander: extensions
    """
    if system not in ['sage', 'python']:
        obj_name = system + '.' + obj_name
    try:
        obj = eval(obj_name, globs)
    except (AttributeError, NameError, SyntaxError):
        return "No object '%s' currently defined." % obj_name
    s = ''
    newline = "\n\n"  # blank line to start new paragraph
    try:
        filename = sageinspect.sage_getfile(obj)
        #i = filename.find('site-packages/sage/')
        #if i == -1:
        s += '**File:** %s' % filename
        s += newline
        #else:
        #    file = filename[i+len('site-packages/sage/'):]
        #    s += 'File:        <html><a href="src_browser?%s">%s</a></html>\n'%(file,file)
    except TypeError:
        pass
    s += '**Type:** %s' % type(obj)
    s += newline
    s += '**Definition:** %s' % sageinspect.sage_getdef(obj, obj_name)
    s += newline
    s += '**Docstring:**'
    s += newline
    s += sageinspect.sage_getdoc(obj, obj_name)
    s = s.rstrip()
    return html_markup(s)
Exemplo n.º 5
0
def docstring(obj_name, globs, system="sage"):
    r"""
    Format an object's docstring to process and display in the Sage
    notebook.
    
    INPUT:

    - ``obj_name`` - a string; a name of an object

    - ``globs`` - a string:object dictionary; a context in which to
      evaluate ``obj_name``

    - ``system`` - a string (default: 'sage'); the system to which to
      confine the search

    OUTPUT:

    - a string containing the object's file, type, definition, and
      docstring or a message stating the object is not defined

    AUTHORS:

    - William Stein: partly taken from IPython for use in Sage

    - Nick Alexander: extensions

    TESTS:

    Check that Trac 10860 is fixed and we can handle Unicode help
    strings in the notebook::

        sage: from sagenb.misc.support import docstring
        sage: D = docstring("r.lm", globs=globals())
    """
    if system not in ["sage", "python"]:
        obj_name = system + "." + obj_name
    try:
        obj = eval(obj_name, globs)
    except (AttributeError, NameError, SyntaxError):
        return "No object '%s' currently defined." % obj_name
    s = ""
    newline = "\n\n"  # blank line to start new paragraph
    try:
        filename = sageinspect.sage_getfile(obj)
        # i = filename.find('site-packages/sage/')
        # if i == -1:
        s += "**File:** %s" % filename
        s += newline
        # else:
        #    file = filename[i+len('site-packages/sage/'):]
        #    s += 'File:        <html><a href="src_browser?%s">%s</a></html>\n'%(file,file)
    except TypeError:
        pass
    s += "**Type:** %s" % type(obj)
    s += newline
    s += "**Definition:** %s" % sageinspect.sage_getdef(obj, obj_name)
    s += newline
    s += "**Docstring:**"
    s += newline
    s += sageinspect.sage_getdoc(obj, obj_name)
    s = s.rstrip()
    return html_markup(s.decode("utf-8"))