Advertisement
rs_al

Python Service

Oct 30th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. import configparser
  2. import servicemanager
  3. import socket
  4. import win32event
  5. import win32service
  6. import win32serviceutil
  7. from datetime import time
  8.  
  9.  
  10. from script.sql import *
  11.  
  12.  
  13. #checking directory for existing excel files
  14. def fl_chk(filepath):
  15.     return os.path.isfile(filepath)
  16.  
  17. #script as service
  18.  
  19.  
  20. class AppServerSvc (win32serviceutil.ServiceFramework):
  21.     _svc_name_ = "PyXLSQL"
  22.     _svc_display_name_ = "Python Excel to SQL"
  23.  
  24.     def __init__(self,args):
  25.         win32serviceutil.ServiceFramework.__init__(self,args)
  26.         self.hWaitStop = win32event.CreateEvent(None,0,0,None)
  27.         #socket.setdefaulttimeout(60)
  28.  
  29.     def SvcStop(self):
  30.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  31.         win32event.SetEvent(self.hWaitStop)
  32.         self.run = False
  33.  
  34.     def SvcDoRun(self):
  35.  
  36.         servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
  37.                               servicemanager.PYS_SERVICE_STARTED,
  38.                               (self._svc_name_,''))
  39.         self.run = True
  40.         self.main()
  41.  
  42.     def main (self):
  43.  
  44.         #reading .ini file
  45.         default_file = "config.ini"
  46.         config = configparser.ConfigParser()
  47.         config.read(default_file)
  48.         path = config.get("excel", "path_default")
  49.         file_1 = path+config.get("excel", "workbook1_default")
  50.         file_2 = path+config.get("excel", "workbook2_default")
  51.         file_3 = path+config.get("excel", "workbook3_default")
  52.         usr = config.get("SQL", "user")
  53.         pwd = config.get("SQL", "password")
  54.         hst = config.get("SQL", "host")
  55.         db = config.get("SQL", "database")
  56.         pt = config.get("SQL", "port")
  57.         ssl_cert = config.get("SQL","ssl_cert")
  58.         ssl_ca = config.get("SQL","ssl_ca")
  59.         ssl_key = config.get("SQL","ssl_key")
  60.         enc = config.get("SQL","encoding")
  61.         sql_config="mysql+pymysql://"+usr+":"+pwd+"@"+hst+":"+pt+"/"+db+"?"+ssl_cert+"&"+ssl_ca+"&"+ssl_key+"&"+enc
  62.  
  63.  
  64.         if (fl_chk(file_1) == True or fl_chk(file_2) == True or fl_chk(file_3) == True):
  65.  
  66.             SQL.sqlconnect(sql_config)
  67.  
  68.             while fl_chk(file_1) == True:
  69.  
  70.                 XLS.xls_wrk(file_1)
  71.                 df_xls_1 = XLS.table_xls(file_1)
  72.                 df_sql_1 = SQL.table_sql(file_1,sql_config)
  73.                 if (df_sql_1.equals(df_xls_1) == False):
  74.                     SQL.sql_import(file_1,sql_config)
  75.                 XLS.remove_temp(file_1)
  76.                 XLS.remove_orig(file_1)
  77.  
  78.             while fl_chk(file_2) == True:
  79.                 XLS.xls_wrk(file_2)
  80.                 df_xls_2 = XLS.table_xls(file_2)
  81.                 df_sql_2 = SQL.table_sql(file_2,sql_config)
  82.                 if (df_sql_2.equals(df_xls_2) == False):
  83.                     SQL.sql_import(file_2,sql_config)
  84.                 XLS.remove_temp(file_2)
  85.                 XLS.remove_orig(file_2)
  86.  
  87.             while fl_chk(file_3) == True:
  88.                 XLS.xls_wrk(file_3)
  89.                 df_xls_3 = XLS.table_xls(file_3)
  90.                 df_sql_3 = SQL.table_sql(file_3,sql_config)
  91.                 if (df_sql_3.equals(df_xls_3) == False):
  92.                     SQL.sql_import(file_3,sql_config)
  93.                 XLS.remove_temp(file_3)
  94.                 XLS.remove_orig(file_3)
  95.  
  96.             SQL.con_close(sql_config)
  97.  
  98.  
  99.         else:
  100.             XLS.remove_orig(file_1)
  101.             XLS.remove_orig(file_2)
  102.             XLS.remove_orig(file_3)
  103.             with open(os.path.dirname(sys.argv[0])+"\\logs\\XLS_files.log", "a+") as text_file:
  104.                 print(f"{str(datetime.datetime.now()).split('.')[0]} - Excel workbooks not found in dir {path}",
  105.                       file=text_file)
  106.  
  107. if __name__ == '__main__':
  108.    if len(sys.argv) == 1:
  109.         servicemanager.Initialize()
  110.         servicemanager.PrepareToHostSingle(AppServerSvc)
  111.         servicemanager.StartServiceCtrlDispatcher()
  112.    else:
  113.         win32serviceutil.HandleCommandLine(AppServerSvc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement