-
Notifications
You must be signed in to change notification settings - Fork 11
/
core.py
156 lines (135 loc) · 5.49 KB
/
core.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import sys
import datetime
import json
import urllib.parse
import logging
from threading import Thread
from requests.utils import requote_uri
from modules.cdGetLowest import getLowestSources
from collections import OrderedDict
class ModuleManager():
"""docstring for ModuleManager"""
def __init__(self,):
self.available = []
self.modules = []
self.entryPoints = {}
for root, dirs, files in os.walk("modules"):
for f in files:
# get python codes
if f.endswith(".py"):
# ignore __init__.py and remove extention
if f != "__init__.py":
modName = os.path.splitext(f)[0]
self.modules.append(modName)
# just look for files, do not dig into sub folders
break
def loadModule(self, sysconfig, args, **kwargs):
utilityLst = sysconfig['SystemUtility']
fromlist = []
excludeList = getattr(args, 'e', None)
includeList = getattr(args, 'm', None)
op_all = getattr(args, 'all', True)
for m in self.modules:
# skip utility script (module will import them automatically)
if m not in utilityLst:
if (excludeList is not None and m not in excludeList) or \
(includeList is not None and m in includeList) or \
op_all or \
((excludeList is None) and (includeList is None)):
fromlist.append(m)
api = __import__('modules', fromlist=fromlist)
# load selected modules
for mod in fromlist:
# remove 'cdGet' prefix
funcName = "get" + mod[5:]
modName = mod
self.entryPoints[modName] = {}
currentMod = getattr(api, mod)
try:
if hasattr(currentMod, "entry"):
funcName = getattr(currentMod, 'entry')
self.entryPoints[modName]["getFunc"] = getattr(
currentMod, funcName)
except Exception as e:
kwargs['logger'].error("ModuleManager: ", e)
sys.exit(1)
if hasattr(currentMod, "moduleTag"):
self.entryPoints[modName]["displayName"] = getattr(
currentMod, 'moduleTag')
else:
self.entryPoints[modName]["displayName"] = mod[5:]
def call(self, moduleName, **kwargs):
if moduleName in self.entryPoints:
url = kwargs['url']
outputArray = kwargs['outputArray']
indexOfOutputArray = kwargs['indexOfOutputArray']
self.entryPoints[moduleName]['getFunc'](
url, outputArray,
indexOfOutputArray,
kwargs['verbose'],
displayArray=kwargs['displayArray'])
else:
kwargs['logger'].error(
'ModuleManager: Error : No such module: %s' % moduleName)
def run(self, args, **kwargs):
if args.local_uri:
url = args.local_uri
else:
url = kwargs['url']
# handle character accents
url = requote_uri(url)
timeout = args.timeout
threads = []
resultDict = kwargs['resultDict']
outputArray = [''] * len(self.entryPoints)
displayArray = [''] * len(self.entryPoints)
now0 = datetime.datetime.now()
parsedUrl = urllib.parse.urlparse(url)
if(len(parsedUrl.scheme) < 1):
url = 'http://' + url
index = 0
modNames = []
for mod in self.entryPoints:
modNames.append(self.entryPoints[mod]["displayName"])
# note that the comma next to mod cannot be left out
newThread = Thread(target=self.call, args=(mod,),
kwargs={'url': url,
'outputArray': outputArray,
'indexOfOutputArray': index,
'verbose': args.verbose,
'displayArray': displayArray})
threads.append(newThread)
index += 1
for t in threads:
t.start()
for t in threads:
t.join(timeout)
# json dictionary to print
resultDict["uri"] = url
resultDict["estimated-creation-date"] = ""
resultDict["earliest-sources"] = []
resultDict["sources"] = {}
for i in range(len(modNames)):
'''
if a module returns a dictionary with sources update the main
dictionary with the sources passed, otherwise assume the module
returns a single string for the earliest date
'''
if type(displayArray[i]) is dict:
resultDict["sources"].update(displayArray[i])
else:
resultDict["sources"][modNames[i]] = {
"earliest": displayArray[i]}
earliest_date, earliest_sources = getLowestSources(
resultDict["sources"])
resultDict["earliest-sources"] = earliest_sources
resultDict["estimated-creation-date"] = earliest_date
values = OrderedDict(resultDict)
r = json.dumps(values, sort_keys=False,
indent=2, separators=(',', ': '))
now1 = datetime.datetime.now() - now0
logging.debug("Runtime in seconds: " + str(now1.seconds))
# end result
kwargs['logger'].log(35, r)
return resultDict