Skip to content

Commit 199ec41

Browse files
onefloidMariusWirtz
authored andcommitted
docs: Add docstrings
1 parent ce4a5fe commit 199ec41

3 files changed

Lines changed: 159 additions & 14 deletions

File tree

TM1py/Exceptions/Exceptions.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
# TM1py Exceptions are defined here
44
from typing import List, Mapping
55

6-
76
class TM1pyTimeout(Exception):
7+
"""Exception for timeout during a REST request."""
8+
89
def __init__(self, method: str, url: str, timeout: float):
10+
"""
11+
:param method: HTTP method used
12+
:param url: URL of the request
13+
:param timeout: Timeout in seconds
14+
"""
915
self.method = method
1016
self.url = url
1117
self.timeout = timeout
@@ -15,7 +21,14 @@ def __str__(self):
1521

1622

1723
class TM1pyVersionException(Exception):
24+
"""Exception for usage of a feature requiring a higher TM1 server version."""
25+
1826
def __init__(self, function: str, required_version, feature: str = None):
27+
"""
28+
:param function: Name of the function
29+
:param required_version: Required TM1 server version
30+
:param feature: Optional feature name
31+
"""
1932
self.function = function
2033
self.required_version = required_version
2134
self.feature = feature
@@ -29,7 +42,13 @@ def __str__(self):
2942

3043

3144
class TM1pyVersionDeprecationException(Exception):
45+
"""Exception for usage of a deprecated feature."""
46+
3247
def __init__(self, function: str, deprecated_in_version):
48+
"""
49+
:param function: Name of the function
50+
:param deprecated_in_version: Version in which the function was deprecated
51+
"""
3352
self.function = function
3453
self.deprecated_in_version = deprecated_in_version
3554

@@ -38,70 +57,103 @@ def __str__(self):
3857

3958

4059
class TM1pyNotAdminException(Exception):
60+
"""Exception for missing admin permissions."""
61+
4162
def __init__(self, function: str):
63+
"""
64+
:param function: Name of the function
65+
"""
4266
self.function = function
4367

4468
def __str__(self):
4569
return f"Function '{self.function}' requires admin permissions"
4670

4771

4872
class TM1pyNotDataAdminException(Exception):
73+
"""Exception for missing DataAdmin permissions."""
74+
4975
def __init__(self, function: str):
76+
"""
77+
:param function: Name of the function
78+
"""
5079
self.function = function
5180

5281
def __str__(self):
5382
return f"Function '{self.function}' requires DataAdmin permissions"
5483

5584

5685
class TM1pyNotSecurityAdminException(Exception):
86+
"""Exception for missing SecurityAdmin permissions."""
87+
5788
def __init__(self, function: str):
89+
"""
90+
:param function: Name of the function
91+
"""
5892
self.function = function
5993

6094
def __str__(self):
6195
return f"Function '{self.function}' requires SecurityAdmin permissions"
6296

6397

6498
class TM1pyNotOpsAdminException(Exception):
99+
"""Exception for missing OperationsAdmin permissions."""
100+
65101
def __init__(self, function: str):
102+
"""
103+
:param function: Name of the function
104+
"""
66105
self.function = function
67106

68107
def __str__(self):
69108
return f"Function '{self.function}' requires OperationsAdmin permissions"
70109

71110

72111
class TM1pyException(Exception):
73-
"""The default exception for TM1py"""
112+
"""The default exception for TM1py."""
74113

75114
def __init__(self, message):
115+
"""
116+
:param message: Exception message
117+
"""
76118
self.message = message
77119

78120
def __str__(self):
79121
return self.message
80122

81123

82124
class TM1pyRestException(TM1pyException):
83-
"""Exception for failing REST operations"""
125+
"""Exception for failing REST operations."""
84126

85127
def __init__(self, response: str, status_code: int, reason: str, headers: Mapping):
128+
"""
129+
:param response: Response text
130+
:param status_code: HTTP status code
131+
:param reason: Reason phrase
132+
:param headers: HTTP headers
133+
"""
86134
super(TM1pyRestException, self).__init__(response)
87135
self._status_code = status_code
88136
self._reason = reason
89137
self._headers = headers
90138

