forked from MozillaSecurity/peach
/
randomdict.py
49 lines (42 loc) · 1.62 KB
/
randomdict.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
import io
from Peach import generator, group
from Peach.generator import *
from Peach.Generators.dictionary import Dictionary
class RandomDict(Dictionary):
_values = None
def __init__(self, group, fileName):
"""
@type group: Group
@param group: Group this Generator belongs to
@type fileName: string
@param fileName: Name of file use
"""
Dictionary.__init__(self, group, fileName)
self._fileName = fileName
self.setGroup(group)
def setRand(self, rand):
self._rand = rand
def next(self):
if self._values is None:
try:
self._fd = io.open(self._fileName, 'r', encoding='utf-8')
if self._fd is None:
raise Exception('Unable to open', self._fileName)
self._values = [i.encode('utf-8').rstrip('\r\n') for i in self._fd.readlines()]
except Exception as e:
raise
oldValue = self._currentValue
self._currentValue = self._rand.choice(self._values)
return self._currentValue
def getRawValue(self):
if self._values is None:
try:
self._fd = io.open(self._fileName, 'r', encoding='utf-8')
if self._fd is None:
raise Exception('Unable to open', self._fileName)
self._values = [i.encode('utf-8') for i in self._fd.readlines()]
except Exception as e:
raise
oldValue = self._currentValue
self._currentValue = self._rand.choice(self._values)
return self._currentValue