def _initVocabs(self, node):
     for objnode in node.getElementsByTagName('object'):
         filename = objnode.getAttribute('name')
         filepath = os.path.join(self.name, filename)
         data = self.environ.readDataFile(filepath)
         if filename.endswith('.vdex') or filename.endswith('.xml'):
             # VDEX file
             try:
                 vdex = VDEXManager(data)
             except VDEXError, e:
                 self._logger.error('Problem with vdex-file: %s' % filepath)
                 raise
             vocabid = vdex.getVocabIdentifier()
             if not vocabid:
                 vocabid = filename[:filename.rfind('.')]
             vocabname = vocabid
             if vocabname in self.context.objectIds():
                 self.context.manage_delObjects([vocabname])
             try:
                 self._logger.info(
                     'Import VDEX file %s with identifier %s' % \
                     (filename, vocabname))
                 self.context.invokeFactory('VdexFileVocabulary', vocabname)
             except BadRequest, e:
                 self._logger.warning(
                     'Import VDEX file %s with identifier %s renamed as %s' % \
                     (filename, vocabid, vocabname))
                 vocabname = normalizeString(vocabid, context=self.context)
                 if vocabname in self.context.objectIds():
                     self.context.manage_delObjects([vocabname])
                 self.context.invokeFactory('VdexFileVocabulary', vocabname)
             self.context[vocabname].importXMLBinding(data)
def loadVdexVocabs(site, directory, files, remove=True):
    atvm = getToolByName(site, "portal_vocabularies")
    for filename in files:
        # load file
        vdexpath = os.path.join(directory, filename)
        if not (os.path.exists(vdexpath) and os.path.isfile(vdexpath)):
            raise ValueError("No valid VDEX import file provided at %s." % vdexpath)
        try:
            data = open(vdexpath, "r").read()
        except Exception, e:
            raise ValueError("Problems while reading VDEX import file " + "provided at %s\n%s\n." % (vdexpath, str(e)))
        vdex = VDEXManager(data)
        vocabname = vdex.getVocabIdentifier()
        if vocabname in atvm.contentIds():
            if remove:
                atvm.manage_delObjects([vocabname])
            else:
                # logging here?
                continue
        atvm.invokeFactory("VdexFileVocabulary", vocabname)
        atvm[vocabname].importXMLBinding(data)
Esempio n. 3
0
def loadVdexVocabs(site, directory, files, remove=True):
    atvm = getToolByName(site, 'portal_vocabularies')
    for filename in files:
        # load file
        vdexpath = os.path.join(directory, filename)
        if not (os.path.exists(vdexpath) and os.path.isfile(vdexpath)):
            raise ValueError('No valid VDEX import file provided at %s.' %
                             vdexpath)
        try:
            data = open(vdexpath, 'r').read()
        except Exception as e:
            raise ValueError('Problems while reading VDEX import file ' +\
                             'provided at %s\n%s\n.' % (vdexpath, str(e)))
        vdex = VDEXManager(data)
        vocabname = vdex.getVocabIdentifier()
        if vocabname in atvm.contentIds():
            if remove:
                atvm.manage_delObjects([vocabname])
            else:
                # logging here?
                continue
        atvm.invokeFactory('VdexFileVocabulary', vocabname)
        atvm[vocabname].importXMLBinding(data)