91139
@property
92140
def status_code(self):
141+
"""HTTP status code."""
93142
return self._status_code
94143

95144
@property
96145
def reason(self):
146+
"""Reason phrase."""
97147
return self._reason
98148

99149
@property
100150
def response(self):
151+
"""Response text."""
101152
return self.message
102153

103154
@property
104155
def headers(self):
156+
"""HTTP headers."""
105157
return self._headers
106158

107159
def __str__(self):
@@ -111,8 +163,13 @@ def __str__(self):
111163

112164

113165
class TM1pyWriteFailureException(TM1pyException):
166+
"""Exception for complete failure of write operations."""
114167

115168
def __init__(self, statuses: List[str], error_log_files: List[str]):
169+
"""
170+
:param statuses: List of failed statuses
171+
:param error_log_files: List of error log file paths
172+
"""
116173
self.statuses = statuses
117174
self.error_log_files = error_log_files
118175

@@ -121,8 +178,14 @@ def __init__(self, statuses: List[str], error_log_files: List[str]):
121178

122179

123180
class TM1pyWritePartialFailureException(TM1pyException):
181+
"""Exception for partial failure of write operations."""
124182

125183
def __init__(self, statuses: List[str], error_log_files: List[str], attempts: int):
184+
"""
185+
:param statuses: List of failed statuses
186+
:param error_log_files: List of error log file paths
187+
:param attempts: Total number of attempts
188+
"""
126189
self.statuses = statuses
127190
self.error_log_files = error_log_files
128191
self.attempts = attempts
@@ -131,4 +194,4 @@ def __init__(self, statuses: List[str], error_log_files: List[str], attempts: in
131194
f"{len(self.statuses)} out of {self.attempts} write operations failed partially. "
132195
f"Details: {self.error_log_files}"
133196
)
134-
super(TM1pyWritePartialFailureException, self).__init__(message)
197+
super(TM1pyWritePartialFailureException, self).__init__(message)

TM1py/Objects/Annotation.py

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ def __init__(
2929
last_updated_by: str = None,
3030
last_updated: str = None,
3131
):
32+
"""
33+
Initialize an Annotation object.
34+
35+
:param comment_value: The value of the annotation comment.
36+
:param object_name: Name of the TM1 object the annotation is attached to.
37+
:param dimensional_context: Iterable of dimension elements providing context.
38+
:param comment_type: Type of the comment (default "ANNOTATION").
39+
:param annotation_id: Unique ID of the annotation.
40+
:param text: Text of the annotation.
41+
:param creator: Creator of the annotation.
42+
:param created: Creation timestamp.
43+
:param last_updated_by: Last user who updated the annotation.
44+
:param last_updated: Last update timestamp.
45+
"""
3246
self._id = annotation_id
3347
self._text = text
3448
self._creator = creator
@@ -45,7 +59,7 @@ def from_json(cls, annotation_as_json: str) -> "Annotation":
4559
"""Alternative constructor
4660
4761
:param annotation_as_json: String, JSON
48-
:return: instance of TM1py.Process
62+
:return: instance of Annotation
4963
"""
5064
annotation_as_dict = json.loads(annotation_as_json)
5165
annotation_id = annotation_as_dict["ID"]
@@ -73,66 +87,123 @@ def from_json(cls, annotation_as_json: str) -> "Annotation":
7387

7488
@property
7589
def body(self) -> str:
90+
"""
91+
Get the annotation body as a JSON string.
92+
93+
:return: JSON string representation of the annotation.
94+
"""
7695
return json.dumps(self._construct_body())
7796

7897
@property
7998
def body_as_dict(self) -> Dict:
99+
"""
100+
Get the annotation body as a dictionary.
101+
102+
:return: Dictionary representation of the annotation.
103+
"""
80104
return self._construct_body()
81105

82106
@property
83107
def comment_value(self) -> str:
108+
"""
109+
Get the comment value.
110+
111+
:return: The comment value string.
112+
"""
84113
return self._comment_value
85114

86115
@property
87116
def text(self) -> str:
117+
"""
118+
Get the annotation text.
119+
120+
:return: The annotation text.
121+
"""
88122
return self._text
89123

