Skip to content

Commit

Permalink
Fix red output errors when processing non-standard ldif files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ludvikjerabek committed Oct 2, 2023
1 parent 53d46a9 commit 935b8c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
10 changes: 8 additions & 2 deletions src/ldif2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 935b8c8

Please sign in to comment.