aiscalator.core.log_regex_analyzer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 18
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A LogRegexAnalyzer.__init__() 0 10 1
A LogRegexAnalyzer.grep_logs() 0 16 4
A LogRegexAnalyzer.artifact() 0 3 1
1
# -*- coding: utf-8 -*-
2
# Apache Software License 2.0
3
#
4
# Copyright (c) 2018, Christophe Duong
5
#
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
9
#
10
# http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17
"""
18
Class to parse output logs from subprocess and catch particular expressions
19
"""
20
import logging
21
from re import search
22
23
24
class LogRegexAnalyzer():
25
    """
26
    A regular expression analyzer object to parse logs and extract
27
    values from patterns in the logs.
28
    ...
29
30
    Attributes
31
    ----------
32
    _artifact : str
33
        Value of the pattern found in the logs
34
    _pattern : bytes
35
        Regular expression to search for in the logs
36
    """
37
38
    def __init__(self, pattern=None, log_level=logging.DEBUG):
39
        """
40
        Parameters
41
        ----------
42
        pattern : pattern
43
            Regular expression to search for in the logs
44
        """
45
        self._artifact = None
46
        self._pattern = pattern
47
        self._log_level = log_level
48
49
# https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
50
    def grep_logs(self, pipe):
51
        """
52
        Reads the logs and extract values defined by the pattern
53
54
        Parameters
55
        ----------
56
        pipe
57
            Stream of logs to analyze
58
        """
59
        logger = logging.getLogger(__name__)
60
        for line in iter(pipe.readline, b''):  # b'\n'-separated lines
61
            logger.log(self._log_level, line.decode("utf-8"))
62
            if self._pattern is not None:
63
                match = search(self._pattern, line)
64
                if match:
65
                    self._artifact = match.group(1).decode("utf-8")
66
67
    def artifact(self):
68
        """ Returns the artifact extracted from the logs."""
69
        return self._artifact
70