Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using regex to match detector classes directly as appose to iterating #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 31 additions & 9 deletions httpagentparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* reliable enough for practical purposes
* assist python web apps to detect clients.
"""
import re

__version__ = '1.8.2'

Expand All @@ -17,15 +18,36 @@ class DetectorsHub(dict):
def __init__(self, *args, **kw):
dict.__init__(self, *args, **kw)
for typ in self._known_types:
self.setdefault(typ, [])
self.setdefault(typ, "")
self.detectors = {}
self.registerDetectors()
self.compileRegexes()

def register(self, detector):
if detector.info_type not in self._known_types:
self[detector.info_type] = [detector]
self._known_types.insert(detector.order, detector.info_type)

if isinstance(detector.look_for, (tuple, list)):
for word in detector.look_for:
self.detectors[detector.info_type + word] = detector
else:
self.detectors[detector.info_type + detector.look_for] = detector
self.addToRegex(detector)

def addToRegex(self, detector):
if isinstance(detector.look_for, (tuple, list)):
look_for = "|".join(detector.look_for)
else:
look_for = detector.look_for

if self.get(detector.info_type):
self[detector.info_type] += "|" + look_for
else:
self[detector.info_type].append(detector)
self[detector.info_type] = look_for

def compileRegexes(self):
for info_type in self._known_types:
self[info_type] = re.compile(self[info_type])

def __iter__(self):
return iter(self._known_types)
Expand Down Expand Up @@ -633,19 +655,20 @@ class prefs: # experimental

detectorshub = DetectorsHub()


def detect(agent, fill_none=False):
"""
fill_none: if name/version is not detected respective key is still added to the result with value None
"""
result = dict(platform=dict(name=None, version=None))
_suggested_detectors = []
# _suggested_detectors = []

for info_type in detectorshub:
detectors = _suggested_detectors or detectorshub[info_type]
for detector in detectors:
# detectors = _suggested_detectors or detectorshub[info_type]
matching = detectorshub[info_type].finditer(agent)
for match in matching:
try:
detector.detect(agent, result)
if detectorshub.detectors[info_type + match.group()].detect(agent, result):
break
except Exception as _err:
pass

Expand All @@ -654,7 +677,6 @@ def detect(agent, fill_none=False):
outer_value = result.setdefault(outer_key, dict())
for inner_key in ('name', 'version'):
outer_value.setdefault(inner_key, None)

return result


Expand Down