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)
Exemple #2
0
 def handle_preview_xml(self):
     """ Return xml preview of suggested new vocabulary """
     fob = StringIO()
     fob.write(self.request.params['preview'].encode('utf-8'))
     fob.seek(0)
     matrix = []
     for row in csv.reader(fob):
         matrix.append(row)
     manager = VDEXManager(matrix=matrix)
     return base64.encodestring(manager.serialize())
Exemple #3
0
    def testMatrixImport(self):
        manager = VDEXManager(resource_stream(__name__, 'test.xml'))
        matrix = manager.exportMatrix()
        new_manager = VDEXManager(matrix=matrix)
        self.assertEquals(matrix, new_manager.exportMatrix())

        data = new_manager.serialize()
        should_be_xml = '<?xml version="1.0" encoding="utf-8" ?>\n<vdex xmlns="http://www.imsglobal.org/xsd/imsvdex_v1p0"><term><termIdentifier>identical</termIdentifier><caption><langstring language="en">is identical with</langstring><langstring language="fr">est identique avec</langstring><langstring language="it">\xc3\xa8 identico con</langstring></caption></term><term><termIdentifier>relative</termIdentifier><caption><langstring language="de">ist verwandt mit</langstring><langstring language="en">is relative of</langstring><langstring language="fr">est parent avec</langstring><langstring language="it">\xc3\xa8 parente di</langstring></caption><term><termIdentifier>child</termIdentifier><caption><langstring language="de">ist Kind von</langstring><langstring language="en">is child of</langstring><langstring language="fr">est enfant de</langstring><langstring language="it">\xc3\xa8 prole di</langstring></caption></term></term></vdex>'
        obj = objectify.fromstring(should_be_xml)
        should_be = etree.tostring(obj, encoding='utf-8', standalone=True)
        self.assertEquals(should_be, 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)
Exemple #5
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)
Exemple #6
0
 def _getManager(self, reset=False, returnerror=False):
     """takes the given file and returns an initialized VDEXManager."""
     if reset and hasattr(self, '_v_vdexmanager'):
         del self._v_vdexmanager
     manager = getattr(self, '_v_vdexmanager', None)
     if manager is not None:
         return manager
     field = self.getField('vdex')
     data = field.getRaw(self)
     lang = self._getLanguage()
     try:
         manager = VDEXManager(str(data), lang='en', fallback=True)
     except VDEXError, e:
         if not returnerror:
             return None
         return str(e)
Exemple #7
0
 def testMatrixExport(self):
     manager = VDEXManager(resource_stream(__name__, 'test.xml'))
     data = manager.exportMatrix()
     should_be = [['Level 0', 'Level 1', 'Caption de', 'Description de', 'Caption en', 'Description en', 'Caption fr', 'Description fr', 'Caption it', 'Description it'], ['identical', '', '', '', 'is identical with', '', 'est identique avec', '', u'\xe8 identico con', ''], ['relative', '', 'ist verwandt mit', '', 'is relative of', '', 'est parent avec', '', u'\xe8 parente di', ''], ['', 'child', 'ist Kind von', '', 'is child of', '', 'est enfant de', '', u'\xe8 prole di', '']]
     self.assertEquals(should_be, data)
Exemple #8
0
    def testTooMuchInput(self):
        manager = VDEXManager(resource_stream(__name__, 'test.xml'))

        xml = manager.serialize()
        matrix = manager.exportMatrix()
        self.assertRaises(AttributeError, VDEXManager, xml, matrix)
Exemple #9
0
 def testEmptyMatrix(self):
     manager = VDEXManager()
     matrix = manager.exportMatrix()
     self.assertEquals([], matrix)
def installVocabularies(context):
    """creates/imports the atvm vocabs."""
    site = context.getSite()
    atvm = getToolByName(site, "portal_vocabularies")
    wftool = getToolByName(site, "portal_workflow")
    vocab_folder = site["vocabularies"]
    atvm_vocabs = [
        "name-accuracy",
        "association-certainty",
        "attestation-confidence",
        "name-completeness",
        "ancient-name-languages",
        "name-types",
    ]
    registry_vocabs = ["time-periods", "place-types"]

    registry = getUtility(IRegistry)
    settings = registry.forInterface(IPleiadesSettings, False)

    for vocabname in atvm_vocabs + registry_vocabs:

        # Read from .vdex file
        vdexpath = os.path.join(os.path.dirname(__file__), "data", "%s.vdex" % vocabname)
        try:
            f = open(vdexpath, "r")
            data = f.read()
            f.close()
        except:
            logger.warn("Problems while reading VDEX import file " + "provided at %s." % vdexpath)
            continue

        if vocabname in atvm_vocabs and vocabname not in vocab_folder.contentIds():
            vid = vocab_folder.invokeFactory("PleiadesVocabulary", vocabname)
            try:
                wftool.doActionFor(vocab_folder[vid], action="publish")
            except WorkflowException:
                pass
            vdex = VDEXManager(data)
            for key in vdex.getVocabularyDict().keys():
                value = vdex.getTermCaptionById(key)
                descr = vdex.getTermDescriptionById(key).capitalize()
                tid = vocab_folder[vid].invokeFactory("PleiadesVocabularyTerm", key, title=value, description=descr)
                try:
                    wftool.doActionFor(vocab_folder[vid][tid], action="publish")
                except WorkflowException:
                    pass
            if vid in atvm.contentIds():
                atvm.manage_delObjects([vid])
            atvm.invokeFactory("AliasVocabulary", vid, target=vocab_folder[vid])

        elif vocabname in registry_vocabs:
            vocabkey = vocabname.replace("-", "_")

            # Avoid overwriting existing terms
            value = getattr(settings, vocabkey, None)
            if value is not None:
                continue

            vdex = VDEXManager(data)
            terms = []
            for key in vdex.getVocabularyDict().keys():
                title = vdex.getTermCaptionById(key)
                descr = vdex.getTermDescriptionById(key).capitalize()
                term = dict(
                    id=key.decode("utf8"),
                    title=title.decode("utf8"),
                    description=descr.decode("utf8"),
                    hidden=False,
                    same_as=None,
                )
                if vocabname == "time-periods":
                    min = None
                    max = None
                    m = re.search(r"\[\[(-{0,1}\d*\.{0,1}\d*)\s*,\s*(-{0,1}\d*\.{0,1}\d*)\]\]", descr)
                    if m is not None:
                        min = int(m.group(1))
                        max = int(m.group(2))
                    term["lower_bound"] = min
                    term["upper_bound"] = max
                terms.append(term)
            setattr(settings, vocabkey, terms)

    # prepopulate arch_remains vocab - if uninitialized
    if not settings.arch_remains:
        settings.arch_remains = [
            {"id": u"unknown", "title": u"Unknown"},
            {"id": u"none", "title": u"None"},
            {"id": u"traces", "title": u"Traces"},
            {"id": u"substantive", "title": u"Substantive"},
            {"id": u"restored", "title": u"Restored"},
            {"id": u"notvisible", "title": u"Not visible"},
        ]

    # prepopulate relationship_types vocab - if uninitialized
    if not settings.relationship_types:
        settings.relationship_types = [
            {"id": u"connection", "title": u"connection", "description": u"", "same_as": None, "hidden": False},
            {"id": u"at", "title": u"at", "description": u"", "same_as": None, "hidden": False},
            {"id": u"on", "title": u"on", "description": u"", "same_as": None, "hidden": False},
            {
                "id": u"part_of_admin",
                "title": u"part of (administrative)",
                "description": u"",
                "same_as": None,
                "hidden": False,
            },
            {
                "id": u"part_of_regional",
                "title": u"part of (regional)",
                "description": u"",
                "same_as": None,
                "hidden": False,
            },
            {"id": u"near", "title": u"near", "description": u"", "same_as": None, "hidden": False},
            {"id": u"intersects", "title": u"intersects", "description": u"", "same_as": None, "hidden": False},
        ]

    # prepopulate location_types vocab - if uninitialized
    if not settings.location_types:
        settings.location_types = [
            {"id": u"associated_modern", "title": u"associated modern"},
            {"id": u"central_point", "title": u"central point"},
            {"id": u"legacy", "title": u"legacy"},
            {"id": u"representative", "title": u"representative"},
        ]
Exemple #11
0
 def init(self):
     self.init_vdex_file()
     self.vdexmanager = VDEXManager(
         resource_stream(__name__, self.file_name))
     self.records = self.get_records_from_xml()