def test_dict_passthrough(self): proxy = EntityProxy.from_dict(model, ENTITY) data = proxy.to_dict() assert data["id"] == ENTITY["id"] assert data["schema"] == ENTITY["schema"] assert "idNumber" in data["properties"] data = proxy.to_full_dict() assert "Ralph Tester" in data["names"]
def simplify_provenance(proxy: EntityProxy) -> EntityProxy: """If there are multiple dates given for some of the provenance fields, we can logically conclude which one is the most meaningful.""" for prop_name in ["modifiedAt", "retrievedAt"]: if proxy.has(prop_name, quiet=True): values = proxy.get(prop_name) proxy.set(prop_name, max(values)) for prop_name in ["createdAt", "authoredAt", "publishedAt"]: if proxy.has(prop_name, quiet=True): values = proxy.get(prop_name) proxy.set(prop_name, min(values)) return proxy
def test_rdf(self): proxy = EntityProxy.from_dict(model, ENTITY) statements = list(proxy.statements) assert 8 == len(statements), len(statements) triples = list(proxy.triples) count = len(statements) + 2 assert count == len(triples), len(triples) proxy = model.make_entity('Person') assert 0 == len(list(proxy.triples))
def test_dict_passthrough(self): proxy = EntityProxy.from_dict(model, ENTITY) data = proxy.to_dict() assert data['id'] == ENTITY['id'] assert data['schema'] == ENTITY['schema'] assert 'idNumber' in data['properties'] data = proxy.to_full_dict() assert ENTITY['schema'] in data['schemata'] assert 'Ralph Tester' in data['names']
def get_proxy(self, data: Dict[str, Any], cleaned: bool = True) -> EntityProxy: """Create an entity proxy to reflect the entity data in the given dictionary. If ``cleaned`` is disabled, all property values are fully re-validated and normalised. Use this if handling input data from an untrusted source.""" if isinstance(data, EntityProxy): return data return EntityProxy.from_dict(self, data, cleaned=cleaned)
def import_vis(infile: Path, outfile: Path) -> None: with path_writer(outfile) as outfh: with open(infile, "r") as infh: data = json.load(infh) if "entities" in data: entities = data.get("entities", data) if "layout" in data: entities = data.get("layout", {}).get("entities", data) for entity_data in ensure_list(entities): entity = EntityProxy.from_dict(model, entity_data) write_entity(outfh, entity)
def _add_node(self, proxy: EntityProxy) -> None: """Derive a node and its value edges from the given proxy.""" entity = Node.from_proxy(proxy) self.nodes[entity.id] = entity for prop, value in proxy.itervalues(): if prop.type not in self.edge_types: continue node = self._get_node_stub(prop, value) edge = Edge(self, entity, node, prop=prop, value=value) if edge.weight > 0: self.edges[edge.id] = edge
def entity_filename( proxy: EntityProxy, base_name: Optional[str] = None, extension: Optional[str] = None ) -> Optional[str]: """Derive a safe filename for the given entity.""" if proxy.schema.is_a("Document"): for extension_ in proxy.get("extension", quiet=True): if extension is not None: break extension = extension_ for file_name in proxy.get("fileName", quiet=True): base_name_, extension_ = splitext(file_name) if base_name is None and len(base_name_): base_name = base_name_ if extension is None and len(extension_): extension = extension_ for mime_type in proxy.get("mimeType", quiet=True): if extension is not None: break extension = guess_extension(mime_type) base_name = base_name or proxy.id return safe_filename(base_name, extension=extension)
def test_inverted_props(self): proxy = EntityProxy.from_dict(model, ENTITY) data = proxy.get_type_inverted() assert 'names' in data assert '1972-05-01' in data['dates'] assert 'countries' not in data proxy.add('nationality', ['vg']) assert 'vg' in proxy.countries data = proxy.get_type_inverted() assert 'countries' in data assert 'vg' in proxy.country_hints, proxy.country_hints assert 'us' in proxy.country_hints, proxy.country_hints
def add(self, proxy: EntityProxy) -> None: """Add an :class:`~followthemoney.proxy.EntityProxy` to the graph and make it either a :class:`~followthemoney.graph.Node` or an :class:`~followthemoney.graph.Edge`.""" if proxy is None: return self.queue(proxy.id, proxy) if proxy.schema.edge: for (source, target) in proxy.edgepairs(): self._add_edge(proxy, source, target) else: self._add_node(proxy)
def test_inverted_props(self): proxy = EntityProxy.from_dict(model, ENTITY) data = proxy.get_type_inverted() assert "names" in data assert "1972-05-01" in data["dates"] assert "countries" not in data proxy.add("nationality", ["vg"]) assert "vg" in proxy.countries data = proxy.get_type_inverted() assert "countries" in data assert "vg" in proxy.country_hints, proxy.country_hints assert "us" in proxy.country_hints, proxy.country_hints
def test_pop(self): proxy = EntityProxy.from_dict(model, ENTITY) get_ids = proxy.get('idNumber') assert get_ids, get_ids ids = proxy.pop('idNumber') assert get_ids == ids, ids assert not proxy.get('idNumber') assert not proxy.pop('idNumber') with assert_raises(InvalidData): proxy.pop('banana') assert not proxy.pop('banana', quiet=True)
def test_pop(self): proxy = EntityProxy.from_dict(model, ENTITY) get_ids = proxy.get("idNumber") assert get_ids, get_ids ids = proxy.pop("idNumber") assert get_ids == ids, ids assert not proxy.get("idNumber") assert not proxy.pop("idNumber") # new in 1.6.1: pop is quiet by default assert not proxy.pop("banana") with raises(InvalidData): proxy.pop("banana", quiet=False)
def test_remove(self): proxy = EntityProxy.from_dict(model, ENTITY) assert "9177171" in proxy.get("idNumber") proxy.remove("idNumber", "9177171") assert "9177171" not in proxy.get("idNumber") assert proxy.has("idNumber") proxy.remove("idNumber", "banana") assert proxy.has("idNumber") proxy.remove("idNumber", "banana", quiet=False) proxy.remove("fruit", "banana") with raises(InvalidData): proxy.remove("fruit", "banana", quiet=False)
def test_remove(self): proxy = EntityProxy.from_dict(model, ENTITY) assert '9177171' in proxy.get('idNumber') proxy.remove('idNumber', '9177171') assert '9177171' not in proxy.get('idNumber') assert proxy.has('idNumber') proxy.remove('idNumber', 'banana') assert proxy.has('idNumber') proxy.remove('idNumber', 'banana', quiet=False) proxy.remove('fruit', 'banana') with assert_raises(InvalidData): proxy.remove('fruit', 'banana', quiet=False)
def test_base_functions(self): proxy = EntityProxy.from_dict(model, ENTITY) assert 'test' in repr(proxy), repr(proxy) assert hash(proxy) == hash(proxy.id) assert proxy.get('name') == ['Ralph Tester'] prop = model.get_qname('Thing:name') assert proxy.get(prop) == ['Ralph Tester'] assert proxy.caption == 'Ralph Tester' assert str(proxy) == 'Ralph Tester' name = 'Ralph the Great' proxy.add('name', name) assert len(proxy.get('name')) == 2 proxy.add('name', None) assert len(proxy.get('name')) == 2 proxy.add('name', '') assert len(proxy.get('name')) == 2 proxy.add('name', ['']) assert len(proxy.get('name')) == 2 assert name in proxy.get('name') assert name in proxy.names, proxy.names with assert_raises(InvalidData): proxy.get('banana') assert [] == proxy.get('banana', quiet=True) assert len(proxy.get('nationality')) == 0 double = EntityProxy.from_dict(model, proxy) assert double == proxy proxy.add('banana', name, quiet=True) with assert_raises(InvalidData): proxy.add('banana', name) with assert_raises(InvalidData): EntityProxy.from_dict(model, {})
def test_max_size(self): t = registry.name proxy = EntityProxy.from_dict(model, ENTITY) prev_size = t.max_size t.max_size = len(proxy) + 10 assert len(proxy.get('name')) == 1 proxy.add('name', 'Louis George Maurice Adolphe Roche Albert Abel') assert len(proxy.get('name')) == 1 proxy.set('name', 'Louis') assert len(proxy.get('name')) == 1, proxy.get('name') proxy.add('name', 'A') assert len(proxy.get('name')) == 2, proxy.get('name') proxy.add('name', 'George') assert len(proxy.get('name')) == 2, proxy.get('name') t.max_size = prev_size
def test_max_size(self): t = registry.name proxy = EntityProxy.from_dict(model, ENTITY) prev_size = t.max_size t.max_size = len(proxy) + 10 assert len(proxy.get("name")) == 1 proxy.add("name", "Louis George Maurice Adolphe Roche Albert Abel") assert len(proxy.get("name")) == 1 proxy.set("name", "Louis") assert len(proxy.get("name")) == 1, proxy.get("name") proxy.add("name", "A") assert len(proxy.get("name")) == 2, proxy.get("name") proxy.add("name", "George") assert len(proxy.get("name")) == 2, proxy.get("name") t.max_size = prev_size
def test_has(self): proxy = EntityProxy.from_dict(model, ENTITY) assert not proxy.has("birthPlace") proxy.add("birthPlace", "Inferno") assert proxy.has("birthPlace") assert 1 == len(proxy.get("birthPlace")) proxy.add("birthPlace", "Hell") assert 2 == len(proxy.get("birthPlace")) proxy.set("birthPlace", "Inferno") assert 1 == len(proxy.get("birthPlace")) with raises(InvalidData): proxy.set("banana", "fruit") assert not proxy.set("banana", "fruit", quiet=True) with raises(InvalidData): proxy.has("banana") assert not proxy.has("banana", quiet=True)
def test_has(self): proxy = EntityProxy.from_dict(model, ENTITY) assert not proxy.has('birthPlace') proxy.add('birthPlace', 'Inferno') assert proxy.has('birthPlace') assert 1 == len(proxy.get('birthPlace')) proxy.add('birthPlace', 'Hell') assert 2 == len(proxy.get('birthPlace')) proxy.set('birthPlace', 'Inferno') assert 1 == len(proxy.get('birthPlace')) with assert_raises(InvalidData): proxy.set('banana', 'fruit') assert not proxy.set('banana', 'fruit', quiet=True) with assert_raises(InvalidData): proxy.has('banana') assert not proxy.has('banana', quiet=True)
def make_entity(self, schema: Union[str, Schema], key_prefix: Optional[str] = None) -> EntityProxy: """Instantiate an empty entity proxy of the given schema type.""" return EntityProxy(self, {"schema": schema}, key_prefix=key_prefix)
def test_context(self): data = {"fruit": "banana", "schema": "Person"} proxy = EntityProxy.from_dict(model, data) res = proxy.clone().to_dict() assert res["fruit"] == data["fruit"], res
def test_len(self): proxy = EntityProxy.from_dict(model, ENTITY) proxy_len = len(proxy) assert proxy_len > 0, proxy_len proxy.add("name", "Some text") assert len(proxy) > proxy_len, (len(proxy), proxy_len)
def create_proxy(): proxy = EntityProxy.from_dict(model, ENTITY) data = proxy.to_dict() return data
def make_entity(self, schema, key_prefix=None): """Instantiate an empty entity proxy of the given schema type.""" return EntityProxy(self, {"schema": schema}, key_prefix=key_prefix)
def make_entity(self, schema, key_prefix=None): return EntityProxy(self, {'schema': schema}, key_prefix=key_prefix)
def get_proxy(self, data, cleaned=True): """Create an entity proxy to reflect the entity data in the given dictionary. If ``cleaned`` is disabled, all property values are fully re-validated and normalised. Use this if handling input data from an untrusted source.""" return EntityProxy.from_dict(self, data, cleaned=cleaned)
def test_context(self): data = {'fruit': 'banana', 'schema': 'Person'} proxy = EntityProxy.from_dict(model, data) res = proxy.clone().to_dict() assert res['fruit'] == data['fruit'], res
def get_proxy(self, data): return EntityProxy.from_dict(self, data)
def get_proxy(self, data, cleaned=True): return EntityProxy.from_dict(self, data, cleaned=cleaned)