Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import configparser
- import servicemanager
- import socket
- import win32event
- import win32service
- import win32serviceutil
- from datetime import time
- from script.sql import *
- #checking directory for existing excel files
- def fl_chk(filepath):
- return os.path.isfile(filepath)
- #script as service
- class AppServerSvc (win32serviceutil.ServiceFramework):
- _svc_name_ = "PyXLSQL"
- _svc_display_name_ = "Python Excel to SQL"
- def __init__(self,args):
- win32serviceutil.ServiceFramework.__init__(self,args)
- self.hWaitStop = win32event.CreateEvent(None,0,0,None)
- #socket.setdefaulttimeout(60)
- def SvcStop(self):
- self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
- win32event.SetEvent(self.hWaitStop)
- self.run = False
- def SvcDoRun(self):
- servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
- servicemanager.PYS_SERVICE_STARTED,
- (self._svc_name_,''))
- self.run = True
- self.main()
- def main (self):
- #reading .ini file
- default_file = "config.ini"
- config = configparser.ConfigParser()
- config.read(default_file)
- path = config.get("excel", "path_default")
- file_1 = path+config.get("excel", "workbook1_default")
- file_2 = path+config.get("excel", "workbook2_default")
- file_3 = path+config.get("excel", "workbook3_default")
- usr = config.get("SQL", "user")
- pwd = config.get("SQL", "password")
- hst = config.get("SQL", "host")
- db = config.get("SQL", "database")
- pt = config.get("SQL", "port")
- ssl_cert = config.get("SQL","ssl_cert")
- ssl_ca = config.get("SQL","ssl_ca")
- ssl_key = config.get("SQL","ssl_key")
- enc = config.get("SQL","encoding")
- sql_config="mysql+pymysql://"+usr+":"+pwd+"@"+hst+":"+pt+"/"+db+"?"+ssl_cert+"&"+ssl_ca+"&"+ssl_key+"&"+enc
- if (fl_chk(file_1) == True or fl_chk(file_2) == True or fl_chk(file_3) == True):
- SQL.sqlconnect(sql_config)
- while fl_chk(file_1) == True:
- XLS.xls_wrk(file_1)
- df_xls_1 = XLS.table_xls(file_1)
- df_sql_1 = SQL.table_sql(file_1,sql_config)
- if (df_sql_1.equals(df_xls_1) == False):
- SQL.sql_import(file_1,sql_config)
- XLS.remove_temp(file_1)
- XLS.remove_orig(file_1)
- while fl_chk(file_2) == True:
- XLS.xls_wrk(file_2)
- df_xls_2 = XLS.table_xls(file_2)
- df_sql_2 = SQL.table_sql(file_2,sql_config)
- if (df_sql_2.equals(df_xls_2) == False):
- SQL.sql_import(file_2,sql_config)
- XLS.remove_temp(file_2)
- XLS.remove_orig(file_2)
- while fl_chk(file_3) == True:
- XLS.xls_wrk(file_3)
- df_xls_3 = XLS.table_xls(file_3)
- df_sql_3 = SQL.table_sql(file_3,sql_config)
- if (df_sql_3.equals(df_xls_3) == False):
- SQL.sql_import(file_3,sql_config)
- XLS.remove_temp(file_3)
- XLS.remove_orig(file_3)
- SQL.con_close(sql_config)
- else:
- XLS.remove_orig(file_1)
- XLS.remove_orig(file_2)
- XLS.remove_orig(file_3)
- with open(os.path.dirname(sys.argv[0])+"\\logs\\XLS_files.log", "a+") as text_file:
- print(f"{str(datetime.datetime.now()).split('.')[0]} - Excel workbooks not found in dir {path}",
- file=text_file)
- if __name__ == '__main__':
- if len(sys.argv) == 1:
- servicemanager.Initialize()
- servicemanager.PrepareToHostSingle(AppServerSvc)
- servicemanager.StartServiceCtrlDispatcher()
- else:
- win32serviceutil.HandleCommandLine(AppServerSvc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement