\r\n

51Degrees Device Detection Python  4.4

Device Detection services for 51Degrees Pipeline

onpremise/metadata_console.py

The device detection data file contains meta data that can provide additional information about the various records in the data model. This example shows how to access this data and display the values available.

A list of the properties will be displayed, along with some additional information about each property.

Finally, the evidence keys that are accepted by device detection are listed. These are the keys that, when added to the evidence collection in flow data, could have some impact on the result returned by device detection.

This example is available in full on GitHub.

This example requires a local data file. The free 'Lite' data file can be acquired by pulling the git submodules under this repository (run `git submodule update --recursive`) or from the device-detection-data GitHub repository.

The Lite data file is only used for illustration, and has limited accuracy and capabilities. Find out about the more capable data files that are available on our pricing page

Required PyPi Dependencies:

1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
4 # Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 #
6 # This Original Work is licensed under the European Union Public Licence
7 # (EUPL) v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # *********************************************************************
22 
23 
43 
44 from pathlib import Path
45 import sys
46 from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder
47 from fiftyone_pipeline_core.logger import Logger
48 from fiftyone_pipeline_core.basiclist_evidence_keyfilter import BasicListEvidenceKeyFilter
49 # pylint: disable=E0402
50 from ..example_utils import ExampleUtils
51 from fiftyone_devicedetection_shared.example_constants import EVIDENCE_VALUES
52 from fiftyone_devicedetection_shared.example_constants import LITE_DATAFILE_NAME
53 
54 bgRed = "\u001b[41m"
55 fgWhite = "\u001b[37;1m"
56 colReset = "\u001b[0m"
57 
58 class MetaDataConsole():
59  def run(self, data_file, logger, output):
60 
61  # In this example, we use the DeviceDetectionPipelineBuilder
62  # and configure it in code. For more information about
63  # pipelines in general see the documentation at
64  # http://51degrees.com/documentation/4.3/_concepts__configuration__builders__index.html
65  pipeline = DeviceDetectionPipelineBuilder(
66  data_file_path = data_file,
67  # We use the low memory profile as its performance is
68  # sufficient for this example. See the documentation for
69  # more detail on this and other configuration options:
70  # http://51degrees.com/documentation/4.3/_device_detection__features__performance_options.html
71  # http://51degrees.com/documentation/4.3/_features__automatic_datafile_updates.html
72  # http://51degrees.com/documentation/4.3/_features__usage_sharing.html
73  performance_profile = "LowMemory",
74  # inhibit sharing usage for this test, usually this
75  # should be set "true"
76  usage_sharing = False,
77  # Inhibit auto-update of the data file for this example
78  auto_update = False,
79  licence_keys = "").add_logger(logger).build()
80  self.outputProperties(pipeline.get_element("device"), output)
81  self.outputEvidenceKeyDetails(pipeline.get_element("device"), output)
82 
83  ExampleUtils.check_data_file(pipeline, logger)
84 
85 
86  def outputEvidenceKeyDetails(self, engine, output):
87  if (issubclass(type(engine.get_evidence_key_filter()), BasicListEvidenceKeyFilter)):
88 
89  # If the evidence key filter extends BasicListEvidenceKeyFilter then we can
90  # display a list of accepted keys.
91  filter = engine.get_evidence_key_filter()
92  output("Accepted evidence keys:")
93  for key in filter.list:
94  output(f"\t{key}")
95  else:
96  output("The evidence key filter has type " +
97  f"{type(engine.get_evidence_key_filter())}. As this does not extend " +
98  "BasicListEvidenceKeyFilter, a list of accepted values cannot be " +
99  "displayed. As an alternative, you can pass evidence keys to " +
100  "filter.filter_evidence_key(string) to see if a particular key will be included " +
101  "or not.")
102  output("For example, header.user-agent is " +
103  ("" if engine.get_evidence_key_filter().filter_evidence_key("header.user-agent") else "not ") +
104  "accepted.")
105 
106  def outputProperties(self, engine, output):
107  for propertyName, property in engine.get_properties().items():
108  # Output some details about the property.
109  # If we're outputting to console then we also add some formatting to make it
110  # more readable.
111  output(f"{bgRed}{fgWhite}Property - {propertyName}{colReset}" +
112  f"[Category: {property['category']}] ({property['type']})")
113 
114 def main(argv):
115  # In this example, by default, the 51degrees "Lite" file needs to be
116  # somewhere in the project space, or you may specify another file as
117  # a command line parameter.
118  #
119  # Note that the Lite data file is only used for illustration, and has
120  # limited accuracy and capabilities.
121  # Find out about the Enterprise data file on our pricing page:
122  # https://51degrees.com/pricing
123  data_file = argv[0] if len(argv) > 0 else ExampleUtils.find_file(LITE_DATAFILE_NAME)
124 
125  # Configure a logger to output to the console.
126  logger = Logger(min_level="info")
127 
128  if (data_file != None):
129  MetaDataConsole().run(data_file, logger, print)
130  else:
131  logger.log("error",
132  "Failed to find a device detection data file. Make sure the " +
133  "device-detection-data submodule has been updated by running " +
134  "`git submodule update --recursive`.")
135 
136 if __name__ == "__main__":
137  main(sys.argv[1:])