2024-06-24 16:28:51 -05:00
|
|
|
import pyodbc
|
|
|
|
import os
|
2024-07-11 16:13:45 -05:00
|
|
|
import logging
|
2024-07-29 14:04:35 -05:00
|
|
|
import tomllib
|
2024-07-11 16:13:45 -05:00
|
|
|
from inexLogging import inexLog
|
2024-06-24 16:28:51 -05:00
|
|
|
import inexConnect
|
2024-07-15 22:32:42 -05:00
|
|
|
from inexDataModel import dataTemplate
|
|
|
|
from inexDataProcessing import processData
|
2024-06-25 13:40:52 -05:00
|
|
|
import json
|
2024-07-25 22:22:08 -05:00
|
|
|
import requests
|
2024-07-29 17:30:11 -05:00
|
|
|
import inexEncoder
|
2024-08-01 11:02:37 -05:00
|
|
|
import inexSqlquery
|
2024-08-10 02:20:26 -05:00
|
|
|
|
2024-06-24 16:28:51 -05:00
|
|
|
class Inex:
|
|
|
|
def __init__(self):
|
|
|
|
"""Initilize config, calls functions from inex-connect.py and inex-logging.py"""
|
|
|
|
# assign libraries
|
|
|
|
self.db = pyodbc
|
2024-07-11 16:13:45 -05:00
|
|
|
self.il = logging
|
2024-06-24 16:28:51 -05:00
|
|
|
self.ic = inexConnect
|
2024-07-25 22:22:08 -05:00
|
|
|
self.r = requests
|
2024-07-29 14:04:35 -05:00
|
|
|
self.tl = tomllib
|
|
|
|
self.os = os
|
|
|
|
self.j = json
|
2024-07-29 17:30:11 -05:00
|
|
|
self.e = inexEncoder.Encoder
|
2024-08-01 11:02:37 -05:00
|
|
|
self.sq = inexSqlquery
|
2024-06-24 16:28:51 -05:00
|
|
|
|
2024-07-29 14:04:35 -05:00
|
|
|
if self.os.path.exists('./config.toml'):
|
|
|
|
config_file_path = './config.toml'
|
|
|
|
with open(config_file_path, 'rb') as c:
|
|
|
|
self.config = self.tl.load(c)
|
|
|
|
|
2024-06-24 16:28:51 -05:00
|
|
|
# set config
|
2024-08-10 02:20:26 -05:00
|
|
|
try:
|
|
|
|
if self.config:
|
|
|
|
self.dbDriver = self.config["database"]["driver"]
|
|
|
|
self.dbServer = self.config["database"]["server"]
|
|
|
|
self.dbDatabase = self.config["database"]["database"]
|
|
|
|
self.dbUser = self.config["database"]["user"]
|
|
|
|
self.dbPassword = self.config["database"]["password"]
|
|
|
|
self.dbQuery = self.config["database"]["query"]
|
|
|
|
self.outputFile = self.config["output"]["filename"]
|
|
|
|
self.useLog = self.config["logging"]["useLog"]
|
|
|
|
self.logPath = self.config["logging"]["logPath"]
|
|
|
|
self.logLevel = self.config["logging"]["logLevel"]
|
|
|
|
self.prdInstanceID = self.config["immutables"]["prd_instance_id"]
|
|
|
|
self.productGUID = self.config["immutables"]["product_guid"]
|
|
|
|
self.productName = self.config["immutables"]["product_name"]
|
|
|
|
self.productVersion = self.config["immutables"]["product_version"]
|
|
|
|
self.tokenFilepath = self.config["output"]["token"]
|
|
|
|
self.selectedPlatform = self.config["fortraPlatform"]["selectedPlatform"]
|
|
|
|
self.writeJsonfile = self.config["output"]["dumpTojson"]
|
|
|
|
self.pushToplatform = self.config["output"]["pushToplatform"]
|
|
|
|
self.queryOverride = self.config["database"]["overrideEmbeddedquery"]
|
2024-08-14 15:00:53 -05:00
|
|
|
self.queryDaystopull = self.config["database"]["daysTopull"]
|
2024-08-10 02:20:26 -05:00
|
|
|
except:
|
|
|
|
print("No config.toml. Please use example file and configure appropriately")
|
|
|
|
exit(1)
|
2024-07-29 14:04:35 -05:00
|
|
|
|
|
|
|
if "dev" in self.selectedPlatform.lower():
|
|
|
|
self.platformConfig = self.config["fortraPlatform"]["dev"]
|
|
|
|
if "stag" in self.selectedPlatform.lower():
|
|
|
|
self.platformConfig = self.config["fortraPlatform"]["stage"]
|
|
|
|
if "prod" in self.selectedPlatform.lower():
|
|
|
|
self.platformConfig = self.config["fortraPlatform"]["prod"]
|
|
|
|
|
2024-07-11 16:13:45 -05:00
|
|
|
#Setup logging
|
|
|
|
inexLog(self)
|
2024-06-24 16:28:51 -05:00
|
|
|
|
|
|
|
# create the connection to the database
|
2024-08-01 11:02:37 -05:00
|
|
|
self.cursor = self.ic.inexSql.connectDatabase(self, self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword)
|
|
|
|
|
2024-06-24 16:28:51 -05:00
|
|
|
|
2024-08-14 15:00:53 -05:00
|
|
|
self.data = self.ic.inexSql.databaseQuery(self, self.cursor, self.sq.sqlQuerymodel.queryData(self.queryOverride,self.dbQuery, self.queryDaystopull))
|
2024-06-24 17:46:00 -05:00
|
|
|
|
2024-07-29 17:30:11 -05:00
|
|
|
self.modifiedData = processData(self.data, dataTemplate, prd_instance_id=self.prdInstanceID,\
|
|
|
|
product_guid=self.productGUID,product_name=self.productName,product_version=self.productVersion)
|
2024-07-29 14:04:35 -05:00
|
|
|
|
2024-07-29 17:30:11 -05:00
|
|
|
if self.pushToplatform:
|
|
|
|
inexConnect.fortraEFC.pushPayload(self)
|
2024-07-11 16:13:45 -05:00
|
|
|
|
2024-07-29 17:30:11 -05:00
|
|
|
# TODO: move this to its own function
|
|
|
|
if self.useLog:
|
|
|
|
self.il.warning(f"Writing to '{self.outputFile}'.")
|
|
|
|
if self.writeJsonfile:
|
|
|
|
with open(self.outputFile, "w") as f:
|
|
|
|
self.j.dump(self.modifiedData, f, indent = 2, cls=self.e)
|
2024-06-24 16:28:51 -05:00
|
|
|
|
|
|
|
# Run
|
|
|
|
if __name__== "__main__":
|
|
|
|
Inex()
|