4 @file SourceTemplateExtractor.py 5 @link: http://hierarchical-cluster-engine.com/ 6 @copyright: Copyright © 2013-2014 IOIX Ukraine 7 @license: http://hierarchical-cluster-engine.com/license/ 26 SOURCE_NAME_FILE =
"file" 27 SOURCE_NAME_HTTP =
"http" 28 POST_BUFF_MACROS = [
"RAW_CONTENT",
"URL"]
45 scheduleStorageData =
None 46 curdatetime = datetime.datetime.now()
47 if "file" in schedule:
48 with open(schedule[
"file"],
"r") as fd: 49 scheduleStorageData = json.loads(fd.read()) 50 if schedule[
"type"] == 0:
51 if additionData[
"parentMD5"] ==
"":
53 elif schedule[
"type"] == 1:
55 elif schedule[
"type"] == 2:
56 if scheduleStorageData
is not None:
57 atTime = datetime.datetime.strptime(schedule[
"at"],
"%Y-%m-%d %H:%M")
59 if "saveAtTime" in scheduleStorageData
and scheduleStorageData[
"saveAtTime"]
is not None:
60 saveAtTime = datetime.datetime.strptime(scheduleStorageData[
"saveAtTime"],
"%Y-%m-%d %H:%M")
61 if saveAtTime != atTime:
62 scheduleStorageData[
"tCount"] = 0
63 scheduleStorageData[
"saveAtTime"] = atTime.strftime(
"%Y-%m-%d %H:%M")
64 if curdatetime > atTime:
65 if scheduleStorageData[
"tCount"] == 0:
67 scheduleStorageData[
"tCount"] += 1
68 elif schedule[
"type"] == 3:
69 if scheduleStorageData
is not None:
70 if "saveNowTime" in scheduleStorageData
and scheduleStorageData[
"saveNowTime"]
is not None:
71 atTime = datetime.datetime.strptime(scheduleStorageData[
"saveNowTime"],
"%Y-%m-%d %H:%M")
73 atTime = datetime.datetime.strptime(schedule[
"at"],
"%Y-%m-%d %H:%M")
75 if curdatetime > (atTime + datetime.timedelta(minutes=int(schedule[
"step"]))):
76 scheduleStorageData[
"saveNowTime"] = curdatetime.strftime(
"%Y-%m-%d %H:%M")
78 if scheduleStorageData
is not None:
79 scheduleStorageData[
"datetime"] = curdatetime.strftime(
"%Y-%m-%d %H:%M")
80 with open(schedule[
"file"],
"w")
as fd:
81 fd.write(json.dumps(scheduleStorageData))
94 if rawContent
is not None:
95 self.
macroDict[
"RAW_CONTENT"] = rawContent
98 templateSourceStruct =
None 100 templateSourceStruct = json.loads(templateSource)
101 except Exception
as excp:
102 logger.debug(
">>> Wrong while json loads from templateSource; err=" + str(excp))
104 if templateSourceStruct
is not None and isinstance(templateSourceStruct, types.ListType):
105 for templateSourceElement
in templateSourceStruct:
108 if "schedule" in templateSourceElement
and templateSourceElement[
"schedule"]
is not None and \
109 self.
scheduleCalc(templateSourceElement[
"schedule"], additionData):
110 if templateSourceElement[
"source"] == SourceTemplateExtractor.SOURCE_NAME_FILE:
111 with open(templateSourceElement[
"request"],
"rb")
as fd:
112 addedElement = json.loads(fd.read())
113 elif templateSourceElement[
"source"] == SourceTemplateExtractor.SOURCE_NAME_HTTP:
116 if addedElement
is not None:
117 if isinstance(addedElement, types.ListType)
and len(addedElement) > 0:
118 ret.append(addedElement[0])
119 elif isinstance(addedElement, types.DictType):
120 ret.append(addedElement)
121 except Exception
as excp:
122 logger.debug(
">>> Something wrong with templateSourceElement procession; err=" + str(excp))
134 contentTypeHeader =
None 135 if "headers" in templateSourceElement:
136 contentTypeHeader = json.loads(templateSourceElement[
"headers"])
137 if templateSourceElement[
"request"].startswith(
"http://")
or \
138 templateSourceElement[
"request"].startswith(
"https://"):
139 requestString = templateSourceElement[
"request"]
142 if requestString
is not None:
143 if templateSourceElement[
"post"]
is None or templateSourceElement[
"post"] ==
"":
144 templateHash = hashlib.md5(requestString).hexdigest()
148 ret = requests.get(requestString, headers=contentTypeHeader)
151 templateHash = hashlib.md5(requestString + templateSourceElement[
"post"]).hexdigest()
156 replacedPost = replacedPost.encode(
"utf-8")
157 logger.debug(
">>> POST Data: requestString:\n" + str(requestString) + \
158 "\ntemplateSourceElement:\n" + str(templateSourceElement[
"post"]) + \
159 "\nreplacedPost:\n" + str(replacedPost) +
"\nheaders:\n" + str(contentTypeHeader))
160 ret = requests.post(requestString, replacedPost, headers=contentTypeHeader)
162 if ret
is not None and ret.status_code == 200
and ret.text
is not None:
163 ret = json.loads(ret.text)
165 logger.debug(
">>> Something wrong with HTTP request, Response code == " + str(ret.status_code) +
166 "content == " + str(ret.text))
178 if post.find(
"%" + elem +
"%") >= 0:
180 ret = post.replace(
"%" + elem +
"%", self.
macroDict[elem])
182 ret = post.replace(
"%" + elem +
"%",
"")