def check(self): if self.clear: log.info('Clearing resource references') self.clear_resourceReferences() log.info('Adding resource references') for path in self.package.resourceDir.files(): if path in self.idevices: for idevice in self.idevices[path]: resource = Resource(idevice, path) if isinstance(idevice, FieldWithResources): galleryimage = GalleryImage(idevice, '', None, mkThumbnail=False) galleryimage._imageResource = resource if isinstance(idevice, Idevice) and idevice.klass == 'ImageMagnifierIdevice': idevice.imageMagnifier.imageResource = resource if isinstance(idevice, Idevice) and idevice.klass == 'GalleryIdevice': for image in idevice.images: if image._imageResource.storageName == resource.storageName: image._imageResource = resource break elif image._thumbnailResource.storageName == resource.storageName: image._thumbnailResource = resource break elif self.package._backgroundImg and path == self.package._backgroundImg.path: self.package._backgroundImg = Resource(self.package, path) for check in dir(self): if check.startswith('check_'): fn = getattr(self, check) log.info('Checking %s' % check[6:]) fn() return self.inconsistencies
def check(self): if self.clear: log.info('Clearing resource references') self.clear_resourceReferences() log.info('Adding resource references') for path in self.package.resourceDir.files(): if path in self.idevices: for idevice in self.idevices[path]: try: resource = Resource(idevice, path) except: msg = "%s referenced in idevice %s of node %s not exists" % (path, idevice.idevice.klass, idevice.parentNode.title) log.error('New inconsistency of type packageResourceNonExistant: %s' % (msg)) continue if isinstance(idevice, FieldWithResources): galleryimage = GalleryImage(idevice, '', None, mkThumbnail=False) galleryimage._imageResource = resource if isinstance(idevice, Idevice) and idevice.klass == 'ImageMagnifierIdevice': idevice.imageMagnifier.imageResource = resource if isinstance(idevice, Idevice) and idevice.klass == 'GalleryIdevice': for image in idevice.images: if image._imageResource.storageName == resource.storageName: image._imageResource = resource break elif image._thumbnailResource.storageName == resource.storageName: image._thumbnailResource = resource break elif self.package._backgroundImg and path == self.package._backgroundImg.path: self.package._backgroundImg = Resource(self.package, path) for check in dir(self): if check.startswith('check_'): fn = getattr(self, check) log.info('Checking %s' % check[6:]) fn() return self.inconsistencies
def check(self): if self.clear: log.info('Clearing resource references') self.clear_resourceReferences() log.info('Adding resource references') for path in self.package.resourceDir.files(): if path in self.idevices: for idevice in self.idevices[path]: try: resource = Resource(idevice, path) except: msg = "%s referenced in idevice %s of node %s not exists" % ( path, idevice.idevice.klass, idevice.parentNode.title) log.error( 'New inconsistency of type packageResourceNonExistant: %s' % (msg)) continue if isinstance(idevice, FieldWithResources): galleryimage = GalleryImage(idevice, '', None, mkThumbnail=False) galleryimage._imageResource = resource if isinstance( idevice, Idevice ) and idevice.klass == 'ImageMagnifierIdevice': idevice.imageMagnifier.imageResource = resource if isinstance( idevice, Idevice) and idevice.klass == 'GalleryIdevice': for image in idevice.images: if image._imageResource.storageName == resource.storageName: image._imageResource = resource break elif image._thumbnailResource.storageName == resource.storageName: image._thumbnailResource = resource break elif self.package._backgroundImg and path == self.package._backgroundImg.path: self.package._backgroundImg = Resource(self.package, path) for check in dir(self): if check.startswith('check_'): fn = getattr(self, check) log.info('Checking %s' % check[6:]) fn() return self.inconsistencies
def check(self): if self.clear: log.info('Clearing resource references') self.clear_resourceReferences() log.info('Adding resource references') for path in self.package.resourceDir.files(): if path in self.idevices: for idevice in self.idevices[path]: resource = Resource(idevice, path) if isinstance(idevice, FieldWithResources): galleryimage = GalleryImage(idevice, '', None, mkThumbnail=False) galleryimage._imageResource = resource if isinstance( idevice, Idevice ) and idevice.klass == 'ImageMagnifierIdevice': idevice.imageMagnifier.imageResource = resource if isinstance( idevice, Idevice) and idevice.klass == 'GalleryIdevice': for image in idevice.images: if image._imageResource.storageName == resource.storageName: image._imageResource = resource break elif image._thumbnailResource.storageName == resource.storageName: image._thumbnailResource = resource break elif self.package._backgroundImg and path == self.package._backgroundImg.path: self.package._backgroundImg = Resource(self.package, path) for check in dir(self): if check.startswith('check_'): fn = getattr(self, check) log.info('Checking %s' % check[6:]) fn() return self.inconsistencies
def convertToFreeText(self): """ Actually do the Converting of ImageWithTextIdevice -> FreeTextIdevice, now that FreeText can hold embeddded images. """ new_content = "" # ensure that an image resource still exists on this ImageWithText, # before trying to add it into the FreeText idevice. # Why? corrupt packages have been seen missing resources... # (usually in with extra package objects as well, probably # from old code doing faulty Extracts, or somesuch nonesense) imageResource_exists = False if self.image.imageResource: # also ensure that it has the correct md5 checksum, since there was # a period in which resource checksums were being created before # the resource zip file was fully closed, and not flushed out: self.image.imageResource.checksumCheck() if os.path.exists(self.image.imageResource.path) and \ os.path.isfile(self.image.imageResource.path): imageResource_exists = True else: log.warn("Couldn't find ImageWithText image when upgrading "\ + self.image.imageResource.storageName) if imageResource_exists: new_content += "<img src=\"resources/" \ + self.image.imageResource.storageName + "\" " if self.image.height: new_content += "height=\"" + self.image.height + "\" " if self.image.width: new_content += "width=\"" + self.image.width + "\" " new_content += "/> \n" elif self.image.imageResource: new_content += "<BR>\n[WARNING: missing image: " \ + self.image.imageResource.storageName + "]\n" if self.caption != "": new_content += "<BR>\n[" + self.caption + "]\n" if self.text.content != "": new_content += "<P>\n" + self.text.content + "\n" # note: this is given a text field which itself did NOT yet have # any embedded media! easier, eh? replacementIdev = FreeTextIdevice(new_content) ########### # now, copy that content field's content into its _w_resourcePaths, # and properly remove the resource directory via Massage.... # for its _wo_resourcePaths: # note that replacementIdev's content field's content # is automatically set at its constructor (above), # as is the default content_w_resourcePaths (a copy of content) # AND the default content_wo_resourcePaths (a copy of content), # so only need to update the content_wo_resourcePaths: replacementIdev.content.content_wo_resourcePaths = \ replacementIdev.content.MassageContentForRenderView( \ replacementIdev.content.content_w_resourcePaths) # Design note: ahhhhh, the above is a good looking reason to possibly # have the MassageContentForRenderView() method # just assume its content_w_resourcePaths as the input # and write the output to its content_wo_resourcePaths..... ####### # next step, add the new IDevice into the same node as this one self.parentNode.addIdevice(replacementIdev) # in passing GalleryImage into the FieldWithResources, # that content field needs to be sure to have an updated # parentNode, courtesy of its idevice: replacementIdev.content.setParentNode() # and semi-manually add/create the current image # resource into the FreeTextIdevice's TextAreaField, content. # the content text will have already been taken care of above, # including the ideal <img src=...> including resources path, # but still need the actual image resource: if imageResource_exists: # Not sure why this can't be imported up top, but it gives # ImportError: cannot import name GalleryImages, # so here it be: from exe.engine.galleryidevice import GalleryImage full_image_path = self.image.imageResource.path new_GalleryImage = GalleryImage(replacementIdev.content, \ self.caption, full_image_path, mkThumbnail=False) # and move it up to the position following this node! while ( self.parentNode.idevices.index(replacementIdev) \ > ( (self.parentNode.idevices.index(self) + 1))): replacementIdev.movePrev() # finally: delete THIS idevice itself, deleting it from the node self.delete()
def convertToFreeText(self): """ Actually do the Converting of MathsIdevice -> FreeTextIdevice, now that FreeText can hold embeddded images. """ new_content = "" # ensure that an image resource still exists on this ImageWithText, # before trying to add it into the FreeText idevice. # Why? corrupt packages have been seen missing resources... # (usually in with extra package objects as well, probably # from old code doing faulty Extracts, or somesuch nonesense) imageResource_exists = False if not self.content.gifResource is None: if os.path.exists(self.content.gifResource.path) and \ os.path.isfile(self.content.gifResource.path): imageResource_exists = True else: log.warn("Couldn't find Maths image when upgrading "\ + self.content.gifResource.storageName) if imageResource_exists: new_content += "<img src=\"resources/" \ + self.content.gifResource.storageName + "\" " # create the expected math resource url for comparison later, # once we do actually create it: math_resource_url="resources/" \ + self.content.gifResource.storageName + ".tex\" " new_content += "exe_math_latex=\"" + math_resource_url + "\" " new_content += "exe_math_size=\"" + repr(self.content.fontsize) \ + "\" " # no height or width for math images, eh? nope. new_content += "/> \n" elif self.content.gifResource: new_content += "<BR>\n[WARNING: missing image: " \ + self.content.gifResource.storageName + "]\n" replacementIdev = FreeTextIdevice(new_content) ########### # now, copy that content field's content into its _w_resourcePaths, # and properly remove the resource directory via Massage.... # for its _wo_resourcePaths: # note that replacementIdev's content field's content # is automatically set at its constructor (above), # as is the default content_w_resourcePaths (a copy of content) # AND the default content_wo_resourcePaths (a copy of content), # so only need to update the content_wo_resourcePaths: replacementIdev.content.content_wo_resourcePaths = \ replacementIdev.content.MassageContentForRenderView( \ replacementIdev.content.content_w_resourcePaths) # Design note: ahhhhh, the above is a good looking reason to possibly # have the MassageContentForRenderView() method # just assume its content_w_resourcePaths as the input # and write the output to its content_wo_resourcePaths..... ####### # next step, add the new IDevice into the same node as this one self.parentNode.addIdevice(replacementIdev) # in passing GalleryImage into the FieldWithResources, # that content field needs to be sure to have an updated # parentNode, courtesy of its idevice: replacementIdev.content.setParentNode() # and semi-manually add/create the current image # resource into the FreeTextIdevice's TextAreaField, content. # the content text will have already been taken care of above, # including the ideal <img src=...> including resources path, # but still need the actual image resource: if imageResource_exists: # Not sure why this can't be imported up top, but it gives # ImportError: cannot import name GalleryImages, # so here it be: from exe.engine.galleryidevice import GalleryImage full_image_path = self.content.gifResource.path # with empty caption: new_GalleryImage = GalleryImage(replacementIdev.content, \ '', full_image_path, mkThumbnail=False) # and.... write the latex_source out into the preview_math_srcfile # such that it can then be passed into the compile command. # Using the desired name (image.gif.tex), write it into tempWebDir: webDir = Path(G.application.tempWebDir) source_tex_name = self.content.gifResource.storageName + ".tex" math_path = webDir.joinpath(source_tex_name) math_filename_str = math_path.abspath().encode('utf-8') log.debug("convertToFreeText: writing LaTeX source into \'" \ + math_filename_str + "\'.") math_file = open(math_filename_str, 'wb') # do we need to append a \n here?: math_file.write(self.content.latex) math_file.flush() math_file.close() # finally, creating a resource for the latex_source as well: new_GalleryLatex = GalleryImage(replacementIdev.content, \ '', math_filename_str, mkThumbnail=False) new_GalleryLatexResource = new_GalleryLatex._imageResource mathsrc_resource_path = new_GalleryLatexResource._storageName # and re-concatenate from the global resources name, # to build the webUrl to the resource: mathsrc_resource_url = new_GalleryLatex.resourcesUrl \ + mathsrc_resource_path # AND compare with the newly built resource_url from above, # to ensure that we've got what we had expected, jah! if (mathsrc_resource_url != math_resource_url): log.warn('The math source was resource-ified differently ' \ + 'than expected, to: ' + mathsrc_resource_url \ + '; the source will need to be recreated.') # right. we COULD go ahead and change the exe_math_latex # attribute to point to the actual mathsrc_resource_url, # EXCEPT that the entire exemath plugin is currently built # with the idea that the source .tex file will always be named # as the mathimage.gif.tex, and this exe_math_latex tag # is really just letting the rest of the world know that # there IS corresponding source expected there. # If exemath is to ever change and use the actual contents # of this exe_math_latex tag (rather than just appended .tex), # then this could be revisited here. else: log.debug('math source was resource-ified properly to: ' \ + mathsrc_resource_url) # and move the new idevice up to the position following this node! while ( self.parentNode.idevices.index(replacementIdev) \ > ( (self.parentNode.idevices.index(self) + 1))): replacementIdev.movePrev() # finally: delete THIS idevice itself, deleting it from the node self.delete()
def embedImageInFeedback(self): """ Actually do the Converting of each question's CaseStudyIdevice's image -> embedded in its feedback field, now that its TextField can hold embeddded images. """ new_content = "" # if no image resource even exists, then no need to continue: # is there a defined? if self.image is None or self.image.imageResource is None: return # likewise, only proceed if the image resource file is found: if not os.path.exists(self.image.imageResource.path) \ or not os.path.isfile(self.image.imageResource.path): return # and if it's just the default blank image, then nothing to do either: if self.image.isDefaultImage: return # get the current image resource info: new_content += "<img src=\"resources/" \ + self.image.imageResource.storageName + "\" " if self.image.height: new_content += "height=\"" + self.image.height + "\" " if self.image.width: new_content += "width=\"" + self.image.width + "\" " new_content += "/> \n" # prepend the new image content to any already existing feedback, # using its content WITH the resources path: new_content += "<BR>\n" new_content += self.feedbackTextArea.content_w_resourcePaths self.feedbackTextArea.content_w_resourcePaths = new_content # set that to its default content: self.feedbackTextArea.content = \ self.feedbackTextArea.content_w_resourcePaths # and massage its content for exporting without resource paths: self.feedbackTextArea.content_wo_resourcePaths = \ self.feedbackTextArea.MassageContentForRenderView( \ self.feedbackTextArea.content_w_resourcePaths) # in passing GalleryImage into feedbackTextArea, a FieldWithResources, # the field needs to be sure to have an updated parentNode, # courtesy of its idevice: self.feedbackTextArea.setParentNode() # (note: FieldWithResources *usually* take care of this automatically # within ProcessPreviewedImages(), but here in this upgrade # path, we are bypassing the general purpose ProcessPreviewed().) # Not sure why this can't be imported up top, but it gives # ImportError: cannot import name GalleryImages, # so here it be: from exe.engine.galleryidevice import GalleryImage full_image_path = self.image.imageResource.path # note: unapplicable caption set to '' in the 2nd parameter: new_GalleryImage = GalleryImage(self.feedbackTextArea, \ '', full_image_path, mkThumbnail=False) # finally, go ahead and clear out the current image object, # which is best (and most safely) done by setting it back to default: self.image.setDefaultImage()