aibi-toolkit 1.2.1__tar.gz → 1.3.0__tar.gz

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.
Files changed (53) hide show
  1. {aibi_toolkit-1.2.1/src/aibi_toolkit.egg-info → aibi_toolkit-1.3.0}/PKG-INFO +20 -5
  2. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/README.md +19 -4
  3. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/pyproject.toml +1 -1
  4. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/__init__.py +3 -1
  5. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/cli.py +20 -7
  6. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/cli_internal.py +44 -15
  7. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/lint.py +6 -0
  8. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/measure.py +0 -2
  9. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/metadata.py +12 -3
  10. aibi_toolkit-1.3.0/src/aibi_toolkit/tmdl.py +253 -0
  11. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0/src/aibi_toolkit.egg-info}/PKG-INFO +20 -5
  12. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit.egg-info/SOURCES.txt +3 -1
  13. aibi_toolkit-1.3.0/tests/test_lint.py +35 -0
  14. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_measure.py +12 -0
  15. aibi_toolkit-1.3.0/tests/test_tmdl.py +118 -0
  16. aibi_toolkit-1.2.1/tests/test_lint.py +0 -13
  17. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/LICENSE +0 -0
  18. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/setup.cfg +0 -0
  19. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/__main__.py +0 -0
  20. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/audit.py +0 -0
  21. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/autofix.py +0 -0
  22. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/config.py +0 -0
  23. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/csvio.py +0 -0
  24. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/dax.py +0 -0
  25. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/diagram.py +0 -0
  26. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/filters.py +0 -0
  27. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/integrity.py +0 -0
  28. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/naming.py +0 -0
  29. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/profile.py +0 -0
  30. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/py.typed +0 -0
  31. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/report.py +0 -0
  32. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/sample.py +0 -0
  33. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit/tables.py +0 -0
  34. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit.egg-info/dependency_links.txt +0 -0
  35. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit.egg-info/entry_points.txt +0 -0
  36. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit.egg-info/requires.txt +0 -0
  37. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/src/aibi_toolkit.egg-info/top_level.txt +0 -0
  38. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_audit.py +0 -0
  39. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_autofix.py +0 -0
  40. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_cli.py +0 -0
  41. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_config.py +0 -0
  42. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_csvio.py +0 -0
  43. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_dax.py +0 -0
  44. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_diagram.py +0 -0
  45. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_filters.py +0 -0
  46. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_integrity.py +0 -0
  47. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_metadata.py +0 -0
  48. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_naming.py +0 -0
  49. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_profile.py +0 -0
  50. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_readme.py +0 -0
  51. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_report.py +0 -0
  52. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_sample.py +0 -0
  53. {aibi_toolkit-1.2.1 → aibi_toolkit-1.3.0}/tests/test_tables.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aibi-toolkit
3
- Version: 1.2.1
3
+ Version: 1.3.0
4
4
  Summary: Clean up Power BI / Tableau semantic-model metadata: naming, format strings, DAX snippets, hygiene auditing, linting, diagrams and reports
5
5
  Author: Sourin Rakshit
6
6
  License: MIT License
@@ -104,6 +104,7 @@ Full module map:
104
104
 
105
105
  | Area | Module |
106
106
  | ------------------ | --------------- |
107
+ | TMDL/PBIP loading | `tmdl` |
107
108
  | Naming | `naming` |
108
109
  | Format strings | `measure` |
109
110
  | DAX snippets | `dax`, `filters`|
@@ -119,6 +120,18 @@ Full module map:
119
120
  | Profiling | `profile` |
120
121
  | CSV round-trip | `csvio` |
121
122
 
123
+ ## Input formats
124
+
125
+ `aibi summary path` — `path` may be either:
126
+
127
+ - a shared-schema JSON dump (`model.json` from your model tool), or
128
+ - a TMDL folder (a `.pbip` project root, a `*.Dataset` folder, or the
129
+ `definition/` directory itself). Tables are read from
130
+ `definition/tables/*.tmdl`, relationships from `definition/relationships.tmdl`;
131
+ every command works unchanged on either shape.
132
+
133
+ Full details and layout diagram: [docs/input.md](docs/input.md).
134
+
122
135
  ## Install
123
136
 
124
137
  Install from PyPI and check the binary works:
@@ -146,7 +159,9 @@ aibi fix --dry-run model.json # preview fixes before writing anything
146
159
 
147
160
  Audit returns exit `1` when findings exist, `0` when clean, so a pipeline
148
161
  that already has `set -e` fails immediately. Pass `--json` to `audit`,
149
- `lint` or `profile` when you want a machine-readable result.
162
+ `lint` or `profile` when you want a machine-readable result. Any loader
163
+ failure (missing path, not valid JSON, wrong top-level shape) exits `2`
164
+ with a one-line reason on stderr — never a traceback.
150
165
 
151
166
  ## How it helps
152
167
 
@@ -171,9 +186,9 @@ that already has `set -e` fails immediately. Pass `--json` to `audit`,
171
186
  hiding). For deep complexity scoring and anti-pattern detection,
172
187
  that is what [aibi-dax](https://github.com/SourinRakshit-ai/aibi-dax)
173
188
  is for.
174
- - **Not a full TMDL/PBIP authoring tool.** It reads shared-schema
175
- model dumps; it does not write them back to disk yet
176
- (tracked in [#6](https://github.com/SourinRakshit-ai/aibi-toolkit/issues/6)).
189
+ - **Not a TMDL/PBIP authoring tool.** It reads TMDL folders and
190
+ JSON dumps, but does not write TMDL back to disk; editing stays in
191
+ your model tool.
177
192
  - **Not a bug finder for DAX logic.** It checks static patterns; it
178
193
  cannot tell you your total is wrong. That still needs `EVALUATE`
179
194
  checks and your eyes.
@@ -53,6 +53,7 @@ Full module map:
53
53
 
54
54
  | Area | Module |
55
55
  | ------------------ | --------------- |
56
+ | TMDL/PBIP loading | `tmdl` |
56
57
  | Naming | `naming` |
57
58
  | Format strings | `measure` |
58
59
  | DAX snippets | `dax`, `filters`|
@@ -68,6 +69,18 @@ Full module map:
68
69
  | Profiling | `profile` |
69
70
  | CSV round-trip | `csvio` |
70
71
 
72
+ ## Input formats
73
+
74
+ `aibi summary path` — `path` may be either:
75
+
76
+ - a shared-schema JSON dump (`model.json` from your model tool), or
77
+ - a TMDL folder (a `.pbip` project root, a `*.Dataset` folder, or the
78
+ `definition/` directory itself). Tables are read from
79
+ `definition/tables/*.tmdl`, relationships from `definition/relationships.tmdl`;
80
+ every command works unchanged on either shape.
81
+
82
+ Full details and layout diagram: [docs/input.md](docs/input.md).
83
+
71
84
  ## Install
72
85
 
73
86
  Install from PyPI and check the binary works:
@@ -95,7 +108,9 @@ aibi fix --dry-run model.json # preview fixes before writing anything
95
108
 
96
109
  Audit returns exit `1` when findings exist, `0` when clean, so a pipeline
97
110
  that already has `set -e` fails immediately. Pass `--json` to `audit`,
98
- `lint` or `profile` when you want a machine-readable result.
111
+ `lint` or `profile` when you want a machine-readable result. Any loader
112
+ failure (missing path, not valid JSON, wrong top-level shape) exits `2`
113
+ with a one-line reason on stderr — never a traceback.
99
114
 
100
115
  ## How it helps
101
116
 
@@ -120,9 +135,9 @@ that already has `set -e` fails immediately. Pass `--json` to `audit`,
120
135
  hiding). For deep complexity scoring and anti-pattern detection,
121
136
  that is what [aibi-dax](https://github.com/SourinRakshit-ai/aibi-dax)
122
137
  is for.
123
- - **Not a full TMDL/PBIP authoring tool.** It reads shared-schema
124
- model dumps; it does not write them back to disk yet
125
- (tracked in [#6](https://github.com/SourinRakshit-ai/aibi-toolkit/issues/6)).
138
+ - **Not a TMDL/PBIP authoring tool.** It reads TMDL folders and
139
+ JSON dumps, but does not write TMDL back to disk; editing stays in
140
+ your model tool.
126
141
  - **Not a bug finder for DAX logic.** It checks static patterns; it
127
142
  cannot tell you your total is wrong. That still needs `EVALUATE`
128
143
  checks and your eyes.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "aibi-toolkit"
3
- version = "1.2.1"
3
+ version = "1.3.0"
4
4
  description = "Clean up Power BI / Tableau semantic-model metadata: naming, format strings, DAX snippets, hygiene auditing, linting, diagrams and reports"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -21,9 +21,10 @@ from . import (
21
21
  report,
22
22
  sample,
23
23
  tables,
24
+ tmdl,
24
25
  )
25
26
 
26
- __version__ = "1.2.1"
27
+ __version__ = "1.3.0"
27
28
  __all__ = [
28
29
  "audit",
29
30
  "autofix",
@@ -41,4 +42,5 @@ __all__ = [
41
42
  "report",
42
43
  "sample",
43
44
  "tables",
45
+ "tmdl",
44
46
  ]
@@ -36,7 +36,7 @@ def main(argv: list[str] | None = None) -> int:
36
36
  s.add_argument("out")
37
37
  if cmd == "fix":
38
38
  s.add_argument("--dry-run", action="store_true", help="print findings without applying")
39
- if cmd in ("audit", "lint", "profile"):
39
+ if cmd in ("audit", "lint", "profile", "summary", "tables"):
40
40
  s.add_argument("--json", action="store_true", help="emit machine-readable JSON instead of text")
41
41
  args = parser.parse_args(argv)
42
42
  return _dispatch(args)
@@ -46,14 +46,27 @@ def _dispatch(args: argparse.Namespace) -> int:
46
46
  from . import cli_internal
47
47
  json_flag = getattr(args, "json", False)
48
48
  if args.command == "summary":
49
- from aibi_toolkit.metadata import load_model, model_summary
50
- s = model_summary(load_model(args.model))
51
- print(f"tables={s['tables']} measures={s['measures']} columns={s['columns']}")
49
+ from aibi_toolkit.metadata import model_summary
50
+
51
+ from . import cli_internal
52
+ s = model_summary(cli_internal._model(args.model))
53
+ if json_flag:
54
+ import json
55
+ print(json.dumps(s))
56
+ else:
57
+ print(f"tables={s['tables']} measures={s['measures']} columns={s['columns']}")
52
58
  return 0
53
59
  if args.command == "tables":
54
- from aibi_toolkit.metadata import list_tables, load_model
55
- for t in list_tables(load_model(args.model)):
56
- print(t)
60
+ from aibi_toolkit.metadata import list_tables
61
+
62
+ from . import cli_internal
63
+ tables = list_tables(cli_internal._model(args.model))
64
+ if json_flag:
65
+ import json
66
+ print(json.dumps(tables))
67
+ else:
68
+ for t in tables:
69
+ print(t)
57
70
  return 0
58
71
  if args.command == "audit":
59
72
  return cli_internal.cmd_audit(args.model, json_out=json_flag)
@@ -1,12 +1,34 @@
1
1
  """Internal CLI command implementations, split out for readability."""
2
2
 
3
3
 
4
+ import sys
5
+
6
+ from aibi_toolkit.metadata import Model
7
+
8
+
9
+ def _model(path: str) -> Model:
10
+ """Load a model from disk or exit 2 with a one-line reason."""
11
+ import json
12
+
13
+ from .metadata import load_model
14
+ try:
15
+ return load_model(path)
16
+ except FileNotFoundError:
17
+ print(f"aibi: cannot read {path}: no such file or directory", file=sys.stderr)
18
+ raise SystemExit(2)
19
+ except json.JSONDecodeError as exc:
20
+ print(f"aibi: {path}: not valid JSON ({exc})", file=sys.stderr)
21
+ raise SystemExit(2)
22
+ except (ValueError, TypeError) as exc:
23
+ print(f"aibi: {path}: {exc}", file=sys.stderr)
24
+ raise SystemExit(2)
25
+
26
+
4
27
  def cmd_audit(model_path: str, json_out: bool = False) -> int:
5
28
  import json
6
29
 
7
30
  from aibi_toolkit.audit import audit_measures, audit_score
8
- from aibi_toolkit.metadata import load_model
9
- model = load_model(model_path)
31
+ model = _model(model_path)
10
32
  findings = audit_measures(model)
11
33
  if json_out:
12
34
  print(json.dumps({"score": audit_score(model), "findings": findings}, indent=2))
@@ -22,8 +44,7 @@ def cmd_lint(model_path: str, json_out: bool = False) -> int:
22
44
  import json
23
45
 
24
46
  from aibi_toolkit.lint import lint_model
25
- from aibi_toolkit.metadata import load_model
26
- findings = lint_model(load_model(model_path))
47
+ findings = lint_model(_model(model_path))
27
48
  if json_out:
28
49
  print(json.dumps({"lint": findings}, indent=2))
29
50
  return 1 if findings else 0
@@ -35,16 +56,19 @@ def cmd_lint(model_path: str, json_out: bool = False) -> int:
35
56
 
36
57
  def cmd_export(model_path: str, out_path: str) -> int:
37
58
  from aibi_toolkit.csvio import export_measures
38
- from aibi_toolkit.metadata import list_measures, load_model
39
- n = export_measures(list_measures(load_model(model_path)), out_path)
59
+ from aibi_toolkit.metadata import list_measures
60
+ try:
61
+ n = export_measures(list_measures(_model(model_path)), out_path)
62
+ except OSError as exc:
63
+ print(f"aibi: cannot write {out_path}: {exc.strerror or exc}", file=sys.stderr)
64
+ return 2
40
65
  print(f"exported {n} measures to {out_path}")
41
66
  return 0
42
67
 
43
68
 
44
69
  def cmd_diagram(model_path: str) -> int:
45
70
  from aibi_toolkit.diagram import mermaid_er
46
- from aibi_toolkit.metadata import load_model
47
- print(mermaid_er(load_model(model_path)))
71
+ print(mermaid_er(_model(model_path)))
48
72
  return 0
49
73
 
50
74
 
@@ -53,8 +77,7 @@ def cmd_fix(model_path: str, dry_run: bool = False) -> int:
53
77
 
54
78
  from aibi_toolkit.autofix import autofix_model
55
79
 
56
- from .metadata import load_model
57
- model = load_model(model_path)
80
+ model = _model(model_path)
58
81
  result = autofix_model(model)
59
82
  for c in result["changes"]:
60
83
  print(f"fixed: {c}")
@@ -64,9 +87,12 @@ def cmd_fix(model_path: str, dry_run: bool = False) -> int:
64
87
 
65
88
 
66
89
  def cmd_report(model_path: str, out_path: str) -> int:
67
- from aibi_toolkit.metadata import load_model
68
90
  from aibi_toolkit.report import write_report
69
- write_report(load_model(model_path), out_path)
91
+ try:
92
+ write_report(_model(model_path), out_path)
93
+ except OSError as exc:
94
+ print(f"aibi: cannot write {out_path}: {exc.strerror or exc}", file=sys.stderr)
95
+ return 2
70
96
  print(f"report written to {out_path}")
71
97
  return 0
72
98
 
@@ -77,7 +103,11 @@ def cmd_synth(out_path: str, tables: int = 3) -> int:
77
103
  from aibi_toolkit.sample import sample_model
78
104
  model = sample_model(tables=tables)
79
105
  from pathlib import Path
80
- Path(out_path).write_text(json.dumps(model, indent=2), encoding="utf-8")
106
+ try:
107
+ Path(out_path).write_text(json.dumps(model, indent=2), encoding="utf-8")
108
+ except OSError as exc:
109
+ print(f"aibi: cannot write {out_path}: {exc.strerror or exc}", file=sys.stderr)
110
+ return 2
81
111
  print(f"sample model written to {out_path}")
82
112
  return 0
83
113
 
@@ -85,9 +115,8 @@ def cmd_synth(out_path: str, tables: int = 3) -> int:
85
115
  def cmd_profile(model_path: str, json_out: bool = False) -> int:
86
116
  import json
87
117
 
88
- from aibi_toolkit.metadata import load_model
89
118
  from aibi_toolkit.profile import profile_model
90
- p = profile_model(load_model(model_path))
119
+ p = profile_model(_model(model_path))
91
120
  if json_out:
92
121
  print(json.dumps(p, indent=2))
93
122
  return 0
@@ -21,6 +21,12 @@ def lint_dax(expression: str) -> list[str]:
21
21
  findings.append(reason)
22
22
  if re.search(r"(?i)\bIFERROR\b", expression):
23
23
  findings.append("IFERROR hides real errors; handle specific cases")
24
+ if re.search(r"(?i)\bCOUNTROWS\s*\(\s*VALUES\s*\(", expression):
25
+ findings.append("COUNTROWS(VALUES(x)) is DISTINCTCOUNT(x) — shorter and faster")
26
+ if re.search(r"(?i)\bIF\s*\(\s*TRUE\s*\(\s*\)\s*,", expression):
27
+ findings.append("IF(TRUE(), x, y) is always x — the condition never branches")
28
+ if re.search(r"(?i)\bIF\s*\(\s*FALSE\s*\(\s*\)\s*,", expression):
29
+ findings.append("IF(FALSE(), x, y) is always y — the condition never branches")
24
30
  return findings
25
31
 
26
32
  def lint_model(model: Model) -> dict[str, list[str]]:
@@ -119,8 +119,6 @@ def validate_format(fmt: str) -> list[str]:
119
119
  issues.append("unbalanced braces")
120
120
  if "%" in fmt and "$" in fmt:
121
121
  issues.append("mixes percent and currency tokens")
122
- if fmt and fmt[0].isdigit():
123
- issues.append("should not start with a digit")
124
122
  return issues
125
123
 
126
124
 
@@ -1,4 +1,4 @@
1
- """Load and inspect semantic-model metadata dumps (TMDL-exported JSON)."""
1
+ """Load and inspect semantic-model metadata dumps (TMDL-exported JSON or TMDL folders)."""
2
2
 
3
3
  from __future__ import annotations
4
4
 
@@ -12,8 +12,17 @@ Table = dict[str, Any]
12
12
 
13
13
 
14
14
  def load_model(path: str) -> Model:
15
- """Load a model JSON dump from disk."""
16
- data = json.loads(Path(path).read_text(encoding="utf-8"))
15
+ """Load a model from disk.
16
+
17
+ Accepts either a `Model` JSON dump or a `.pbip`-style TMDL directory
18
+ (root, `*.Dataset`, or the `definition/` folder). Both produce the same
19
+ shared dict, so every command works on either.
20
+ """
21
+ p = Path(path)
22
+ if p.is_dir():
23
+ from .tmdl import load_pbip
24
+ return load_pbip(p)
25
+ data = json.loads(p.read_text(encoding="utf-8"))
17
26
  if not isinstance(data, dict):
18
27
  raise TypeError("model dump must be a JSON object")
19
28
  return data
@@ -0,0 +1,253 @@
1
+ """Load a `.pbip`-style TMDL semantic-model folder into the shared model dict.
2
+
3
+ The whole toolkit reads one shape: `Model = {"tables": [...], "relationships": [...]}`.
4
+ JSON dumps and TMDL folders produce exactly that same dict, so every command
5
+ keeps working on either input. Use `metadata.load_model(path)` for either.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+ from pathlib import Path
12
+ from typing import Any
13
+
14
+ Model = dict[str, Any]
15
+
16
+ _TMDL_NAME = re.compile(r"^\s*'((?:[^']|'')*)'|^\s*(\S+)")
17
+
18
+
19
+ def _strip_desc(line: str) -> str | None:
20
+ """Return the description text if this is a `/// ...` line, else None."""
21
+ s = line.strip()
22
+ if s.startswith("///"):
23
+ return s[3:].strip()
24
+ return None
25
+
26
+
27
+ def _parse_measure_name(line: str) -> tuple[str, str] | None:
28
+ """Parse `measure 'Name' = <start-of-expr>` or `measure Name = <start-of-expr>`."""
29
+ m = re.match(r"^(?P<comment>\s*)(?P<kw>measure)\s+(?P<rest>.*)$", line, re.IGNORECASE)
30
+ if not m:
31
+ return None
32
+ rest = m.group("rest")
33
+ if "=" not in rest:
34
+ return None
35
+ head, sep, tail = rest.partition("=")
36
+ if sep != "=":
37
+ return None
38
+ return _tmdl_name(head.strip()), tail.strip()
39
+
40
+
41
+ def _dedent(text: str) -> str:
42
+ lines = text.splitlines()
43
+ stripped = [line for line in lines if line.strip()]
44
+ if not stripped:
45
+ return ""
46
+ margin = min(len(line) - len(line.lstrip(" \t")) for line in stripped)
47
+ return "\n".join(line[margin:] for line in lines).strip("\n")
48
+
49
+
50
+ def _tmdl_name(raw: str) -> str:
51
+ """Unquote a TMDL name and unescape doubled quotes."""
52
+ raw = raw.strip()
53
+ if raw.startswith("'") and raw.endswith("'") and len(raw) >= 2:
54
+ inner = raw[1:-1]
55
+ return inner.replace("''", "'")
56
+ return raw
57
+
58
+
59
+ def _parse_table_tmdl(text: str) -> Model:
60
+ tables: list[dict[str, Any]] = []
61
+ table: dict[str, Any] | None = None
62
+ measure: dict[str, Any] | None = None
63
+ node: dict[str, Any] | None = None # object current properties attach to
64
+ in_measure_block = False
65
+ block_lines: list[str] = []
66
+
67
+ for raw in text.splitlines():
68
+ stripped = raw.rstrip()
69
+ s = stripped.strip()
70
+ if not s or s.startswith("//"):
71
+ continue
72
+ _ = _strip_desc(s) # descriptions are legal but carry no model data
73
+ first, _, rest = s.partition(" ")
74
+
75
+ if in_measure_block:
76
+ assert node is not None
77
+ close = re.match(r"^\s*```\s*$", s)
78
+ if close:
79
+ body = "\n".join(block_lines[1:]).strip("\n")
80
+ node["expression"] = _dedent(body)
81
+ in_measure_block = False
82
+ continue
83
+ block_lines.append(stripped)
84
+ continue
85
+
86
+ if first.lower() == "table":
87
+ table = {"name": _tmdl_name(rest.strip()), "columns": [], "measures": []}
88
+ tables.append(table)
89
+ node = table
90
+ measure = None
91
+ continue
92
+
93
+ if table is None:
94
+ continue
95
+
96
+ if first.lower() == "measure":
97
+ named = _parse_measure_name(stripped)
98
+ if named is None:
99
+ continue
100
+ name, expr_start = named
101
+ measure = {"name": name, "expression": expr_start}
102
+ table["measures"].append(measure)
103
+ node = measure
104
+ if expr_start.strip() == "```":
105
+ in_measure_block = True
106
+ block_lines = ["```"]
107
+ continue
108
+
109
+ if first.lower() == "column":
110
+ col = {"name": _tmdl_name(rest.strip())}
111
+ table["columns"].append(col)
112
+ node = col
113
+ measure = None
114
+ continue
115
+
116
+ if first.lower() in {"partition", "hierarchy", "calendar",
117
+ "calculationGroup", "expression", "annotation"}:
118
+ # accepted but not needed for the toolkit's checks
119
+ node = None
120
+ measure = None
121
+ continue
122
+
123
+ if node is not None and ":" not in s and s.startswith("isHidden"):
124
+ node["isHidden"] = True
125
+ continue
126
+
127
+ if node is not None and ":" in s:
128
+ key, _, value = s.partition(":")
129
+ key, value = key.strip().lower(), value.strip()
130
+ if not key:
131
+ continue
132
+ fields = {
133
+ "datatype": "dataType",
134
+ "sourcecolumn": "sourceColumn",
135
+ "formatstring": "formatString",
136
+ "summarizeby": "summarizeBy",
137
+ "sortbycolumn": "sortByColumn",
138
+ "fromcolumn": "fromColumn",
139
+ "tocolumn": "toColumn",
140
+ "cardinality": "cardinality",
141
+ }
142
+ if key == "isactive":
143
+ node["isActive"] = value.lower() != "false"
144
+ elif key == "crossfilteringbehavior":
145
+ node["crossFilterDirection"] = "both" if value.lower().startswith("both") else "one"
146
+ elif key in fields:
147
+ node[fields[key]] = value
148
+ continue
149
+
150
+ return {"tables": tables}
151
+
152
+
153
+ def _parse_relationship_tmdl(text: str) -> list[dict[str, Any]]:
154
+ rels: list[dict[str, Any]] = []
155
+ node: dict[str, Any] | None = None
156
+
157
+ for raw in text.splitlines():
158
+ s = raw.strip()
159
+ if not s or s.startswith(("///", "//")):
160
+ continue
161
+ first, _, rest = s.partition(" ")
162
+
163
+ if first.lower() == "relationship":
164
+ node = {}
165
+ rels.append(node)
166
+ parsed = _tmdl_name(rest.strip())
167
+ node["name"] = parsed
168
+ node["isActive"] = True
169
+ continue
170
+
171
+ if node is not None and ":" in s:
172
+ key, _, value = s.partition(":")
173
+ key, value = key.strip(), value.strip()
174
+ lowered = key.lower()
175
+ if lowered == "fromcolumn":
176
+ node["fromColumn"] = value
177
+ elif lowered == "tocolumn":
178
+ node["toColumn"] = value
179
+ elif lowered == "isactive":
180
+ node["isActive"] = value.lower() != "false"
181
+ elif lowered == "crossfilteringbehavior":
182
+ node["crossFilterDirection"] = "both" if value.lower().startswith("both") else "one"
183
+ elif lowered == "cardinality":
184
+ node["cardinality"] = value
185
+ continue
186
+
187
+ out: list[dict[str, Any]] = []
188
+ for r in rels:
189
+ from_col = r.get("fromColumn", "")
190
+ to_col = r.get("toColumn", "")
191
+ from_table, _, _ = from_col.partition(".")
192
+ to_table, _, _ = to_col.partition(".")
193
+ out.append(
194
+ {
195
+ "fromTable": _tmdl_name(from_table or "?"),
196
+ "toTable": _tmdl_name(to_table or "?"),
197
+ "fromColumn": _tmdl_name(from_col.partition(".")[2].strip() if "." in from_col else from_col),
198
+ "toColumn": _tmdl_name(to_col.partition(".")[2].strip() if "." in to_col else to_col),
199
+ "isActive": r.get("isActive", True),
200
+ "crossFilterDirection": r.get("crossFilterDirection", "one"),
201
+ "cardinality": r.get("cardinality"),
202
+ }
203
+ )
204
+ return out
205
+
206
+
207
+ def _find_definition(path: Path) -> Path | None:
208
+ """Locate the `definition/` folder for a .pbip project root, dataset or definition dir."""
209
+ if (path / "tables").is_dir():
210
+ return path
211
+ direct = path / "definition"
212
+ if (direct / "tables").is_dir():
213
+ return direct
214
+ for folder in path.iterdir() if path.is_dir() else []:
215
+ if folder.is_dir():
216
+ nested = folder / "definition"
217
+ if (nested / "tables").is_dir():
218
+ return nested
219
+ if path.is_dir():
220
+ deep = list(path.rglob("definition/tables"))
221
+ if deep:
222
+ return deep[0].parent
223
+ return None
224
+
225
+
226
+ def load_pbip(path: str | Path) -> Model:
227
+ """Load a TMDL folder into the shared model dict; raises ValueError if not found.
228
+
229
+ Args accept a .pbip project root, a `*.Dataset` folder, or a direct
230
+ `definition/` directory. Tables in `definition/tables/*.tmdl` and
231
+ relationships in `definition/relationships.tmdl` are parsed; anything
232
+ else is ignored.
233
+ """
234
+ root = Path(path)
235
+ if not root.is_dir():
236
+ raise ValueError(f"path is not a directory: {root}")
237
+
238
+ definition = _find_definition(root)
239
+ if definition is None:
240
+ raise ValueError(
241
+ f"no TMDL tables found under {root} — expected definition/tables/*.tmdl"
242
+ )
243
+
244
+ tables: list[dict[str, Any]] = []
245
+ for tmdl in sorted((definition / "tables").glob("*.tmdl")):
246
+ tables.extend(_parse_table_tmdl(tmdl.read_text(encoding="utf-8"))["tables"])
247
+
248
+ relationships: list[dict[str, Any]] = []
249
+ rels = definition / "relationships.tmdl"
250
+ if rels.is_file():
251
+ relationships.extend(_parse_relationship_tmdl(rels.read_text(encoding="utf-8")))
252
+
253
+ return {"tables": tables, "relationships": relationships}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aibi-toolkit
3
- Version: 1.2.1
3
+ Version: 1.3.0
4
4
  Summary: Clean up Power BI / Tableau semantic-model metadata: naming, format strings, DAX snippets, hygiene auditing, linting, diagrams and reports
5
5
  Author: Sourin Rakshit
6
6
  License: MIT License
@@ -104,6 +104,7 @@ Full module map:
104
104
 
105
105
  | Area | Module |
106
106
  | ------------------ | --------------- |
107
+ | TMDL/PBIP loading | `tmdl` |
107
108
  | Naming | `naming` |
108
109
  | Format strings | `measure` |
109
110
  | DAX snippets | `dax`, `filters`|
@@ -119,6 +120,18 @@ Full module map:
119
120
  | Profiling | `profile` |
120
121
  | CSV round-trip | `csvio` |
121
122
 
123
+ ## Input formats
124
+
125
+ `aibi summary path` — `path` may be either:
126
+
127
+ - a shared-schema JSON dump (`model.json` from your model tool), or
128
+ - a TMDL folder (a `.pbip` project root, a `*.Dataset` folder, or the
129
+ `definition/` directory itself). Tables are read from
130
+ `definition/tables/*.tmdl`, relationships from `definition/relationships.tmdl`;
131
+ every command works unchanged on either shape.
132
+
133
+ Full details and layout diagram: [docs/input.md](docs/input.md).
134
+
122
135
  ## Install
123
136
 
124
137
  Install from PyPI and check the binary works:
@@ -146,7 +159,9 @@ aibi fix --dry-run model.json # preview fixes before writing anything
146
159
 
147
160
  Audit returns exit `1` when findings exist, `0` when clean, so a pipeline
148
161
  that already has `set -e` fails immediately. Pass `--json` to `audit`,
149
- `lint` or `profile` when you want a machine-readable result.
162
+ `lint` or `profile` when you want a machine-readable result. Any loader
163
+ failure (missing path, not valid JSON, wrong top-level shape) exits `2`
164
+ with a one-line reason on stderr — never a traceback.
150
165
 
151
166
  ## How it helps
152
167
 
@@ -171,9 +186,9 @@ that already has `set -e` fails immediately. Pass `--json` to `audit`,
171
186
  hiding). For deep complexity scoring and anti-pattern detection,
172
187
  that is what [aibi-dax](https://github.com/SourinRakshit-ai/aibi-dax)
173
188
  is for.
174
- - **Not a full TMDL/PBIP authoring tool.** It reads shared-schema
175
- model dumps; it does not write them back to disk yet
176
- (tracked in [#6](https://github.com/SourinRakshit-ai/aibi-toolkit/issues/6)).
189
+ - **Not a TMDL/PBIP authoring tool.** It reads TMDL folders and
190
+ JSON dumps, but does not write TMDL back to disk; editing stays in
191
+ your model tool.
177
192
  - **Not a bug finder for DAX logic.** It checks static patterns; it
178
193
  cannot tell you your total is wrong. That still needs `EVALUATE`
179
194
  checks and your eyes.
@@ -22,6 +22,7 @@ src/aibi_toolkit/py.typed
22
22
  src/aibi_toolkit/report.py
23
23
  src/aibi_toolkit/sample.py
24
24
  src/aibi_toolkit/tables.py
25
+ src/aibi_toolkit/tmdl.py
25
26
  src/aibi_toolkit.egg-info/PKG-INFO
26
27
  src/aibi_toolkit.egg-info/SOURCES.txt
27
28
  src/aibi_toolkit.egg-info/dependency_links.txt
@@ -45,4 +46,5 @@ tests/test_profile.py
45
46
  tests/test_readme.py
46
47
  tests/test_report.py
47
48
  tests/test_sample.py
48
- tests/test_tables.py
49
+ tests/test_tables.py
50
+ tests/test_tmdl.py
@@ -0,0 +1,35 @@
1
+ from aibi_toolkit.lint import lint_dax
2
+
3
+
4
+ def test_divide_warning():
5
+ assert any("DIVIDE" in f for f in lint_dax("ASP = [Sales] / [Units]"))
6
+
7
+
8
+ def test_iferror():
9
+ assert any("IFERROR" in f for f in lint_dax("X = IFERROR([A]/[B], 0)"))
10
+
11
+
12
+ def test_clean():
13
+ assert lint_dax("ASP = DIVIDE([Sales], [Units], 0)") == []
14
+
15
+
16
+ def test_countrows_values_lint():
17
+ flag = lint_dax
18
+ assert any("DISTINCTCOUNT" in f for f in flag("COUNTROWS(VALUES('Sales'[Region])) + 1"))
19
+ assert any("DISTINCTCOUNT" in f for f in flag("COUNTROWS( VALUES([Region]) )"))
20
+ assert flag("COUNTROWS(Sales)") == []
21
+ assert flag("RANGE(VALUES([Region]))") == []
22
+
23
+
24
+ def test_if_true_false_branch_lint():
25
+ flag = lint_dax
26
+ assert any("always x" in f for f in flag("IF(TRUE(), 1, 2)"))
27
+ assert any("always y" in f for f in flag("IF(FALSE(), 1, 2)"))
28
+ assert flag("IF([cond], 1, 2)") == []
29
+ assert flag("IF([active], [a], [b])") == []
30
+
31
+
32
+ def test_if_true_case_insensitive():
33
+ flag = lint_dax
34
+ assert any("always x" in f for f in flag("IF(true(), 1, 2)"))
35
+ assert any("always y" in f for f in flag("IF(FALSE(), 1, 2)"))
@@ -46,6 +46,18 @@ def test_validate_format():
46
46
  assert validate_format("#,0") == []
47
47
 
48
48
 
49
+ def test_validate_format_percent_formats_pass():
50
+ from aibi_toolkit.measure import validate_format
51
+ assert validate_format("0.00%") == []
52
+ assert validate_format("0.0%") == []
53
+
54
+
55
+ def test_validate_format_unbalanced_braces():
56
+ from aibi_toolkit.measure import validate_format
57
+ assert validate_format("{") == ["unbalanced braces"]
58
+ assert validate_format("a}b") == ["unbalanced braces"]
59
+
60
+
49
61
  def test_scale():
50
62
  from aibi_toolkit.measure import scale_number
51
63
  assert scale_number(1.005) == 1.0
@@ -0,0 +1,118 @@
1
+ """Tests for the TMDL loader: a .pbip folder feeds into the same model dict."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from pathlib import Path
7
+
8
+ from aibi_toolkit.metadata import list_measures, list_tables, load_model, model_summary
9
+ from aibi_toolkit.tmdl import load_pbip
10
+
11
+ FIXTURE = Path(__file__).parent / "fixtures" / "pbip"
12
+
13
+
14
+ def test_load_pbip_reads_tables_measures_columns():
15
+ model = load_pbip(FIXTURE)
16
+ by_name = {t["name"]: t for t in model["tables"]}
17
+ assert set(by_name) == {"Sales", "Customer", "Date"}
18
+
19
+ sales = by_name["Sales"]
20
+ measures = {m["name"]: m for m in sales["measures"]}
21
+ assert measures["Total Sales"]["formatString"] == "$ #,##0.00"
22
+ assert "SUMX" in measures["Total Sales"]["expression"]
23
+ assert measures["No Format Measure"].get("formatString") is None
24
+ assert len(sales["columns"]) == 4
25
+
26
+ customer = by_name["Customer"]
27
+ assert [m["name"] for m in customer["measures"]] == ["# Customers"]
28
+
29
+ date = by_name["Date"]
30
+ assert date["measures"] == []
31
+ assert len(date["columns"]) == 1
32
+
33
+
34
+ def test_load_pbip_reads_relationships_into_shared_schema():
35
+ model = load_pbip(FIXTURE)
36
+ by_tab = {(r["fromTable"], r["toTable"]): r for r in model["relationships"]}
37
+ assert by_tab[("Sales", "Date")]["fromColumn"] == "OrderDate"
38
+ assert by_tab[("Sales", "Date")]["isActive"] is True
39
+ assert by_tab[("Sales", "Customer")]["isActive"] is False
40
+
41
+
42
+ def test_summary_and_list_work_on_tmdl():
43
+ model = load_model(str(FIXTURE))
44
+ counts = model_summary(model)
45
+ assert counts["tables"] == 3
46
+ assert counts["measures"] == 4
47
+ assert counts["columns"] == 7
48
+ assert set(list_tables(model)) == {"Sales", "Customer", "Date"}
49
+ names = {(m["table"], m["name"]) for m in list_measures(model)}
50
+ assert ("Sales", "Total Sales") in names
51
+ assert ("Customer", "# Customers") in names
52
+
53
+
54
+ def test_load_model_dispatches_directory_to_tmdl(tmp_path):
55
+ # a directory is walked for a TMDL definition/, not read as JSON
56
+ json_dir = tmp_path / "fixture.json"
57
+ json_dir.write_text(json.dumps({"tables": [{"name": "JSONTable", "columns": [], "measures": []}]}))
58
+ loaded_json = load_model(str(json_dir))
59
+ assert loaded_json["tables"][0]["name"] == "JSONTable"
60
+
61
+ loaded_tmdl = load_model(str(FIXTURE))
62
+ assert {t["name"] for t in loaded_tmdl["tables"]} == {"Sales", "Customer", "Date"}
63
+
64
+
65
+ def test_load_model_directory_without_definition_is_a_clear_error(tmp_path):
66
+ try:
67
+ load_pbip(tmp_path)
68
+ except ValueError as exc:
69
+ assert "no TMDL tables" in str(exc)
70
+ else:
71
+ raise AssertionError("expected ValueError")
72
+
73
+
74
+ def test_definition_folder_directly_is_accepted():
75
+ direct = FIXTURE / "definition"
76
+ model = load_pbip(direct)
77
+ assert {t["name"] for t in model["tables"]} == {"Sales", "Customer", "Date"}
78
+
79
+
80
+ def test_summed_measure_expression_survives_roundtrip():
81
+ measures = {m["name"]: m for m in load_pbip(FIXTURE)["tables"][2]["measures"]}
82
+ assert measures["Total Sales"]["expression"] == "SUMX(\n\tSales,\n\tSales[Quantity]\n)"
83
+
84
+
85
+ def test_cli_path_loads_tmdl_folder():
86
+ from aibi_toolkit.cli import main
87
+
88
+ rc = main(["summary", str(FIXTURE)])
89
+ assert rc == 0
90
+
91
+
92
+ def test_cli_audit_subcommand_on_tmdl_fixture():
93
+ from aibi_toolkit.cli import main
94
+
95
+ rc = main(["audit", str(FIXTURE)])
96
+ assert rc in (0, 1)
97
+
98
+
99
+ def test_summary_and_tables_json_output(capsys, tmp_path):
100
+ """`--json` on summary/tables emits stable machine-readable payloads."""
101
+ from aibi_toolkit.cli import main
102
+
103
+ model = tmp_path / "model.json"
104
+ model.write_text(
105
+ '{"tables": ['
106
+ '{"name": "Customer", "columns": [{"name": "id"}], "measures": []},'
107
+ '{"name": "Sales", "columns": [{"name": "id"}, {"name": "qty"}], "measures": [{"name": "m1"}]}'
108
+ ']}',
109
+ encoding="utf-8",
110
+ )
111
+ assert main(["summary", str(model), "--json"]) == 0
112
+ import json as _json
113
+
114
+ out = _json.loads(capsys.readouterr().out.strip())
115
+ assert out == {"tables": 2, "measures": 1, "columns": 3}
116
+
117
+ assert main(["tables", str(model), "--json"]) == 0
118
+ assert _json.loads(capsys.readouterr().out.strip()) == ["Customer", "Sales"]
@@ -1,13 +0,0 @@
1
- from aibi_toolkit.lint import lint_dax
2
-
3
-
4
- def test_divide_warning():
5
- assert any("DIVIDE" in f for f in lint_dax("ASP = [Sales] / [Units]"))
6
-
7
-
8
- def test_iferror():
9
- assert any("IFERROR" in f for f in lint_dax("X = IFERROR([A]/[B], 0)"))
10
-
11
-
12
- def test_clean():
13
- assert lint_dax("ASP = DIVIDE([Sales], [Units], 0)") == []
File without changes
File without changes