Skip to content

Commit 4ac6d16

Browse files
committed
optimized functions
1 parent fb21636 commit 4ac6d16

File tree

1 file changed

+80
-187
lines changed

1 file changed

+80
-187
lines changed

src/sempy_labs/report/_reportwrapper.py

Lines changed: 80 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,89 +1799,47 @@ def __set_annotation(self, json_file: dict, name: str, value: str) -> dict:
17991799

18001800
return json_file
18011801

1802-
def set_report_annotation(self, annotation_name: str, annotation_value: str):
1803-
"""
1804-
Sets an annotation on the report. If the annotation already exists, the annotation value is updated.
1805-
1806-
Parameters
1807-
----------
1808-
annotation_name : str
1809-
Name of the annotation.
1810-
annotation_value : str
1811-
Value of the annotation.
1812-
"""
1813-
1814-
file_path = "definition/report.json"
1815-
1816-
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
1817-
file = _decode_b64(payload)
1818-
json_file = _load_json(file)
1819-
1820-
new_file = self.__set_annotation(
1821-
json_file, name=annotation_name, value=annotation_value
1822-
)
1823-
new_payload = _conv_b64(new_file)
1824-
1825-
self._update_single_file(file_name=file_path, new_payload=new_payload)
1826-
1827-
def set_page_annotation(
1828-
self, page_name: str, annotation_name: str, annotation_value: str
1829-
):
1830-
"""
1831-
Sets an annotation on a page of the report. If the annotation already exists, the annotation value is updated.
1832-
1833-
Parameters
1834-
----------
1835-
page_name : str
1836-
The page name (ID) or page display name.
1837-
annotation_name : str
1838-
Name of the annotation.
1839-
annotation_value : str
1840-
Value of the annotation.
1841-
"""
1842-
1843-
page_id, page_display, file_path = helper.resolve_page_name(
1844-
self, page_name=page_name
1845-
)
1846-
1847-
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
1848-
file = _decode_b64(payload)
1849-
json_file = _load_json(file)
1850-
1851-
new_file = self.__set_annotation(
1852-
json_file, name=annotation_name, value=annotation_value
1853-
)
1854-
new_payload = _conv_b64(new_file)
1855-
1856-
self._update_single_file(file_name=file_path, new_payload=new_payload)
1857-
1858-
def set_visual_annotation(
1802+
def set_annotation(
18591803
self,
1860-
page_name: str,
1861-
visual_name: str,
18621804
annotation_name: str,
18631805
annotation_value: str,
1806+
page_name: Optional[str] = None,
1807+
visual_name: Optional[str] = None,
18641808
):
18651809
"""
1866-
Sets an annotation on a visual of the report. If the annotation already exists, the annotation value is updated.
1810+
Sets an annotation on the report/page/visual. If the annotation already exists, the annotation value is updated.
1811+
In order to set a report annotation, leave page_name=None, visual_name=None.
1812+
In order to set a page annotation, leave visual_annotation=None.
1813+
In order to set a visual annotation, set all parameters.
18671814
18681815
Parameters
18691816
----------
1870-
page_name : str
1871-
The page name (ID) or page display name.
1872-
visual_name : str
1873-
The name (ID) of the visual.
18741817
annotation_name : str
18751818
Name of the annotation.
18761819
annotation_value : str
18771820
Value of the annotation.
1821+
page_name : str, default=None
1822+
The page name or page display name.
1823+
Set this annotation when setting an annotation on a page or visual.
1824+
visual_name : str, default=None
1825+
The visual name.
1826+
Set this property when setting an annotation on a visual.
18781827
"""
18791828

1880-
page_name, page_display_name, visual_name, file_path = (
1881-
helper.resolve_visual_name(
1882-
self, page_name=page_name, visual_name=visual_name
1829+
if page_name is None and visual_name is None:
1830+
file_path = "definition/report.json"
1831+
elif page_name is not None and visual_name is None:
1832+
page_id, page_display, file_path = helper.resolve_page_name(
1833+
self, page_name=page_name
18831834
)
1884-
)
1835+
elif page_name is not None and visual_name is not None:
1836+
page_name, page_display_name, visual_name, file_path = (
1837+
helper.resolve_visual_name(
1838+
self, page_name=page_name, visual_name=visual_name
1839+
)
1840+
)
1841+
else:
1842+
raise ValueError(f"{icons.red_dot}")
18851843

18861844
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
18871845
file = _decode_b64(payload)
@@ -1906,76 +1864,44 @@ def __remove_annotation(self, json_file: dict, name: str) -> dict:
19061864

19071865
return json_file
19081866

1909-
def remove_report_annotation(self, annotation_name: str):
1910-
"""
1911-
Removes an annotation on the report.
1912-
1913-
Parameters
1914-
----------
1915-
annotation_name : str
1916-
Name of the annotation.
1917-
"""
1918-
1919-
file_path = "definition/report.json"
1920-
1921-
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
1922-
file = _decode_b64(payload)
1923-
json_file = _load_json(file)
1924-
1925-
new_file = self.__remove_annotation(json_file, name=annotation_name)
1926-
new_payload = _conv_b64(new_file)
1927-
1928-
self._update_single_file(file_name=file_path, new_payload=new_payload)
1929-
1930-
def remove_page_annotation(self, page_name: str, annotation_name: str):
1931-
"""
1932-
Removes an annotation on the page within the report.
1933-
1934-
Parameters
1935-
----------
1936-
page_name : str
1937-
The page name (ID) or page display name.
1938-
annotation_name : str
1939-
Name of the annotation.
1940-
"""
1941-
1942-
page_id, page_display, file_path = helper.resolve_page_name(
1943-
self, page_name=page_name
1944-
)
1945-
1946-
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
1947-
file = _decode_b64(payload)
1948-
json_file = _load_json(file)
1949-
1950-
new_file = self.__remove_annotation(json_file, name=annotation_name)
1951-
new_payload = _conv_b64(new_file)
1952-
1953-
self._update_single_file(file_name=file_path, new_payload=new_payload)
1954-
1955-
def remove_visual_annotation(
1867+
def remove_annotation(
19561868
self,
1957-
page_name: str,
1958-
visual_name: str,
19591869
annotation_name: str,
1870+
page_name: Optional[str] = None,
1871+
visual_name: Optional[str] = None,
19601872
):
19611873
"""
1962-
Removes an annotation on the visual within the report.
1874+
Removes an annotation on the report/page/visual.
1875+
In order to remove a report annotation, leave page_name=None, visual_name=None.
1876+
In order to remove a page annotation, leave visual_annotation=None.
1877+
In order to remove a visual annotation, set all parameters.
19631878
19641879
Parameters
19651880
----------
1966-
page_name : str
1967-
The page name (ID) or page display name.
1968-
visual_name : str
1969-
The name (ID) of the visual.
19701881
annotation_name : str
19711882
Name of the annotation.
1883+
page_name : str, default=None
1884+
The page name or page display name.
1885+
Set this annotation when setting an annotation on a page or visual.
1886+
visual_name : str, default=None
1887+
The visual name.
1888+
Set this property when setting an annotation on a visual.
19721889
"""
19731890

1974-
page_name, page_display_name, visual_name, file_path = (
1975-
helper.resolve_visual_name(
1976-
self, page_name=page_name, visual_name=visual_name
1891+
if page_name is None and visual_name is None:
1892+
file_path = "definition/report.json"
1893+
elif page_name is not None and visual_name is None:
1894+
page_id, page_display, file_path = helper.resolve_page_name(
1895+
self, page_name=page_name
19771896
)
1978-
)
1897+
elif page_name is not None and visual_name is not None:
1898+
page_name, page_display_name, visual_name, file_path = (
1899+
helper.resolve_visual_name(
1900+
self, page_name=page_name, visual_name=visual_name
1901+
)
1902+
)
1903+
else:
1904+
raise ValueError(f"{icons.red_dot}")
19791905

19801906
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
19811907
file = _decode_b64(payload)
@@ -1994,82 +1920,49 @@ def __get_annotation_value(self, json_file: dict, name: str) -> str:
19941920
if ann.get("name") == name:
19951921
return ann.get("value")
19961922

1997-
def get_report_annotation_value(self, annotation_name: str) -> str:
1998-
"""
1999-
Retrieves the annotation value for a report annotation.
2000-
2001-
Parameters
2002-
----------
2003-
annotation_name : str
2004-
Name of the annotation.
2005-
2006-
Returns
2007-
-------
2008-
str
2009-
The annotation value.
2010-
"""
2011-
2012-
file_path = "definition/report.json"
2013-
2014-
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
2015-
file = _decode_b64(payload)
2016-
json_file = _load_json(file)
2017-
2018-
return self.__get_annotation_value(json_file, name=annotation_name)
2019-
2020-
def get_page_annotation_value(self, page_name: str, annotation_name: str) -> str:
2021-
"""
2022-
Retrieves the annotation value for a page annotation.
2023-
2024-
Parameters
2025-
----------
2026-
page_name : str
2027-
The page name (ID) or page display name.
2028-
annotation_name : str
2029-
Name of the annotation.
2030-
2031-
Returns
2032-
-------
2033-
str
2034-
The annotation value.
2035-
"""
2036-
2037-
page_id, page_display, file_path = helper.resolve_page_name(
2038-
self, page_name=page_name
2039-
)
2040-
2041-
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
2042-
file = _decode_b64(payload)
2043-
json_file = _load_json(file)
2044-
2045-
return self.__get_annotation_value(json_file, name=annotation_name)
2046-
2047-
def get_visual_annotation_value(
2048-
self, page_name: str, visual_name: str, annotation_name: str
1923+
def get_annotation_value(
1924+
self,
1925+
annotation_name: str,
1926+
page_name: Optional[str] = None,
1927+
visual_name: Optional[str] = None,
20491928
) -> str:
20501929
"""
2051-
Retrieves the annotation value for a visual annotation.
1930+
Retrieves the annotation value of an annotation on the report/page/visual.
1931+
In order to retrieve a report annotation value, leave page_name=None, visual_name=None.
1932+
In order to retrieve a page annotation value, leave visual_annotation=None.
1933+
In order to retrieve a visual annotation value, set all parameters.
20521934
20531935
Parameters
20541936
----------
2055-
page_name : str
2056-
The page name (ID) or page display name.
2057-
visual_name : str
2058-
The name (ID) of the visual.
20591937
annotation_name : str
20601938
Name of the annotation.
1939+
page_name : str, default=None
1940+
The page name or page display name.
1941+
Set this annotation when setting an annotation on a page or visual.
1942+
visual_name : str, default=None
1943+
The visual name.
1944+
Set this property when setting an annotation on a visual.
20611945
20621946
Returns
20631947
-------
20641948
str
20651949
The annotation value.
20661950
"""
20671951

2068-
page_name, page_display_name, visual_name, file_path = (
2069-
helper.resolve_visual_name(
2070-
self, page_name=page_name, visual_name=visual_name
1952+
if page_name is None and visual_name is None:
1953+
file_path = "definition/report.json"
1954+
elif page_name is not None and visual_name is None:
1955+
page_id, page_display, file_path = helper.resolve_page_name(
1956+
self, page_name=page_name
20711957
)
2072-
)
1958+
elif page_name is not None and visual_name is not None:
1959+
page_name, page_display_name, visual_name, file_path = (
1960+
helper.resolve_visual_name(
1961+
self, page_name=page_name, visual_name=visual_name
1962+
)
1963+
)
1964+
else:
1965+
raise ValueError(f"{icons.red_dot}")
20731966

20741967
payload = self.rdef[self.rdef["path"] == file_path]["payload"].iloc[0]
20751968
file = _decode_b64(payload)

0 commit comments

Comments
 (0)