Update public source
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
import csv
|
||||
import logging
|
||||
|
||||
|
||||
class OUIImporter:
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
resolver,
|
||||
files
|
||||
):
|
||||
|
||||
self.logger = logging.getLogger(
|
||||
"oui-import"
|
||||
)
|
||||
|
||||
self.resolver = resolver
|
||||
|
||||
self.files = files
|
||||
|
||||
|
||||
# =====================================================
|
||||
# Импорт IEEE
|
||||
# =====================================================
|
||||
|
||||
def import_database(self):
|
||||
|
||||
|
||||
count = 0
|
||||
|
||||
skipped = 0
|
||||
|
||||
|
||||
stats = {
|
||||
|
||||
"MA-L": 0,
|
||||
|
||||
"MA-M": 0,
|
||||
|
||||
"MA-S": 0
|
||||
|
||||
}
|
||||
|
||||
|
||||
for filename in self.files:
|
||||
|
||||
|
||||
if not filename.exists():
|
||||
|
||||
self.logger.warning(
|
||||
f"{filename.name} not found"
|
||||
)
|
||||
|
||||
continue
|
||||
|
||||
|
||||
self.logger.info(
|
||||
f"Importing {filename.name}"
|
||||
)
|
||||
|
||||
|
||||
with open(
|
||||
|
||||
filename,
|
||||
|
||||
"r",
|
||||
|
||||
encoding="utf-8",
|
||||
|
||||
errors="ignore"
|
||||
|
||||
) as file:
|
||||
|
||||
|
||||
reader = csv.DictReader(
|
||||
file
|
||||
)
|
||||
|
||||
|
||||
for row in reader:
|
||||
|
||||
|
||||
registry = row.get(
|
||||
"Registry"
|
||||
)
|
||||
|
||||
assignment = row.get(
|
||||
"Assignment"
|
||||
)
|
||||
|
||||
organization = row.get(
|
||||
"Organization Name"
|
||||
)
|
||||
|
||||
|
||||
if not registry:
|
||||
|
||||
continue
|
||||
|
||||
|
||||
if registry not in stats:
|
||||
|
||||
continue
|
||||
|
||||
|
||||
if not assignment:
|
||||
|
||||
skipped += 1
|
||||
|
||||
continue
|
||||
|
||||
|
||||
if not organization:
|
||||
|
||||
skipped += 1
|
||||
|
||||
continue
|
||||
|
||||
|
||||
assignment = (
|
||||
|
||||
assignment
|
||||
|
||||
.replace(
|
||||
"-",
|
||||
""
|
||||
)
|
||||
|
||||
.replace(
|
||||
":",
|
||||
""
|
||||
)
|
||||
|
||||
.replace(
|
||||
".",
|
||||
""
|
||||
)
|
||||
|
||||
.strip()
|
||||
|
||||
.upper()
|
||||
|
||||
)
|
||||
|
||||
|
||||
if not all(
|
||||
|
||||
char in "0123456789ABCDEF"
|
||||
|
||||
for char in assignment
|
||||
|
||||
):
|
||||
|
||||
skipped += 1
|
||||
|
||||
continue
|
||||
|
||||
|
||||
if len(
|
||||
assignment
|
||||
) not in (
|
||||
|
||||
6,
|
||||
|
||||
7,
|
||||
|
||||
9
|
||||
|
||||
):
|
||||
|
||||
skipped += 1
|
||||
|
||||
continue
|
||||
|
||||
|
||||
result = self.resolver.add_vendor(
|
||||
|
||||
assignment,
|
||||
|
||||
organization.strip(),
|
||||
|
||||
registry
|
||||
|
||||
)
|
||||
|
||||
|
||||
if result:
|
||||
|
||||
count += 1
|
||||
|
||||
stats[
|
||||
registry
|
||||
] += 1
|
||||
|
||||
else:
|
||||
|
||||
skipped += 1
|
||||
|
||||
|
||||
self.resolver.commit()
|
||||
|
||||
|
||||
total = self.resolver.count()
|
||||
|
||||
|
||||
self.logger.info(
|
||||
f"Imported: {count}"
|
||||
)
|
||||
|
||||
self.logger.info(
|
||||
f"Skipped: {skipped}"
|
||||
)
|
||||
|
||||
self.logger.info(
|
||||
f"Database records: {total}"
|
||||
)
|
||||
|
||||
return {
|
||||
|
||||
"imported": count,
|
||||
|
||||
"skipped": skipped,
|
||||
|
||||
"total": total,
|
||||
|
||||
"stats": stats
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user