Example #1
0
def pygments_style(page, elem, dummy_uid='42', vlam=None):
    cssclass = config[page.username]['style']
    wrap = False
    if vlam is not None:
        show_vlam = create_show_vlam(page, elem, vlam)
    elif 'show_vlam' in elem.attrib['title']:
        show_vlam = create_show_vlam(page, elem, elem.attrib['title'])
        wrap = True
    else:
        show_vlam = None
    language = elem.attrib['title'].split()[0]
    if language in ['py_code', 'python_code']:
        language = "python"
    text = extract_code(elem)
    styled_code = _style(text, language, cssclass)
    if vlam is None:
        vlam = elem.attrib['title']
    if 'linenumber' in vlam:
        styled_code = add_linenumber(styled_code, vlam)

    markup = fromstring(styled_code)
    elem[:] = markup[:]
    elem.text = markup.text
    if 'class' in elem.attrib:
        elem.attrib['class'] += " " + CRUNCHY_PYGMENTS
    else:
        elem.attrib['class'] = CRUNCHY_PYGMENTS
    if not page.includes("pygment_cssclass"):
        # replacing class name for security reasons.
        page.add_css_code(HtmlFormatter(style=cssclass).get_style_defs("."+cssclass).replace(cssclass, CRUNCHY_PYGMENTS))
        page.add_include("pygment_cssclass")
    if wrap:
        wrap_in_div(elem, dummy_uid, '', "show_vlam", show_vlam)
    return text, show_vlam
Example #2
0
def insert_interpreter(page, elem, uid):
    """inserts an interpreter (and the js code to initialise an interpreter)"""

    vlam = elem.attrib["title"]
    interp_kind = select_type(vlam, config[page.username]["override_default_interpreter"], elem)

    # When a security mode is set to "display ...", we only parse the
    # page, but no Python execution from is allowed from that page.
    # If that is the case, we won't include javascript either, to make
    # thus making the source easier to read.
    show = True
    if not ("display" in config[page.username]["page_security_level"](page.url) or interp_kind == None):
        include_interpreter(interp_kind, page, uid)
        log_id = util.extract_log_id(vlam)
        if log_id:
            t = "interpreter"
            config[page.username]["logging_uids"][uid] = (log_id, t)
    else:
        log_id = False
        show = False

    # then we can go ahead and add html markup, extracting the Python
    # code to be executed in the process - we will not need this code;
    # this could change in a future version where we could add a button to
    # have the code automatically "injected" and executed by the
    # interpreter, thus saving some typing by the user.

    python_code = util.extract_code(elem)
    if util.is_interpreter_session(python_code):
        elem.attrib["title"] = "pycon"
        python_code = util.extract_code_from_interpreter(python_code)
    else:
        elem.attrib["title"] = "python"
    dummy, show_vlam = plugin["services"].style(page, elem, None, vlam)
    elem.attrib["title"] = vlam
    if log_id:
        config[page.username]["log"][log_id] = [tostring(elem)]
    util.wrap_in_div(elem, uid, vlam, "interpreter", show_vlam)

    if config[page.username]["popups"] and interp_kind is not None and not page.includes("interpreter_helper"):
        page.add_include("interpreter_helper")
        # insert popup helper
        img = Element(
            "img",
            src="/images/help.png",
            style="height:32px;",
            title="cluetip Hello %s! " % page.username + titles[interp_kind],
            rel=help_files[interp_kind],
        )
        elem.append(img)
        plugin["services"].insert_cluetip(page, img, uid)
    plugin["services"].insert_io_subwidget(page, elem, uid, interp_kind=interp_kind, sample_code=python_code, show=show)
    plugin["services"].insert_tooltip(page, elem, uid)
    return
