aboutsummaryrefslogtreecommitdiff
path: root/tools.py
blob: 862f15d18e403720ef3b240d5988ac0b922e6713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#/bin/python3

import sys
import re
import colorama
from termcolor import colored
colorama.init()

from collections import Counter

import commands

def offline_counts(file):
    counts = Counter()
    for sentence in commands.tail(file, 4):
        counts.update(word.strip('.,?!"\'').upper() for word in sentence.decode('utf-8').split())
    return counts["OFFLINE"]

def show_help():
    print("{} [command]".format(sys.argv[0]))
    print("{}            Check internet status, log status in file and, if\n            {}needed, restart interface.".format(sys.argv[0], " " * len(sys.       argv[0])))
    print("{} tail       Follow log file like tail command".format(sys.argv[0]))
    print("{} cat        Like cat command".format(sys.argv[0]))

offline = re.compile(r'.*OFFLINE.*', re.IGNORECASE)
online = re.compile(r'.*ONLINE.*', re.IGNORECASE)

def status_color(str_line):
    if type(str_line) is bytes:
        str_line = str_line.decode('utf-8')
    if offline.search(str_line):
        return colored(str_line, 'red')
    elif online.search(str_line):
        return colored(str_line, 'green')
    else:
        return str_line