dataspector 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ """Stateless COBOL copybook line cleaning and clause parsing helpers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from typing import Any
7
+
8
+ from dataspector.coboller.constants import (
9
+ BINARY_SIGN_QUALIFIER_RE,
10
+ BLANK_ZERO_RE,
11
+ BYTE_LENGTH_RE,
12
+ CLAUSE_KEYWORDS,
13
+ INDEXED_RE,
14
+ JUSTIFIED_RE,
15
+ LEVEL_START_RE,
16
+ OCCURS_RE,
17
+ PIC_RE,
18
+ REDEFINES_RE,
19
+ SIGN_RE,
20
+ SKIP_LINE_PREFIXES,
21
+ USAGE_RE,
22
+ VALUE_RE,
23
+ SignMode,
24
+ )
25
+
26
+
27
+ def clean_cobol_line(line: str) -> str:
28
+ """Strip fixed-format COBOL sequence/comment/ID areas and return code text.
29
+
30
+ Parameters
31
+ ----------
32
+ line
33
+ Raw single line from a COBOL copybook.
34
+
35
+ Returns
36
+ -------
37
+ str
38
+ Cleaned statement fragment, or ``""`` for blank/comment lines.
39
+ """
40
+ seq_area = line[:6].replace(" ", "")
41
+ has_seq_area = not seq_area or seq_area.isdigit()
42
+ if len(line) >= 7 and line[6] in ("*", "/") and has_seq_area:
43
+ return ""
44
+ if len(line) >= 7 and seq_area.isdigit() and line[6] == " ":
45
+ line = line[7:72] if len(line) > 72 else line[7:]
46
+ elif len(line) > 72 and line[72:].strip().isdigit():
47
+ line = line[:72]
48
+
49
+ if "*>" in line:
50
+ line = line.split("*>", 1)[0]
51
+
52
+ stripped = line.strip()
53
+ if not stripped or stripped.startswith("*"):
54
+ return ""
55
+ return stripped
56
+
57
+
58
+ def extract_statements(rawtext: str) -> list[str]:
59
+ """Join multi-line COBOL statements and split on terminating periods.
60
+
61
+ Parameters
62
+ ----------
63
+ rawtext
64
+ Full COBOL copybook text.
65
+
66
+ Returns
67
+ -------
68
+ list[str]
69
+ Cleaned period-stripped COBOL statements.
70
+ """
71
+ statements: list[str] = []
72
+ buffer = ""
73
+ for raw_line in rawtext.splitlines():
74
+ cleaned = clean_cobol_line(raw_line)
75
+ if not cleaned:
76
+ continue
77
+ buffer = f"{buffer} {cleaned}".strip() if buffer else cleaned
78
+ if buffer.endswith("."):
79
+ statements.append(buffer[:-1].strip())
80
+ buffer = ""
81
+ return statements
82
+
83
+
84
+ def parse_condition_values(raw_value: str) -> tuple[str, ...]:
85
+ """Parse discrete literal values from a level-88 ``VALUE`` clause.
86
+
87
+ Parameters
88
+ ----------
89
+ raw_value
90
+ Raw substring following ``VALUE``/``VALUES`` in a level-88 declaration.
91
+
92
+ Returns
93
+ -------
94
+ tuple[str, ...]
95
+ Tuple of normalized discrete literal values (empty for ``THRU`` ranges).
96
+ """
97
+ upper = raw_value.upper()
98
+ if " THRU " in upper or " THROUGH " in upper:
99
+ return ()
100
+ tokens = re.findall(r"'([^']*)'|\"([^\"]*)\"|(\S+)", raw_value)
101
+ values: list[str] = []
102
+ figurative = {
103
+ "SPACE": " ",
104
+ "SPACES": " ",
105
+ "ZERO": "0",
106
+ "ZEROS": "0",
107
+ "ZEROES": "0",
108
+ }
109
+ for single_q, double_q, bare in tokens:
110
+ if single_q or (single_q == "" and not double_q and not bare):
111
+ values.append(single_q)
112
+ elif double_q:
113
+ values.append(double_q)
114
+ else:
115
+ b_up = bare.upper()
116
+ if b_up in ("ALL", "IS", "ARE"):
117
+ continue
118
+ values.append(figurative.get(b_up, bare))
119
+ return tuple(values)
120
+
121
+
122
+ def parse_statement(stmt: str) -> dict[str, Any] | None:
123
+ """Parse a single period-stripped COBOL data description statement.
124
+
125
+ Parameters
126
+ ----------
127
+ stmt
128
+ Cleaned COBOL statement without its trailing period.
129
+
130
+ Returns
131
+ -------
132
+ dict[str, Any] | None
133
+ Dictionary of extracted clauses (``level``, ``name``, ``pic``,
134
+ ``usage``, ``occurs``, ``redefines``, etc.), or ``None`` if *stmt* is
135
+ not a data description entry.
136
+ """
137
+ upper = stmt.upper()
138
+ if upper.startswith(SKIP_LINE_PREFIXES):
139
+ return None
140
+
141
+ m = LEVEL_START_RE.match(stmt)
142
+ if m is None:
143
+ return None
144
+
145
+ level_str = m.group("level")
146
+ rest = m.group("rest").strip()
147
+ if not rest:
148
+ return None
149
+
150
+ parts = rest.split(None, 1)
151
+ first_token = parts[0]
152
+ if first_token.upper() in CLAUSE_KEYWORDS:
153
+ name = "FILLER"
154
+ clauses = rest
155
+ else:
156
+ name = first_token
157
+ clauses = parts[1] if len(parts) > 1 else ""
158
+
159
+ redefines_m = REDEFINES_RE.search(clauses)
160
+ occurs_m = OCCURS_RE.search(clauses)
161
+ indexed_m = INDEXED_RE.search(clauses)
162
+ pic_m = PIC_RE.search(clauses)
163
+ byte_length_m = BYTE_LENGTH_RE.search(clauses)
164
+ usage_m = USAGE_RE.search(clauses)
165
+ bin_sign_m = BINARY_SIGN_QUALIFIER_RE.search(clauses)
166
+ sign_m = SIGN_RE.search(clauses)
167
+ value_m = VALUE_RE.search(clauses)
168
+
169
+ sign_mode: SignMode | None = None
170
+ if sign_m is not None:
171
+ is_leading = sign_m.group("pos").upper() == "LEADING"
172
+ is_separate = sign_m.group("sep") is not None
173
+ if is_leading and is_separate:
174
+ sign_mode = SignMode.LEADING_SEPARATE
175
+ elif is_leading:
176
+ sign_mode = SignMode.LEADING_OVERPUNCH
177
+ elif is_separate:
178
+ sign_mode = SignMode.TRAILING_SEPARATE
179
+ else:
180
+ sign_mode = SignMode.TRAILING_OVERPUNCH
181
+
182
+ usage_str = usage_m.group("usage").upper() if usage_m is not None else None
183
+
184
+ return {
185
+ "level": level_str,
186
+ "name": name,
187
+ "redefines": redefines_m.group("redefines") if redefines_m else None,
188
+ "occurs": occurs_m.group("occurs") if occurs_m else None,
189
+ "occurs_min": occurs_m.group("min") if occurs_m else None,
190
+ "depending_on": occurs_m.group("depending") if occurs_m else None,
191
+ "indexedby": indexed_m.group("indexedby") if indexed_m else None,
192
+ "pic": pic_m.group("pic") if pic_m else None,
193
+ "byte_length": byte_length_m.group("byte_length") if byte_length_m else None,
194
+ "comp": usage_str,
195
+ "usage": usage_str,
196
+ "binary_sign_qualifier": bin_sign_m.group("qual").upper() if bin_sign_m else None,
197
+ "sign_mode": sign_mode,
198
+ "value": value_m.group("value").strip() if value_m else None,
199
+ "blank_when_zero": bool(BLANK_ZERO_RE.search(clauses)),
200
+ "justified": bool(JUSTIFIED_RE.search(clauses)),
201
+ }