Exemplo n.º 1
0
def push_input(request):
    """An http request handler to deal with stdin"""
    uid = request.args["uid"]
    pageid = uid.split("_")[0]
    # echo back to output:
    if python_version >= 3:
        request.data = request.data.decode('utf-8')
    in_to_browser = utilities.changeHTMLspecialCharacters(request.data)
    in_to_browser = in_to_browser.replace('\\', r'\\')
    output_buffers[pageid].put_output("<span class='%s'>"%interface.generic_output +
                                            in_to_browser + "</span>", uid)
    # display help menu on a seperate div
    if request.data.startswith("help("):
        output_buffers[pageid].help_flag = True

    # ipython style help
    if request.data.rstrip().endswith("?"):
        output_buffers[pageid].help_flag = True
        help_str = "help(" + request.data.rstrip()[:-1] + ")\n"
        input_buffers[uid].put(help_str)
    else:
        input_buffers[uid].put(request.data)

    request.send_response(200)
    request.end_headers()
Exemplo n.º 2
0
    def __init__(self, filehandle, url, username=None, remote=False, local=False):
        """
        read a page, processes it and outputs a completely transformed one,
        ready for display in browser.

        url should be just a path if crunchy accesses the page locally, or
        the full URL if it is remote.
        """
        if username is None:
            username = interface.unknown_user
        BasePage.__init__(self, username=username)
        self.url = url

        # Assign tutorial type
        self.is_remote = remote # True if remote tutorial, on the web
        self.is_local = local  # True if local tutorial, not from the server root
        if not (remote or local):
            self.is_from_root = True # if local tutorial from server root
        else:
            self.is_from_root = False

        # Create the proper tree structure from the html file
        try:
            self.create_tree(filehandle)  # assigns self.tree
        except: # reports formatted traceback in browser window
            text = changeHTMLspecialCharacters(handle_exception(False))
            self.tree = et.ElementTree(et.fromstring(
                            "<html><head/><body><pre>%s</pre></body></html>"%text))
            return

        # Removing pre-existing javascript, unwanted objects and
        # all kinds of other potential security holes
        remove_unwanted(self.tree, self) # from the security module

        self.find_head()  # assigns self.head
        self.find_body()  # assigns self.body

        # Crunchy's main work: processing vlam instructions, etc.
        try:
            self.process_tags()
        except Exception:
            self.body.clear()
            self.body.tag = "body"
            pre = et.Element('pre')
            pre.text = handle_exception(False)
            self.body.append(pre)
            return

        # adding the javascript for communication between the browser and the server
        self.insert_js_file("/javascript/jquery.js")
        self.add_js_code(comet_js % self.pageid)

        # Extra styling
        self.add_crunchy_style() # first Crunchy's style
        self.add_user_style()    # user's preferences can override Crunchy's
        return
Exemplo n.º 3
0
def load_python(request):
    """Loads python file from disk, inserts it into an html template
       and then creates new page
       """
    url = request.args["url"]
    # we may want to use urlopen for this?
    python_code = open(url).read()
    python_code = changeHTMLspecialCharacters(python_code)

    if interactive:
        interpreter_python_code = "__name__ = '__main__'\n" + python_code
    else:
        interpreter_python_code = python_code
    html_template = """
    <html>
    <head><title>%s</title></head>
    <body>
    <h1> %s </h1>
    <p>You can either use the interpreter to interact "live" with the
    Python file, or the editor.  To "feed" the file to the interpreter,
    first click on the editor icon next to it, and then click on the
    "Execute" button.
    <h3 > Using the interpreter </h3>
    <p>Click on the editor icon and feed the code to the interpreter.</p>
    <pre title="interpreter no_pre"> %s </pre>
     <h3 > Using the editor</h3>
    <pre title="editor"> %s </pre>

    </body>
    </html>
    """ % (url, url, interpreter_python_code, python_code)

    fake_file = Python_file(html_template)
    page = plugin['create_vlam_page'](fake_file,
                                      url,
                                      local=True,
                                      username=request.crunchy_username)

    request.send_response(200)
    request.end_headers()
    request.wfile.write(page.read().encode('utf-8'))
Exemplo n.º 4
0
    def write(self, data):
        """write some data"""

        # First, check to see whether this is intended for comet at
        # all. This lets us use pdb, among other things, without
        # characters being escaped for HTML.
        uid = threading.currentThread().getName()
        if not self.__redirect(uid):
            try:
                return self.default_out.write(data)
            except:
                return self.default_out.write(data.encode('utf-8'))

        # Note: even though we create interpreters in separate threads
        # identified by their uid, Borg interpreters share a common
        # state.  As a result, if we have long running code in one
        # Borg interpreter, there can be exchange of input or output between
        # the code running in that interpreter and code entered in another one.
        pageid = uid.split("_")[0]
        data = utilities.changeHTMLspecialCharacters(data)

        debug_msg("write --- data , " + data.replace('\\', r'\\'), 4)
        #Note: in the following, it is important to ensure that the
        # py_prompt class is surrounded by single quotes - not double ones.
        # normal prompt

        for _prompt in ['&gt;&gt;&gt; ', # normal prompt
                        '... ', # normal continuation prompt
                        '--&gt; ', # isolated prompt
                        '&lt;t&gt;&gt;&gt; ', # type info prompt
                        '_u__) ', # parrot
                        '_u__)) ' # Parrots
                        ]:
            dd = data.split('crunchy_py_prompt%s' % _prompt)
            data = ("<span class='%s'>%s" % (interface.generic_prompt, _prompt)).join(dd)

        data = data.replace('\\', r'\\')
        data = "<span class='%s'>%s</span>" % (self.buf_class, data)
        output_buffers[pageid].put_output(data, uid)
Exemplo n.º 5
0
def load_python(request):
    """Loads python file from disk, inserts it into an html template
       and then creates new page
       """
    url = request.args["url"]
    # we may want to use urlopen for this?
    python_code = open(url).read()
    python_code = changeHTMLspecialCharacters(python_code)

    if interactive:
        interpreter_python_code = "__name__ = '__main__'\n" + python_code
    else:
        interpreter_python_code = python_code
    html_template = """
    <html>
    <head><title>%s</title></head>
    <body>
    <h1> %s </h1>
    <p>You can either use the interpreter to interact "live" with the
    Python file, or the editor.  To "feed" the file to the interpreter,
    first click on the editor icon next to it, and then click on the
    "Execute" button.
    <h3 > Using the interpreter </h3>
    <p>Click on the editor icon and feed the code to the interpreter.</p>
    <pre title="interpreter no_pre"> %s </pre>
     <h3 > Using the editor</h3>
    <pre title="editor"> %s </pre>

    </body>
    </html>
    """ % (url, url, interpreter_python_code, python_code)

    fake_file = Python_file(html_template)
    page = plugin['create_vlam_page'](fake_file, url, local=True,
                                      username=request.crunchy_username)

    request.send_response(200)
    request.end_headers()
    request.wfile.write(page.read().encode('utf-8'))
Exemplo n.º 6
0
    def __init__(self,
                 filehandle,
                 url,
                 username=None,
                 remote=False,
                 local=False):
        """
        read a page, processes it and outputs a completely transformed one,
        ready for display in browser.

        url should be just a path if crunchy accesses the page locally, or
        the full URL if it is remote.
        """
        if username is None:
            username = interface.unknown_user
        BasePage.__init__(self, username=username)
        self.url = url

        # Assign tutorial type
        self.is_remote = remote  # True if remote tutorial, on the web
        self.is_local = local  # True if local tutorial, not from the server root
        if not (remote or local):
            self.is_from_root = True  # if local tutorial from server root
        else:
            self.is_from_root = False

        # Create the proper tree structure from the html file
        try:
            self.create_tree(filehandle)  # assigns self.tree
        except:  # reports formatted traceback in browser window
            text = changeHTMLspecialCharacters(handle_exception(False))
            self.tree = et.ElementTree(
                et.fromstring(
                    "<html><head/><body><pre>%s</pre></body></html>" % text))
            return

        # Removing pre-existing javascript, unwanted objects and
        # all kinds of other potential security holes
        remove_unwanted(self.tree, self)  # from the security module

        self.find_head()  # assigns self.head
        self.find_body()  # assigns self.body

        # Crunchy's main work: processing vlam instructions, etc.
        try:
            self.process_tags()
        except Exception:
            self.body.clear()
            self.body.tag = "body"
            pre = et.Element('pre')
            pre.text = handle_exception(False)
            self.body.append(pre)
            return

        # adding the javascript for communication between the browser and the server
        self.insert_js_file("/javascript/jquery.js")
        self.add_js_code(comet_js % self.pageid)

        # Extra styling
        self.add_crunchy_style()  # first Crunchy's style
        self.add_user_style()  # user's preferences can override Crunchy's
        return