File size: 7,324 Bytes
622387b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 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 `<digits>/<digits>` 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 (`<digits>/<digits>` 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 `<digits>/<digits>` 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
|