def get_annotated(context): # in the future we may want to work directly on the __annotation__ # attribute to get back a generator based on the PREFIX instead. items = [i for i in IAnnotations(of).items() if IExposureFileNote.providedBy(i[1])] return items
def rebuild_note(context): """\ Cleans up exposure file notes. """ if not IExposureFile.providedBy(context): raise TypeError('context does not provide IExposureFile') # Get all annotations that provides IExposureFileNote annotations = IAnnotation(context) # XXX save data first # delete for key in annotations.keys(): if IExposureFileNote.providedBy(annotations[key]): del annotations[key]
def cellml_v0_2tov0_3(context): """Migration script specific to models.cellml.org.""" import traceback from zope.annotation.interfaces import IAnnotations from pmr2.app.annotation.interfaces import IExposureFileNote try: from pmr2.app.exposure.browser.browser import \ ExposureFileTypeAnnotatorForm except ImportError: from pmr2.app.browser.exposure import ExposureFileTypeAnnotatorForm try: from pmr2.app.adapter import ExposureSourceAdapter except ImportError: from pmr2.app.exposure.adapter import ExposureSourceAdapter logger = getLogger('pmr2.app') catalog = getToolByName(context, 'portal_catalog') props = getToolByName(context, 'portal_properties') # This is the path to the ExposureFileType object defined for the # CellML type. It needs to be set using the portal_properties tool. cellml_type = props.site_properties.getProperty('cellml_type_path') cellml_force = props.site_properties.getProperty('cellml_force_all') cellml_notes = set(['cmeta', 'basic_mathml', 'basic_ccode']) cellml_type_obj = cellml_type and catalog(path=cellml_type)[0].getObject() cellml_type_notes = cellml_type_obj and cellml_type_obj.views cellml_type_tags = cellml_type_obj and cellml_type_obj.tags or () def migrate(context, annotations, oldnotes): # construct a set of old notes to verify whether or not to use # the CellML profile. oldnote_set = set(oldnotes) session_file = None if 'opencellsession' in oldnote_set: # It can die here because something wrong with getting the # vocabulary from the manifest. session_file = annotations['opencellsession'].filename # discard this. oldnote_set.remove('opencellsession') groups = {} groups['opencellsession'] = [('filename', session_file),] if cellml_force or (cellml_type_notes and oldnote_set == cellml_notes): context.file_type = cellml_type # update views context.views = cellml_type_notes context.setSubject(cellml_type_tags) groups['license_citation'] = [('format', u'cellml_rdf_metadata')] groups['source_text'] = [('langtype', u'xml')] else: # reuse the existing views, and unknown profile. pass # annotate using the form. form = ExposureFileTypeAnnotatorForm(context, None) # It can die here too because of various reasons (workspace # missing, malformed input data, incompatibilities of existing # data with new formatting scheme, stray electrons, etc.). form._annotate(groups) files = catalog(portal_type='ExposureFile') for file in files: context = file.getObject() annotations = IAnnotations(context) oldnotes = [] for k, v in annotations.iteritems(): if not IExposureFileNote.providedBy(v): continue # we only want annotations created in v0.2, which is # identified by the usage of non-prefixed keys. if k in context.views: oldnotes.append(k) if not oldnotes: # not doing anything since old notes are not found. logger.info('`%s` has no pmr2.app v0.2 styled notes to migrate.' % context.absolute_url_path()) continue try: migrate(context, annotations, oldnotes) except: logger.error('Failed to migrate `%s` - it may be left in an ' 'inconsistent state!' % context.absolute_url_path()) logger.warning(traceback.format_exc()) # naturally don't remove the old notes if we blew up. continue # remove old notes for k in oldnotes: del annotations[k] logger.info('`%s` had its notes migrated successfully.' % context.absolute_url_path())
def del_note(context, key): # purges ExposureFileNotes ann = IAnnotations(context) k = PREFIX + key if k in ann and IExposureFileNote.providedBy(ann[k]): del ann[k]