From 935b8c8658ebbcf05f1a0f59ea6c96d51c96fd77 Mon Sep 17 00:00:00 2001 From: Ludvik Jerabek Date: Mon, 2 Oct 2023 13:32:36 -0400 Subject: [PATCH] Fix red output errors when processing non-standard ldif files. --- pyproject.toml | 2 +- src/ldif2csv.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9df460b..5009633 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ where = ["src"] [project] name = "ldif2csv" -version = "1.0.0" +version = "1.0.1" readme = "README.md" description = "Conversion tool for LDIF to CSV" license = { text = "MIT" } diff --git a/src/ldif2csv.py b/src/ldif2csv.py index 4e05439..d955c03 100644 --- a/src/ldif2csv.py +++ b/src/ldif2csv.py @@ -6,10 +6,16 @@ from ldif import LDIFParser +class LDIFParserNoError(LDIFParser): + # Remove annoying warnings + def _error(self, msg): + if self._strict: + raise ValueError(msg) + def get_ldif_attributes(filename) -> List: attributes = set() with open(filename, "rb") as ldif_file: - parser = LDIFParser(ldif_file, strict=False) + parser = LDIFParserNoError(ldif_file, strict=False) for dn, record in parser.parse(): attributes = (attributes | set(record.keys())) ldif_file.close() @@ -21,7 +27,7 @@ def get_ldif_attributes(filename) -> List: def generate_csv(ldif_attributes: List[str], filename: str, output: str, delimiter: str, separator: str): # Open the LDIF file for reading with open(filename, "rb") as ldif_file: - parser = LDIFParser(ldif_file, strict=False) + parser = LDIFParserNoError(ldif_file, strict=False) with open(output, 'w', newline='', encoding='utf-8-sig') as report_file: csvwriter = csv.DictWriter(report_file, fieldnames=ldif_attributes, extrasaction='ignore', delimiter=delimiter) csvwriter.writeheader()