Files
aufbau2csv/CMDB2CSV_Aufbaumaster.py
Sven Riwoldt f7f8c52455 init
2024-10-19 12:31:37 +02:00

486 lines
21 KiB
Python
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PyQt5.QtWidgets import QApplication, QDialog, QFileDialog
from PyQt5.uic import loadUi
import sys
from pathlib import Path
import pandas as pd
from icecream import ic
#import numpy as np
class MainUI(QDialog): # erbt von QDialog
def __init__(self):
#super (MainUI, self).__init__() #Aufrufen des Konstruktors von QDialog
super().__init__()
loadUi ("Aufbaumaster2CSV.ui", self)
self.filename = None
self.path = None
self.df = None
self.dfb = None
self.openBtn.clicked.connect(self.open_file_dialog)
self.saveBtn.clicked.connect(self.save_file_dialog)
self.unique_groups = None
self.unique_typs = None
self.gruppierung = self.comboBoxGrp
self.gruppierung.activated.connect(self.changegroup)
self.geraetetyp = self.comboBoxGT
self.geraetetyp.activated.connect(self.changegtyp)
self.rBtnHostnames.setChecked(True)
self.rBtnHostnames.host = "Hostnamen"
self.rBtnAlles.setChecked(False)
self.rBtnAlles.host = "Alles"
self.rBtnHostnames.toggled.connect(self.onClicked)
self.rBtnAlles.toggled.connect(self.onClicked)
self.filter = "Hostnamen"
self.group = None
self.typ = None
def changegroup(self):
self.group = self.gruppierung.currentText()
# ic("Gruppen: ", self.group)
def changegtyp(self):
self.typ = self.geraetetyp.currentText()
# ic("Typ: ", self.typ)
def open_file_dialog(self):
self.filename, _ = QFileDialog.getOpenFileName(
self,
"Aufbaumaster",
"\\Volumes\\Daten01\\Documents\\toCSV",
"Images (*.xls *.xlsx *.xlsb)"
)
# self.filename_edit.setPixmap(QPixmap(self.filename))
self.lblCMDBPath.setText(self.filename)
if self.filename:
self.path = Path(self.filename)
#self.changetable()
self.collectdata()
def onClicked(self):
pass
# radioButton = self.sender()
# if radioButton.isChecked():
# self.filter = radioButton.host
# ic("Filter: ", self.filter)
#print ("Filter ", self.filter)
#if self.filter == "Hostnamen":
# self.df = self.df.dropna(subset=['Hostname'])
#self.df = (self.df['Hostname']).dropna(how="all")
#self.df = self.df['Hostname'].str.len() > 0
# print(self.df['Hostname'])
#else:
#self.df = self.df.reset_index(drop=True)
# self.df = self.dfb
# self.unique_groups = None
# self.unique_typs = None
# self.changetable()
# print(self.df['Hostname'])
def collectdata(self):
# Einlesen der Sheets
self.df_vte = pd.read_excel(self.filename,header=7,na_filter=False,sheet_name="VTE-Input",engine='pyxlsb')
#print(self.df_vte.head)
self.df_cmdb = pd.read_excel(self.filename,header=2,sheet_name="CMDB", engine='pyxlsb')
#self.df_sw_asset = pd.read_excel(self.filename,sheet_name="SW-Asset", engine='pyxlsb')
# Nach relevanten Spalten filtern
self.df_vte = self.df_vte[self.df_vte['Gruppierung']=='Grundgerät']
#self.df_vte = self.df_vte.columns.difference(self.df_cmdb.columns)
self.df_cmdb = self.df_cmdb[self.df_cmdb['Filter'].isin(['Grundgerät','Erweiterung'])]
# Einfügen von Spalten in die CMDB-Tabelle
self.dfb = self.df_vte.merge(self.df_cmdb, on=['Hostname'], how='left')
#self.dfb = self.dfb.T.drop_duplicates().T
self.dfb = self.dfb.drop(columns=['Bestelldatum_y','Hersteller_y','Lieferant_y','RAM_y','SAP-Nr._y','Lieferschein_y','Lieferschein-Nr._y'])
self.dfb = self.dfb.rename(columns={'Bestelldatum_x':'Bestelldatum','Hersteller_x':'Hersteller','Lieferant_x':'Lieferant'})
ic(self.df_cmdb)
# Zusammenfassen aller geänderten Werte
# Wenn Gruppierung nicht ausgewählt wurde, muss es separat ausgelesen werden
#self.changegroup()
#ic(self.changegroup())
#self.changegtyp()
#ic(self.changegtyp())
#if self.filter: # Wenn True dann nur mit Hostnamen
# print("Filter ", self.filter)
#ic(self.filter)
def save_file_dialog(self):
pass
# ic(self.df)
# options = QFileDialog.Options()
# options |= QFileDialog.DontUseNativeDialog
# self.savefilename, _ = QFileDialog.getSaveFileName(self,
# "Save File", "", "CSV(*.csv);;CSV Files(*.csv)", options = options)
#self.df['HE'] = self.df['HE'].apply(lambda x: x if pd.isnull(x) else str(int(x)))
#self.df['Anz. HE'] = self.df['Anz. HE'].apply(lambda x: x if pd.isnull(x) else str(int(x)))
#self.df['Objekt-Titel'] = self.df['Hostname']
# für Überarbeitung zwei Spalten
#self.df["Mandanten ID"] = self.df["Hostname"]
#self.df["Segment"] = self.df["Hostname"]
#self.df = self.df[self.df['Gerätetyp'] == self.typ]
#self.df = self.df.drop(['Ursprungsdatei', 'Preis aus finaler Bestellung','Bestellung initiiert', 'Kommentar', 'Gruppierung', 'Zuordnung zur Serien-Nr.', 'Bemerkung','in PMA-ISEM erfasst','CMDB erfasst','BSI Kenner'], axis=1)
#self.df = self.df.rename(columns = {"Modell / Artikel / \nAbweichungen": "Modell",
# "Auftragsnr. Beschaffung": "Auftragsnr.",
# "Name der Bestellung": "Bestellung",
# "SAP Nr": "SAP",
# "Beschaffung und Lieferinfo": "Lieferinfo",
# })
#self.df['SAP'] = self.df['SAP'].apply(lambda x: x if pd.isnull(x) else str(int(x)))
#self.df.loc[self.df['Rack'] == "FT.31_Rack1_Reihe22_Platz584", "Rack"] = "g1.22.584-590"
#self.df.loc[self.df['Rack'] == "FT.42_Rack1_Reihe4_Platz501", "Rack"] = "g2.04.501-507"
#self.df['Lieferschein-Nr.'] = self.df['Lieferschein-Nr.'].map(str)
#self.df['Angebot-Nr.'] = self.df['Angebot-Nr.'].map(str)
#if self.typ == "VM" or self.typ == "SFP" or self.typ == "SFP+":
# self.group = None;
#if self.typ == "SFP" or self.typ == "SFP+":
# self.df = self.df.drop(["Etage","Raum","Reihe","Rack","HE","Einschub","Montage","Anz. HE", "Formfaktor", "Stadt", "Gebäude", "Lizenz-Key\nWV-Nr.", "RuB-ID","Läuft auf", "Clusterpartner", "CMDB-Status", "Mandanten ID", "Segment"], axis=1)
# #self.df['Angebot-Nr.'].fillna('')
# self.df['Angebot-Nr.'] = self.df['Angebot-Nr.'].replace('nan','', regex=True)
# self.df['Lieferschein-Nr.'] = self.df['Lieferschein-Nr.'].replace('nan','', regex=True)
#self.df['Gerätetyp'] = self.df['Gerätetyp'].replace('SFP+','SFP', regex=False)
# Zeilen mit ungenauen Bezeichnern entfernen
# indexAge = df[(df['Name'] == 'John Holland') | (df['Position'] == 'SG')].index
# df.drop(indexAge, inplace=True)
# S-1110-SFP == Medienkonverter
# indexModell = self.df[ (self.df['Modell'] == 'CISCO TRANSCEIVER MODULE 1000BASE-LX / LH SFP')|
# (self.df['Modell'] == 'Nokia SFP GIGE Base T RJ45  R6&6 DDM -40/85C') |
# (self.df['Modell'] == 'S-1110-SFP') |
# (self.df['Modell'] == 'Nokia SFP GIGE LX-LC ROHS 6/6 DDM -40/85C')].index
#self.df.drop(indexModell, inplace=True)
#self.df['Modell'] = self.df['Modell'].replace('SFP-MM-1G / PSFP-1000-M2LC05','PSFP-1000-M2LC05', regex=False)
#self.df['Modell'] = self.df['Modell'].replace('PSFP-1000-M2LC05 / SFP-MM-1G','PSFP-1000-M2LC05', regex=False)
#self.df['Modell'] = self.df['Modell'].replace('Perle PSFP -1000-S2LC10','PSFP-1000-S2LC10', regex=False)
#self.df['Modell'] = self.df['Modell'].replace('10G Base Active Optical SFP + Cable 2m','SFP-10G-AOC2M=', regex=False)
#self.df['Modell'] = self.df['Modell'].replace('10G Base Active Optical SFP + Cable 3m','SFP-10G-AOC3M=', regex=False)
# self.df['Modell'] = self.df['Modell'].replace('10G Base Active Optical SFP + Cable 2m','SFP-10G-AOC2M=', regex=False)
#self.df["Objekt-Bezeichner"] = self.df["Modell"]
#self.df = self.df[self.df['Gruppierung'] == self.group]
# ic(self.typ)
# ic(self.group)
# ic(self.df)
# Entfernt, da ich neue Objektgruppe Terminalserver angelegt habe
# if self.typ == "Server" and self.group == "Grundgerät":
# self.df['Kategorie'] = "Server"
# elif self.typ == "Terminalserver" and self.group == "Grundgerät":
# self.df['Kategorie'] = "Konsolenserver"
#self.df.replace(to_replace=r'ohne', value='', regex=False)
#pd.is_numeric_dtype(self.df['SAP']):
# pd.to_numeric(self.df['A'], errors='coerce')
# ic(self.df)
# ic(self.filter)
#if self.filter == "Hostnamen":
# self.df = self.df.dropna(subset=['Hostname'])
# self.df = self.df.apply(self.prüfe_und_setze_wert, axis=1)
#ic(type(self.df['Hostname'].str[4:6]))
#self.df["Mandanten ID"] = self.df['Hostname'].str[4:6]
#ic(self.df['Hostname'].str[6:8])
#self.df["Segment"] = self.df['Hostname'].str[6:8]
#https://stackoverflow.com/questions/33266344/np-where-not-working-in-my-pandas
#self.df['Mandanten ID'] = np.where(self.df['Hostname'].str[4:6] == "10", 'm10 CMP Plattform / iM.0 / iM.4 / PAP sIMCP',
#np.where(self.df['Hostname'].str[4:6] == "19", 'm19 Testumgebung',
#np.where(self.df['Hostname'].str[4:6] == "20", 'm20 iM.1 Interner Mandant \(Schutzbedarf sehr hoch\)')))
#self.df['Mandanten ID'].where(self.df['Mandanten ID'] == "19", "m19 Testumgebung")
#ic(type(self.df['Hostname'].str[4:6]).astype(int))
#ic(self.df['Hostname'].str[4:6].any())
#ic(self.df['Hostname'].str[4:6].all())
#if (self.df['Hostname'].str[4:6]).any():
#tmid = self.mandantenid(self.df['Hostname'] )
#ic(tmid[4:6])
#ic(type(self.mandantenid(self.df['Hostname'])[4:6]))
#tmid = self.mandantenid(self.df['Hostname'])
#if self.mandantenid(self.df["Hostname"]) == "10":
# self.df['Mandanten ID'] = 'm10 CMP Plattform / iM.0 / iM.4 / PAP sIMCP'
#else:
# self.df['Mandanten ID'] = "Test"
# elif self.mandantenid(self.df['Hostname'])[4:6] == "19":
# self.df['Mandanten ID'] = 'm19 Testumgebung'
# else:
# self.df['Mandanten ID'] = self.mandantenid(self.df['Hostname'])[4:6]
#if self.mandantenid():
#if self.df['Hostname'].any().str.contains('(?![a-z]{4})[0-9]{2}', regex=True):
#if self.df['Hostname'].any().str.contains('([0-9]{2,2}(?![a-z])', regex=True):
# pass
# if self.df['Hostname'].str[4:6] =="10":
# self.df['Mandanten ID'] = 'm10 CMP Plattform / iM.0 / iM.4 / PAP sIMCP'
#self.df['Mandanten ID'] = 'm10 CMP Plattform / iM.0 / iM.4 / PAP sIMCP'
# elif (self.df['Hostname'].str[4:6]).any() == "19":
# self.df['Mandanten ID'] = 'm19 Testumgebung'
# else:
# self.df['Mandanten ID'] = self.df["Hostname"].str[4:6]
# match item(self.df['Hostname'].str[4:6]):
# case '10':
# self.df["Mandanten ID"] = "m10 CMP Plattform / iM.0 / iM.4 / PAP sIMCP"
# case other:
# self.df["Mandanten ID"] = ""
# m19 Testumgebung 19
# m20 iM.1 Interner Mandant (Schutzbedarf sehr hoch) 20
# m21 iM.2 Interner Mandant (Schutzbedarf normal) 21
# m22 iM.3 Interner Mandant (Schutzbedarf hoch) 22
# m50 sEMCP.Instanz-1 (CMP Zentral) 50
# m51 Classified OPS 51
# m73 FWP Lüftersteuerung 73
# m75 Fawkes 75
# m76 Demo 76
# m77 Fritz54 77
# m78 ITZBund mSBC 78
# m79 BOS-Spur 79
# m99 Releasemanagement 99
# m80 ACDC 80
# ic(self.df)
#self.df = self.df['Gerätetyp'] = self.gruppierung.currentText()
#print(self.gruppierung.currentText())
# if self.selected_typ_value != "VM":
# self.df = self.df[self.df['Gruppierung'] == selected_group_value]
# self.df.to_csv(self.savefilename, index=None, header=True, encoding='utf-8')
# print("Datei wurde gespeichert:", self.savefilename)
# def changetable(self):
# with self.path:
# self.df = pd.DataFrame(pd.read_excel(self.filename))#,dtype=str)
# self.dfb = self.df
#
# self.unique_groups = self.df['Gruppierung'].unique().tolist()
# #print("unique_group: ",self.unique_groups)
# self.unique_typs = self.df['Gerätetyp'].unique().tolist()
# for i in self.unique_groups:
# if isinstance(i, str): # Check ob i ein str ist
# if i !="nan":
# self.gruppierung.addItem(i)
# for i in self.unique_typs:
# if isinstance(i, str):
# if i !="nan":
# self.geraetetyp.addItem(i)
#print(self.df)
# def mandantenid(self,id):
# ic("MandantenID = ",id," ",id[1][4:6])
# return int(id[1][4:6])
#
# #return id.get( key= "Hostname")
# pass
def prüfe_und_setze_wert(self, zeile):
#ic(zeile)
if zeile['Mandanten ID'][4:6] == "10": # Indexe 4 und 5 entsprechen den Stellen 5 und 6
zeile['Mandanten ID'] = 'm10 CMP Plattform / iM.0 / iM.4 / PAP sIMCP'
elif zeile['Mandanten ID'][4:6] == "19":
zeile['Mandanten ID'] = 'm19 Testumgebung'
elif zeile['Mandanten ID'][4:6] == "20":
zeile['Mandanten ID'] = 'm20 iM.1 Interner Mandant (Schutzbedarf sehr hoch)'
elif zeile['Mandanten ID'][4:6] == "21":
zeile['Mandanten ID'] = 'm21 iM.2 Interner Mandant (Schutzbedarf normal)'
elif zeile['Mandanten ID'][4:6] == "22":
zeile['Mandanten ID'] = 'm22 iM.3 Interner Mandant (Schutzbedarf hoch)'
elif zeile['Mandanten ID'][4:6] == "50":
zeile['Mandanten ID'] = 'm50 sEMCP.Instanz-1 (CMP Zentral)'
elif zeile['Mandanten ID'][4:6] == "51":
zeile['Mandanten ID'] = 'm51 Classified OPS'
elif zeile['Mandanten ID'][4:6] == "73":
zeile['Mandanten ID'] = 'm73 FWP Lüftersteuerung'
elif zeile['Mandanten ID'][4:6] == "75":
zeile['Mandanten ID'] = 'm75 Fawkes'
elif zeile['Mandanten ID'][4:6] == "76":
zeile['Mandanten ID'] = 'm76 Demo'
elif zeile['Mandanten ID'][4:6] == "77":
zeile['Mandanten ID'] = 'm77 Fritz54 '
elif zeile['Mandanten ID'][4:6] == "78":
zeile['Mandanten ID'] = 'm78 ITZBund mSBC'
elif zeile['Mandanten ID'][4:6] == "79":
zeile['Mandanten ID'] = 'm79 BOS-Spur'
elif zeile['Mandanten ID'][4:6] == "99":
zeile['Mandanten ID'] = 'm99 Releasemanagement'
elif zeile['Mandanten ID'][4:6] == "80":
zeile['Mandanten ID'] = 'm80 ACDC'
if zeile['Segment'][6:8] == "00":
zeile['Segment'] = 'm10 mgmt CMP Core'
ic('m10 mgmt CMP Core')
elif zeile['Segment'][6:8] == "01":
zeile['Segment'] = 'm10 mgmt Infrastructure'
elif zeile['Segment'][6:8] == "02":
zeile['Segment'] = 'm10 mgmt Administration'
elif zeile['Segment'][6:8] == "03":
zeile['Segment'] = 'm10 mgmt zLightsOut'
elif zeile['Segment'][6:8] == "04":
zeile['Segment'] = 'm10 CTS'
elif zeile['Segment'][6:8] == "05":
zeile['Segment'] = 'm10-OSS'
elif zeile['Segment'][6:8] == "10":
zeile['Segment'] = 'm10 CMP2sIM'
elif zeile['Segment'][6:8] == "11":
zeile['Segment'] = 'm10 Intern-CMP iM0'
elif zeile['Segment'][6:8] == "12":
zeile['Segment'] = 'm20 iM.1 Interner Mandant 1 (SB sehr hoch)'
elif zeile['Segment'][6:8] == "13":
zeile['Segment'] = 'm21 iM.2 Interner Mandant 2 (SB normal)'
elif zeile['Segment'][6:8] == "14":
zeile['Segment'] = 'm22 iM.3 Interner Mandant 3 (SB hoch)'
elif zeile['Segment'][6:8] == "15":
zeile['Segment'] = 'm10 iM.4 Interner Mandant 4 (CNMS2CMP)'
elif zeile['Segment'][6:8] == "20":
zeile['Segment'] = 'VRD-Plattform (PAP-Anbindung)'
elif zeile['Segment'][6:8] == "21":
zeile['Segment'] = 'VRD-Plattform (LightsOut + Basisinfrastruktur + Cache)'
elif zeile['Segment'][6:8] == "22":
zeile['Segment'] = 'VRD-Plattform (Admin)'
elif zeile['Segment'][6:8] == "23":
zeile['Segment'] = 'VRD-Plattform (Basisinfrastruktur Mandant zentral)'
elif zeile['Segment'][6:8] == "24":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "25":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "26":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "27":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "28":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "29":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "30":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "31":
zeile['Segment'] = 'VRD-Mandant (Infrastruktur)'
elif zeile['Segment'][6:8] == "32":
zeile['Segment'] = 'VRD-Mandant (Admin)'
elif zeile['Segment'][6:8] == "33":
zeile['Segment'] = 'VRD-Mandant (Anbindung)'
elif zeile['Segment'][6:8] == "34":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "35":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "36":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "37":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "38":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "39":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "40":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "41":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "42":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "43":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "44":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "45":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "46":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "47":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "48":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "49":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "50":
zeile['Segment'] = 'Spare'
elif zeile['Segment'][6:8] == "51":
zeile['Segment'] = 'VRD-Mandant (Kundensegment Infra)'
elif zeile['Segment'][6:8] == "52":
zeile['Segment'] = 'VRD-Mandant (Kundensegment Admin)'
elif zeile['Segment'][6:8] == "53":
zeile['Segment'] = 'VRD-Mandant (Kundensegment)'
elif zeile['Segment'][6:8] == "b0":
zeile['Segment'] = 'mgmt BS Übergang zum CMP Core'
elif zeile['Segment'][6:8] == "ba":
zeile['Segment'] = 'mgmt BS Bon BTW'
elif zeile['Segment'][6:8] == "bb":
zeile['Segment'] = 'mgmt BS Bon LRS'
elif zeile['Segment'][6:8] == "bc":
zeile['Segment'] = 'mgmt BS Ber W55'
elif zeile['Segment'][6:8] == "bd":
zeile['Segment'] = 'mgmt BS Ber PS'
elif zeile['Segment'][6:8] == "be":
zeile['Segment'] = 'mgmt BS Ber NDA'
elif zeile['Segment'][6:8] == "bf":
zeile['Segment'] = 'mgmt BS HanTMX'
elif zeile['Segment'][6:8] == "bg":
zeile['Segment'] = 'mgmt BS Ber DS'
elif zeile['Segment'][6:8] == "c0":
zeile['Segment'] = 'mgmt CMP Übergang Internet'
elif zeile['Segment'][6:8] == "x1":
zeile['Segment'] = 'm10 SecA2sIM'
return zeile
if __name__ == "__main__" :
app = QApplication (sys.argv)
ui = MainUI()
ui.show()
app.exec_()