main.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import csv
  2. import os
  3. import re
  4. import datetime
  5. DIR_WORK = "./"
  6. LIST_BAD_ADAPTER = ['Wireless', 'Bluetooth', 'Wireless', 'WiFi',
  7. 'Kaspersky', 'VirtualBox', 'TAP-Windows',
  8. 'Wintun', '802.11', 'VMware', 'VPN', 'Wi-Fi',
  9. '1394', 'Mobile']
  10. def check_correct_controller(name):
  11. return any(ext not in name for ext in LIST_BAD_ADAPTER)
  12. class ObjectReady:
  13. def get_len_dict(self):
  14. return len(self.__dict__)
  15. def get_paths():
  16. list_path = []
  17. for root, _, files in os.walk(DIR_WORK):
  18. list_path += [
  19. F"{root}/{file}" for file in files if file.split('.')[-1] == 'csv' and "result" not in file]
  20. return list_path
  21. def convert_mb_to_gb(val):
  22. numbers = int(''.join([x for x in val if x.isdigit()]))
  23. return str(round(numbers/1024, 1))+' GB'
  24. def get_ser_motherboard(row):
  25. if len(row) > 0:
  26. if row[0][6] != 'To be filled by O.E.M.':
  27. return row[0][6]
  28. else:
  29. return None
  30. else:
  31. return None
  32. def get_data_from_file(path, obj):
  33. with open(path, 'r', encoding='utf-16') as file:
  34. cs = csv.reader(file, delimiter=',')
  35. next(cs)
  36. line_info_computers = next(cs)
  37. obj.os = line_info_computers[7]
  38. obj.motherboard = get_ser_motherboard(
  39. [x for x in cs if x[0] == '6200'])
  40. obj.cpu = line_info_computers[13]
  41. obj.ram = convert_mb_to_gb(line_info_computers[14])
  42. network_params = [x for x in cs if len(
  43. x) > 2 and x[0] == '2600' and check_correct_controller(x[3])]
  44. if len(network_params) != 0:
  45. i = 1
  46. for network in network_params:
  47. setattr(obj, F'ip{i}', network[6].split(' ')[0])
  48. setattr(obj, F'mac{i}', network[-3])
  49. i += 1
  50. else:
  51. obj.ip1 = None
  52. obj.mac1 = None
  53. file.close()
  54. def get_ready_information():
  55. err_file = open(F"Error_file-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}", "w")
  56. list_objects = []
  57. for path in get_paths():
  58. obj = ObjectReady()
  59. obj.frame = path.split('/')[-2]
  60. obj.cabinet = re.findall(
  61. r"\/([\w+?\ ?[а-яА-Я]+\ ?|\w+?\ ?[a-zA-Z]+\ ?|\d+])[\.|\,]", path)[-1]
  62. try:
  63. get_data_from_file(path, obj)
  64. except UnicodeDecodeError as ude:
  65. err_file.write(F"{path} --> {ude}\n")
  66. except StopIteration as si:
  67. err_file.write(F"{path} --> Пустой файл или тмпо того\n")
  68. list_objects.append(obj)
  69. return list_objects
  70. def create_csv(list_obj):
  71. max_index = max([(v.get_len_dict(), i) for i, v in enumerate(list_obj)])[1]
  72. namefile = F"result-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}"
  73. with open(F"{namefile}.csv", 'w', newline='') as f:
  74. name_fields = [v for v in list_obj[max_index].__dict__]
  75. writer = csv.DictWriter(f, fieldnames=name_fields,
  76. delimiter=';', quoting=csv.QUOTE_NONNUMERIC)
  77. writer.writeheader()
  78. [writer.writerow(elem.__dict__) for elem in list_obj]
  79. f.close()
  80. if __name__ == '__main__':
  81. ready_information = get_ready_information()
  82. create_csv(ready_information)