[PATCH 02 of 14 skos] Get rid of monkeypatches and workaround imports for cubicweb < 3.21

Denis Laxalde denis.laxalde at logilab.fr
Mon Feb 19 12:27:53 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1519029286 -3600
#      Mon Feb 19 09:34:46 2018 +0100
# Node ID 2b4d0044cccb91b4bdd7168a783f702e642c81d7
# Parent  948c0c93f80669ae010a5972257ed982efa29cf3
Get rid of monkeypatches and workaround imports for cubicweb < 3.21

Related to #17133718.

diff --git a/__init__.py b/__init__.py
--- a/__init__.py
+++ b/__init__.py
@@ -22,7 +22,6 @@ from six import text_type, binary_type
 import cubicweb
 
 CW_VERSION = tuple(map(int, cubicweb.__version__.split('.')[:2]))
-POST_321 = CW_VERSION >= (3, 21)
 POST_323 = CW_VERSION >= (3, 23)
 
 
diff --git a/rdfio.py b/rdfio.py
--- a/rdfio.py
+++ b/rdfio.py
@@ -20,12 +20,7 @@ from urlparse import urlparse
 
 from six import PY2, text_type, string_types
 
-from cubes.skos import POST_321
-if POST_321:
-    # use new CW 3.21 dataimport API
-    from cubicweb.dataimport.importer import ExtEntity
-else:
-    from cubes.skos import ExtEntity
+from cubicweb.dataimport.importer import ExtEntity
 
 
 TYPE_PREDICATE_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
diff --git a/site_cubicweb.py b/site_cubicweb.py
--- a/site_cubicweb.py
+++ b/site_cubicweb.py
@@ -19,92 +19,9 @@
 
 from datetime import datetime
 
-from six import text_type
-
 from logilab.common.decorators import monkeypatch
 
-from cubes.skos import POST_321, POST_323
-
-if not POST_321:
-    from copy import copy
-    import inspect
-
-    from cubicweb.dataimport import MetaGenerator, NoHookRQLObjectStore
-
-    @monkeypatch(MetaGenerator)
-    def __init__(self, cnx, baseurl=None, source=None):
-        self._cnx = cnx
-        if baseurl is None:
-            config = cnx.vreg.config
-            baseurl = config['base-url'] or config.default_base_url()
-        if not baseurl[-1] == '/':
-            baseurl += '/'
-        self.baseurl = baseurl
-        if source is None:
-            source = cnx.repo.system_source
-        self.source = source
-        self.create_eid = cnx.repo.system_source.create_eid
-        self.time = datetime.now()
-        # attributes/relations shared by all entities of the same type
-        self.etype_attrs = []
-        self.etype_rels = []
-        # attributes/relations specific to each entity
-        self.entity_attrs = ['cwuri']
-        schema = cnx.vreg.schema
-        rschema = schema.rschema
-        for rtype in self.META_RELATIONS:
-            # skip owned_by / created_by if user is the internal manager
-            if cnx.user.eid == -1 and rtype in ('owned_by', 'created_by'):
-                continue
-            if rschema(rtype).final:
-                self.etype_attrs.append(rtype)
-            else:
-                self.etype_rels.append(rtype)
-
-    @monkeypatch(MetaGenerator)
-    def init_entity(self, entity):
-        entity.eid = self.create_eid(self._cnx)
-        extid = entity.cw_edited.get('cwuri')
-        for attr in self.entity_attrs:
-            if attr in entity.cw_edited:
-                # already set, skip this attribute
-                continue
-            genfunc = self.generate(attr)
-            if genfunc:
-                entity.cw_edited.edited_attribute(attr, genfunc(entity))
-        if isinstance(extid, text_type):
-            extid = extid.encode('utf-8')
-        return self.source, extid
-
-    @monkeypatch(NoHookRQLObjectStore)
-    def create_entity(self, etype, **kwargs):
-        for key, value in kwargs.iteritems():
-            kwargs[key] = getattr(value, 'eid', value)
-        entity, rels = self.metagen.base_etype_dicts(etype)
-        # make a copy to keep cached entity pristine
-        entity = copy(entity)
-        entity.cw_edited = copy(entity.cw_edited)
-        entity.cw_clear_relation_cache()
-        entity.cw_edited.update(kwargs, skipsec=False)
-        entity_source, extid = self.metagen.init_entity(entity)
-        cnx = self._cnx
-        self.source.add_info(cnx, entity, entity_source, extid)
-        self.source.add_entity(cnx, entity)
-        kwargs = dict()
-        if inspect.getargspec(self.add_relation).keywords:
-            kwargs['subjtype'] = entity.cw_etype
-        for rtype, targeteids in rels.iteritems():
-            # targeteids may be a single eid or a list of eids
-            inlined = self.rschema(rtype).inlined
-            try:
-                for targeteid in targeteids:
-                    self.add_relation(cnx, entity.eid, rtype, targeteid,
-                                      inlined, **kwargs)
-            except TypeError:
-                self.add_relation(cnx, entity.eid, rtype, targeteids,
-                                  inlined, **kwargs)
-        self._nb_inserted_entities += 1
-        return entity
+from cubes.skos import POST_323
 
 
 if not POST_323:



More information about the saem-devel mailing list