Example #3
0
def insert_bare_editor(page, elem, uid):
    """inserts a 'bare' editor, python code, but no execution buttons.

    Common code to both insert_editor() and insert_alternate_python().
    """
    vlam = elem.attrib["title"]
    log_id = util.extract_log_id(vlam)
    if log_id:
        t = 'editor'
        config[page.username]['logging_uids'][uid] = (log_id, t)

    # When a security mode is set to "display ...", we only parse the
    # page, but no Python execution from is allowed from that page.
    # If that is the case, we won't include javascript either,
    # thus making the source easier to read.
    if 'display' not in config[page.username]['page_security_level'](page.url):
        if not page.includes("exec_included"):
            page.add_include("exec_included")
            page.add_js_code(exec_jscode)
    # then we can go ahead and add html markup, extracting the Python
    # code to be executed in the process
    python_code = util.extract_code(elem)
    if util.is_interpreter_session(python_code):
        elem.attrib['title'] = "pycon"
        python_code = util.extract_code_from_interpreter(python_code)
    else:
        elem.attrib['title'] = "python"
    dummy, show_vlam = plugin['services'].style(page, elem, None, vlam)
    elem.attrib['title'] = vlam
    if log_id:
        config[page.username]['log'][log_id] = [tostring(elem)]
    util.wrap_in_div(elem, uid, vlam, "editor", show_vlam)
    if config[page.username]['popups']:
        # insert popup helper
        img = Element("img",
                      src="/images/help.png",
                      style="height:32px;",
                      title="cluetip Hello %s! " % page.username +
                      "This is an Editor; click for more",
                      rel="/docs/popups/editor.html")
        elem.append(img)
        plugin['services'].insert_cluetip(page, img, uid)

    if (("no_copy" in vlam) and not ("no_pre" in vlam)) or (not python_code):
        python_code = "\n"
    plugin['services'].insert_editor_subwidget(page, elem, uid, python_code)
    return vlam
Example #4
0
def insert_bare_editor(page, elem, uid):
    """inserts a 'bare' editor, python code, but no execution buttons.

    Common code to both insert_editor() and insert_alternate_python().
    """
    vlam = elem.attrib["title"]
    log_id = util.extract_log_id(vlam)
    if log_id:
        t = 'editor'
        config[page.username]['logging_uids'][uid] = (log_id, t)

    # When a security mode is set to "display ...", we only parse the
    # page, but no Python execution from is allowed from that page.
    # If that is the case, we won't include javascript either,
    # thus making the source easier to read.
    if 'display' not in config[page.username]['page_security_level'](page.url):
        if not page.includes("exec_included"):
            page.add_include("exec_included")
            page.add_js_code(exec_jscode)
    # then we can go ahead and add html markup, extracting the Python
    # code to be executed in the process
    python_code = util.extract_code(elem)
    if util.is_interpreter_session(python_code):
        elem.attrib['title'] = "pycon"
        python_code = util.extract_code_from_interpreter(python_code)
    else:
        elem.attrib['title'] = "python"
    dummy, show_vlam = plugin['services'].style(page, elem, None, vlam)
    elem.attrib['title'] = vlam
    if log_id:
        config[page.username]['log'][log_id] = [tostring(elem)]
    util.wrap_in_div(elem, uid, vlam, "editor", show_vlam)
    if config[page.username]['popups']:
        # insert popup helper
        img = Element("img", src="/images/help.png", style="height:32px;",
                title = "cluetip Hello %s! "%page.username + "This is an Editor; click for more",
                rel = "/docs/popups/editor.html")
        elem.append(img)
        plugin['services'].insert_cluetip(page, img, uid)

    if (("no_copy" in vlam) and not ("no_pre" in vlam)) or (not python_code):
        python_code = "\n"
    plugin['services'].insert_editor_subwidget(page, elem, uid, python_code)
    return vlam
Example #5
0
def select_type(vlam, c, elem):
    """determines the interpreter type that should be inserted based on
       user configuration and vlam information."""
    if c is None:
        # go with interpreter specified in tutorial
        if "isolated" in vlam or "Human" in vlam:
            interp_kind = "isolated"
        elif "parrot" in vlam:
            interp_kind = "parrot"
        elif "Parrots" in vlam:
            interp_kind = "Parrots"
        elif "TypeInfoConsole" in vlam:
            interp_kind = "TypeInfoConsole"
        #  Unfortunately, IPython interferes with Crunchy; I'm commenting it out, keeping it in as a reference.
        ##    elif "ipython" in vlam:
        ##        interp_kind = "ipython"
        elif "python_tutorial" in vlam:
            text = util.extract_code(elem)
            if util.is_interpreter_session(text):
                interp_kind = "borg"
            else:
                return  # assume it is not an interpreter session.
        else:
            interp_kind = "borg"
    else:
        if c == "isolated" or c == "Human":
            interp_kind = "isolated"
        elif c == "parrot":
            interp_kind = "parrot"
        elif c == "Parrots":
            interp_kind = "Parrots"
        elif "TypeInfoConsole" in vlam:
            interp_kind = "TypeInfoConsole"
        #  Unfortunately, IPython interferes with Crunchy; I'm commenting it out, keeping it in as a reference.
        ##    elif "ipython" in vlam:
        ##        interp_kind = "ipython"
        else:
            interp_kind = "borg"
    return interp_kind
Example #6
0
def select_type(vlam, c, elem):
    '''determines the interpreter type that should be inserted based on
       user configuration and vlam information.'''
    if c is None:
        # go with interpreter specified in tutorial
        if "isolated" in vlam or "Human" in vlam:
            interp_kind = "isolated"
        elif 'parrot' in vlam:
            interp_kind = 'parrot'
        elif 'Parrots' in vlam:
            interp_kind = "Parrots"
        elif 'TypeInfoConsole' in vlam:
            interp_kind = "TypeInfoConsole"
    #  Unfortunately, IPython interferes with Crunchy; I'm commenting it out, keeping it in as a reference.
    ##    elif "ipython" in vlam:
    ##        interp_kind = "ipython"
        elif 'python_tutorial' in vlam:
            text = util.extract_code(elem)
            if util.is_interpreter_session(text):
                interp_kind = 'borg'
            else:
                return  # assume it is not an interpreter session.
        else:
            interp_kind = "borg"
    else:
        if c == "isolated" or c == "Human":
            interp_kind = "isolated"
        elif c == 'parrot':
            interp_kind = 'parrot'
        elif c == 'Parrots':
            interp_kind = "Parrots"
        elif 'TypeInfoConsole' in vlam:
            interp_kind = "TypeInfoConsole"
    #  Unfortunately, IPython interferes with Crunchy; I'm commenting it out, keeping it in as a reference.
    ##    elif "ipython" in vlam:
    ##        interp_kind = "ipython"
        else:
            interp_kind = "borg"
    return interp_kind
Example #7
0
def pygments_style(page, elem, dummy_uid='42', vlam=None):
    cssclass = config[page.username]['style']
    wrap = False
    if vlam is not None:
        show_vlam = create_show_vlam(page, elem, vlam)
    elif 'show_vlam' in elem.attrib['title']:
        show_vlam = create_show_vlam(page, elem, elem.attrib['title'])
        wrap = True
    else:
        show_vlam = None
    language = elem.attrib['title'].split()[0]
    if language in ['py_code', 'python_code']:
        language = "python"
    text = extract_code(elem)
    styled_code = _style(text, language, cssclass)
    if vlam is None:
        vlam = elem.attrib['title']
    if 'linenumber' in vlam:
        styled_code = add_linenumber(styled_code, vlam)

    markup = fromstring(styled_code)
    elem[:] = markup[:]
    elem.text = markup.text
    if 'class' in elem.attrib:
        elem.attrib['class'] += " " + CRUNCHY_PYGMENTS
    else:
        elem.attrib['class'] = CRUNCHY_PYGMENTS
    if not page.includes("pygment_cssclass"):
        # replacing class name for security reasons.
        page.add_css_code(
            HtmlFormatter(style=cssclass).get_style_defs("." +
                                                         cssclass).replace(
                                                             cssclass,
                                                             CRUNCHY_PYGMENTS))
        page.add_include("pygment_cssclass")
    if wrap:
        wrap_in_div(elem, dummy_uid, '', "show_vlam", show_vlam)
    return text, show_vlam
Example #8
0
def pdb_widget_callback(page, elem, uid):
    """Handles embedding suitable code into the page in order to display and
    run pdb"""

    # When a security mode is set to "display ...", we only parse the
    # page, but no Python execution from is allowed from that page.
    # If that is the case, we won't include javascript either, to make
    # thus making the source easier to read.
    if 'display' not in config[page.username]['page_security_level'](page.url):
        if not page.includes("pdb_included"):
            page.add_include("pdb_included")
            #element tree always escape < to &lt; and break my js code , so...
            page.insert_js_file("/pdb_js%s.js"%plugin['session_random_id'])
        if not page.includes("pdb_css_code"):
            page.add_include("pdb_css_code")
            page.add_css_code(pdb_css)
    # next, we style the code, also extracting it in a useful form ...

    vlam = elem.attrib["title"]
    python_code = util.extract_code(elem)
    if util.is_interpreter_session(python_code):
        elem.attrib['title'] = "pycon"
        python_code = util.extract_code_from_interpreter(python_code)
    else:
        elem.attrib['title'] = "python"
    code, show_vlam = plugin['services'].style(page, elem, None, vlam)
    elem.attrib['title'] = vlam
    util.wrap_in_div(elem, uid, vlam, "pdb", show_vlam)

    plugin['services'].insert_editor_subwidget(page, elem, uid, python_code)

    t = SubElement(elem, "h4", style="background-color:white;color:darkblue;")
    t.text = _("Local Namespace")
    local_ns_div = SubElement(elem, "div")
    local_ns_div.attrib["id"] = "local_ns_%s"%uid

    #some spacing:
    SubElement(elem, "br")

    btn = SubElement(elem, "button")
    btn.text = _("Start PDB")
    btn.attrib["onclick"] = "init_pdb('%s');" %(uid)
    btn.attrib["id"] = "btn_start_pdb_%s" % uid

    btn = SubElement(elem, "button")
    btn.text = _("Next Step")
    btn.attrib["id"] = "btn_next_step_%s" % uid
    btn.attrib["disabled"] = "disabled"

    btn = SubElement(elem, "button")
    btn.text = _("Step Into")
    btn.attrib["id"] = "btn_step_into_%s" % uid
    btn.attrib["disabled"] = "disabled"

    btn = SubElement(elem, "button")
    btn.text = _("Return")
    btn.attrib["id"] = "btn_return_%s" % uid
    btn.attrib["disabled"] = "disabled"

    btn = SubElement(elem, "button")
    btn.text = _("Next Multiple Steps")
    btn.attrib["id"] = "btn_next_many_steps_%s" % uid
    btn.attrib["disabled"] = "disabled"
    input1 = SubElement(elem, 'input', id='input_many_'+uid, size='4', value='1')

    t = SubElement(elem, "h4", style="background-color:white;color:darkblue;")
    t.text = _("Output")
    # finally, an output subwidget:
    plugin['services'].insert_io_subwidget(page, elem, uid)

    #register before_ouput hook
    plugin['services'].register_io_hook('before_output', pdb_filter, uid)

    #create pdb file cache for uid
    pdb_py_files[uid] = {}
Example #9
0
def insert_interpreter(page, elem, uid):
    """inserts an interpreter (and the js code to initialise an interpreter)"""

    vlam = elem.attrib["title"]
    interp_kind = select_type(
        vlam, config[page.username]['override_default_interpreter'], elem)

    # When a security mode is set to "display ...", we only parse the
    # page, but no Python execution from is allowed from that page.
    # If that is the case, we won't include javascript either, to make
    # thus making the source easier to read.
    show = True
    if not ('display' in config[page.username]['page_security_level'](page.url)
            or interp_kind == None):
        include_interpreter(interp_kind, page, uid)
        log_id = util.extract_log_id(vlam)
        if log_id:
            t = 'interpreter'
            config[page.username]['logging_uids'][uid] = (log_id, t)
    else:
        log_id = False
        show = False

    # then we can go ahead and add html markup, extracting the Python
    # code to be executed in the process - we will not need this code;
    # this could change in a future version where we could add a button to
    # have the code automatically "injected" and executed by the
    # interpreter, thus saving some typing by the user.

    python_code = util.extract_code(elem)
    if util.is_interpreter_session(python_code):
        elem.attrib['title'] = "pycon"
        python_code = util.extract_code_from_interpreter(python_code)
    else:
        elem.attrib['title'] = "python"
    dummy, show_vlam = plugin['services'].style(page, elem, None, vlam)
    elem.attrib['title'] = vlam
    if log_id:
        config[page.username]['log'][log_id] = [tostring(elem)]
    util.wrap_in_div(elem, uid, vlam, "interpreter", show_vlam)

    if config[page.username][
            'popups'] and interp_kind is not None and not page.includes(
                "interpreter_helper"):
        page.add_include("interpreter_helper")
        # insert popup helper
        img = Element("img",
                      src="/images/help.png",
                      style="height:32px;",
                      title="cluetip Hello %s! " % page.username +
                      titles[interp_kind],
                      rel=help_files[interp_kind])
        elem.append(img)
        plugin['services'].insert_cluetip(page, img, uid)
    plugin['services'].insert_io_subwidget(page,
                                           elem,
                                           uid,
                                           interp_kind=interp_kind,
                                           sample_code=python_code,
                                           show=show)
    plugin['services'].insert_tooltip(page, elem, uid)
    return
