pypbip 0.1.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.
pypbip-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pedro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
pypbip-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,60 @@
1
+ Metadata-Version: 2.4
2
+ Name: pypbip
3
+ Version: 0.1.0
4
+ Summary: Python library for handling Power BI semantic model, report, and diagram artifacts
5
+ Author: Pedro
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://pypi.org/project/pypbip/
8
+ Keywords: powerbi,semantic-model,tabular,report
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3 :: Only
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Software Development :: Libraries
18
+ Requires-Python: >=3.9
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Dynamic: license-file
22
+
23
+ # pypbip
24
+
25
+ `pypbip` is a Python library for handling Power BI project artifacts in code, including semantic model (`model.bim`), report metadata, and diagram layout structures.
26
+
27
+ ## Features
28
+
29
+ - Work with semantic model objects such as tables, columns, measures, and DAX expressions.
30
+ - Parse and write report sections and visual container metadata.
31
+ - Handle diagram layouts and nodes programmatically.
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ pip install pypbip
37
+ ```
38
+
39
+ ## Quick Start
40
+
41
+ ```python
42
+ from pypbip import ModelBim, Report, DiagramLayout
43
+
44
+ # load a model.bim structure
45
+ model = ModelBim.from_file("path/to/model.bim")
46
+
47
+ # load report metadata
48
+ report = Report.from_file("path/to/report.json")
49
+
50
+ # load diagram layout metadata
51
+ layout = DiagramLayout.from_file("path/to/diagramLayout.json")
52
+ ```
53
+
54
+ ## Compatibility
55
+
56
+ - Python 3.9+
57
+
58
+ ## License
59
+
60
+ MIT
pypbip-0.1.0/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # pypbip
2
+
3
+ `pypbip` is a Python library for handling Power BI project artifacts in code, including semantic model (`model.bim`), report metadata, and diagram layout structures.
4
+
5
+ ## Features
6
+
7
+ - Work with semantic model objects such as tables, columns, measures, and DAX expressions.
8
+ - Parse and write report sections and visual container metadata.
9
+ - Handle diagram layouts and nodes programmatically.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ pip install pypbip
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ ```python
20
+ from pypbip import ModelBim, Report, DiagramLayout
21
+
22
+ # load a model.bim structure
23
+ model = ModelBim.from_file("path/to/model.bim")
24
+
25
+ # load report metadata
26
+ report = Report.from_file("path/to/report.json")
27
+
28
+ # load diagram layout metadata
29
+ layout = DiagramLayout.from_file("path/to/diagramLayout.json")
30
+ ```
31
+
32
+ ## Compatibility
33
+
34
+ - Python 3.9+
35
+
36
+ ## License
37
+
38
+ MIT
@@ -0,0 +1,37 @@
1
+ [build-system]
2
+ requires = ["setuptools>=69", "wheel", "build"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "pypbip"
7
+ version = "0.1.0"
8
+ description = "Python library for handling Power BI semantic model, report, and diagram artifacts"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = "MIT"
12
+ authors = [
13
+ { name = "Pedro" }
14
+ ]
15
+ keywords = ["powerbi", "semantic-model", "tabular", "report"]
16
+ classifiers = [
17
+ "Development Status :: 3 - Alpha",
18
+ "Intended Audience :: Developers",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3 :: Only",
21
+ "Programming Language :: Python :: 3.9",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Topic :: Software Development :: Libraries"
26
+ ]
27
+ dependencies = []
28
+
29
+ [project.urls]
30
+ Homepage = "https://pypi.org/project/pypbip/"
31
+
32
+ [tool.setuptools]
33
+ package-dir = {"" = "src"}
34
+ include-package-data = false
35
+
36
+ [tool.setuptools.packages.find]
37
+ where = ["src"]
pypbip-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,21 @@
1
+ from .model_bim import ModelBim, SemanticModel, Table, DaxExpression, Column, Measure
2
+ from .report import Report, Section, VisualContainer, ResourcePackage
3
+ from .diagram import DiagramLayout, Diagram, DiagramNode
4
+
5
+ __all__ = [
6
+ "ModelBim",
7
+ "SemanticModel",
8
+ "Table",
9
+ "DaxExpression",
10
+ "Column",
11
+ "Measure",
12
+ "Report",
13
+ "Section",
14
+ "VisualContainer",
15
+ "ResourcePackage",
16
+ "DiagramLayout",
17
+ "Diagram",
18
+ "DiagramNode",
19
+ ]
20
+
21
+
@@ -0,0 +1,9 @@
1
+ from .diagram_layout import DiagramLayout
2
+ from .diagram import Diagram
3
+ from .diagram_node import DiagramNode
4
+
5
+ __all__ = [
6
+ "DiagramLayout",
7
+ "Diagram",
8
+ "DiagramNode",
9
+ ]
@@ -0,0 +1,68 @@
1
+ from typing import Dict, List
2
+
3
+ from .diagram_node import DiagramNode
4
+ from ..json_util import dump_json
5
+
6
+
7
+ class Diagram:
8
+ def __init__(
9
+ self,
10
+ name: str,
11
+ ordinal: int,
12
+ nodes: List[DiagramNode],
13
+ scroll_position: Dict[str, float],
14
+ zoom_value: float,
15
+ pin_key_fields_to_top: bool,
16
+ show_extra_header_info: bool,
17
+ hide_key_fields_when_collapsed: bool,
18
+ tables_locked: bool,
19
+ ):
20
+ self.name = name
21
+ self.ordinal = ordinal
22
+ self.nodes = nodes
23
+ self.scroll_position = scroll_position
24
+ self.zoom_value = zoom_value
25
+ self.pin_key_fields_to_top = pin_key_fields_to_top
26
+ self.show_extra_header_info = show_extra_header_info
27
+ self.hide_key_fields_when_collapsed = hide_key_fields_when_collapsed
28
+ self.tables_locked = tables_locked
29
+ self.node_list = [n.name for n in self.nodes]
30
+ self.summary = {
31
+ "nodes": self.node_list,
32
+ }
33
+
34
+ @classmethod
35
+ def from_dict(cls, data: dict) -> "Diagram":
36
+ return cls(
37
+ name=data.get("name"),
38
+ ordinal=data.get("ordinal"),
39
+ nodes=[DiagramNode.from_dict(n) for n in data.get("nodes")],
40
+ scroll_position=data.get("scrollPosition"),
41
+ zoom_value=data.get("zoomValue"),
42
+ pin_key_fields_to_top=data.get("pinKeyFieldsToTop"),
43
+ show_extra_header_info=data.get("showExtraHeaderInfo"),
44
+ hide_key_fields_when_collapsed=data.get("hideKeyFieldsWhenCollapsed"),
45
+ tables_locked=data.get("tablesLocked"),
46
+ )
47
+
48
+ def to_dict(self) -> dict:
49
+ return {
50
+ "ordinal": self.ordinal,
51
+ "scrollPosition": self.scroll_position,
52
+ "nodes": [n.to_dict() for n in self.nodes],
53
+ "name": self.name,
54
+ "zoomValue": self.zoom_value,
55
+ "pinKeyFieldsToTop": self.pin_key_fields_to_top,
56
+ "showExtraHeaderInfo": self.show_extra_header_info,
57
+ "hideKeyFieldsWhenCollapsed": self.hide_key_fields_when_collapsed,
58
+ "tablesLocked": self.tables_locked,
59
+ }
60
+
61
+ def to_json(self, indent: int = 2) -> str:
62
+ return dump_json(self.to_dict(), indent=indent)
63
+
64
+
65
+ if __name__ == "__main__":
66
+ diagram_dict = {"name": "ExampleDiagram", "ordinal": 0, "nodes": []}
67
+ diagram = Diagram.from_dict(diagram_dict)
68
+ print(diagram.to_json())
@@ -0,0 +1,115 @@
1
+ import contextlib
2
+ from ..json_util import read_json_file
3
+ from typing import List, Dict
4
+ from .diagram import Diagram
5
+ from .diagram_node import DiagramNode
6
+ from ..report import Section
7
+ from ..model_bim import ModelBim, Table
8
+
9
+
10
+ class DiagramLayout:
11
+ def __init__(
12
+ self,
13
+ version: str,
14
+ diagrams: List[Diagram],
15
+ selectedDiagram: str,
16
+ defaultDiagram: str,
17
+ ):
18
+ self.version = version
19
+ self.diagrams = diagrams
20
+ self.selectedDiagram = selectedDiagram
21
+ self.defaultDiagram = defaultDiagram
22
+
23
+ @classmethod
24
+ def from_dict(cls, data: dict) -> "DiagramLayout":
25
+ return cls(
26
+ version=data.get("version"),
27
+ diagrams=[Diagram.from_dict(d) for d in data.get("diagrams", [])],
28
+ selectedDiagram=data.get("selectedDiagram"),
29
+ defaultDiagram=data.get("defaultDiagram"),
30
+ )
31
+
32
+ def to_dict(self) -> dict:
33
+ return {
34
+ "version": self.version,
35
+ "diagrams": [d.to_dict() for d in self.diagrams],
36
+ "selectedDiagram": self.selectedDiagram,
37
+ "defaultDiagram": self.defaultDiagram,
38
+ }
39
+
40
+ @classmethod
41
+ def from_file(cls, file_path: str) -> "DiagramLayout":
42
+ data = read_json_file(file_path)
43
+ return cls.from_dict(data)
44
+
45
+ def create_nodes(self, tables: List[str], mapping: Dict[str, Table]) -> List[DiagramNode]:
46
+ nodes = []
47
+ width = 300
48
+ height = 300
49
+ for idx, table in enumerate(tables):
50
+ with contextlib.suppress(Exception):
51
+ node_dict = {
52
+ "location": {
53
+ "x": 0 + idx * (width + 30),
54
+ "y": 50 + 0 * height,
55
+ },
56
+ "nodeIndex": table,
57
+ "nodeLineageTag": mapping[table].lineageTag,
58
+ "size": {
59
+ "height": height,
60
+ "width": width
61
+ },
62
+ "zIndex": 0
63
+ }
64
+ node = DiagramNode.from_dict(node_dict)
65
+ nodes.append(node)
66
+ return nodes
67
+
68
+ def create_diagram(self, nodes: List[DiagramNode], name: str) -> Diagram:
69
+ diagram_dict = {
70
+ "ordinal": 99,
71
+ "scrollPosition": {
72
+ "x": 0,
73
+ "y": 0
74
+ },
75
+ "nodes": [node.to_dict() for node in nodes],
76
+ "name": f"@ {name}",
77
+ "zoomValue": 100,
78
+ "pinKeyFieldsToTop": True,
79
+ "showExtraHeaderInfo": False,
80
+ "hideKeyFieldsWhenCollapsed": False,
81
+ "tablesLocked": False
82
+ }
83
+ return Diagram.from_dict(diagram_dict)
84
+
85
+ def delete_existing_diagrams_with_section_names(self, section_names):
86
+ self.diagrams = [
87
+ diagram
88
+ for diagram in self.diagrams
89
+ if not diagram.name.startswith("@")
90
+ ]
91
+
92
+ def add_sections_diagrams(self, sections: List[Section], dataset: ModelBim):
93
+ section_names = []
94
+ sections_diagrams = []
95
+ for section in sections:
96
+ tables_from_fields = []
97
+ used_measures = [f.split(".")[-1] for f in section.used_fields if f.split(".")[-1] in dataset.model.measures.keys()]
98
+ for measure in used_measures:
99
+ field_used_tables = dataset.model.measures[measure].used_tables
100
+ tables_from_fields.extend(field_used_tables)
101
+ used_tables = section.used_tables + tables_from_fields
102
+ used_tables = list(set(used_tables))
103
+ nodes = self.create_nodes(used_tables, dataset.model.tables)
104
+ diagram = self.create_diagram(nodes, section.displayName)
105
+ sections_diagrams.append(diagram)
106
+ section_names.append(section.displayName)
107
+
108
+ self.delete_existing_diagrams_with_section_names(section_names)
109
+ self.diagrams.extend(sections_diagrams)
110
+
111
+
112
+ if __name__ == "__main__":
113
+ diagramLayout_path = "BaseDataSet.Dataset/diagramLayout.json"
114
+ dl = DiagramLayout.from_file(diagramLayout_path)
115
+ print(dl.to_dict())
@@ -0,0 +1,51 @@
1
+ from typing import Optional, Dict
2
+
3
+
4
+ class DiagramNode:
5
+ def __init__(
6
+ self,
7
+ name: str,
8
+ location: Dict[str, float],
9
+ node_lineage_tag: str,
10
+ size: Dict[str, float],
11
+ z_index: int,
12
+ expanded_height: Optional[float] = None,
13
+ ):
14
+ self.name = name
15
+ self.location = location
16
+ self.node_lineage_tag = node_lineage_tag
17
+ self.size = size
18
+ self.z_index = z_index
19
+ self.expanded_height = expanded_height
20
+
21
+ @classmethod
22
+ def from_dict(cls, data: dict) -> "DiagramNode":
23
+ return cls(
24
+ name=data.get("nodeIndex"),
25
+ location=data.get("location"),
26
+ node_lineage_tag=data.get("nodeLineageTag"),
27
+ size=data.get("size"),
28
+ z_index=data.get("zIndex"),
29
+ expanded_height=data.get("expandedHeight"),
30
+ )
31
+
32
+ def to_dict(self) -> dict:
33
+ base_dict = {
34
+ "location": self.location,
35
+ "nodeIndex": self.name,
36
+ "nodeLineageTag": self.node_lineage_tag,
37
+ "size": self.size,
38
+ "zIndex": self.z_index,
39
+ }
40
+ if self.expanded_height is not None:
41
+ base_dict["expandedHeight"] = self.expanded_height
42
+
43
+ for k in list(base_dict.keys()):
44
+ if base_dict[k] in (None, "", {}):
45
+ del base_dict[k]
46
+ return base_dict
47
+
48
+
49
+ if __name__ == "__main__":
50
+ node = DiagramNode.from_dict({"nodeIndex": "ExampleNode", "location": {"x": 0, "y": 0}, "nodeLineageTag": "example-tag", "size": {"height": 100, "width": 200}, "zIndex": 0})
51
+ print(node.to_json())
@@ -0,0 +1,24 @@
1
+ import json
2
+
3
+
4
+ def write_json_file(filepath, content):
5
+ with open(filepath, "w", encoding="utf-8", newline="\n") as output_file:
6
+ json.dump(content, output_file, indent=2, ensure_ascii=False)
7
+
8
+
9
+ def read_json_file(file_path) -> dict:
10
+ with open(file_path, "r", encoding="utf-8") as f:
11
+ return json.load(f)
12
+
13
+
14
+ def dump_json(obj, indent=2) -> str:
15
+ return json.dumps(obj, indent=indent, ensure_ascii=False)
16
+
17
+
18
+ def dump_json_inline(obj) -> str:
19
+ return json.dumps(obj)
20
+
21
+
22
+ def parse_json(s: str):
23
+ return json.loads(s)
24
+
@@ -0,0 +1,17 @@
1
+ from .model_bim import ModelBim
2
+ from .semantic_model import SemanticModel
3
+ from .table import Table
4
+ from .dax_expression import DaxExpression
5
+ from .column import Column
6
+ from .measures import Measure
7
+
8
+ __all__ = [
9
+ "ModelBim",
10
+ "SemanticModel",
11
+ "Table",
12
+ "DaxExpression",
13
+ "Column",
14
+ "Measure",
15
+ ]
16
+
17
+
@@ -0,0 +1,104 @@
1
+ from ..json_util import read_json_file, dump_json
2
+ from .dax_expression import DaxExpression
3
+
4
+
5
+ class Column:
6
+ def __init__(
7
+ self,
8
+ sortByColumn,
9
+ expression,
10
+ isDataTypeInferred,
11
+ extendedProperties,
12
+ dataType,
13
+ description,
14
+ summarizeBy,
15
+ type,
16
+ isHidden,
17
+ name,
18
+ formatString,
19
+ changedProperties,
20
+ isNameInferred,
21
+ relatedColumnDetails,
22
+ sourceColumn,
23
+ lineageTag,
24
+ displayFolder,
25
+ annotations,
26
+ ):
27
+ self.sortByColumn = sortByColumn
28
+ self.expression = DaxExpression(expression)
29
+ self.isDataTypeInferred = isDataTypeInferred
30
+ self.extendedProperties = extendedProperties
31
+ self.dataType = dataType
32
+ self.description = description
33
+ self.summarizeBy = summarizeBy
34
+ self.type = type
35
+ self.isHidden = isHidden
36
+ self.name = name
37
+ self.formatString = formatString
38
+ self.changedProperties = changedProperties
39
+ self.isNameInferred = isNameInferred
40
+ self.relatedColumnDetails = relatedColumnDetails
41
+ self.sourceColumn = sourceColumn
42
+ self.lineageTag = lineageTag
43
+ self.displayFolder = displayFolder
44
+ self.annotations = annotations
45
+
46
+ @classmethod
47
+ def from_file(cls, file_path: str) -> "Column":
48
+ data: dict = read_json_file(file_path)
49
+ return Column.from_dict(data)
50
+
51
+ @classmethod
52
+ def from_dict(cls, data: dict) -> "Column":
53
+ return cls(
54
+ sortByColumn=data.get("sortByColumn"),
55
+ expression=data.get("expression"),
56
+ isDataTypeInferred=data.get("isDataTypeInferred"),
57
+ extendedProperties=data.get("extendedProperties"),
58
+ dataType=data.get("dataType"),
59
+ description=data.get("description"),
60
+ summarizeBy=data.get("summarizeBy"),
61
+ type=data.get("type"),
62
+ isHidden=data.get("isHidden"),
63
+ name=data.get("name"),
64
+ formatString=data.get("formatString"),
65
+ changedProperties=data.get("changedProperties"),
66
+ isNameInferred=data.get("isNameInferred"),
67
+ relatedColumnDetails=data.get("relatedColumnDetails"),
68
+ sourceColumn=data.get("sourceColumn"),
69
+ lineageTag=data.get("lineageTag"),
70
+ displayFolder=data.get("displayFolder"),
71
+ annotations=data.get("annotations"),
72
+ )
73
+
74
+ def to_dict(self) -> dict:
75
+ base_dict = {
76
+ "name": self.name,
77
+ "annotations": self.annotations,
78
+ "changedProperties": self.changedProperties,
79
+ "dataType": self.dataType,
80
+ "description": self.description,
81
+ "displayFolder": self.displayFolder,
82
+ "expression": self.expression.to_dict(),
83
+ "extendedProperties": self.extendedProperties,
84
+ "formatString": self.formatString,
85
+ "isDataTypeInferred": self.isDataTypeInferred,
86
+ "isHidden": self.isHidden,
87
+ "isNameInferred": self.isNameInferred,
88
+ "lineageTag": self.lineageTag,
89
+ "relatedColumnDetails": self.relatedColumnDetails,
90
+ "sortByColumn": self.sortByColumn,
91
+ "sourceColumn": self.sourceColumn,
92
+ "summarizeBy": self.summarizeBy,
93
+ "type": self.type,
94
+ }
95
+ for k in list(base_dict.keys()):
96
+ if base_dict[k] in (None, "", []):
97
+ del base_dict[k]
98
+ return base_dict
99
+
100
+ def to_json(self, indent: int = 2) -> str:
101
+ return dump_json(self.to_dict(), indent=indent)
102
+
103
+ def clean_expression(self):
104
+ self.expression.clean_expression()