sobjects/cwxmlparser.py
changeset 9474 dd96b877879d
parent 8900 010a59e12d89
child 9750 566f8fce5168
equal deleted inserted replaced
9473:53974a06d646 9474:dd96b877879d
    29       }
    29       }
    30    }
    30    }
    31 
    31 
    32 """
    32 """
    33 
    33 
    34 from datetime import datetime, timedelta, time
    34 from datetime import datetime, time
    35 from urllib import urlencode
       
    36 from cgi import parse_qs # in urlparse with python >= 2.6
    35 from cgi import parse_qs # in urlparse with python >= 2.6
    37 
    36 
    38 from logilab.common.date import todate, totime
    37 from logilab.common.date import todate, totime
    39 from logilab.common.textutils import splitstrip, text_to_dict
    38 from logilab.common.textutils import splitstrip, text_to_dict
    40 from logilab.common.decorators import classproperty
    39 from logilab.common.decorators import classproperty
    55 def convert_date(ustr):
    54 def convert_date(ustr):
    56     return todate(datetime.strptime(ustr, '%Y-%m-%d'))
    55     return todate(datetime.strptime(ustr, '%Y-%m-%d'))
    57 DEFAULT_CONVERTERS['Date'] = convert_date
    56 DEFAULT_CONVERTERS['Date'] = convert_date
    58 def convert_datetime(ustr):
    57 def convert_datetime(ustr):
    59     if '.' in ustr: # assume %Y-%m-%d %H:%M:%S.mmmmmm
    58     if '.' in ustr: # assume %Y-%m-%d %H:%M:%S.mmmmmm
    60         ustr = ustr.split('.',1)[0]
    59         ustr = ustr.split('.', 1)[0]
    61     return datetime.strptime(ustr, '%Y-%m-%d %H:%M:%S')
    60     return datetime.strptime(ustr, '%Y-%m-%d %H:%M:%S')
    62 DEFAULT_CONVERTERS['Datetime'] = convert_datetime
    61 DEFAULT_CONVERTERS['Datetime'] = convert_datetime
    63 # XXX handle timezone, though this will be enough as TZDatetime are
    62 # XXX handle timezone, though this will be enough as TZDatetime are
    64 # serialized without time zone by default (UTC time). See
    63 # serialized without time zone by default (UTC time). See
    65 # cw.web.views.xmlrss.SERIALIZERS.
    64 # cw.web.views.xmlrss.SERIALIZERS.
   167             else:
   166             else:
   168                 etype = schemacfg.schema.otype.name
   167                 etype = schemacfg.schema.otype.name
   169                 ttype = schemacfg.schema.stype.name
   168                 ttype = schemacfg.schema.stype.name
   170             etyperules = self.source.mapping.setdefault(etype, {})
   169             etyperules = self.source.mapping.setdefault(etype, {})
   171             etyperules.setdefault((rtype, role, action), []).append(
   170             etyperules.setdefault((rtype, role, action), []).append(
   172                 (ttype, options) )
   171                 (ttype, options))
   173             self.source.mapping_idx[schemacfg.eid] = (
   172             self.source.mapping_idx[schemacfg.eid] = (
   174                 etype, rtype, role, action, ttype)
   173                 etype, rtype, role, action, ttype)
   175 
   174 
   176     def del_schema_config(self, schemacfg, checkonly=False):
   175     def del_schema_config(self, schemacfg, checkonly=False):
   177         """deleted CWSourceSchemaConfig, modify mapping accordingly"""
   176         """deleted CWSourceSchemaConfig, modify mapping accordingly"""
   202 
   201 
   203         * `item` is an {attribute: value} dictionary
   202         * `item` is an {attribute: value} dictionary
   204         * `rels` is for relations and structured as
   203         * `rels` is for relations and structured as
   205            {role: {relation: [(related item, related rels)...]}
   204            {role: {relation: [(related item, related rels)...]}
   206         """
   205         """
   207         entity = self.extid2entity(str(item['cwuri']),  item['cwtype'],
   206         entity = self.extid2entity(str(item['cwuri']), item['cwtype'],
   208                                    cwsource=item['cwsource'], item=item)
   207                                    cwsource=item['cwsource'], item=item)
   209         if entity is None:
   208         if entity is None:
   210             return None
   209             return None
   211         if entity.eid in self._processed_entities:
   210         if entity.eid in self._processed_entities:
   212             return entity
   211             return entity
   430         for ttype, options in rules:
   429         for ttype, options in rules:
   431             searchattrs = splitstrip(options.get('linkattr', ''))
   430             searchattrs = splitstrip(options.get('linkattr', ''))
   432             self._related_link(ttype, others, searchattrs)
   431             self._related_link(ttype, others, searchattrs)
   433 
   432 
   434     def _related_link(self, ttype, others, searchattrs):
   433     def _related_link(self, ttype, others, searchattrs):
   435         def issubset(x,y):
   434         def issubset(x, y):
   436             return all(z in y for z in x)
   435             return all(z in y for z in x)
   437         eids = [] # local eids
   436         eids = [] # local eids
   438         log = self.parser.import_log
   437         log = self.parser.import_log
   439         for item, rels in others:
   438         for item, rels in others:
   440             if item['cwtype'] != ttype:
   439             if item['cwtype'] != ttype: