Browse Source

Adding logger functionality and conditional wait

sachinmanawadi271 7 years ago
parent
commit
b6d9fb2704
6 changed files with 104 additions and 66 deletions
  1. 16 42
      BussinessLogic.py
  2. 0 9
      FirstPython.iml
  3. 20 8
      app.py
  4. 22 2
      automate.py
  5. 36 5
      conf.py
  6. 10 0
      logger.py

+ 16 - 42
BussinessLogic.py

@@ -1,5 +1,6 @@
 from conf import Config
 from pywinauto.application import Application
+from logger import log
 import time
 import os
 import datetime
@@ -11,68 +12,41 @@ class business_logic:
         _config = Config()
         backslash = "\\"
         app_path = _config.Moved_Path+backslash+file
-        print(app_path)
+        #print(app_path)
         os.startfile(app_path)
         time.sleep(50)
         print("Sleep End!!!")
         app = Application().connect(path=r"C:\Program Files\TechSmith\Camtasia 9\CamtasiaStudio.exe")
+
         #for TRIAL
         if _config.Trial == 'YES':
+            #app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Finish').SetFocus()
             app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Finish').Click()
         #end for TRIAL
-        hwndwrappercamtasiastudioexebbedcaecbaeffaf = app[u'Camtasia 9']
+        child_elements = app[u'Camtasia 9']
         #remark
-        hwndwrappercamtasiastudioexebbedcaecbaeffaf.ClickInput(coords=(_config.Share_Btn_X, _config.Share_Btn_Y))
-        hwndwrappercamtasiastudioexebbedcaecbaeffaf.TypeKeys("{DOWN}")
-        hwndwrappercamtasiastudioexebbedcaecbaeffaf.TypeKeys("{ENTER}")
-        time.sleep(10)
-        print("Sleep End!!!")
+        child_elements.ClickInput(coords=(_config.Share_Btn_X, _config.Share_Btn_Y))
+        child_elements.TypeKeys("{DOWN}")
+        child_elements.TypeKeys("{ENTER}")
+        #time.sleep(10)
+
         #for TRIAL
         if _config.Trial == 'YES':
-            hwndwrappercamtasiastudioexebbedcaecbaeffaf.ClickInput(coords=(_config.Water_Mark_Btn_X, _config.Water_Mark_Btn_Y))
+            child_elements.Wait('visible',timeout=20)
+            child_elements.ClickInput(coords=(_config.Water_Mark_Btn_X, _config.Water_Mark_Btn_Y))
         #end for TRIAL
         for i in range(_config.Dialogs):
         #for no_dialogs in _config.Dialogs:
-            time.sleep(2)
+            #time.sleep(2)
+            child_elements.Wait('visible',timeout=20)
             app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Next').Click()
 
-        #time.sleep(2)
-        #app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Next').Click()
-        #time.sleep(2)
-        #app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Next').Click()
-        #time.sleep(2)
-        #app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Next').Click()
-        #time.sleep(2)
-        #app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Next').Click()
-
         #app.Window_(best_match='Dialog', top_level_only=True).PrintControlIdentifiers()
         app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(title="Untitled Project",class_name="Edit").SetText(time.time())
         app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Finish').Click()
         time.sleep(2)
+        #child_elements.Wait('visible',timeout=20)
         app.kill_()
-        time.sleep(2)
+        time.sleep(10)
         print("Rendering Succcessful")
         #Finish rendering
-
-
-
-
-       # static.SetWindowText("sachin")
-        #print(datetime.datetime.now())
-        #print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
-        #app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Next').Click()
-        #app = Application().connect(path=r"C:\Program Files\TechSmith\Camtasia 9\CamtasiaStudio.exe")
-        #hwndwrappercamtasiastudioexebcfebfcbbefbad  = app[u'Camtasia 9']
-        #hwndwrappercamtasiastudioexebcfebfcbbefbad.Minimize()
-        #app.Window_(title='Camtasia 9').MenuSelect('File')
-        #app.CamtasiaStudio.draw_outline()
-        #app['Camtasia 9'].menu_select("Share")
-        #xyz = app.CamtasiaStudio.Menu_select("Help->About Camtasia")
-        #app.Share.print_control_identifiers()
-        #app.PopupMenu.Menu().get_menu_path("Cancel")[0].click()
-        #app.Window_(best_match='Dialog', top_level_only=True).ChildWindow(best_match='Cancel').Click()
-        #os.startfile(app_path)
-        #autoit.run("E:\sample\TK Praktikum1.trec")
-        #autoit.win_wait_active("[CLASS:Notepad]", 3)
-        #autoit.win_close("[CLASS:Notepad]")
-        #autoit.control_click("[Class:#32770]", "Button2")

+ 0 - 9
FirstPython.iml

@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module type="PYTHON_MODULE" version="4">
-  <component name="NewModuleRootManager" inherit-compiler-output="true">
-    <exclude-output />
-    <content url="file://$MODULE_DIR$" />
-    <orderEntry type="inheritedJdk" />
-    <orderEntry type="sourceFolder" forTests="false" />
-  </component>
-</module>

+ 20 - 8
app.py

@@ -1,21 +1,33 @@
 from automate import Automate
 from BussinessLogic import business_logic
 from conf import Config
+from logger import log
+import sys
+
+#Globals
 Files=[]
 
