From f11db45f5f80b60f8eeb9384ffcdfee6cc7465ea Mon Sep 17 00:00:00 2001 From: coxifred Date: Tue, 30 Jun 2020 23:39:17 +0200 Subject: [PATCH] Some work --- .gitignore | 3 + python/build.gradle | 1 + python/communication/abstractCode.py | 31 +++++ python/superwatt.py | 4 + python/utils/__init__.py | 0 python/utils/functions.py | 180 +++++++++++++++++++++++++++ python/utils/singleton.py | 23 ++++ 7 files changed, 242 insertions(+) create mode 100644 .gitignore create mode 100644 python/communication/abstractCode.py create mode 100644 python/utils/__init__.py create mode 100644 python/utils/functions.py create mode 100644 python/utils/singleton.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fedc3ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.gradle +python/.gradle +python/utils/__pycache__ diff --git a/python/build.gradle b/python/build.gradle index 70c59b1..51df8f9 100644 --- a/python/build.gradle +++ b/python/build.gradle @@ -2,6 +2,7 @@ python { pip 'serial:0.0.97' pip 'crcmod:1.7' pip 'pyusb:1.0.2' + pip 'requests:2.24.0' python.scope = VIRTUALENV envPath = '.gradle/python' diff --git a/python/communication/abstractCode.py b/python/communication/abstractCode.py new file mode 100644 index 0000000..f74038f --- /dev/null +++ b/python/communication/abstractCode.py @@ -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 diff --git a/python/superwatt.py b/python/superwatt.py index f3c7672..d6b2d93 100644 --- a/python/superwatt.py +++ b/python/superwatt.py @@ -1,4 +1,8 @@ import serial, time, sys, string +from utils.functions import Functions +from os import listdir +from utils.singleton import Singleton + def main(): diff --git a/python/utils/__init__.py b/python/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/utils/functions.py b/python/utils/functions.py new file mode 100644 index 0000000..d06db7f --- /dev/null +++ b/python/utils/functions.py @@ -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) diff --git a/python/utils/singleton.py b/python/utils/singleton.py new file mode 100644 index 0000000..25cae5f --- /dev/null +++ b/python/utils/singleton.py @@ -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)