コード例 #1
0
    def set_image(self, text):
        """ set_image: Replace image string with downloaded image checksum
            Args:
                text (str): text to parse for image strings
            Returns:string with checksums in place of image strings and
                list of files that were downloaded from string
        """
        # Make sure image hasn't already been replaced
        if exercises.CONTENT_STORAGE_PLACEHOLDER in text:
            return text, []

        # Set up return values and regex
        exercise_file = None
        path_text = text.strip().replace('\\n', '')
        graphie_reg = re.compile(WEB_GRAPHIE_URL_REGEX, flags=re.IGNORECASE)
        graphie_match = graphie_reg.match(path_text)
        # If it is a web+graphie, download svg and json files,
        # Otherwise, download like other files
        if graphie_match:
            path_text = graphie_match.group().replace("web+graphie:", "")
            exercise_file = _ExerciseGraphieFile(path_text)
        elif get_base64_encoding(text):
            exercise_file = _ExerciseBase64ImageFile(path_text)
        else:
            exercise_file = _ExerciseImageFile(path_text)

        exercise_file.assessment_item = self
        filename = exercise_file.process_file()

        text = text.replace(
            path_text,
            exercises.CONTENT_STORAGE_FORMAT.format(
                exercise_file.get_replacement_str()))

        return text, [exercise_file]
コード例 #2
0
    def convert_base64_to_file(self):
        # Get hash of content for cache key
        hashed_content = hashlib.md5()
        hashed_content.update(self.encoding.encode('utf-8'))
        key = "ENCODED: {} (base64 encoded)".format(hashed_content.hexdigest())

        if not config.UPDATE and FILECACHE.get(key):
            return FILECACHE.get(key).decode('utf-8')

        config.LOGGER.info("\tConverting base64 to file")

        extension = get_base64_encoding(self.encoding).group(1)
        assert extension in [
            file_formats.PNG, file_formats.JPG, file_formats.JPEG
        ], "Base64 files must be images in jpg or png format"

        tempf = tempfile.NamedTemporaryFile(suffix=".{}".format(extension),
                                            delete=False)
        tempf.close()
        write_base64_to_file(self.encoding, tempf.name)
        filename = "{}.{}".format(get_hash(tempf.name), file_formats.PNG)

        copy_file_to_storage(filename, tempf.name)
        os.unlink(tempf.name)
        FILECACHE.set(key, bytes(filename, "utf-8"))
        return filename
コード例 #3
0
 def set_image(self, text):
     """
     Save image resource at `text` (path or url) to storage, then return the
     replacement string and the necessary exercicse image file object.
     Args:
       - text (str): path or url to parse as an exercise image resource
     Returns: (new_text, files)
       - `new_text` (str): replacement string for the original `text` string
       - `files` (list): list of files that were downloaded from `text`
     """
     # Make sure `text` hasn't already been processed
     if exercises.CONTENT_STORAGE_PLACEHOLDER in text:
         return text, []
     # Strip `text` of whitespace
     stripped_text = text.strip().replace('\\n', '')
     # If `stripped_text` is a web+graphie: path, we need special processing
     graphie_regex = re.compile(WEB_GRAPHIE_URL_REGEX, flags=re.IGNORECASE)
     graphie_match = graphie_regex.match(stripped_text)
     if graphie_match:
         is_web_plus_graphie = True
         graphie_rawpath = graphie_match.groupdict()['rawpath']
         graphie_path = graphie_rawpath.replace("//", "https://")
         exercise_image_file = _ExerciseGraphieFile(graphie_path)
     elif get_base64_encoding(stripped_text):
         is_web_plus_graphie = False
         exercise_image_file = _ExerciseBase64ImageFile(stripped_text)
     else:
         is_web_plus_graphie = False
         exercise_image_file = _ExerciseImageFile(stripped_text)
     # Setup link to assessment item
     exercise_image_file.assessment_item = self
     # Process file to make the replacement_str available
     _filename = exercise_image_file.process_file()
     # Get `new_text` = the replacement path for the image resource
     new_text = exercises.CONTENT_STORAGE_FORMAT.format(
         exercise_image_file.get_replacement_str())
     if is_web_plus_graphie:  # need to put back the `web+graphie:` prefix
         new_text = "web+graphie:" + new_text
     return new_text, [exercise_image_file]