Explorar o código

Загрузить файлы ''

AlexSidorov %!s(int64=2) %!d(string=hai) anos
pai
achega
4e3994a129
Modificáronse 1 ficheiros con 108 adicións e 0 borrados
  1. 108 0
      main.py

+ 108 - 0
main.py

@@ -0,0 +1,108 @@
+import csv
+import os
+import re
+import datetime
+
+DIR_WORK = "./"
+LIST_BAD_ADAPTER = ['Wireless', 'Bluetooth', 'Wireless', 'WiFi',
+                    'Kaspersky', 'VirtualBox', 'TAP-Windows',
+                    'Wintun', '802.11', 'VMware', 'VPN', 'Wi-Fi',
+                    '1394', 'Mobile']
+
+
+def check_correct_controller(name):
+    return any(ext not in name for ext in LIST_BAD_ADAPTER)
+
+
+class ObjectReady:
+    def get_len_dict(self):
+        return len(self.__dict__)
+
+
+def get_paths():
+    list_path = []
+    for root, _, files in os.walk(DIR_WORK):
+        list_path += [
+            F"{root}/{file}" for file in files if file.split('.')[-1] == 'csv' and "result" not in file]
+    return list_path
+
+
+def convert_mb_to_gb(val):
+    numbers = int(''.join([x for x in val if x.isdigit()]))
+    return str(round(numbers/1024, 1))+' GB'
+
+
+def get_ser_motherboard(row):
+    if len(row) > 0:
+        if row[0][6] != 'To be filled by O.E.M.':
+            return row[0][6]
+        else:
+            return None
+    else:
+        return None
+
+
+def get_data_from_file(path, obj):
+    with open(path, 'r', encoding='utf-16') as file:
+        cs = csv.reader(file, delimiter=',')
+        next(cs)
+        line_info_computers = next(cs)
+        obj.os = line_info_computers[7]
+        obj.motherboard = get_ser_motherboard(
+            [x for x in cs if x[0] == '6200'])
+        obj.cpu = line_info_computers[13]
+        obj.ram = convert_mb_to_gb(line_info_computers[14])
+
+        network_params = [x for x in cs if len(
+            x) > 2 and x[0] == '2600' and check_correct_controller(x[3])]
+
+        if len(network_params) != 0:
+            i = 1
+            for network in network_params:
+                setattr(obj, F'ip{i}', network[6].split(' ')[0])
+                setattr(obj, F'mac{i}', network[-3])
+                i += 1
+        else:
+            obj.ip1 = None
+            obj.mac1 = None
+
+        file.close()
+
+
+def get_ready_information():
+    err_file = open(F"Error_file-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}", "w")
+    list_objects = []
+    for path in get_paths():
+        
+        obj = ObjectReady()
+        obj.frame = path.split('/')[-2]
+        obj.cabinet = re.findall(
+            r"\/([\w+?\ ?[а-яА-Я]+\ ?|\w+?\ ?[a-zA-Z]+\ ?|\d+])[\.|\,]", path)[-1]
+        
+        try:
+            get_data_from_file(path, obj)
+        except UnicodeDecodeError as ude:
+            err_file.write(F"{path} --> {ude}\n")
+        except StopIteration as si:
+            err_file.write(F"{path} --> Пустой файл или тмпо того\n")
+
+        list_objects.append(obj)
+    return list_objects
+
+
+def create_csv(list_obj):
+    max_index = max([(v.get_len_dict(), i) for i, v in enumerate(list_obj)])[1]
+    namefile = F"result-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}"
+
+    with open(F"{namefile}.csv", 'w', newline='') as f:
+        name_fields = [v for v in list_obj[max_index].__dict__]
+        writer = csv.DictWriter(f, fieldnames=name_fields,
+                                delimiter=';', quoting=csv.QUOTE_NONNUMERIC)
+        writer.writeheader()
+        [writer.writerow(elem.__dict__) for elem in list_obj]
+        f.close()
+
+
+if __name__ == '__main__':
+    ready_information = get_ready_information()
+    create_csv(ready_information)