-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclampeid.py
94 lines (82 loc) · 2.02 KB
/
clampeid.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
#!/usr/bin/env python
#
# vim: tabstop=4 shiftwidth=4 noexpandtab foldmethod=indent
#
class DataException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class SigParser:
def __init__(self, ndb, lineNumber):
self.sigLineNumber = lineNumber
self.ndb = ndb
self.title = None
self.signature = None
self.ep = None
def parse(self, line):
if len(line) < 5:
return
if line[0] == '[':
self.title = line[1:-1].replace(':', ';')
elif line[0:9] == 'signature':
if line [0:12] == 'signature = ':
self.signature = line[12:]
else:
raise DataException("incorrect data")
elif line[0:9] == "ep_only =":
if line == "ep_only = true":
self.ep = True
elif line == "ep_only = false":
self.ep = False
else:
raise DataException("ep_only has bad format")
def verify(self, sig):
chain = 0
maxChain = 0
for elem in sig.split(' '):
if elem == '??':
maxChain = max(chain, maxChain)
chain = 0
else:
chain += 1
if (chain > 1) or (maxChain > 1):
return True
return False
def flush(self):
if not self.verify(self.signature):
print " [-] warning, dropping signature ", self.title
return
if self.ep == True:
self.ndb.write('(ep) '+self.title+':1:EP+0:')
else:
self.ndb.write('(gl) '+self.title+':1:*:')
self.ndb.write(self.signature.replace(' ', ''))
self.ndb.write('\n')
class DbParser:
def __init__(self, filename, ndbName):
self.fd = open(filename, "r")
self.ndb = open(ndbName, "w")
def __del__(self):
self.fd.close()
self.ndb.close()
def parse(self):
lineNo = 0
sigLine = 1
sp = None
while True:
line = self.fd.readline()
if not line:
break
line = line.strip()
lineNo += 1
if len(line) and line[0] == '[':
if sp:
sp.flush()
sp = SigParser(self.ndb, lineNo)
if sp:
sp.parse(line)
if sp:
sp.flush()
p = DbParser("userdb.txt", "clampeid.ndb")
p.parse()