Skip to content

Commit

Permalink
Merge pull request #33 from casework/add_inherence_uuid_functions
Browse files Browse the repository at this point in the history
Add inherence UUID functions
  • Loading branch information
ajnelson-nist authored May 11, 2023
2 parents a09234e + b031538 commit 27d13e4
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 136 deletions.
129 changes: 105 additions & 24 deletions case_exiftool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import os
import typing

import case_utils
import case_utils.inherent_uuid
import rdflib.plugins.sparql
import rdflib.util
from case_utils.namespace import (
Expand Down Expand Up @@ -69,6 +69,11 @@
help="A file recording the output of ExifTool run against some file. Expects exiftool was run with -binary, -duplicates, and -xmlFormat.",
required=True,
)
argument_parser.add_argument(
"--use-deterministic-uuids",
action="store_true",
help="Use UUIDs computed using the case_utils.inherent_uuid module.",
)
argument_parser.add_argument(
"out_graph",
help="A self-contained RDF graph file, in the format either requested by --output-format or guessed based on extension.",
Expand Down Expand Up @@ -145,13 +150,23 @@ class ExifToolRDFMapper(object):
Those interested in extending this tool's mapping coverage of ExifTool IRIs are encouraged to update the method map_raw_and_printconv_iri.
"""

def __init__(self, graph: rdflib.Graph, ns_base: rdflib.Namespace) -> None:
def __init__(
self,
graph: rdflib.Graph,
ns_base: rdflib.Namespace,
*args: typing.Any,
use_deterministic_uuids: bool = False,
**kwargs: typing.Any,
) -> None:
assert isinstance(graph, rdflib.Graph)

self._exif_dictionary_dict: typing.Optional[
typing.Dict[str, rdflib.Literal]
] = None
self._graph = graph

self._use_deterministic_uuids = use_deterministic_uuids

self._kv_dict_raw: typing.Dict[rdflib.URIRef, rdflib.term.Node] = dict()
self._kv_dict_printconv: typing.Dict[rdflib.URIRef, rdflib.term.Node] = dict()
self._mime_type: typing.Optional[str] = None
Expand Down Expand Up @@ -522,9 +537,18 @@ def n_camera_object_device_facet(self) -> rdflib.URIRef:
Initialized on first access.
"""
if self._n_camera_object_device_facet is None:
self._n_camera_object_device_facet = self.ns_base[
"DeviceFacet-" + case_utils.local_uuid.local_uuid()
]
if self.use_deterministic_uuids:
self._n_camera_object_device_facet = (
case_utils.inherent_uuid.get_facet_uriref(
self.n_camera_object,
NS_UCO_OBSERVABLE.DeviceFacet,
namespace=self.ns_base,
)
)
else:
self._n_camera_object_device_facet = self.ns_base[
"DeviceFacet-" + case_utils.local_uuid.local_uuid()
]
self.graph.add(
(
self._n_camera_object_device_facet,
Expand All @@ -547,9 +571,16 @@ def n_content_data_facet(self) -> rdflib.URIRef:
Initialized on first access.
"""
if self._n_content_data_facet is None:
self._n_content_data_facet = self.ns_base[
"ContentDataFacet-" + case_utils.local_uuid.local_uuid()
]
if self.use_deterministic_uuids:
self._n_content_data_facet = case_utils.inherent_uuid.get_facet_uriref(
self.n_observable_object,
NS_UCO_OBSERVABLE.ContentDataFacet,
namespace=self.ns_base,
)
else:
self._n_content_data_facet = self.ns_base[
"ContentDataFacet-" + case_utils.local_uuid.local_uuid()
]
self.graph.add(
(
self._n_content_data_facet,
Expand Down Expand Up @@ -590,9 +621,16 @@ def n_exif_facet(self) -> rdflib.URIRef:
Initialized on first access.
"""
if self._n_exif_facet is None:
self._n_exif_facet = self.ns_base[
"EXIFFacet-" + case_utils.local_uuid.local_uuid()
]
if self.use_deterministic_uuids:
self._n_exif_facet = case_utils.inherent_uuid.get_facet_uriref(
self.n_observable_object,
NS_UCO_OBSERVABLE.EXIFFacet,
namespace=self.ns_base,
)
else:
self._n_exif_facet = self.ns_base[
"EXIFFacet-" + case_utils.local_uuid.local_uuid()
]
self.graph.add(
(self._n_exif_facet, NS_RDF.type, NS_UCO_OBSERVABLE.EXIFFacet)
)
Expand All @@ -607,9 +645,16 @@ def n_file_facet(self) -> rdflib.URIRef:
Initialized on first access.
"""
if self._n_file_facet is None:
self._n_file_facet = self.ns_base[
"FileFacet-" + case_utils.local_uuid.local_uuid()
]
if self.use_deterministic_uuids:
self._n_file_facet = case_utils.inherent_uuid.get_facet_uriref(
self.n_observable_object,
NS_UCO_OBSERVABLE.FileFacet,
namespace=self.ns_base,
)
else:
self._n_file_facet = self.ns_base[
"FileFacet-" + case_utils.local_uuid.local_uuid()
]
self.graph.add(
(self._n_file_facet, NS_RDF.type, NS_UCO_OBSERVABLE.FileFacet)
)
Expand Down Expand Up @@ -638,9 +683,18 @@ def n_location_object_latlong_facet(self) -> rdflib.URIRef:
Initialized on first access.
"""
if self._n_location_object_latlong_facet is None:
self._n_location_object_latlong_facet = self.ns_base[
"LatLongCoordinatesFacet-" + case_utils.local_uuid.local_uuid()
]
if self.use_deterministic_uuids:
self._n_location_object_latlong_facet = (
case_utils.inherent_uuid.get_facet_uriref(
self.n_location_object,
NS_UCO_LOCATION.LatLongCoordinatesFacet,
namespace=self.ns_base,
)
)
else:
self._n_location_object_latlong_facet = self.ns_base[
"LatLongCoordinatesFacet-" + case_utils.local_uuid.local_uuid()
]
self.graph.add(
(
self._n_location_object_latlong_facet,
Expand Down Expand Up @@ -683,9 +737,18 @@ def n_raster_picture_facet(self) -> rdflib.URIRef:
Initialized on first access.
"""
if self._n_raster_picture_facet is None:
self._n_raster_picture_facet = self.ns_base[
"RasterPictureFacet-" + case_utils.local_uuid.local_uuid()
]
if self.use_deterministic_uuids:
self._n_raster_picture_facet = (
case_utils.inherent_uuid.get_facet_uriref(
self.n_observable_object,
NS_UCO_OBSERVABLE.RasterPictureFacet,
namespace=self.ns_base,
)
)
else:
self._n_raster_picture_facet = self.ns_base[
"RasterPictureFacet-" + case_utils.local_uuid.local_uuid()
]
self.graph.add(
(
self._n_raster_picture_facet,
Expand Down Expand Up @@ -754,9 +817,18 @@ def n_unix_file_permissions_facet(self) -> rdflib.URIRef:
Initialized on first access.
"""
if self._n_unix_file_permissions_facet is None:
self._n_unix_file_permissions_facet = self.ns_base[
"UNIXFilePermissionsFacet-" + case_utils.local_uuid.local_uuid()
]
if self.use_deterministic_uuids:
self._n_unix_file_permissions_facet = (
case_utils.inherent_uuid.get_facet_uriref(
self.n_observable_object,
NS_UCO_OBSERVABLE.UNIXFilePermissionsFacet,
namespace=self.ns_base,
)
)
else:
self._n_unix_file_permissions_facet = self.ns_base[
"UNIXFilePermissionsFacet-" + case_utils.local_uuid.local_uuid()
]
self.graph.add(
(
self._n_unix_file_permissions_facet,
Expand Down Expand Up @@ -791,6 +863,13 @@ def oo_slug(self, value: str) -> None:
assert isinstance(value, str)
self._oo_slug = value

@property
def use_deterministic_uuids(self) -> bool:
"""
No setter provided.
"""
return self._use_deterministic_uuids


def main() -> None:
case_utils.local_uuid.configure()
Expand Down Expand Up @@ -820,7 +899,9 @@ def main() -> None:
out_graph.namespace_manager.bind("uco-observable", NS_UCO_OBSERVABLE)
out_graph.namespace_manager.bind("uco-types", NS_UCO_TYPES)

exiftool_rdf_mapper = ExifToolRDFMapper(out_graph, NS_BASE)
exiftool_rdf_mapper = ExifToolRDFMapper(
out_graph, NS_BASE, use_deterministic_uuids=args.use_deterministic_uuids
)
exiftool_rdf_mapper.map_raw_and_printconv_rdf(args.raw_xml, args.print_conv_xml)

# _logger.debug("args.output_format = %r." % args.output_format)
Expand Down
1 change: 1 addition & 0 deletions tests/govdocs1/files/799/987/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ analysis.ttl: \
--output-format turtle \
--print-conv-xml 799987_printConv.xml \
--raw-xml 799987_raw.xml \
--use-deterministic-uuids \
__$@
source $(top_srcdir)/tests/venv/bin/activate \
&& case_validate \
Expand Down
Loading

0 comments on commit 27d13e4

Please sign in to comment.