Example #10
0
def pdb_widget_callback(page, elem, uid):
    """Handles embedding suitable code into the page in order to display and
    run pdb"""

    # When a security mode is set to "display ...", we only parse the
    # page, but no Python execution from is allowed from that page.
    # If that is the case, we won't include javascript either, to make
    # thus making the source easier to read.
    if 'display' not in config[page.username]['page_security_level'](page.url):
        if not page.includes("pdb_included"):
            page.add_include("pdb_included")
            #element tree always escape < to &lt; and break my js code , so...
            page.insert_js_file("/pdb_js%s.js" % plugin['session_random_id'])
        if not page.includes("pdb_css_code"):
            page.add_include("pdb_css_code")
            page.add_css_code(pdb_css)
    # next, we style the code, also extracting it in a useful form ...

    vlam = elem.attrib["title"]
    python_code = util.extract_code(elem)
    if util.is_interpreter_session(python_code):
        elem.attrib['title'] = "pycon"
        python_code = util.extract_code_from_interpreter(python_code)
    else:
        elem.attrib['title'] = "python"
    code, show_vlam = plugin['services'].style(page, elem, None, vlam)
    elem.attrib['title'] = vlam
    util.wrap_in_div(elem, uid, vlam, "pdb", show_vlam)

    plugin['services'].insert_editor_subwidget(page, elem, uid, python_code)

    t = SubElement(elem, "h4", style="background-color:white;color:darkblue;")
    t.text = _("Local Namespace")
    local_ns_div = SubElement(elem, "div")
    local_ns_div.attrib["id"] = "local_ns_%s" % uid

    #some spacing:
    SubElement(elem, "br")

    btn = SubElement(elem, "button")
    btn.text = _("Start PDB")
    btn.attrib["onclick"] = "init_pdb('%s');" % (uid)
    btn.attrib["id"] = "btn_start_pdb_%s" % uid

    btn = SubElement(elem, "button")
    btn.text = _("Next Step")
    btn.attrib["id"] = "btn_next_step_%s" % uid
    btn.attrib["disabled"] = "disabled"

    btn = SubElement(elem, "button")
    btn.text = _("Step Into")
    btn.attrib["id"] = "btn_step_into_%s" % uid
    btn.attrib["disabled"] = "disabled"

    btn = SubElement(elem, "button")
    btn.text = _("Return")
    btn.attrib["id"] = "btn_return_%s" % uid
    btn.attrib["disabled"] = "disabled"

    btn = SubElement(elem, "button")
    btn.text = _("Next Multiple Steps")
    btn.attrib["id"] = "btn_next_many_steps_%s" % uid
    btn.attrib["disabled"] = "disabled"
    input1 = SubElement(elem,
                        'input',
                        id='input_many_' + uid,
                        size='4',
                        value='1')

    t = SubElement(elem, "h4", style="background-color:white;color:darkblue;")
    t.text = _("Output")
    # finally, an output subwidget:
    plugin['services'].insert_io_subwidget(page, elem, uid)

    #register before_ouput hook
    plugin['services'].register_io_hook('before_output', pdb_filter, uid)

    #create pdb file cache for uid
    pdb_py_files[uid] = {}