Advertisement
hakegawa

Untitled

Aug 1st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import os
  2. from ConfigParser import ConfigParser
  3.  
  4.  
  5. class FileFinder:
  6. def file_finder(self, path, mask):
  7. files = []
  8. for root, dirs, filenames in os.walk(path):
  9. for f in filenames:
  10. if mask in f:
  11. files.append(f)
  12. return files
  13.  
  14.  
  15. class GetLocators:
  16. result = None
  17.  
  18. def parse_config(self, loc_files):
  19. result = dict()
  20. parser = ConfigParser()
  21. parser.read(loc_files)
  22. for section in parser.sections():
  23. result[section] = {'type': parser.get(section, 'type'), 'value': parser.get(section, 'value')}
  24.  
  25. locators = result.keys()
  26.  
  27. return locators
  28.  
  29. class CheckLocatorInFile:
  30. def check_locator_in_file(self, locators, py_files):
  31. for py_file in py_files:
  32. with open(py_file, 'r') as inF:
  33. for line in inF:
  34. for locator in locators:
  35. if locator in line:
  36. locators.remove(locator)
  37.  
  38. print locators
  39. return locators
  40.  
  41.  
  42. class CheckUnusedLocators:
  43. ff = FileFinder()
  44. gl = GetLocators()
  45. cl = CheckLocatorInFile()
  46.  
  47. def check_unused_locators(self):
  48. # get python files from script dir
  49. py_files_dir = os.getcwd()
  50. mask = '.py'
  51. py_files = self.ff.file_finder(py_files_dir, mask)
  52. py_files = [py_file for py_file in py_files if str(py_file).endswith('.py')]
  53.  
  54. # get locators list
  55. loc_files_dir = str(py_files_dir) + '/config'
  56. mask = 'selectors'
  57. loc_files = self.ff.file_finder(loc_files_dir, mask)
  58. new_loc_files = []
  59. for lc in loc_files:
  60. lc = './config/' + str(lc)
  61. new_loc_files.append(lc)
  62.  
  63. locators = self.gl.parse_config(new_loc_files)
  64.  
  65. # check if locator is in file
  66. result = self.cl.check_locator_in_file(locators, py_files)
  67. if result:
  68. for r in result:
  69. print 'We have unused locators: {locator}'.format(locator=r)
  70.  
  71. cu = CheckUnusedLocators()
  72. cu.check_unused_locators()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement