Exemple #1
0
def generateTinyMCEmath(
    request,
    package,
    tinyMCEwin,
    tinyMCEwin_name,
    tinyMCEfield,
    latex_source,
    math_fontsize,
    preview_image_filename,
    preview_math_srcfile,
):

    """
    Based off of handleTinyMCEimageChoice(), 
    handleTinyMCEmath() is similar in that it places a .gif math image 
    (and a corresponding .tex LaTeX source file) into the previews dir.
    Rather than copying the image from a user-selected directory, though,
    this routine actually generates the math image using mimetex.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath("previews")

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert(_(u"Preview directory %s is a file, cannot replace it") % previewDir)
        log.error(
            "Couldn't preview tinyMCE-chosen image: " + "Preview dir %s is a file, cannot replace it" % previewDir
        )
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    # if errors == 0:
    #    localImagePath = Path(local_filename)
    #    if not localImagePath.exists() or not localImagePath.isfile():
    #        client.alert( \
    #             _(u'Image file %s is not found, cannot preview it') \
    #             % localImagePath)
    #        log.error("Couldn't find tinyMCE-chosen image: %s" \
    #                % localImagePath)
    #        callback_errors = "Image file %s not found, cannot preview" \
    #                % localImagePath
    #        errors += 1

    # the mimetex usage code was swiped from the Math iDevice:
    if latex_source <> "":

        # first write the latex_source out into the preview_math_srcfile,
        # such that it can then be passed into the compile command:
        math_filename = previewDir.joinpath(preview_math_srcfile)
        math_filename_str = math_filename.abspath().encode("utf-8")
        log.info("handleTinyMCEmath: using LaTeX source: " + latex_source)
        log.debug("writing LaTeX source into '" + math_filename_str + "'.")
        math_file = open(math_filename, "wb")
        # do we need to append a \n here?:
        math_file.write(latex_source)
        math_file.flush()
        math_file.close()

        try:
            use_latex_sourcefile = math_filename_str
            tempFileName = compile(use_latex_sourcefile, math_fontsize, latex_is_file=True)
        except Exception, e:
            client.alert(_("MimeTeX compile failed!\n%s" % str(e)))
            log.error("handleTinyMCEmath unable to compile LaTeX using " + "mimetex, error = " + str(e))
            raise

        # copy the file into previews
        server_filename = previewDir.joinpath(preview_image_filename)
        log.debug(
            "handleTinyMCEmath copying math image from '"
            + tempFileName
            + "' to '"
            + server_filename.abspath().encode("utf-8")
            + "'."
        )
        shutil.copyfile(tempFileName, server_filename.abspath().encode("utf-8"))

        # Delete the temp file made by compile
        Path(tempFileName).remove()
def generateTinyMCEmath(request, package, tinyMCEwin, tinyMCEwin_name, \
                         tinyMCEfield, latex_source, math_fontsize, \
                         preview_image_filename, preview_math_srcfile):
    """
    Based off of handleTinyMCEimageChoice(), 
    handleTinyMCEmath() is similar in that it places a .gif math image 
    (and a corresponding .tex LaTeX source file) into the previews dir.
    Rather than copying the image from a user-selected directory, though,
    this routine actually generates the math image using mimetex.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath('previews')

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " \
                + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert( \
            _(u'Preview directory %s is a file, cannot replace it') \
            % previewDir)
        log.error("Couldn't preview tinyMCE-chosen image: "+
                  "Preview dir %s is a file, cannot replace it" \
                  % previewDir)
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    #if errors == 0:
    #    localImagePath = Path(local_filename)
    #    if not localImagePath.exists() or not localImagePath.isfile():
    #        client.alert( \
    #             _(u'Image file %s is not found, cannot preview it') \
    #             % localImagePath)
    #        log.error("Couldn't find tinyMCE-chosen image: %s" \
    #                % localImagePath)
    #        callback_errors = "Image file %s not found, cannot preview" \
    #                % localImagePath
    #        errors += 1

    # the mimetex usage code was swiped from the Math iDevice:
    if latex_source <> "":

        # first write the latex_source out into the preview_math_srcfile,
        # such that it can then be passed into the compile command:
        math_filename = previewDir.joinpath(preview_math_srcfile)
        math_filename_str = math_filename.abspath().encode('utf-8')
        log.info("handleTinyMCEmath: using LaTeX source: " + latex_source)
        log.debug("writing LaTeX source into \'" \
                + math_filename_str + "\'.")
        math_file = open(math_filename, 'wb')
        # do we need to append a \n here?:
        math_file.write(latex_source)
        math_file.flush()
        math_file.close()

        try:
            use_latex_sourcefile = math_filename_str
            tempFileName = compile(use_latex_sourcefile, math_fontsize, \
                    latex_is_file=True)
        except Exception, e:
            client.alert(_('MimeTeX compile failed!\n%s' % str(e)))
            log.error("handleTinyMCEmath unable to compile LaTeX using "\
                +"mimetex, error = " + str(e))
            raise

        # copy the file into previews
        server_filename = previewDir.joinpath(preview_image_filename)
        log.debug("handleTinyMCEmath copying math image from \'"\
                + tempFileName + "\' to \'" \
                + server_filename.abspath().encode('utf-8') + "\'.")
        shutil.copyfile(tempFileName, \
                server_filename.abspath().encode('utf-8'))

        # Delete the temp file made by compile
        Path(tempFileName).remove()
Exemple #3
0
def previewTinyMCEimage(request, package, tinyMCEwin, tinyMCEwin_name, tinyMCEfield, local_filename, preview_filename):

    """
    Once an image is selected in the file browser that is spawned by the 
    TinyMCE image dialog, copy this file (which is local to the user's 
    machine) into the server space, under a preview directory 
    (after checking if this exists, and creating it if necessary).
    Note that this IS a "cheat", in violation of the client-server 
    separation, but can be done since we know that the eXe server is 
    actually sitting on the client host.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    log.debug(
        "handleTinyMCEimageChoice: image local = " + local_filename + ", base=" + os.path.basename(local_filename)
    )

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath("previews")

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert(_(u"Preview directory %s is a file, cannot replace it") % previewDir)
        log.error(
            "Couldn't preview tinyMCE-chosen image: " + "Preview dir %s is a file, cannot replace it" % previewDir
        )
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    if errors == 0:
        log.debug("handleTinyMCEimageChoice: originally, local_filename=" + local_filename)
        local_filename = unicode(local_filename, "utf-8")
        log.debug("handleTinyMCEimageChoice: in unicode, local_filename=" + local_filename)

        localImagePath = Path(local_filename)
        log.debug("handleTinyMCEimageChoice: after Path, localImagePath= " + localImagePath)
        if not localImagePath.exists() or not localImagePath.isfile():
            client.alert(_(u"Local file %s is not found, cannot preview it") % localImagePath)
            log.error("Couldn't find tinyMCE-chosen image: %s" % localImagePath)
            callback_errors = "Image file %s not found, cannot preview" % localImagePath
            errors += 1

    try:
        # joinpath needs its join arguments to already be in Unicode:
        # preview_filename = toUnicode(preview_filename);
        # but that's okay, cuz preview_filename is now URI safe, right?
        log.debug("URIencoded preview filename=" + preview_filename)

        server_filename = previewDir.joinpath(preview_filename)
        log.debug(
            "handleTinyMCEimageChoice copying image from '"
            + local_filename
            + "' to '"
            + server_filename.abspath()
            + "'."
        )
        shutil.copyfile(local_filename, server_filename.abspath())

        # new optional description file to provide the
        # actual base filename, such that once it is later processed
        # copied into the resources directory, it can be done with
        # only the basename.   Otherwise the resource filenames
        # are too long for some users, preventing them from making
        # backup CDs of the content, for example.
        #
        # Remember that the full path of the
        # file is only used here as an easy way to keep the names
        # unique WITHOUT requiring a roundtrip call from the Javascript
        # to this server, and back again, a process which does not
        # seem to work with tinyMCE in the mix.  BUT, once tinyMCE's
        # part is done, and this image processed, it can be returned
        # to just its basename, since the resource parts have their
        # own unique-ification mechanisms already in place.

        descrip_file_path = Path(server_filename + ".exe_info")
        log.debug(
            "handleTinyMCEimageChoice creating preview " + "description file '" + descrip_file_path.abspath() + "'."
        )
        descrip_file = open(descrip_file_path, "wb")

        # safety measures against TinyMCE, otherwise it will
        # later take ampersands and entity-escape them into '&amp;',
        # and filenames with hash signs will not be found, etc.:
        unspaced_filename = local_filename.replace(" ", "_")
        unhashed_filename = unspaced_filename.replace("#", "_num_")
        unamped_local_filename = unhashed_filename.replace("&", "_and_")
        log.debug("and setting new file basename as: " + unamped_local_filename)
        my_basename = os.path.basename(unamped_local_filename)
        descrip_file.write((u"basename=" + my_basename).encode("utf-8"))

        descrip_file.flush()
        descrip_file.close()

    except Exception, e:
        client.alert(_("SAVE FAILED!\n%s" % str(e)))
        log.error("handleTinyMCEimageChoice unable to copy local image " + "file to server prevew, error = " + str(e))
        raise
def previewTinyMCEimage(request, package, tinyMCEwin, tinyMCEwin_name, \
                         tinyMCEfield, local_filename, preview_filename):
    """
    Once an image is selected in the file browser that is spawned by the 
    TinyMCE image dialog, copy this file (which is local to the user's 
    machine) into the server space, under a preview directory 
    (after checking if this exists, and creating it if necessary).
    Note that this IS a "cheat", in violation of the client-server 
    separation, but can be done since we know that the eXe server is 
    actually sitting on the client host.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    log.debug('handleTinyMCEimageChoice: image local = ' + local_filename +
              ', base=' + os.path.basename(local_filename))

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath('previews')

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " \
                + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert( \
            _(u'Preview directory %s is a file, cannot replace it') \
            % previewDir)
        log.error("Couldn't preview tinyMCE-chosen image: "+
                  "Preview dir %s is a file, cannot replace it" \
                  % previewDir)
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    if errors == 0:
        log.debug('handleTinyMCEimageChoice: originally, local_filename=' +
                  local_filename)
        local_filename = unicode(local_filename, 'utf-8')
        log.debug('handleTinyMCEimageChoice: in unicode, local_filename=' +
                  local_filename)

        localImagePath = Path(local_filename)
        log.debug('handleTinyMCEimageChoice: after Path, localImagePath= ' +
                  localImagePath)
        if not localImagePath.exists() or not localImagePath.isfile():
            client.alert( \
                 _(u'Local file %s is not found, cannot preview it') \
                 % localImagePath)
            log.error("Couldn't find tinyMCE-chosen image: %s" \
                    % localImagePath)
            callback_errors = "Image file %s not found, cannot preview" \
                    % localImagePath
            errors += 1

    try:
        # joinpath needs its join arguments to already be in Unicode:
        #preview_filename = toUnicode(preview_filename);
        # but that's okay, cuz preview_filename is now URI safe, right?
        log.debug('URIencoded preview filename=' + preview_filename)

        server_filename = previewDir.joinpath(preview_filename)
        log.debug("handleTinyMCEimageChoice copying image from \'"\
                + local_filename + "\' to \'" \
                + server_filename.abspath() + "\'.")
        shutil.copyfile(local_filename, \
                server_filename.abspath())

        # new optional description file to provide the
        # actual base filename, such that once it is later processed
        # copied into the resources directory, it can be done with
        # only the basename.   Otherwise the resource filenames
        # are too long for some users, preventing them from making
        # backup CDs of the content, for example.
        #
        # Remember that the full path of the
        # file is only used here as an easy way to keep the names
        # unique WITHOUT requiring a roundtrip call from the Javascript
        # to this server, and back again, a process which does not
        # seem to work with tinyMCE in the mix.  BUT, once tinyMCE's
        # part is done, and this image processed, it can be returned
        # to just its basename, since the resource parts have their
        # own unique-ification mechanisms already in place.

        descrip_file_path = Path(server_filename + ".exe_info")
        log.debug("handleTinyMCEimageChoice creating preview " \
                + "description file \'" \
                + descrip_file_path.abspath() + "\'.")
        descrip_file = open(descrip_file_path, 'wb')

        # safety measures against TinyMCE, otherwise it will
        # later take ampersands and entity-escape them into '&amp;',
        # and filenames with hash signs will not be found, etc.:
        unspaced_filename = local_filename.replace(' ', '_')
        unhashed_filename = unspaced_filename.replace('#', '_num_')
        unamped_local_filename = unhashed_filename.replace('&', '_and_')
        log.debug("and setting new file basename as: " +
                  unamped_local_filename)
        my_basename = os.path.basename(unamped_local_filename)
        descrip_file.write((u"basename=" + my_basename).encode('utf-8'))

        descrip_file.flush()
        descrip_file.close()

    except Exception, e:
        client.alert(_('SAVE FAILED!\n%s' % str(e)))
        log.error("handleTinyMCEimageChoice unable to copy local image "\
                +"file to server prevew, error = " + str(e))
        raise