diff --git a/casparser/parsers/__init__.py b/casparser/parsers/__init__.py index a56ad4c..f2d50d6 100644 --- a/casparser/parsers/__init__.py +++ b/casparser/parsers/__init__.py @@ -90,61 +90,71 @@ def read_cas_pdf( # parser / investor extractor calls — every pypdfium2 open re-runs # the password decrypt + content-stream parse, so the savings on # multi-page detailed statements are significant. + # Open the document once and ALWAYS close it before returning. + # pypdfium2 tracks pages / text-pages as children of the document; + # leaving the document open leaks those handles and makes pdfium emit + # "objects still open" at interpreter / library teardown. `close()` + # cascades to all child handles created during parsing. The parsed + # `data` is plain pydantic models holding no pdfium references, so it + # is safe to return after the document is closed. doc = _open_document(filename, password) - - file_type = detect_file_type(filename, password, _doc=doc) - if file_type == FileType.UNKNOWN: - raise CASParseError( - "Could not identify the CAS issuer. Supported issuers are " - "CAMS, KFintech, NSDL, and CDSL." - ) - - if file_type in (FileType.CAMS, FileType.KFINTECH): - cas_type = detect_cas_type(filename, password, _doc=doc) - if cas_type == CASFileType.DETAILED: - from . import cams_detailed - - data: Union[CASData, NSDLCASData] = cams_detailed.parse( - filename, - password, - file_type=file_type, - _doc=doc, - ) - elif cas_type == CASFileType.SUMMARY: - from . import cams_summary - - data = cams_summary.parse( - filename, - password, - file_type=file_type, - _doc=doc, - ) - else: + try: + file_type = detect_file_type(filename, password, _doc=doc) + if file_type == FileType.UNKNOWN: raise CASParseError( - "Could not identify whether this is a DETAILED or " "SUMMARY CAMS / KFin statement." + "Could not identify the CAS issuer. Supported issuers are " + "CAMS, KFintech, NSDL, and CDSL." ) - if sort_transactions and isinstance(data, CASData): - data = _sort_transactions(data) - elif file_type == FileType.NSDL: - from . import nsdl - data = nsdl.parse_nsdl( - filename, - password, - file_type=FileType.NSDL, - _doc=doc, - ) - elif file_type == FileType.CDSL: - from . import cdsl + if file_type in (FileType.CAMS, FileType.KFINTECH): + cas_type = detect_cas_type(filename, password, _doc=doc) + if cas_type == CASFileType.DETAILED: + from . import cams_detailed - data = cdsl.parse_cdsl( - filename, - password, - file_type=FileType.CDSL, - _doc=doc, - ) - else: # pragma: no cover — handled above - raise CASParseError(f"Unsupported file type: {file_type}") + data: Union[CASData, NSDLCASData] = cams_detailed.parse( + filename, + password, + file_type=file_type, + _doc=doc, + ) + elif cas_type == CASFileType.SUMMARY: + from . import cams_summary + + data = cams_summary.parse( + filename, + password, + file_type=file_type, + _doc=doc, + ) + else: + raise CASParseError( + "Could not identify whether this is a DETAILED or " + "SUMMARY CAMS / KFin statement." + ) + if sort_transactions and isinstance(data, CASData): + data = _sort_transactions(data) + elif file_type == FileType.NSDL: + from . import nsdl + + data = nsdl.parse_nsdl( + filename, + password, + file_type=FileType.NSDL, + _doc=doc, + ) + elif file_type == FileType.CDSL: + from . import cdsl + + data = cdsl.parse_cdsl( + filename, + password, + file_type=FileType.CDSL, + _doc=doc, + ) + else: # pragma: no cover — handled above + raise CASParseError(f"Unsupported file type: {file_type}") + finally: + doc.close() if output == "dict": return data diff --git a/casparser/parsers/cdsl.py b/casparser/parsers/cdsl.py index 0b82d30..cf3774b 100644 --- a/casparser/parsers/cdsl.py +++ b/casparser/parsers/cdsl.py @@ -59,6 +59,13 @@ INE_ISIN_RE = re.compile(r"^IN[E9][0-9A-Z]{8}\d$") # `0.196` unit balance prints as `.196`), so the integer part is # optional when a decimal part is present. NUMERIC_RE = re.compile(r"^-?(?:[\d,]+(?:\.\d+)?|\.\d+)$") +# A long CDSL folio wraps its tail onto the next display line, which the +# extractor emits as a separate cell — e.g. folio "91012112582/0" splits +# into "910121125" | "82/0". The tail is a `/` token; we +# splice it back onto the folio head (the on-page hyphen at the wrap is +# only a line-break indicator, absent from the authoritative +# "Folio No :" block). +FOLIO_TAIL_RE = re.compile(r"\d+/\d+") PERIOD_RE = re.compile( r"(?:for\s+the\s+period\s+from|statement\s+for\s+the\s+period\s+from)\s+" @@ -543,14 +550,29 @@ def _parse_mf_holdings_row( # Folio = cell right after ISIN (`/` or just digits). folio = None + folio_end = isin_idx + 1 if isin_idx + 1 < len(block.cells): folio = block.cells[isin_idx + 1].text.strip() or None + # A long folio wraps its tail into the NEXT cell: + # "910121125" | "82/0" | DIRECT | units | ... + # The real folio "91012112582/0" is split across two cells. Splice + # the `/` tail back on (no separator — the on-page + # hyphen is just the wrap indicator); otherwise the folio is + # truncated to its head and the tail is mistaken for the + # distribution-mode column below. + if folio and isin_idx + 2 < len(block.cells): + tail = block.cells[isin_idx + 2].text.strip() + if FOLIO_TAIL_RE.fullmatch(tail): + folio = folio + tail + folio_end = isin_idx + 2 + # Discriminate 13-cell (has ARN/DIRECT) vs 7-cell (no such column). - has_distrib_col = isin_idx + 2 < len(block.cells) and not _looks_numeric( - block.cells[isin_idx + 2].text - ) - data_start = isin_idx + (3 if has_distrib_col else 2) + # The distribution-mode discriminator is the cell right after the + # folio (which may span two cells when it wrapped). + disc_idx = folio_end + 1 + has_distrib_col = disc_idx < len(block.cells) and not _looks_numeric(block.cells[disc_idx].text) + data_start = disc_idx + (1 if has_distrib_col else 0) numerics = [c.text.strip() for c in block.cells[data_start:] if _looks_numeric(c.text)] if len(numerics) < 3: return None