97 lines
2.9 KiB
Python
Executable File
97 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import configparser
|
|
import datetime
|
|
import fnmatch
|
|
import os
|
|
import re
|
|
import sqlite3
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
|
|
c = configparser.ConfigParser()
|
|
c.read("config.ini")
|
|
|
|
today_str = "{:%Y%m%d}".format(datetime.datetime.now())
|
|
MOS_LOG_PATH = c.get("MOS", "XML_LOG_PATH")
|
|
|
|
# TODO: Store and read from database or ini!
|
|
LAST_PARSED_FILE = "MOSMessage-20170221-120000.xml"
|
|
|
|
# Scan available MOSMessage logs and collect those we still need to scan
|
|
files_to_parse = []
|
|
for file in os.listdir(MOS_LOG_PATH):
|
|
if fnmatch.fnmatch(file, "MOSMessage-*.xml"):
|
|
# Only use less-than to re-read the file we had last (might have add. data)
|
|
if file < LAST_PARSED_FILE:
|
|
continue
|
|
files_to_parse.append(MOS_LOG_PATH + file)
|
|
|
|
num_files = len(files_to_parse)
|
|
print("Found {} files to be parsed.".format(num_files))
|
|
|
|
#sys.exit(0)
|
|
|
|
def get_text_or_default(parent_node, node_names, default_value):
|
|
if not isinstance(node_names, list):
|
|
node_names = [node_names]
|
|
node = parent_node
|
|
for n in node_names:
|
|
node = node.find(n)
|
|
if node is None:
|
|
return default_value
|
|
return node.text
|
|
|
|
def parse_mos_xml(mosElem):
|
|
mosId = mosElem.find("mosID").text
|
|
mosObj = mosElem.find("mosObj")
|
|
objId = mosObj.find("objID").text
|
|
status = mosObj.find("status").text
|
|
|
|
if status == "DELETED":
|
|
return False
|
|
|
|
objData = {
|
|
"mosId": mosId,
|
|
"objId": objId,
|
|
"status": status,
|
|
"objSlug": get_text_or_default(mosObj, "objSlug", ""),
|
|
"objPath": get_text_or_default(mosObj, ["objPaths", "objPath"], ""),
|
|
"objGroup": get_text_or_default(mosObj, "objGroup", ""),
|
|
"objAir": get_text_or_default(mosObj, "objAir", ""),
|
|
"created": get_text_or_default(mosObj, "created", ""),
|
|
"createdBy": get_text_or_default(mosObj, "createdBy", ""),
|
|
"changed": get_text_or_default(mosObj, "changed", ""),
|
|
"changedBy": get_text_or_default(mosObj, "changedBy", ""),
|
|
"planningId": get_text_or_default(mosObj, ["mosExternalMetadata", "mosPayload", "planningID"], "")
|
|
}
|
|
#print(repr(objData))
|
|
return objData
|
|
|
|
file_count = 0
|
|
last_file = ""
|
|
for file in files_to_parse:
|
|
file_count += 1
|
|
last_file = os.path.basename(file)
|
|
tree = ET.parse(file)
|
|
print("\n{} [{:d}/{:d}]: ".format(file, file_count, num_files), end="")
|
|
|
|
root = tree.getroot()
|
|
# Walk all <mosNotify...> objects
|
|
for elem in root:
|
|
# Skip everything which is not a mosObj message
|
|
if elem.attrib.get("command") != "mosObj":
|
|
continue
|
|
|
|
mos = elem.find("mos")
|
|
objData = parse_mos_xml(mos)
|
|
if objData:
|
|
# TODO: Write objData to database
|
|
print(".", end="")
|
|
else:
|
|
print("x", end="")
|
|
|
|
print("")
|
|
print("Last file: {}".format(last_file))
|