Skip to content

Commit

Permalink
Some work
Browse files Browse the repository at this point in the history
  • Loading branch information
coxifred committed Jun 30, 2020
1 parent e12d1fa commit f11db45
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.gradle
python/.gradle
python/utils/__pycache__
1 change: 1 addition & 0 deletions python/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
31 changes: 31 additions & 0 deletions python/communication/abstractCode.py
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
4 changes: 4 additions & 0 deletions python/superwatt.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
Empty file added python/utils/__init__.py
Empty file.
180 changes: 180 additions & 0 deletions python/utils/functions.py
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)
23 changes: 23 additions & 0 deletions python/utils/singleton.py
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)

0 comments on commit f11db45

Please sign in to comment.