+log = log()
 Auto = Automate()
 #Initializes from the configuration file
-Auto.Init()
+log.logger.info('Initialization called')
+status = Auto.Init()
+if status == 0:
+    log.logger.error('Initialization failed.Exiting application')
+
 
-Path = Config( )
-#Get the folder paths to copy from src to dest
-#src_folder = Path.get_Original_Path()
-#dest_folder = Path.get_Moved_Path()
-Auto.copyFiles(Path.Original_Path,Path.Moved_Path)
+Path = Config()
+#Copy the files from src path to dest path inorder to create a backup
+status = Auto.copyFiles(Path.Original_Path,Path.Moved_Path)
+
+if status == 0:
+    log.logger.error('Copying files failed. Exiting Application')
+    sys.exit(0)
+else:
+    log.logger.info('File copy successfull')
 
 #Get the files from the dest folder to start the conversion process
 Files=Auto.GetFiles(Path.Moved_Path)
-
+bi = business_logic()
 for file in Files:
-    bi = business_logic()
     bi.convert_files(file)

+ 22 - 2
automate.py

@@ -1,13 +1,19 @@
 from conf import Config
 import os
 import shutil
+from logger import log
 
 
 class Automate:
     #Initialization Class for Automation
+    log = log()
+
     def Init(self):
         __configuration__ = Config()
-        __configuration__.read_config()
+        status = __configuration__.read_config()
+        if status == 0:
+            return 0
+        return 1
 
     def GetFiles(self, paths):
         files = []
@@ -16,8 +22,22 @@ class Automate:
         return files
 
     def copyFiles(self, src_folder,dest_folder):
+        if src_folder == "" or dest_folder == "":
+            log.logger.error('Source or Destination Folder does not exists')
+            return 0
+
+        if not os.listdir(src_folder):
+            log.logger.error('No files found in src folder')
+            return 0
+
         for file in os.listdir(src_folder):
             print ("File being moved -----> " + file)
             full_file_name = os.path.join( src_folder, file )
             if os.path.isfile(full_file_name):
-                shutil.move( full_file_name, dest_folder )
+                try:
+                    shutil.copy( full_file_name, dest_folder )
+                except shutil.Error as e:
+                    log.logger.error('Could not copy the files %s',e)
+                    return 0
+                return 1
+

+ 36 - 5
conf.py

@@ -1,4 +1,5 @@
 import xml.etree.ElementTree as ET
+from logger import log
 
 class Config:
     'Configuration Class for File Paths'
@@ -10,28 +11,57 @@ class Config:
     Share_Btn_Y = 0
     Water_Mark_Btn_X = 0
     Water_Mark_Btn_Y = 0
-
-    #def __init__(self):
-    #    self.Original_Path = ''
-    #    self.Moved_Path = ''
+    log = log()
 
     def read_config(self):
+        log.logger.info('Reading configuration file')
         tree = ET.parse('configuration.xml')
         root = tree.getroot()
         #Get the folder path where the files are present
         path = root.find('Path')
-        #PathConfig.filePaths.append(path.get('path1'))
+        if path == "":
+            log.logger.info('Failed to read the file path attribute')
+            return 0
+
         Config.Original_Path = path.get('path1')
+        if Config.Original_Path == "":
+            log.logger.info('Failed to read the file path')
+            return 0
+
         #Get the folder path where files are to be moved
         move_path = root.find('MovePath')
+        if move_path == "":
+            log.logger.info('Failed to read the copy file path attribute')
+            return 0
+
         Config.Moved_Path = move_path.get('path2')
+        if Config.Moved_Path == "":
+            log.logger.info('Failed to read the copy file path')
+            return 0
+
         #Get the no. of dialogs screens during coversion
         no_screens = root.find('No_Screens')
+        if no_screens == "":
+            log.logger.error('Failed to read the no. of dialog box attribute')
+            return 0
+
         Config.Dialogs = no_screens.get('dialog')
+        if Config.Dialogs == "":
+            log.logger.error('Failed to read the no. of dialog boxes')
+            return 0
+
         Config.Dialogs = int(Config.Dialogs)
+
         #Get if Camtasia has Trial license
         trial = root.find('TRIAL')
+        if trial == "":
+            log.logger.error('Failed to read the trial license attribute')
+            return 0
         Config.Trial = trial.get('trial')
+        if Config.Trial == "":
+            log.logger.error('Failed to read the trial license value')
+            return 0
+
         #Get the co-ordinates of the share button
         share_btn = root.find('Share_BTN_COOR')
         Config.Share_Btn_X = share_btn.get('x')
@@ -44,6 +74,7 @@ class Config:
         Config.Water_Mark_Btn_X = int(Config.Water_Mark_Btn_X)
         Config.Water_Mark_Btn_Y = watermark_btn.get('y')
         Config.Water_Mark_Btn_Y = int(Config.Water_Mark_Btn_Y)
+        return 1
 
 
 

+ 10 - 0
logger.py

@@ -0,0 +1,10 @@
+import logging
+
+class log:
+    #Initialize logger
+    logger = logging.getLogger('app')
+    hdlr = logging.FileHandler('App.log',mode='w')
+    formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
+    hdlr.setFormatter(formatter)
+    logger.addHandler(hdlr)
+    logger.setLevel(logging.DEBUG)