Data-Test-Executer Framework speziell zum Test von Datenverarbeitungen mit Datengenerierung, Systemvorbereitungen, Einspielungen, ganzheitlicher diversifizierender Vergleich
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

145 lines
4.4 KiB

# Funktionen zum Dateizugriff mit Suchen, Lesen, Schreiben
# ------------------------------------------------------------
"""
"""
import codecs
import os
import os.path
import re
import basic.message
import basic.program
from pprint import pp
def getDump(obj):
result=""
print (str(type(obj)))
result = vars(obj)
return str(result)
# if type(obj) == "__dict__"
def getFiles(msg, path, pattern, conn):
"""
search filenames in the directory - if conn is set search remote
:param msg: -- msg-Objekt
:param path: -- Pfad - String
:param pattern: -- Dateiname als Pattern
:param conn:
:return: Array mit gefundenen Dateien, nur Dateiname
"""
if conn is not None:
return getRemoteFiles(msg, path, pattern, conn)
job = basic.program.Job.getInstance()
verify = int(job.getDebugLevel("file_tool"))
out = []
msg.debug(verify, "getFiles " + path + " , " + pattern)
for f in os.listdir(path):
msg.debug(verify, "getFiles " + f)
if re.search(pattern, f):
msg.debug(verify, "match " + f)
out.append(f)
return out
def getRemoteFiles(msg, path, pattern, conn):
"""
search filenames in the directory - if conn is set search remote
:param msg: -- msg-Objekt
:param path: -- Pfad - String
:param pattern: -- Dateiname als Pattern
:param conn:
:return: Array mit gefundenen Dateien, nur Dateiname
"""
def getFilesRec(msg, path, pattern):
"""
Sucht Dateien im Verzeichnis rekursiv
:param msg: -- msg-Objekt
:param path: -- Pfad - String
:param pattern: -- Dateiname als Pattern
:return: Array mit gefundenen Dateien, absoluter Pfad
"""
job = basic.program.Job.getInstance()
verify = int(job.getDebugLevel("file_tool"))
out = []
msg.debug(verify, "getFilesRec " + path + " , " + pattern)
for (r, dirs, files) in os.walk(path):
for f in files:
msg.debug(verify, "getFilesRec " + f)
if re.search(pattern, f):
msg.debug(verify, "match " + f)
out.append(os.path.join(r, f))
return out
def getTree(msg, pfad):
job = basic.program.Job.getInstance()
verify = int(job.getDebugLevel("file_tool"))
msg.debug(verify, "getTree " + pfad )
tree = {}
files = []
for f in os.listdir(pfad):
if os.path.isDir(os.path.join(pfad, f)):
tree[f] = getTree(msg, os.path.join(pfad, f))
elif os.path.isFile(os.path.join(pfad, f)):
files.append(f)
tree["_files_"] = files
return tree
def mkPaths(msg, pfad):
job = basic.program.Job.getInstance()
verify = int(job.getDebugLevel("file_tool"))
modus = job.conf.confs["paths"]["mode"]
os.makedirs(pfad, exist_ok=True)
def getFileEncoding(msg, path):
print("--- getFileEncoding "+path)
encodings = ['utf-8', 'iso-8859-1'] # add more
for e in encodings:
print(e)
try:
fh = codecs.open(path, 'r', encoding=e)
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
except:
print("except")
else:
print('opening the file with encoding: %s ' % e)
return e
return detectFileEncode(msg, path)
def detectFileEncode(msg, path): # return ""
job = basic.program.Job.getInstance()
verify = int(job.getDebugLevel("file_tool"))
print(path)
cntIso = 0
cntUtf = 0
with open(path, 'rb') as file:
while (byte := file.read(1)):
i = int.from_bytes(byte, "little")
#byte = file.read(1)
if ((i == 196) or (i == 228) or (i == 214) or (i == 246) or (i == 220) or (i == 252) or (i == 191)):
cntIso += 1
if (i == 160):
pass
elif (i > 127):
cntUtf += 1
if (cntIso > cntUtf):
return 'iso-8859-1'
return 'utf-8'
def readFileLines(msg, path):
job = basic.program.Job.getInstance()
verify = int(job.getDebugLevel("file_tool"))
enc = detectFileEncode(msg, path)
with open(path, 'r', encoding=enc) as file:
lines = file.read().splitlines()
file.close()
return lines
def writeFileText(msg, path, text, enc="utf-8"):
job = basic.program.Job.getInstance()
verify = int(job.getDebugLevel("file_tool"))
with open(path, 'w', encoding=enc) as file:
file.write(text)
file.close()