forked from dobin/ffw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
71 lines (53 loc) · 1.74 KB
/
utils.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
#!/usr/bin/env python2
import pickle
import os
import logging
import sys
import glob
"""
Several utility functions.
Mostly related to reading pickle files.
Shared by different phases of the framework.
"""
def readPickleFile(fileName):
data = None
with open(fileName, 'rb') as f:
data = pickle.load(f)
return data
def fixMsgs(messages):
n = 0
for msg in messages:
msg["index"] = n
n += 1
def loadInputs(config):
inputs = []
inputFiles = glob.glob(os.path.join(config["inputs"], '*'))
for inputFile in inputFiles:
try:
with open(inputFile, 'rb') as f:
data = pickle.load(f)
fixMsgs(data)
inputs.append(data)
except:
#logging.error("E: " + str(e))
pass
print("Loaded " + str(len(inputs)) + " inputs")
return inputs
def setupSlaveLoggingWithFile(threadId):
f = 'ffw-debug-slave-' + str(threadId) + '.log'
fileh = logging.FileHandler(f, 'a')
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
fileh.setFormatter(formatter)
log = logging.getLogger() # root logger
for hdlr in log.handlers[:]: # remove all old handlers
log.removeHandler(hdlr)
log.addHandler(fileh) # set the new handler
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.WARN)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)