-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_string_core.py
180 lines (155 loc) · 7.39 KB
/
find_string_core.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#
"""
Scan APS logfile and extract relevant items
"""
# Version INIT Started 08.Dec.2019 Author Gerhard Zellermann
# - adapted from scanAPSlog.py
# Version 3 Status 30.Nov.2021
# - convert characters fom search text to '_' if not allowed in path name
import sys
import os
import glob
from email.utils import formatdate
import datetime
from datetime import timezone
import time
import zipfile
from decimal import *
import binascii
import copy
def scanLogfile(fn, entries):
global fn_base # keep first match in case of wild card file list
global log
global varlog
global newLoop
global loopCount
if filecount == 0 : # initalize file loop
fn_base = fn + '.' + var_Name # mod V3
log = open(fn_base + '.found', 'w')
#log.write('AAPS logfile scan for string"'+varLabel+'"n created on ' + formatdate(localtime=True) + '\n')
log.write('FILE='+fn + '\n')
global lcount
lcount = 0
if isZip:
with zipfile.ZipFile(fn) as z:
for filename in z.namelist():
lf = z.open(filename) # has only 1 member file
else:
lf = open(fn, 'r')
#lf = open(fn, 'r')
notEOF = True # needed because "for zeile in lf" does not work with AAPS 2.5
cont = 'MORE' # in case nothing found
while notEOF: # needed because "for zeile in lf" does not work with AAPS 2.5
try: # needed because "for zeile in lf" does not work with AAPS 2.5
zeile = lf.readline() # needed because "for zeile in lf" does not work with AAPS 2.5
if isZip: zeile = str(zeile)[2:-3]# strip off the "'b....'\n" remaining from the bytes to str conversion
if zeile == '': # needed because "for zeile in lf" does not work with AAPS 2.5
notEOF = False # needed because "for zeile in lf" does not work with AAPS 2.5
break # needed because "for zeile in lf" does not work with AAPS 2.5
lcount += 1
#print(zeile)
if zeile.find(varLabel) >=0:
log.write('row'+f'{str(lcount):>6} '+zeile+'\n')
print('row'+f'{str(lcount):>6} '+zeile)
loopCount += 1
except UnicodeDecodeError: # needed because "for zeile in lf" does not work with AAPS 2.5 containing non-printing ASCII codes
lcount += 1 # skip this line, it contains non-ASCII characters!
lf.close()
return cont
def parameters_known(myseek, arg2, variantFile, startLabel, stoppLabel, entries):
# start of top level analysis
global fn
global varLabel, var_Name # mod V3
global fn_first
global filecount, loopCount
global t_startLabel, t_stoppLabel
global varFile
global isAndroid # flag for running on Android
global isZip # flag for input file type
global newLoop # flag whether data collection for new loop started
#global entries
t_startLabel= startLabel
t_stoppLabel= stoppLabel
filecount = 0
loopCount = 0
oldloopCount= 0
myfile = ''
#arg2 = arg2.replace('_', ' ') # get rid of the UNDERSCOREs
#doit = arg2.split('/')
varFile = variantFile # on Windows
varLabel = os.path.basename(varFile) # do not overwrite the calling arg value
if varLabel[len(varLabel)-4:] == '.dat' : # drop the tail coming from DOS type ahead
varLabel = varLabel[:-4]
print('initially:', varLabel)
var_Name = varLabel.replace('"', '_') # mod V3
var_Name = var_Name.replace(':', '_') # mod V3
var_Name = var_Name.replace('{', '_') # mod V3
var_Name = var_Name.replace('}', '_') # mod V3
var_Name = var_Name.replace('[', '_') # mod V3
var_Name = var_Name.replace(']', '_') # mod V3
print('finally :', varLabel)
logListe = glob.glob(myseek+myfile, recursive=False)
if arg2[:7] == 'Android' :
isAndroid = True
else:
isAndroid = False
for fn in logListe:
ftype = fn[len(fn)-3:]
useFile = False
if isAndroid and ftype=='log': useFile = True
elif not isAndroid and (ftype=='zip' or ftype=='log' or ftype.find('.')>0) : useFile = True # valid logfiles should end with "_.0" thru "_.99" or "zip"
#print(ftype, str(useFile))
if useFile:
isZip = ( ftype == 'zip')
if filecount == 0 : # initalize file loop
#ce_file = fn + '.' + varLabel + '.txt'
#cel = open(ce_file, 'w')
#cel.write('AAPS scan from ' + varLabel + ' for SMB comparison created on ' + formatdate(localtime=True) + '\n')
#cel.write('FILE='+fn + '\n')
#cel.close()
#my_ce_file(ce_file) # exports name to determine_basal.py
fn_first = fn
if not isAndroid: log_msg ('Scanning logfile '+fn)
cont = scanLogfile(fn, entries)
#print('returned to parameters_known:', CarbReqGram, 'when:', CarbReqTime)
if oldloopCount < loopCount:
log_msg(' contained ' + str(loopCount-oldloopCount) + ' occurences')
oldloopCount = loopCount
filecount += 1
if cont == 'STOP': break # end of time window reached
if filecount == 0 :
log_msg ('no such logfile: "'+myseek+'"')
return
#loopCount = len(loop_mills)
if loopCount == 0 :
log_msg ('no entries found in logfile(s): "'+myseek+'"')
#return #sys.exit()
log.write('ENDE\n')
log.close()
#varlog.close()
if loopCount > 0 : # --- save the results from current logfile --------------
sepLine = ''
for i in range(177):
sepLine += '-'
#tabz = 'Totals:'+ f'{round(origSMBsum,1):>116} {round(emulSMBsum,1):>4} {round(origBasalint,2):>10} {round(emulBasalint,2):>7}'
#print(sepLine + '\n' + tabz)
#xyf.write(sepLine + '\n' + tabz + '\n')
#xyf.close()
print('Found', str(loopCount),'occurences in', str(filecount),'files\nresults saved in file "'+ fn_base + '.found"')
pass
def set_tty(printframe, txtbox, channel): # for GIU
global how_to_print
how_to_print = channel
global runframe
runframe = printframe
global lfd
lfd = txtbox
def log_msg(msg): # for GUI
if how_to_print == 'GUI':
lfd['state'] = 'normal'
lfd.insert('end', msg + '\n')
lfd.see('end')
lfd['state'] = 'disabled'
runframe.update() # update frame display
else:
print(msg)