Inex/inex.py

71 lines
2.3 KiB
Python
Raw Normal View History

import pandas
import pyodbc
import os
2024-07-11 16:13:45 -05:00
import logging
import datetime
from tomllib import load
2024-07-11 16:13:45 -05:00
from inexLogging import inexLog
import inexConnect
2024-07-15 22:32:42 -05:00
from inexDataModel import dataTemplate
from inexDataProcessing import processData
import json
import decimal
class Inex:
def __init__(self):
"""Initilize config, calls functions from inex-connect.py and inex-logging.py"""
if os.path.exists('./config.toml'):
config_file_path = './config.toml'
with open(config_file_path, 'rb') as c:
self.config = load(c)
# assign libraries
self.pd = pandas
self.db = pyodbc
self.tm = datetime
2024-07-11 16:13:45 -05:00
self.il = logging
self.ic = inexConnect
# set 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"]
2024-06-24 17:46:00 -05:00
self.outputFile = self.config["output"]["filename"]
2024-07-11 16:13:45 -05:00
self.useLog = self.config["logging"]["useLog"]
self.logPath = self.config["logging"]["logPath"]
self.logLevel = self.config["logging"]["logLevel"]
#Setup logging
inexLog(self)
# create the connection to the database
2024-07-11 16:13:45 -05:00
self.cursor = self.ic.connectDatabase(self, self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword)
2024-07-11 16:13:45 -05:00
self.data = self.ic.databaseQuery(self, self.cursor, self.dbQuery)
2024-07-15 22:32:42 -05:00
# print(f"returned data: {self.data}")
self.modifiedData = processData(self.data, dataTemplate)
2024-06-24 17:46:00 -05:00
2024-07-15 22:32:42 -05:00
print(self.modifiedData)
2024-06-24 17:46:00 -05:00
2024-07-11 16:13:45 -05:00
# TODO: move this to its own function
if self.useLog:
self.il.warning(f"Writing to '{self.outputFile}'.")
with open(self.outputFile, "w") as f:
2024-07-15 22:32:42 -05:00
json.dump(self.modifiedData, f, indent = 2, cls=Encoder)
2024-07-11 16:13:45 -05:00
# TODO: Move this class to it's own file
class Encoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return int(o)
if isinstance(o, datetime.datetime):
return str(o)
return super().default(o)
# Run
if __name__== "__main__":
Inex()