Advertisement
Guest User

Untitled

a guest
May 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3.  
  4. import time
  5. import os
  6. import sys
  7. import pandas as pd
  8. import inspect
  9. import MailUtil
  10. import ConfigUtil
  11.  
  12. start = time.time()
  13. mail_notif = True
  14.  
  15. def lineno():
  16. """Returns the current line number in our program."""
  17. return inspect.currentframe().f_back.f_lineno
  18. #################################################
  19. # Folder File Extract #
  20. #################################################
  21. cwd = os.path.dirname(os.path.abspath(__file__))
  22. config_file = os.path.join(cwd, "Config/Config.xml")
  23. appconfig = ConfigUtil.configread(config_file, 'appsettings')
  24.  
  25. try:
  26. fold_path = sys.argv[1]
  27. print(fold_path)
  28. if fold_path is None or not os.path.exists(fold_path):
  29. raise Exception
  30. except Exception as e:
  31. print('invalid argument {}'.format(fold_path),e, lineno())
  32. mail_notif = False
  33. sys.exit()
  34.  
  35. try:
  36. fileToSave = appconfig['FILE_TO_BE_SAVED']
  37. a = open(fileToSave, 'w+')
  38. for path, subdirs, files in os.walk(fold_path):
  39. for fileName in files:
  40. # print(fileName)
  41. if fileName.endswith('.txt'):
  42. # print (fileName)
  43. a.write(fileName + '\n')
  44. a.close()
  45.  
  46. except(NameError, ValueError, AttributeError, IOError, IndexError, KeyError) as e:
  47. print ('Wrong File!. Please Try Again ', lineno())
  48. mail_notif = False
  49. try:
  50. exl_path = sys.argv[2]
  51. if exl_path is None:
  52. raise Exception
  53. except Exception as e:
  54. print ("Excel Argument not passed, Need File ", lineno())
  55. mail_notif = False
  56. sys.exit()
  57.  
  58. try:
  59. excel_save_text = appconfig['EXL_FILE']
  60. data = pd.read_excel(exl_path)
  61. df1 = data.iloc[int(appconfig['row']):int(appconfig['row1']), int(appconfig['col']):int(appconfig['col1'])]
  62. for key, value in df1.iteritems():
  63. print(" ".format(key, value))
  64. df1.to_csv(excel_save_text, mode='w+', sep='\n', index=None, header=None)
  65.  
  66. except(NameError, ValueError, AttributeError, IOError, IndexError, KeyError) as e:
  67. print(e, "Wrong definition file. Please Provide the correct File".format(lineno()))
  68. mail_notif = False
  69.  
  70. # Comparison Operation to compare 2 set of files
  71. compare_file = appconfig['COMPARE_FILE']
  72. with open(excel_save_text, 'r') as masterData:
  73. with open(fileToSave, 'r') as userData:
  74. with open(compare_file, 'w') as Newdata:
  75. usedFile = [x.strip('\n') for x in list(userData)]
  76. usedFile.sort()
  77. masterFile = [x.strip('\n') for x in list(masterData)]
  78. masterFile.sort()
  79. for line in usedFile:
  80. if line not in masterFile:
  81. Newdata.write(line + '\n')
  82.  
  83. if mail_notif:
  84. subject = appconfig['subject']
  85. mail_dl = appconfig['mail_dl']
  86. sender = appconfig['sender']
  87. body = appconfig['body']
  88. smtp_server = appconfig['SMTP_MAIL_SRVR']
  89. port_no = appconfig['SMTP_PORT']
  90. MailUtil.mail_notification(subject, body, sender, mail_dl, smtp_server, port_no,appconfig, attachment_file=compare_file)
  91.  
  92. end = time.time()
  93. print("Time taken to process".format(end-start))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement