-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSearchBaseAttrByMultiProcess.py
More file actions
86 lines (76 loc) · 3.17 KB
/
SearchBaseAttrByMultiProcess.py
File metadata and controls
86 lines (76 loc) · 3.17 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Author: AsherYang
Email : ouyangfan1991@gmail.com
Date : 2018/12/8
Desc : 利用多进程在文件中搜索关键字
本类是传递 line 数据出去,但是发现会出现 QWinEventNotifier cannot have more than 62 enabled at one time 的问题
故,先拆分为 SearchKeywordByMultiProcess.py 和 SearchBaseAttrByMultiProcess.py 分别处理完line 再进行回调结果
"""
import os
import re
import json
from MultiProcessFile import MultiProcessFile
from EncodeUtil import _translateUtf8
from analyticslog.BaseAttrBean import BaseAttrBean
def process_wrapper(fname, chunkStart, chunkSize, keyword="base attribute info"):
# print '----- process_wrapper PID:%s , chunkSize:%d ' % (os.getpid(), chunkSize)
with open(fname) as f:
f.seek(chunkStart)
lines = f.read(chunkSize).splitlines()
baseAttrBeanList = []
for line in lines:
# print '---> line: %s, keyword: %s ' % (line, keyword)
textLine = str(_translateUtf8(line))
# print '--> textLine: ', textLine
textLineLower = textLine.lower()
keywordIndex = textLineLower.find(str(_translateUtf8(keyword)).lower())
if keywordIndex != -1:
baseAttrJson = filterBaseAttr2Json(textLine)
# print '==> baseAttrJsonDict: ', baseAttrJson
if baseAttrJson:
baseAttr = BaseAttrBean()
baseAttr.binderNumber = baseAttrJson['mId']
baseAttr.machineMode = baseAttrJson['devName']
baseAttr.osVersion = baseAttrJson['osVer']
# print 'baseAttr: ', baseAttr
baseAttrBeanList.append(baseAttr)
return baseAttrBeanList
# 找出有效的基础信息,进行json 转换
def filterBaseAttr2Json(searchedBaseAttr):
if not searchedBaseAttr:
return None
baseAttrJson = None
baseAttrList = re.findall(r'BaseAttr(\{.+})', searchedBaseAttr)
# print '---> baseAttrList: ', baseAttrList
if baseAttrList:
for baseAttrStr in baseAttrList:
baseAttrJson = convertStr2JsonStr(baseAttrStr)
# print '==> baseAttrJson: ', baseAttrJson
if baseAttrJson:
return baseAttrJson
return baseAttrJson
# 将字符串转换为json 格式
# http://www.runoob.com/python/python-reg-expressions.html
def convertStr2JsonStr(string):
if not string:
return None
string = string.replace("\'", "\"")
strList = re.findall(r'([A-Za-z0-9]+=)', string)
for strTmp in strList:
strTmp = strTmp.replace("=", "")
string = string.replace(strTmp, "\"" + strTmp + "\"")
string = string.replace("=", ":")
# print '---> string: ', string
# print '---> strList: ', strList
jsonStr = None
try:
jsonStr = json.loads(string)
except Exception as e:
pass
# print "json >>> ", jsonStr
return jsonStr
class SearchBaseAttrByMultiProcess(MultiProcessFile):
def __init__(self, fname, cls_instance, status_call_back, keyword="base attribute info"):
MultiProcessFile.__init__(self, fname, cls_instance, status_call_back, process_wrapper, keyword)