Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from fiftyone_pipeline_engines.aspectdata import AspectData
from fiftyone_pipeline_engines.aspectdata_dictionary import AspectDataDictionary
from fiftyone_pipeline_core.aspectproperty_value import AspectPropertyValue
from fiftyone_pipeline_core.elementdata import _CheckedDictWrites

import json

Expand Down Expand Up @@ -60,7 +61,7 @@ def _cloud_response(flowdata):
return data.get("cloud")


class HardwareProfileData(dict, AspectData):
class HardwareProfileData(_CheckedDictWrites, dict, AspectData):
"""!
One hardware profile from a profile lookup, read in the way the device
data is read. It is Aspect Data, so a property is read as an attribute,
Expand All @@ -72,6 +73,10 @@ class HardwareProfileData(dict, AspectData):
Attribute access, get(), [] and in compare names ignoring letter case.
A property whose name is also a dict method, such as keys, is read
with get() or [].

A profile is part of the hardware data, and is written only while the
engine processes. Once it has finished, setting an attribute or writing
through a dict method raises an exception.
"""

# Attributes set in the constructor, which are never read as properties.
Expand All @@ -93,6 +98,18 @@ def __init__(self, flow_element, contents):
# reported in the same way for both.
self.missing_property_service = CloudMissingPropertyService()

@property
def _finished(self):
# Finished when the hardware data that holds the profile is, as
# FlowData records that on the data it holds, not on each profile.
holder = self.__dict__.get("_holder")
return (self.__dict__.get("_own_finished", False)
or (holder is not None and holder._finished))

@_finished.setter
def _finished(self, finished):
self.__dict__["_own_finished"] = finished

def _matching_key(self, key):
"""!
The key held for a property name given in another letter case, or
Expand Down Expand Up @@ -247,6 +264,8 @@ def process_internal(self, flowdata):
contents["profiles"] = devices

data = AspectDataDictionary(self, contents)
for device in devices:
device._holder = data

flowdata.set_element_data(data)

Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************

"""
Every FlowElement this package defines is processed, then a write to its
ElementData is expected to fail, as the thread safety page of the
specification requires.
"""

import importlib
import inspect
import json
import pkgutil
import unittest

import fiftyone_devicedetection_cloud
from fiftyone_pipeline_core.elementdata_dictionary import \
ElementDataDictionary
from fiftyone_pipeline_core.flowelement import FlowElement
from fiftyone_pipeline_core.pipelinebuilder import PipelineBuilder
from fiftyone_devicedetection_cloud.devicedetection_cloud import \
DeviceDetectionCloud
from fiftyone_devicedetection_cloud.hardwareprofile_cloud import \
HardwareProfileCloud

FINISHED = "finished processing"


def item_store(data):
"""The dictionary the data keeps its values in."""
if isinstance(data, dict):
return data
return data.contents


# FlowElements that only other FlowElements build on.
BASES = set()


class StandInRequestEngine(FlowElement):
"""Stands in for a Cloud Request Engine whose answer has an empty
section for each of the given data keys."""

def __init__(self, keys):
super(StandInRequestEngine, self).__init__()
self.datakey = "cloud"
self.flow_element_properties = {key: {} for key in keys}
self.answer = json.dumps({key: {} for key in keys})

def process_internal(self, flowdata):
flowdata.set_element_data(
ElementDataDictionary(self, {"cloud": self.answer}))


def defined_flow_elements(package):
"""Every FlowElement class defined in the package's own modules."""
found = set()
for module in pkgutil.walk_packages(package.__path__,
package.__name__ + "."):
loaded = importlib.import_module(module.name)
for _, cls in inspect.getmembers(loaded, inspect.isclass):
if (issubclass(cls, FlowElement)
and cls.__module__.startswith(package.__name__)):
found.add(cls)
return found


class FinishedWritesTests(unittest.TestCase):

def test_every_flow_element_refuses_writes_once_finished(self):
processed = set()
for element in [DeviceDetectionCloud(), HardwareProfileCloud()]:
builder = PipelineBuilder({"add_javascript_builder": False,
"use_setheader_properties": False})
flowdata = builder \
.add(StandInRequestEngine([element.datakey])) \
.add(element).build().create_flowdata()
flowdata.process()
data = flowdata.get(element.datakey)
with self.assertRaises(Exception) as caught:
data.written_after_processing = True
self.assertIn(FINISHED, str(caught.exception))
with self.assertRaises(Exception) as caught:
item_store(data)["written_after_processing"] = True
self.assertIn(FINISHED, str(caught.exception))
processed.add(type(element))
self.assertEqual(
defined_flow_elements(fiftyone_devicedetection_cloud) - BASES, processed)

def test_a_hardware_profile_refuses_writes_once_finished(self):
writes = (
("attribute", lambda p: setattr(p, "written", True)),
("[]=", lambda p: p.__setitem__("written", True)),
("del", lambda p: p.__delitem__("hardwarevendor")),
("update", lambda p: p.update(written=True)),
("pop", lambda p: p.pop("hardwarevendor")),
("popitem", lambda p: p.popitem()),
("setdefault", lambda p: p.setdefault("written", True)),
("clear", lambda p: p.clear()),
("|=", lambda p: p.__ior__({"written": True})),
)
element = HardwareProfileCloud()
request = StandInRequestEngine([element.datakey])
request.answer = json.dumps({element.datakey: {
"profiles": [{"hardwarevendor": "Apple"}]}})
flowdata = PipelineBuilder({"add_javascript_builder": False,
"use_setheader_properties": False}) \
.add(request).add(element).build().create_flowdata()
flowdata.process()
profile = flowdata.get(element.datakey).get("profiles")[0]
for name, write in writes:
with self.subTest(name):
with self.assertRaises(Exception) as caught:
write(profile)
self.assertIn(FINISHED, str(caught.exception))
self.assertEqual(["hardwarevendor"], list(profile))
self.assertEqual("Apple", profile.hardwarevendor.value())


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************

"""
Every FlowElement this package defines is processed, then a write to its
ElementData is expected to fail, as the thread safety page of the
specification requires. Reading values from the results is not a write.
"""

import importlib
import inspect
import pkgutil
import unittest

import fiftyone_devicedetection_onpremise
from fiftyone_devicedetection_onpremise.constants import RESULTS_CLOSED
from fiftyone_devicedetection_onpremise.devicedetection_onpremise import \
DeviceDetectionOnPremise
from fiftyone_pipeline_core.flowelement import FlowElement
from fiftyone_pipeline_core.pipelinebuilder import PipelineBuilder

DATA_FILE = "../../cxx/data/device-detection/51Degrees-LiteV4.1.hash"
USER_AGENT = ("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148")
FINISHED = "finished processing"


def defined_flow_elements(package):
"""Every FlowElement class defined in the package's own modules."""
found = set()
for module in pkgutil.walk_packages(package.__path__,
package.__name__ + "."):
loaded = importlib.import_module(module.name)
for _, cls in inspect.getmembers(loaded, inspect.isclass):
if (issubclass(cls, FlowElement)
and cls.__module__.startswith(package.__name__)):
found.add(cls)
return found


class FinishedWritesTests(unittest.TestCase):

def test_every_flow_element_refuses_writes_once_finished(self):
engine = DeviceDetectionOnPremise(
data_file_path=DATA_FILE, licence_keys="", auto_update=False)
pipeline = PipelineBuilder().add(engine).build()
self.addCleanup(pipeline.close)
with pipeline.create_flowdata() as flowdata:
flowdata.evidence.add("header.user-agent", USER_AGENT)
flowdata.process()
data = flowdata.get("device")
self.assertTrue(data.ismobile.value())
with self.assertRaises(Exception) as caught:
data.ismobile = False
self.assertIn(FINISHED, str(caught.exception))
self.assertTrue(data.ismobile.value())
self.assertEqual(
{DeviceDetectionOnPremise},
defined_flow_elements(fiftyone_devicedetection_onpremise))

def test_the_data_can_be_closed_once_finished(self):
# Closing releases the native results, which is not a write, so it
# works on finished data, as closing the FlowData does.
engine = DeviceDetectionOnPremise(
data_file_path=DATA_FILE, licence_keys="", auto_update=False)
pipeline = PipelineBuilder().add(engine).build()
self.addCleanup(pipeline.close)
with pipeline.create_flowdata() as flowdata:
flowdata.evidence.add("header.user-agent", USER_AGENT)
flowdata.process()
data = flowdata.get("device")
data.close()
with self.assertRaises(Exception) as caught:
data.get("ismobile")
self.assertEqual(RESULTS_CLOSED, str(caught.exception))
with self.assertRaises(Exception) as caught:
data.ismobile = False
self.assertIn(FINISHED, str(caught.exception))


if __name__ == "__main__":
unittest.main()
Loading
Loading