示例#1
0
    def entity_from_json(self, e):
        """
        Create an LcEntity subclass from a json-derived dict

        this could use some serious refactoring
        :param e:
        :return:
        """
        if 'tags' in e:
            self._entity_from_old_json(e)
            return
        chars = None
        exchs = None
        e_id = e.pop('entityId')
        ext_ref = e.pop('externalId')
        uid = self._key_to_id(e_id)
        etype = e.pop('entityType')
        origin = e.pop('origin')
        if etype == 'quantity':
            # can't move this to entity because we need _create_unit- so we wouldn't gain anything
            unit, _ = self._create_unit(e.pop('referenceUnit'))
            e['referenceUnit'] = unit
            entity = LcQuantity(uid, **e)
        elif etype == 'flow':
            try:
                e.pop('referenceQuantity')
            except KeyError:
                pass
            if 'characterizations' in e:
                chars = e.pop('characterizations')
            entity = LcFlow(uid, **e)
            if chars is not None:
                for c in chars:
                    v = None
                    q = self[c['quantity']]
                    if q is None:
                        import json, sys
                        print(ext_ref)
                        json.dump(c, sys.stdout, indent=2)

                        raise KeyError
                    if 'value' in c:
                        v = c['value']
                    if 'isReference' in c:
                        is_ref = c['isReference']
                    else:
                        is_ref = False
                    entity.add_characterization(q, reference=is_ref, value=v)
        elif etype == 'process':
            # note-- we want to abandon referenceExchange notation, but we need to leave it in for backward compat
            try:
                rx = e.pop('referenceExchange')
            except KeyError:
                rx = None
            if 'exchanges' in e:
                exchs = e.pop('exchanges')
            entity = LcProcess(uid, **e)
            if exchs is not None:
                for x in exchs:
                    v = None
                    # is_ref = False
                    f = self[x['flow']]
                    d = x['direction']
                    if 'value' in x:
                        v = x['value']
                    if 'isReference' in x:
                        # is_ref = x['isReference']
                        entity.add_reference(f, d)
                    # TODO: handle allocations -- I think this will "just work" if v is a dict
                    entity.add_exchange(f, d, value=v)
                rx = None
            if rx is not None and rx != 'None':
                try:
                    direc, flow = rx.split(': ')
                    entity['referenceExchange'] = Exchange(process=entity, flow=self[flow], direction=direc)
                except AttributeError:
                    print('rx: [%s]' % rx)
                except ValueError:
                    pass
        else:
            raise TypeError('Unknown entity type %s' % e['entityType'])

        entity.origin = origin
        entity.set_external_ref(ext_ref)
        self.add(entity)