mirror of
https://github.com/jerikan-network/cmdb.git
synced 2024-05-06 04:54:50 +00:00
No history. If you have the original history, you can use: ``` git replace THISCOMMIT b0b998bd1c651e308ac71a9158e07e5c3521a281 ```
32 lines
963 B
Python
32 lines
963 B
Python
"""Classifier-related functions."""
|
|
|
|
import re
|
|
import functools
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Classifier(object):
|
|
|
|
def __init__(self, classifier):
|
|
self.classifier = classifier
|
|
|
|
@functools.lru_cache(maxsize=None)
|
|
def scope(self, device):
|
|
"""Build scope for a given device from the classifier structure."""
|
|
matchers = self.classifier["matchers"]
|
|
scope = {}
|
|
for matcher in matchers:
|
|
for regex in matcher:
|
|
mo = re.search(regex, device)
|
|
if mo:
|
|
logger.debug("device {} match regex {}".format(device,
|
|
regex))
|
|
for k, v in matcher[regex].items():
|
|
if isinstance(v, str):
|
|
scope[k] = mo.expand(v)
|
|
else:
|
|
scope[k] = v
|
|
return scope
|