90124
@property
91125
def dimensional_context(self) -> List[str]:
126+
"""
127+
Get the dimensional context.
128+
129+
:return: List of dimension elements providing context.
130+
"""
92131
return self._dimensional_context
93132

94133
@property
95134
def created(self) -> str:
135+
"""
136+
Get the creation timestamp.
137+
138+
:return: Creation timestamp as string.
139+
"""
96140
return self._created
97141

98142
@property
99143
def object_name(self) -> str:
144+
"""
145+
Get the object name.
146+
147+
:return: Name of the TM1 object.
148+
"""
100149
return self._object_name
101150

102151
@property
103152
def last_updated(self) -> str:
153+
"""
154+
Get the last updated timestamp.
155+
156+
:return: Last update timestamp as string.
157+
"""
104158
return self._last_updated
105159

106160
@property
107161
def last_updated_by(self) -> str:
162+
"""
163+
Get the last user who updated the annotation.
164+
165+
:return: Username of last updater.
166+
"""
108167
return self._last_updated_by
109168

110169
@comment_value.setter
111170
def comment_value(self, value: str):
171+
"""
172+
Set the comment value.
173+
174+
:param value: New comment value.
175+
"""
112176
self._comment_value = value
113177

114178
@property
115179
def id(self) -> str:
180+
"""
181+
Get the annotation ID.
182+
183+
:return: Annotation ID string.
184+
"""
116185
return self._id
117186

118187
def move(self, dimension_order: Iterable[str], dimension: str, target_element: str, source_element: str = None):
119-
"""Move annotation on given dimension from source_element to target_element
188+
"""
189+
Move annotation on given dimension from source_element to target_element.
120190
121-
:param dimension_order: List, order of the dimensions in the cube
122-
:param dimension: dimension name
123-
:param target_element: target element name
124-
:param source_element: source element name
125-
:return:
191+
:param dimension_order: List, order of the dimensions in the cube.
192+
:param dimension: Dimension name.
193+
:param target_element: Target element name.
194+
:param source_element: Source element name (optional).
195+
:return: None
126196
"""
127197
for i, dimension_name in enumerate(dimension_order):
128198
if dimension_name.lower() == dimension.lower():
129199
if not source_element or self._dimensional_context[i] == source_element:
130200
self._dimensional_context[i] = target_element
131201

132202
def _construct_body(self) -> Dict:
133-
"""construct the ODATA conform JSON represenation for the Annotation entity.
203+
"""
204+
Construct the ODATA conform JSON representation for the Annotation entity.
134205
135-
:return: string, the valid JSON
206+
:return: Dictionary, the valid JSON.
136207
"""
137208
dimensional_context = [{"Name": element} for element in self._dimensional_context]
138209
body = collections.OrderedDict()
@@ -151,6 +222,12 @@ def _construct_body(self) -> Dict:
151222
return body
152223

153224
def construct_body_for_post(self, cube_dimensions) -> Dict:
225+
"""
226+
Construct the body for POST requests to create an annotation.
227+
228+
:param cube_dimensions: List of cube dimension names.
229+
:return: Dictionary for POST request body.
230+
"""
154231
body = collections.OrderedDict()
155232
body["Text"] = self.text
156233
body["ApplicationContext"] = [
@@ -167,4 +244,4 @@ def construct_body_for_post(self, cube_dimensions) -> Dict:
167244
body["commentType"] = "ANNOTATION"
168245
body["commentLocation"] = ",".join(self.dimensional_context)
169246

170-
return body
247+
return body

TM1py/Services/ApplicationService.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ def __init__(self, tm1_rest: RestService):
3434
self._rest = tm1_rest
3535

3636
def get_all_public_root_names(self, **kwargs):
37+
"""
38+
Retrieve all public root application names.
3739
40+
:param kwargs: Additional arguments for the REST request.
41+
:return: List of public root application names.
42+
"""
3843
url = "/Contents('Applications')/Contents"
3944
response = self._rest.GET(url, **kwargs)
4045
applications = list(application["Name"] for application in response.json()["value"])

0 commit comments

Comments
 (0)