Advertisement
Guest User

Python_lang

a guest
Jul 14th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. """
  2. Module can be used for creation and/or uploading both pixls and piexpert designs in all available languages
  3. """
  4.  
  5. import json
  6. import os
  7. import argparse
  8. import time
  9. import traceback
  10. import Locators
  11.  
  12. from selenium import webdriver
  13. from selenium.webdriver.common.by import By
  14.  
  15. from webfeatures import set_up
  16. from webfeatures import get_elem_or_null
  17. from webfeatures import create_piexpert
  18. from webfeatures import delete_all_designs
  19. from webfeatures import wait_for_loading
  20. from webfeatures import time_string
  21. from webfeatures import login
  22. from webfeatures import click
  23. from webfeatures import hover_element
  24.  
  25.  
  26. __author__ = 'achubarov'
  27.  
  28.  
  29. def download_and_rename(wdriver, directory, tab_name, language, script):
  30. """
  31. Download and rename files, using js-script from design page
  32.  
  33. :param wdriver: webdriver
  34. :param directory: directory to download to
  35. :param tab_name: name of tab to download from
  36. :param language: current piexpert language
  37. :param script: script to execute
  38. """
  39.  
  40. # get current number of files in directory
  41. files_num = len(os.listdir(directory))
  42.  
  43.  
  44.  
  45. # execute script
  46. wdriver.execute_script(script)
  47.  
  48. # wait for downloading starts
  49. t1 = time.time()
  50. while len(os.listdir(directory)) - files_num != 2 and time.time() - t1 < 5.0:
  51. continue
  52.  
  53. # wait for downloading finish
  54. last = '.part'
  55. while ".part" in last:
  56. try:
  57. files = [os.path.join(directory, file) for file in os.listdir(directory)]
  58. files.sort(key=os.path.getmtime, reverse=True)
  59. last = os.path.join(directory, files[0])
  60. time.sleep(0.1)
  61. except FileNotFoundError:
  62. break
  63.  
  64. time.sleep(1)
  65. files = [os.path.join(directory, file) for file in os.listdir(directory)]
  66. files.sort(key=os.path.getmtime, reverse=True)
  67. last = os.path.join(directory, files[0])
  68.  
  69.  
  70.  
  71. # rename downloaded file
  72. #new_name = os.path.join(directory, '_'.join([tab_name, language, last.split('\\')[-1]]))
  73.  
  74.  
  75. new_name = os.path.join(directory, '_'.join([last.split('\\')[-1], tab[key], language]))
  76. try:
  77. os.rename(last, new_name)
  78. except FileExistsError:
  79. print("error on rename{file}".format(file=new_name))
  80. os.rename(last, new_name + '1')
  81.  
  82.  
  83. if __name__ == '__main__':
  84. # Parsing JSON configuration file
  85. json_file = r'create-all-reports-config.json'
  86.  
  87. try:
  88. with open(json_file) as file:
  89. json_data = json.load(file)
  90. except FileNotFoundError:
  91. print("JSON file wasn't found on specified location")
  92. exit(-1)
  93.  
  94. default_site = json_data["url"]
  95. default_user = json_data["user"]["login"]
  96. default_password = json_data["user"]["password"]
  97. default_report = json_data["reportDirectoryPath"]
  98. default_download = json_data["downloadDirectoryPath"]
  99. default_fp = json_data["firefoxProfile"]
  100. default_languages = json_data["languages"]
  101.  
  102. # if anywhere (in JSON or in CLI) boolean is True -- it will results as true for script
  103.  
  104. # Possible CL arguments for parser
  105. parser = argparse.ArgumentParser(add_help=True,
  106. description='Arguments for script')
  107. parser.add_argument('-s', '--site',
  108. default=default_site,
  109. type=str,
  110. help='Address to log in')
  111. parser.add_argument('-u', '--user',
  112. default=default_user,
  113. type=str,
  114. help='User for log in')
  115. parser.add_argument('-p', '--password',
  116. default=default_password,
  117. type=str,
  118. help='Password for log in')
  119. parser.add_argument('--download-dir',
  120. default=default_download,
  121. type=str,
  122. help='Download directory (full path only)')
  123. parser.add_argument('--report-dir',
  124. default=default_report,
  125. type=str,
  126. help='Report directory (full path only)')
  127. parser.add_argument('--ff-profile',
  128. default=default_fp,
  129. type=str,
  130. action="store",
  131. help='path to firefox profile, set up for downloading without confirm')
  132. parser.add_argument('--optimization',
  133. action="store_true",
  134. help='Flag for optimization in piexpert\'s designs')
  135. parser.add_argument('--creation-only',
  136. action="store_true",
  137. help='Flag for only creation of designs, without any downloads')
  138. parser.add_argument('--delete-all',
  139. action="store_true",
  140. help='Delete all designs after completion')
  141. parser.add_argument('-l', '--languages',
  142. nargs='+',
  143. choices=['en', 'ru', 'zh_hans', 'zh_hant', 'ja', 'ko'],
  144. default=default_languages,
  145. help='Languages for exports')
  146. group = parser.add_mutually_exclusive_group()
  147. group.add_argument('--piexpert-only',
  148. action="store_true",
  149. help='Only creates all piexpert\'s exports')
  150. group.add_argument('--pixls-only',
  151. action="store_true",
  152. help='Only creates all pixls\' exports')
  153. args = parser.parse_args()
  154.  
  155. if args.languages:
  156. languages = set(args.languages)
  157. else:
  158. languages = ['en', 'ru', 'zh_hans', 'zh_hant', 'ja', 'ko']
  159.  
  160. if not os.path.exists(args.report_dir):
  161. os.makedirs(args.report_dir)
  162.  
  163. output_file = open((args.report_dir + '\\report_all_designs{time.tm_hour:02d}_{time.tm_min:02d}_'
  164. '{time.tm_mday:02d}_{time.tm_mon:02d}_'
  165. '{time.tm_year}.txt'.format(time=time.localtime())), 'w')
  166. output_file.write('Started.\n{time}\n'.format(time=time_string()))
  167.  
  168. dateTimeStamp = time.strftime('%Y.%m.%d %H.%M.%S')
  169.  
  170. try:
  171. for lang in languages:
  172.  
  173. output_file.write('Language: {lang}\n'.format(lang=lang))
  174. # create download directory
  175. download_dir = args.download_dir + '\\download_' + lang + '_' + dateTimeStamp
  176. if not os.path.exists(download_dir) and not (args.creation_only or json_data["creation-only"]):
  177. os.makedirs(download_dir)
  178.  
  179. # Configure firefox profile
  180. fp = webdriver.FirefoxProfile(args.ff_profile)
  181. fp.set_preference('browser.download.folderList', 2)
  182. fp.set_preference('browser.download.manager.showWhenStarting', False)
  183. fp.set_preference('browser.download.dir', download_dir)
  184. fp.set_preference('browser.helperApps.neverAsk.saveToDisk',
  185. 'application/x-zip, text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  186.  
  187. driver = set_up(fp=fp, implicitly_wait_timeout=60)
  188. login(driver, site=args.site, user=args.user, password=args.password)
  189.  
  190. lang_locator = (By.ID, "switchLangTo_{language}".format(language=lang))
  191. lang_elem = get_elem_or_null(driver, lang_locator)
  192. if lang_elem:
  193. click(driver, lang_locator, sleep_time=1)
  194. wait_for_loading(driver)
  195.  
  196. # Counting all available designs
  197. click(driver, Locators.IndexPage.NEW_DESIGN)
  198. links = driver.find_elements(By.XPATH, "//a[contains(@class,'npw-treeleaf')]")
  199. total_num = len(links)
  200. output_file.write('Total versions for {lang} should be {num}\n'.format(lang=lang, num=total_num))
  201.  
  202. if not (args.pixls_only or json_data["pixls-only"]):
  203. for link_num in range(total_num):
  204. links = driver.find_elements(By.XPATH, "//a[contains(@class,'npw-treeleaf')]")
  205. parent_link = links[link_num].find_element(By.XPATH, '../../..')
  206.  
  207. links[link_num].click()
  208. if driver.find_element(*Locators.DesignCreation.PIEXPERT).is_enabled():
  209. family_name = ''.join([parent_link.find_element(By.TAG_NAME, "p").text, ' >> ', links[link_num].text])
  210. output_file.write('{num}. {family}\n'.format(num=link_num + 1, family=family_name))
  211.  
  212. click(driver, Locators.DesignCreation.PIEXPERT, expl_wait=2)
  213. print('{num}. {family}'.format(num=link_num + 1, family=family_name))
  214. else:
  215. continue
  216. create_piexpert(driver, va_list=[(5, 1)], opt=args.optimization or json_data["optimization"])
  217. if not (args.creation_only or json_data["creation-only"]):
  218.  
  219. # all to PDF
  220. download_and_rename(driver, download_dir, 'all_tabs', lang, "allTabsExport(1);")
  221.  
  222. tabs = driver.find_elements(By.XPATH, "//div[@id='designTabs']/ul/li/a")
  223. for tab in tabs:
  224. tab.click()
  225. wait_for_loading(driver)
  226. # XLS
  227. download_and_rename(driver, download_dir, tab.text, lang, "currTabExport(1);")
  228.  
  229. # PDF
  230. download_and_rename(driver, download_dir, tab.text, lang, "currTabExport(2);")
  231.  
  232. # RTF
  233. download_and_rename(driver, download_dir, tab.text, lang, "currTabExport(3);")
  234.  
  235. driver.get(args.site)
  236. # driver.execute_script("closeDesign();")
  237. wait_for_loading(driver)
  238.  
  239. click(driver, Locators.IndexPage.NEW_DESIGN)
  240. wait_for_loading(driver)
  241.  
  242. if not (args.piexpert_only or json_data["piexpert-only"]):
  243. for link_num in range(total_num):
  244. links = driver.find_elements(By.XPATH, "//a[contains(@class,'npw-treeleaf')]")
  245. parent_link = links[link_num].find_element(By.XPATH, '../../..')
  246. family_name = ''.join([parent_link.find_element(By.TAG_NAME, "p").text,
  247. ' >> ',
  248. links[link_num].text])
  249. print(family_name)
  250. if len(driver.window_handles) > 1:
  251. driver.switch_to_window(driver.window_handles[0])
  252. output_file.write('{num}. {family}\n'.format(num=link_num + 1, family=family_name))
  253. links[link_num].click()
  254. wait_for_loading(driver)
  255. click(driver, Locators.DesignCreation.PIXLS)
  256. wait_for_loading(driver)
  257. if not (args.creation_only or json_data["creation-only"]):
  258.  
  259. # all to PDF
  260. download_and_rename(driver, download_dir, 'all_tabs', lang, "allTabsExport(1);")
  261.  
  262. tabs = driver.find_elements(By.XPATH, "//div[@id='designTabs']/ul/li/a")
  263.  
  264. for tab in tabs:
  265. tab.click()
  266. wait_for_loading(driver)
  267. # XLS
  268. download_and_rename(driver, download_dir, tab.text, lang, "currTabExport(1);")
  269.  
  270. # PDF
  271.  
  272. download_and_rename(driver, download_dir, tab.text, lang, "currTabExport(2);")
  273.  
  274. # RTF
  275. download_and_rename(driver, download_dir, tab.text, lang, "currTabExport(3);")
  276.  
  277. driver.get(args.site)
  278. # driver.execute_script("closeDesign();")
  279. wait_for_loading(driver)
  280.  
  281. click(driver, Locators.IndexPage.NEW_DESIGN)
  282. wait_for_loading(driver)
  283.  
  284. # Cleaning up
  285. if args.delete_all or json_data["delete-all"]:
  286. click(driver, Locators.DesignCreation.CANCEL)
  287. delete_all_designs(driver)
  288.  
  289. driver.quit()
  290.  
  291. output_file.write('Finished.\n{time}\n'.format(time=time_string()))
  292. output_file.close()
  293.  
  294. except:
  295. # driver.quit()
  296. output_file.write('Error occurred!\n')
  297. traceback.print_exc(file=output_file)
  298. output_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement