-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpinfo_2_rulebases_fws.py
54 lines (45 loc) · 1.74 KB
/
cpinfo_2_rulebases_fws.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
import re
from os import listdir
import time
import xlwt
# timer trigger to count time at the end of a script
start_time = time.time()
# folder with cpinfo files
listFF = listdir('C:/python/cpinfo/')
def rulebases(some_str):
"""trying to find rulebases info in CPinfo"""
# creating final variable to return back from this function
rulebases_final = ''
# regex for rulebases searching
rulebases = re.findall(
b"----\n.*\/rulebases_5_0.fws\n[-]*\n([\S\s]*?\n\)\n)", some_str)
# if rulebases found, decoding and changing to final variable
if rulebases:
rulebases_final = ([x.decode("cp1251") for x in rulebases])
return rulebases_final
# open file from listdir cpinfo in read_binary mode
for file in listFF:
# printing info to the IDLE
print('Trying to open file ' + file)
# opening file
f = open('C:/python/cpinfo//{0}'.format(file), 'rb')
# printing info to the IDLE
print('File opened. Working on file ' + file)
# reading file into variable
some_str = f.read()
# opening/creating file in the format CPinfo_Name_Rulebases_5_0.fws
rb_file = open('C:/python/rulebases_5_0//{0}'.format
(file + '_rulebases_5_0.fws'), 'w')
# filling variable with rulebases() function return variable
rulebases_final = rulebases(some_str)
# changing to the string and writing final result variable into file
rb_file.write(''.join(rulebases_final))
# closing rulebasesfile
rb_file.close()
# closing cpinfo file
f.close()
# printing info to the IDLE
print(file + ' analyzing finished\n\n'
'---------------------------------------------------')
# checking script time and printing to the IDLE
print("--- Script time = %s seconds ---" % (time.time() - start_time))