Advertisement
Guest User

Untitled

a guest
Jan 12th, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import time
  2.     import os
  3.     import sys
  4.     import argparse
  5.     import psutil
  6.     from pywinauto.application import Application
  7.     from pywinauto import timings
  8.    
  9.    
  10.     def type_keys(string, element):
  11.         """Type a string char by char to Element window"""
  12.         for char in string:
  13.             element.type_keys(char)
  14.    
  15.     def main():  
  16.         # Parse arguments from cmd
  17.         parser = argparse.ArgumentParser()
  18.         parser.add_argument("workbook", help = "Path to .pbix file")
  19.         parser.add_argument("--workspace", help = "name of online Power BI service work space to publish in", default = "My workspace")
  20.         parser.add_argument("--refresh-timeout", help = "refresh timeout", default = 30000, type = int)
  21.         parser.add_argument("--no-publish", dest='publish', help="don't publish, just save", default = True, action = 'store_false' )
  22.         parser.add_argument("--init-wait", help = "initial wait time on startup", default = 15, type = int)
  23.         args = parser.parse_args()
  24.    
  25.         timings.after_clickinput_wait = 1
  26.         WORKBOOK = args.workbook
  27.         WORKSPACE = args.workspace
  28.         INIT_WAIT = args.init_wait
  29.         INIT_WAIT = 60
  30.         REFRESH_TIMEOUT = args.refresh_timeout
  31.    
  32.         # Kill running PBI
  33.         PROCNAME = "PBIDesktop.exe"
  34.         for proc in psutil.process_iter():
  35.             # check whether the process name matches
  36.             if proc.name() == PROCNAME:
  37.                 proc.kill()
  38.         time.sleep(3)
  39.    
  40.         # Start PBI and open the workbook
  41.         print("Starting Power BI")
  42.         os.system('start "" "' + WORKBOOK + '"')
  43.         print("Waiting ",INIT_WAIT,"sec")
  44.         time.sleep(INIT_WAIT)
  45.    
  46.         # Connect pywinauto
  47.         print("Identifying Power BI window")
  48.         app = Application(backend = 'uia').connect(path = PROCNAME)
  49.         win = app.window(title_re = '.*Power BI Desktop')
  50.         time.sleep(5)
  51.         win.wait("enabled", timeout = 300)
  52.         win.Save.wait("enabled", timeout = 300)
  53.         win.set_focus()
  54.         win.Home.click_input()
  55.         win.Save.wait("enabled", timeout = 300)
  56.         win.wait("enabled", timeout = 300)
  57.    
  58.         # Refresh
  59.         print("Refreshing")
  60.         win.Refresh.click_input()
  61.         #wait_win_ready(win)
  62.         time.sleep(5)
  63.         print("Waiting for refresh end (timeout in ", REFRESH_TIMEOUT,"sec)")
  64.         win.wait("enabled", timeout = REFRESH_TIMEOUT)
  65.    
  66.         # Save
  67.         print("Saving")
  68.         type_keys("%1", win)
  69.         #wait_win_ready(win)
  70.         time.sleep(5)
  71.         win.wait("enabled", timeout = REFRESH_TIMEOUT)
  72.    
  73.         # Publish
  74.         if args.publish:
  75.             print("Publish")
  76.             win.Publish.click_input()
  77.             publish_dialog = win.child_window(auto_id = "KoPublishToGroupDialog")
  78.             publish_dialog.child_window(title = WORKSPACE).click_input()
  79.             publish_dialog.Select.click()
  80.             try:
  81.                 win.Replace.wait('visible', timeout = 10)
  82.             except Exception:
  83.                 pass
  84.             if win.Replace.exists():
  85.                 win.Replace.click_input()
  86.             win["Got it"].wait('visible', timeout = REFRESH_TIMEOUT)
  87.             win["Got it"].click_input()
  88.    
  89.         #Close
  90.         print("Exiting")
  91.         win.close()
  92.    
  93.         # Force close
  94.         for proc in psutil.process_iter():
  95.             if proc.name() == PROCNAME:
  96.                 proc.kill()
  97.    
  98.            
  99.     if __name__ == '__main__':
  100.         try:
  101.             main()
  102.         except Exception as e:
  103.             print(e)
  104.             sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement