\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:

  • fiftyone_devicedetection
1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2022 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 from ..example_utils import ExampleUtils
50 from fiftyone_devicedetection_shared.example_constants import EVIDENCE_VALUES
51 from fiftyone_devicedetection_shared.example_constants import LITE_DATAFILE_NAME
52 
53 bgRed = "\u001b[41m"
54 fgWhite = "\u001b[37;1m"
55 colReset = "\u001b[0m"
56 
57 class MetaDataConsole():
58  def run(self, data_file, logger, output):
59 
60  # In this example, we use the DeviceDetectionPipelineBuilder
61  # and configure it in code. For more information about
62  # pipelines in general see the documentation at
63  # http://51degrees.com/documentation/4.3/_concepts__configuration__builders__index.html
64  pipeline = DeviceDetectionPipelineBuilder(
65  data_file_path = data_file,
66  # We use the low memory profile as its performance is
67  # sufficient for this example. See the documentation for
68  # more detail on this and other configuration options:
69  # http://51degrees.com/documentation/4.3/_device_detection__features__performance_options.html
70  # http://51degrees.com/documentation/4.3/_features__automatic_datafile_updates.html
71  # http://51degrees.com/documentation/4.3/_features__usage_sharing.html
72  performance_profile = "LowMemory",
73  # inhibit sharing usage for this test, usually this
74  # should be set "true"
75  usage_sharing = False,
76  # Inhibit auto-update of the data file for this example
77  auto_update = False,
78  licence_keys = "").add_logger(logger).build()
79  self.outputProperties(pipeline.get_element("device"), output)
80  self.outputEvidenceKeyDetails(pipeline.get_element("device"), output)
81 
82  ExampleUtils.check_data_file(pipeline, logger)
83 
84 
85  def outputEvidenceKeyDetails(self, engine, output):
86  if (issubclass(type(engine.get_evidence_key_filter()), BasicListEvidenceKeyFilter)):
87 
88  # If the evidence key filter extends BasicListEvidenceKeyFilter then we can
89  # display a list of accepted keys.
90  filter = engine.get_evidence_key_filter()
91  output("Accepted evidence keys:")
92  for key in filter.list:
93  output(f"\t{key}")
94  else:
95  output("The evidence key filter has type " +
96  f"{type(engine.get_evidence_key_filter())}. As this does not extend " +
97  "BasicListEvidenceKeyFilter, a list of accepted values cannot be " +
98  "displayed. As an alternative, you can pass evidence keys to " +
99  "filter.filter_evidence_key(string) to see if a particular key will be included " +
100  "or not.")
101  output("For example, header.user-agent is " +
102  ("" if engine.get_evidence_key_filter().filter_evidence_key("header.user-agent") else "not ") +
103  "accepted.")
104 
105  def outputProperties(self, engine, output):
106  for propertyName, property in engine.get_properties().items():
107  # Output some details about the property.
108  # If we're outputting to console then we also add some formatting to make it
109  # more readable.
110  output(f"{bgRed}{fgWhite}Property - {propertyName}{colReset}" +
111  f"[Category: {property['category']}] ({property['type']})")
112 
113 def main(argv):
114  # In this example, by default, the 51degrees "Lite" file needs to be
115  # somewhere in the project space, or you may specify another file as
116  # a command line parameter.
117  #
118  # Note that the Lite data file is only used for illustration, and has
119  # limited accuracy and capabilities.
120  # Find out about the Enterprise data file on our pricing page:
121  # https://51degrees.com/pricing
122  data_file = argv[0] if len(argv) > 0 else ExampleUtils.find_file(LITE_DATAFILE_NAME)
123 
124  # Configure a logger to output to the console.
125  logger = Logger(min_level="info")
126 
127  if (data_file != None):
128  MetaDataConsole().run(data_file, logger, print)
129  else:
130  logger.log("error",
131  "Failed to find a device detection data file. Make sure the " +
132  "device-detection-data submodule has been updated by running " +
133  "`git submodule update --recursive`.")
134 
135 if __name__ == "__main__":
136  main(sys.argv[1:])