def remove_prefix_dates(entity: EntityProxy) -> EntityProxy: """If an entity has multiple values for a date field, you may want to remove all those that are prefixes of others. For example, if a Person has both a birthDate of 1990 and of 1990-05-01, we'd want to drop the mention of 1990.""" for prop in entity.iterprops(): if prop.type == registry.date: values = remove_prefix_date_values(entity.get(prop)) entity.set(prop, values) return entity
def remove_checksums(proxy: EntityProxy) -> EntityProxy: """When accepting entities via a web API, it would consistute a security risk to allow a user to submit checksum-type properties. These can be traded in for access to said files if they exist in the underlying content-addressed storage. It seems safest to just remove all checksums from entities when they are untrusted user input.""" for prop in proxy.iterprops(): if prop.type == registry.checksum: proxy.pop(prop) return proxy
def apply(self, proxy: EntityProxy, shallow: bool = False) -> EntityProxy: """Rewrite an entity proxy so all IDs mentioned are limited to the namespace.""" signed = proxy.clone() signed.id = self.sign(proxy.id) if not shallow: for prop in proxy.iterprops(): if prop.type != registry.entity: continue for value in signed.pop(prop): value = get_entity_id(value) signed.add(prop, self.sign(value)) return signed
def sieve_entity( entity: EntityProxy, schemata: Iterable[str], properties: Iterable[str], types: Iterable[str], ) -> Optional[EntityProxy]: for schema in schemata: if entity.schema.is_a(schema): return None for prop in entity.iterprops(): if prop.name in properties or prop.qname in properties: entity.pop(prop, quiet=True) elif prop.type.name in types: entity.pop(prop, quiet=True) return entity