-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.gradle | ||
python/.gradle | ||
python/utils/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
import math | ||
import datetime | ||
from datetime import timedelta | ||
from abc import ABCMeta, abstractmethod | ||
from utils.functions import Functions | ||
from utils.singleton import Singleton | ||
|
||
class AbstractClassRule(): | ||
__metaclass__ = ABCMeta | ||
|
||
def __init__(self,singleton): | ||
|
||
self.error=0 | ||
self.accept=-1 | ||
self.singleton=singleton | ||
|
||
def run(self): | ||
try: | ||
self.filter() | ||
return self.accept | ||
except Exception as err: | ||
self.error=1 | ||
self.errorText=str(err).replace(" ", "_") | ||
raise | ||
finally: | ||
self.endtime=datetime.datetime.now() | ||
|
||
def filter(self): | ||
Functions.log("DBG","Abstract filter, you should not see this message","CORE") | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import datetime | ||
import subprocess | ||
import re | ||
import sys | ||
import threading | ||
import hashlib | ||
import requests | ||
import warnings | ||
import json | ||
from utils.singleton import Singleton | ||
|
||
|
||
|
||
class Functions: | ||
|
||
@staticmethod | ||
def log(level, message, source): | ||
date = str(datetime.datetime.now()) | ||
singleton=Singleton() | ||
aLog=date + " " + "[" + threading.current_thread().name + "] " + level + " " + source + " " + message | ||
if ( level == "DBG" ): | ||
if singleton.debug: | ||
Functions.logdebug() | ||
print(aLog) | ||
elif ( level == "ERR" or level == "WNG" or level == "DEAD" ): | ||
Functions.logred() | ||
print(aLog) | ||
elif ( level == "ASK" ): | ||
Functions.logyellow() | ||
print(aLog) | ||
else: | ||
Functions.lognormal() | ||
print(aLog) | ||
if ( level == "DEAD" ): | ||
sys.exit(1) | ||
singleton.logs.append(aLog) | ||
|
||
@staticmethod | ||
def logred(): | ||
sys.stdout.write("\033[1;37;41m") | ||
|
||
@staticmethod | ||
def logyellow(): | ||
sys.stdout.write("\033[1;33;40m") | ||
|
||
@staticmethod | ||
def lognormal(): | ||
sys.stdout.write("\033[1;37;40m") | ||
|
||
@staticmethod | ||
def logdebug(): | ||
sys.stdout.write("\033[1;36;40m") | ||
|
||
@staticmethod | ||
def requestHttp(request): | ||
Functions.log("DBG","HttpRequest: " + request,"Functions") | ||
requests.packages.urllib3.disable_warnings() | ||
r = requests.get(request, verify=False) | ||
Functions.log("DBG","Response code: " + str(r.status_code),"Functions") | ||
if r.status_code is 200: | ||
body = r.content | ||
array=body.split("\n") | ||
while '' in array: | ||
array.pop(array.index('')) | ||
return array | ||
else: | ||
Functions.log("ERR","Error while request " + str(r.text),"Functions") | ||
raise Exception('Error while request') | ||
|
||
@staticmethod | ||
def getFieldFromString(string,delimiter,fieldNumber): | ||
return re.split(delimiter,string)[fieldNumber] | ||
|
||
@staticmethod | ||
def getFromFieldFromString(string,delimiter,fieldNumber): | ||
tab=re.split(delimiter,string); | ||
return tab[fieldNumber:] | ||
|
||
@staticmethod | ||
def getFirstMatchInAFile(string,file): | ||
with open(file, "r") as f: | ||
for line in f.readlines(): | ||
if string in line: | ||
f.close() | ||
return(line.rstrip('\n')) | ||
|
||
@staticmethod | ||
def getFirstMatchInArray(string,array): | ||
for line in array: | ||
if string in line: | ||
return(line.rstrip('\n')) | ||
@staticmethod | ||
def getLastMatchInArray(string,array): | ||
returnLine="" | ||
for line in array: | ||
if string in line: | ||
returnLine=line | ||
return returnLine | ||
|
||
@staticmethod | ||
def getLastMatchReInArray(regexp,array): | ||
returnLine="" | ||
for line in array: | ||
if re.match(regexp, line) is not None: | ||
returnLine=line | ||
return returnLine | ||
|
||
@staticmethod | ||
def getFirstMatchInLine(string,line): | ||
array=line.split("\n") | ||
return Functions.getFirstMatchInArray(string,array).rstrip('\n') | ||
|
||
@staticmethod | ||
def displayFromLastSeenPatternFromArray(string,array): | ||
returnArray=[] | ||
for line in array: | ||
if string in line: | ||
del returnArray[:] | ||
returnArray.append(line) | ||
else: | ||
returnArray.append(line) | ||
return returnArray | ||
|
||
@staticmethod | ||
def getLastMatchInLine(string,line): | ||
array=line.split("\n") | ||
return Functions.getLastMatchInArray(string,array).rstrip('\n') | ||
|
||
@staticmethod | ||
def kommandShell(aKommand): | ||
return_output=subprocess.check_output(aKommand,shell=True).rstrip('\n') | ||
return return_output | ||
|
||
@staticmethod | ||
def kommandShellInArray(aKommand): | ||
return_output=Functions.kommandShell(aKommand).split('\n') | ||
return return_output | ||
|
||
@staticmethod | ||
def getDateFormat(format): | ||
if format == "default": | ||
format='%Y%m%d%H%M%S' | ||
return datetime.datetime.now().strftime(format) | ||
|
||
@staticmethod | ||
def getDateFormatFromDate(date,format): | ||
if format == "default": | ||
format='%Y%m%d%H%M%S' | ||
return date.datetime.strftime(format) | ||
|
||
@staticmethod | ||
def getDateFormatFromString(stringDate,format): | ||
if format == "default": | ||
format='%Y%m%d%H%M%S' | ||
return datetime.datetime.strptime(stringDate,format) | ||
|
||
@staticmethod | ||
def loadFileInALine(file): | ||
lines="" | ||
with open(file, "r") as f: | ||
for line in f.readlines(): | ||
lines += line +"\n" | ||
f.close() | ||
return(lines.rstrip('\n+')) | ||
|
||
@staticmethod | ||
def writeArrayInAFile(file,array): | ||
Functions.log("DBG","Writing " + str(len(array)) + " line(s) in " + file,"Functions") | ||
aFile = open(file, "w") | ||
for line in array: | ||
aFile.write(line + "\n"); | ||
|
||
@staticmethod | ||
def loadFileInArray(file): | ||
lines=[] | ||
with open(file, "r") as f: | ||
for line in f.readlines(): | ||
lines.append(line.rstrip('\n+')) | ||
f.close() | ||
return(lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Singleton(object): | ||
class __Singleton: | ||
def __str__(self): | ||
return self | ||
|
||
def __init__(self): | ||
self.hostName='' | ||
self.debug=False | ||
self.version='' | ||
self.params={} | ||
self.logs=[] | ||
|
||
instance = None | ||
|
||
def __new__(c): | ||
if not Singleton.instance: | ||
Singleton.instance = Singleton.__Singleton() | ||
return Singleton.instance | ||
|
||
def __getattr__(self, attr): | ||
return getattr(self.instance, attr) | ||
def __setattr__(self, attr): | ||
return setattr(self.instance, attr) |