ITK-dev-shared-components 0.0.6__tar.gz → 1.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.
Files changed (31) hide show
  1. itk_dev_shared_components-1.1.0/PKG-INFO +58 -0
  2. itk_dev_shared_components-1.1.0/README.md +39 -0
  3. itk_dev_shared_components-1.1.0/itk_dev_shared_components/graph/authentication.py +64 -0
  4. itk_dev_shared_components-1.1.0/itk_dev_shared_components/graph/mail.py +317 -0
  5. itk_dev_shared_components-1.1.0/itk_dev_shared_components/sap/__init__.py +0 -0
  6. {ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP → itk_dev_shared_components-1.1.0/itk_dev_shared_components/sap}/gridview_util.py +26 -56
  7. {ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP → itk_dev_shared_components-1.1.0/itk_dev_shared_components/sap}/multi_session.py +69 -45
  8. itk_dev_shared_components-1.1.0/itk_dev_shared_components/sap/opret_kundekontakt.py +107 -0
  9. itk_dev_shared_components-1.1.0/itk_dev_shared_components/sap/sap_login.py +185 -0
  10. {ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP → itk_dev_shared_components-1.1.0/itk_dev_shared_components/sap}/sap_util.py +7 -15
  11. itk_dev_shared_components-1.1.0/itk_dev_shared_components/sap/tree_util.py +72 -0
  12. itk_dev_shared_components-1.1.0/itk_dev_shared_components.egg-info/PKG-INFO +58 -0
  13. itk_dev_shared_components-1.1.0/itk_dev_shared_components.egg-info/SOURCES.txt +19 -0
  14. itk_dev_shared_components-1.1.0/itk_dev_shared_components.egg-info/requires.txt +5 -0
  15. itk_dev_shared_components-1.1.0/itk_dev_shared_components.egg-info/top_level.txt +1 -0
  16. {ITK_dev_shared_components-0.0.6 → itk_dev_shared_components-1.1.0}/pyproject.toml +6 -3
  17. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/opret_kundekontakt.py +0 -73
  18. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/sap_login.py +0 -119
  19. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/tree_util.py +0 -71
  20. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components.egg-info/PKG-INFO +0 -19
  21. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components.egg-info/SOURCES.txt +0 -17
  22. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components.egg-info/requires.txt +0 -2
  23. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components.egg-info/top_level.txt +0 -1
  24. ITK_dev_shared_components-0.0.6/PKG-INFO +0 -19
  25. ITK_dev_shared_components-0.0.6/README.md +0 -5
  26. ITK_dev_shared_components-0.0.6/tests/test_gridview_util.py +0 -75
  27. {ITK_dev_shared_components-0.0.6 → itk_dev_shared_components-1.1.0}/LICENSE +0 -0
  28. {ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP → itk_dev_shared_components-1.1.0/itk_dev_shared_components}/__init__.py +0 -0
  29. {ITK_dev_shared_components-0.0.6/ITK_dev_shared_components → itk_dev_shared_components-1.1.0/itk_dev_shared_components/graph}/__init__.py +0 -0
  30. {ITK_dev_shared_components-0.0.6/ITK_dev_shared_components.egg-info → itk_dev_shared_components-1.1.0/itk_dev_shared_components.egg-info}/dependency_links.txt +0 -0
  31. {ITK_dev_shared_components-0.0.6 → itk_dev_shared_components-1.1.0}/setup.cfg +0 -0
@@ -0,0 +1,58 @@
1
+ Metadata-Version: 2.1
2
+ Name: itk_dev_shared_components
3
+ Version: 1.1.0
4
+ Summary: Shared components to use in RPA projects
5
+ Author-email: ITK Development <itk-rpa@mkb.aarhus.dk>
6
+ Project-URL: Homepage, https://github.com/itk-dev-rpa/itk-dev-shared-components
7
+ Project-URL: Bug Tracker, https://github.com/itk-dev-rpa/itk-dev-shared-components/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: Microsoft :: Windows
11
+ Requires-Python: >=3.7
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: pywin32>=306
15
+ Requires-Dist: selenium>=4.10.0
16
+ Requires-Dist: msal>=1.24.1
17
+ Requires-Dist: requests>=2.31.0
18
+ Requires-Dist: beautifulsoup4>=4.12.2
19
+
20
+ # itk-dev-shared-components
21
+
22
+ ## Links
23
+
24
+ [Documentation](https://itk-dev-rpa.github.io/itk-dev-shared-components-docs/)
25
+
26
+ [Pypi](https://pypi.org/project/ITK-dev-shared-components/)
27
+
28
+ ## Installation
29
+
30
+ ```
31
+ pip install itk-dev-shared-components
32
+ ```
33
+
34
+ ## Intro
35
+
36
+ This python library contains helper modules for RPA development.
37
+ It's based on the need of [ITK Dev](https://itk.aarhus.dk/), but it has been
38
+ generalized to be useful for others as well.
39
+
40
+ ## Integrations
41
+
42
+ ### SAP Gui
43
+
44
+ Helper functions for using SAP Gui. A few examples include:
45
+
46
+ - Login to SAP.
47
+ - Handling multiple sessions in multiple threads.
48
+ - Convenience functions for gridviews and trees.
49
+
50
+ ### Microsoft Graph
51
+
52
+ Helper functions for using Microsoft Graph to read emails from shared inboxes.
53
+ Some examples are:
54
+
55
+ - Authentication using username and password.
56
+ - List and get emails.
57
+ - Get attachment data.
58
+ - Move and delete emails.
@@ -0,0 +1,39 @@
1
+ # itk-dev-shared-components
2
+
3
+ ## Links
4
+
5
+ [Documentation](https://itk-dev-rpa.github.io/itk-dev-shared-components-docs/)
6
+
7
+ [Pypi](https://pypi.org/project/ITK-dev-shared-components/)
8
+
9
+ ## Installation
10
+
11
+ ```
12
+ pip install itk-dev-shared-components
13
+ ```
14
+
15
+ ## Intro
16
+
17
+ This python library contains helper modules for RPA development.
18
+ It's based on the need of [ITK Dev](https://itk.aarhus.dk/), but it has been
19
+ generalized to be useful for others as well.
20
+
21
+ ## Integrations
22
+
23
+ ### SAP Gui
24
+
25
+ Helper functions for using SAP Gui. A few examples include:
26
+
27
+ - Login to SAP.
28
+ - Handling multiple sessions in multiple threads.
29
+ - Convenience functions for gridviews and trees.
30
+
31
+ ### Microsoft Graph
32
+
33
+ Helper functions for using Microsoft Graph to read emails from shared inboxes.
34
+ Some examples are:
35
+
36
+ - Authentication using username and password.
37
+ - List and get emails.
38
+ - Get attachment data.
39
+ - Move and delete emails.
@@ -0,0 +1,64 @@
1
+ """This module is responsible for authenticating a Microsoft Graph
2
+ connection."""
3
+
4
+ import msal
5
+
6
+
7
+ # pylint: disable-next=too-few-public-methods
8
+ class GraphAccess:
9
+ """An object that handles access to the Graph api.
10
+ This object should not be created directly but instead
11
+ using one of the authorize methods in the graph.authentication module.
12
+ """
13
+ def __init__(self, app: msal.PublicClientApplication, scopes: list[str]) -> str:
14
+ self.app = app
15
+ self.scopes = scopes
16
+
17
+ def get_access_token(self):
18
+ """Get the access token to Graph.
19
+ This function automatically reuses an existing token
20
+ or refreshes an expired one.
21
+
22
+ Raises:
23
+ RuntimeError: If the access token couldn't be acquired.
24
+
25
+ Returns:
26
+ str: The Graph access token.
27
+ """
28
+ account = self.app.get_accounts()[0]
29
+ token = self.app.acquire_token_silent(self.scopes, account)
30
+
31
+ if "access_token" in token:
32
+ return token['access_token']
33
+
34
+ if 'error_description' in token:
35
+ raise RuntimeError(f"Token could not be acquired. {token['error_description']}")
36
+
37
+ raise RuntimeError("Something went wrong. No error description was returned from Graph.")
38
+
39
+
40
+ def authorize_by_username_password(username: str, password: str, *, client_id: str, tenant_id: str) -> GraphAccess:
41
+ """Get a bearer token for the given user.
42
+ This is used in most other Graph API calls.
43
+
44
+ Args:
45
+ username: The username of the user (email address).
46
+ password: The password of the user.
47
+ client_id: The Graph API client id in 8-4-4-12 format.
48
+ tenant_id: The Graph API tenant id in 8-4-4-12 format.
49
+
50
+ Returns:
51
+ GraphAccess: The GraphAccess object used to authorize Graph access.
52
+ """
53
+ authority = f"https://login.microsoftonline.com/{tenant_id}"
54
+ scopes = ["https://graph.microsoft.com/.default"]
55
+
56
+ app = msal.PublicClientApplication(client_id, authority=authority)
57
+ app.acquire_token_by_username_password(username, password, scopes)
58
+
59
+ graph_access = GraphAccess(app, scopes)
60
+
61
+ # Test connection
62
+ graph_access.get_access_token()
63
+
64
+ return graph_access
@@ -0,0 +1,317 @@
1
+ """This module is responsible for accessing emails using the Microsoft Graph API."""
2
+
3
+ from dataclasses import dataclass, field
4
+ import io
5
+
6
+ import requests
7
+ from bs4 import BeautifulSoup
8
+
9
+ from itk_dev_shared_components.graph.authentication import GraphAccess
10
+
11
+
12
+ @dataclass
13
+ # pylint: disable-next=too-many-instance-attributes
14
+ class Email:
15
+ """A class representing an email."""
16
+ user: str
17
+ id: str = field(repr=False)
18
+ received_time: str
19
+ sender: str
20
+ receivers: list[str]
21
+ subject: str
22
+ body: str = field(repr=False)
23
+ body_type: str
24
+ has_attachments: bool
25
+
26
+ def get_text(self) -> str:
27
+ """Get the body as plain text.
28
+ If the body is html it's converted to plaintext.
29
+ If the body is text it's returned as is.
30
+
31
+ Returns:
32
+ str: The body as plain text.
33
+ """
34
+ if self.body_type == 'html':
35
+ soup = BeautifulSoup(self.body, "html.parser")
36
+ return soup.get_text().strip()
37
+
38
+ return self.body
39
+
40
+
41
+ @dataclass
42
+ class Attachment:
43
+ """A dataclass representing an email Attachment.
44
+ It contains the graph id, name and size of the attachment.
45
+ To get the actual data call graph.mail.get_attachment_data.
46
+ """
47
+ email: Email = field(repr=False)
48
+ id: str = field(repr=False)
49
+ name: str
50
+ size: int
51
+
52
+
53
+ def get_emails_from_folder(user: str, folder_path: str, graph_access: GraphAccess) -> tuple[Email]:
54
+ """Get all emails from the specified user and folder.
55
+ You need to authorize against Graph to get the GraphAccess before using this function
56
+ see the graph.authentication module.
57
+
58
+ Args:
59
+ user: The user who owns the folder.
60
+ folder_path: The absolute path of the folder e.g. 'Inbox/Economy/May'
61
+ graph_access: The GraphAccess object used to authenticate.
62
+
63
+ Returns:
64
+ tuple[Email]: The emails from the given folder.
65
+ """
66
+ folder_id = get_folder_id_from_path(user, folder_path, graph_access)
67
+
68
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{user}/mailFolders/{folder_id}/messages?$top=1000"
69
+
70
+ response = _get_request(endpoint, graph_access)
71
+ emails_raw = response.json()['value']
72
+
73
+ return _unpack_email_response(user, emails_raw)
74
+
75
+
76
+ def get_email_as_mime(email: Email, graph_access: GraphAccess) -> io.BytesIO:
77
+ """Get an email as a file-like object in MIME format.
78
+
79
+ Args:
80
+ email: The email to get as MIME.
81
+ graph_access: The GraphAccess object used to authenticate.
82
+
83
+ Returns:
84
+ io.BytesIO: A file-like object of the MIME file.
85
+ """
86
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{email.user}/messages/{email.id}/$value"
87
+ response = _get_request(endpoint, graph_access)
88
+ data = response.content
89
+ return io.BytesIO(data)
90
+
91
+
92
+ def get_folder_id_from_path(user: str, folder_path: str, graph_access: GraphAccess) -> str:
93
+ """Get the Graph id of a folder based on the path of the folder.
94
+ You need to authorize against Graph to get the GraphAccess before using this function
95
+ see the graph.authentication module.
96
+
97
+ Args:
98
+ user: The user who owns the folder.
99
+ folder_path: The absolute path of the folder e.g. 'Inbox/Economy/May'
100
+ graph_access: The GraphAccess object used to authenticate.
101
+
102
+ Raises:
103
+ ValueError: If a folder in the path can't be found.
104
+
105
+ Returns:
106
+ str: The UUID of the folder in Graph.
107
+ """
108
+ folders = folder_path.split("/")
109
+ main_folder = folders[0]
110
+ child_folders = folders[1:]
111
+
112
+ folder_id = None
113
+
114
+ # Get main folder
115
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{user}/mailFolders"
116
+ response = _get_request(endpoint, graph_access).json()
117
+ folder_id = _find_folder(response, main_folder)
118
+ if folder_id is None:
119
+ raise ValueError(f"Top level folder '{main_folder}' was not found for user '{user}'.")
120
+
121
+ # Get child folders
122
+ for child_folder in child_folders:
123
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{user}/mailFolders/{folder_id}/childFolders"
124
+ response = _get_request(endpoint, graph_access).json()
125
+ folder_id = _find_folder(response, child_folder)
126
+ if folder_id is None:
127
+ raise ValueError(f"Child folder '{child_folder}' not found under '{main_folder}' for user '{user}'.")
128
+
129
+ return folder_id
130
+
131
+
132
+ def list_email_attachments(email: Email, graph_access: GraphAccess) -> tuple[Attachment]:
133
+ """List all attachments of the given email. This function only gets the id, name and size
134
+ of the attachment. Use get_attachment_data to get the actual data of an attachment.
135
+
136
+ Args:
137
+ email: The email which attachments to list.
138
+ graph_access: The GraphAccess object used to authenticate.
139
+
140
+ Returns:
141
+ tuple[Attachment]: A tuple of Attachment objects describing the attachments.
142
+ """
143
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{email.user}/messages/{email.id}/attachments?$select=name,size,id"
144
+ response = _get_request(endpoint, graph_access).json()
145
+
146
+ attachments = []
147
+ for att in response['value']:
148
+ attachments.append(Attachment(email, att['id'], att['name'], att['size']))
149
+
150
+ return tuple(attachments)
151
+
152
+
153
+ def get_attachment_data(attachment: Attachment, graph_access: GraphAccess) -> io.BytesIO:
154
+ """Get a file-like object representing the attachment.
155
+
156
+ Args:
157
+ attachment: The attachment to get.
158
+ graph_access: The GraphAccess object used to authenticate.
159
+
160
+ Returns:
161
+ io.BytesIO: A file-like object representing the attachment.
162
+ """
163
+ email = attachment.email
164
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{email.user}/messages/{email.id}/attachments/{attachment.id}/$value"
165
+ response = _get_request(endpoint, graph_access)
166
+ data_bytes = response.content
167
+ return io.BytesIO(data_bytes)
168
+
169
+
170
+ def move_email(email: Email, folder_path: str, graph_access: GraphAccess, *, well_known_folder: bool = False) -> None:
171
+ """Move an email to another folder under the same user.
172
+ If well_known_folder is true, the folder path is assumed to be a well defined folder.
173
+ See https://learn.microsoft.com/en-us/graph/api/resources/mailfolder?view=graph-rest-1.0
174
+ for a list of well defined folder names.
175
+
176
+ Args:
177
+ email: The email to move.
178
+ folder_path: The absolute path to the new folder. E.g. 'Inbox/Economy/May'
179
+ graph_access: The GraphAccess object used to authenticate.
180
+ well_known_folder: Whether the path is a 'well known folder'. Defaults to False.
181
+ """
182
+ if well_known_folder:
183
+ folder_id = folder_path
184
+ else:
185
+ folder_id = get_folder_id_from_path(email.user, folder_path, graph_access)
186
+
187
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{email.user}/messages/{email.id}/move"
188
+
189
+ token = graph_access.get_access_token()
190
+ headers = {
191
+ 'Authorization': f"Bearer {token}",
192
+ 'Content-Type': "application/json"
193
+ }
194
+
195
+ body = {
196
+ 'destinationId': folder_id
197
+ }
198
+
199
+ response = requests.post(
200
+ url=endpoint,
201
+ headers=headers,
202
+ json=body,
203
+ timeout=30
204
+ )
205
+ response.raise_for_status()
206
+
207
+ new_id = response.json()['id']
208
+ email.id = new_id
209
+
210
+
211
+ def delete_email(email: Email, graph_access: GraphAccess, *, permanent: bool = False) -> None:
212
+ """Delete an email from the mailbox.
213
+ If permanent is true the email is completely removed from the user's mailbox.
214
+ If permanent is false the email is instead moved to the Deleted Items folder.
215
+
216
+ Args:
217
+ email: The email to delete.
218
+ graph_access: The GraphAccess object used to authenticate.
219
+ permanent: Whether to permanently remove the email or not. Defaults to False.
220
+ """
221
+ if permanent:
222
+ endpoint = f"https://graph.microsoft.com/v1.0/users/{email.user}/messages/{email.id}"
223
+
224
+ token = graph_access.get_access_token()
225
+ headers = {'Authorization': f"Bearer {token}"}
226
+
227
+ response = requests.delete(
228
+ url=endpoint,
229
+ headers=headers,
230
+ timeout=30
231
+ )
232
+ response.raise_for_status()
233
+ else:
234
+ move_email(email, "deleteditems", graph_access, well_known_folder=True)
235
+
236
+
237
+ def _find_folder(response: dict, target_folder: str) -> str:
238
+ """Find the target folder in
239
+
240
+ Args:
241
+ response: The json dict of the HTTP response.
242
+ target_folder: The folder to find.
243
+
244
+ Returns:
245
+ str: The id of the target folder.
246
+ """
247
+ for g_folder in response['value']:
248
+ if g_folder['displayName'] == target_folder:
249
+ return g_folder['id']
250
+ return None
251
+
252
+
253
+ def _unpack_email_response(user: str, emails_raw: list[dict[str, str]]) -> tuple[Email]:
254
+ """Unpack a json HTTP response and create a list of Email objects.
255
+
256
+ Args:
257
+ user: The user who owns the email folder.
258
+ json: The json dictionary created by response.json().
259
+
260
+ Returns:
261
+ tuple[Email]: A tuple of Email objects.
262
+ """
263
+ emails = []
264
+
265
+ for email in emails_raw:
266
+ mail_id = email['id']
267
+ received_time = email['receivedDateTime']
268
+ sender = email['from']['emailAddress']['address']
269
+ receivers = [r['emailAddress']['address'] for r in email['toRecipients']]
270
+ subject = email['subject']
271
+ body = email['body']['content']
272
+ body_type = email['body']['contentType']
273
+ has_attachments = email['hasAttachments']
274
+
275
+ emails.append(
276
+ Email(
277
+ user,
278
+ mail_id,
279
+ received_time,
280
+ sender,
281
+ receivers,
282
+ subject,
283
+ body,
284
+ body_type,
285
+ has_attachments
286
+ )
287
+ )
288
+
289
+ return tuple(emails)
290
+
291
+
292
+ def _get_request(endpoint: str, graph_access: GraphAccess) -> requests.models.Response:
293
+ """Sends a get request to the given Graph endpoint using the GraphAccess
294
+ and returns the json object of the response.
295
+
296
+ Args:
297
+ endpoint: The URL of the Graph endpoint.
298
+ graph_access: The GraphAccess object used to authenticate.
299
+ timeout: Timeout in seconds of the HTTP request. Defaults to 10.
300
+
301
+ Returns:
302
+ Response: The response object of the GET request.
303
+
304
+ Raises:
305
+ HTTPError: Any errors raised while performing GET request.
306
+ """
307
+ token = graph_access.get_access_token()
308
+ headers = {'Authorization': f"Bearer {token}"}
309
+
310
+ response = requests.get(
311
+ endpoint,
312
+ headers=headers,
313
+ timeout=30
314
+ )
315
+ response.raise_for_status()
316
+
317
+ return response
@@ -1,4 +1,5 @@
1
- """This module provides static functions to peform common tasks with SAP GuiGridView COM objects."""
1
+ """This module provides static functions to perform common tasks with SAP GuiGridView COM objects."""
2
+
2
3
 
3
4
  def scroll_entire_table(grid_view, return_to_top=False) -> None:
4
5
  """This function scrolls through the entire table to load all cells.
@@ -6,14 +7,13 @@ def scroll_entire_table(grid_view, return_to_top=False) -> None:
6
7
  Args:
7
8
  grid_view: A SAP GuiGridView object.
8
9
  return_to_top: Whether to return the table to the first row after scrolling. Defaults to False.
9
-
10
- Returns:
11
- _type_: _description_
12
- """
10
+ """
11
+ if grid_view.RowCount == 0 or grid_view.VisibleRowCount == 0:
12
+ return
13
13
 
14
14
  for i in range(0, grid_view.RowCount, grid_view.VisibleRowCount):
15
15
  grid_view.FirstVisibleRow = i
16
-
16
+
17
17
  if return_to_top:
18
18
  grid_view.FirstVisibleRow = 0
19
19
 
@@ -24,11 +24,11 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
24
24
  Args:
25
25
  grid_view: A SAP GuiGridView object.
26
26
  pre_load: Whether to first scroll through the table to load all values.
27
- If a row hasn't been loaded before reading, the row data will be empty.
27
+ If a row hasn't been loaded before reading, the row data will be empty.
28
28
 
29
29
  Returns:
30
30
  tuple[tuple[str]]: A 2D tuple of all cell values in the gridview.
31
- """
31
+ """
32
32
 
33
33
  if pre_load:
34
34
  scroll_entire_table(grid_view, True)
@@ -43,24 +43,24 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
43
43
  for c in columns:
44
44
  v = grid_view.GetCellValue(r, c)
45
45
  row_data.append(v)
46
-
46
+
47
47
  output.append(tuple(row_data))
48
-
48
+
49
49
  return tuple(output)
50
50
 
51
51
 
52
- def get_row(grid_view, row:int, scroll_to_row=False) -> tuple[str]:
52
+ def get_row(grid_view, row: int, scroll_to_row=False) -> tuple[str]:
53
53
  """Returns the data of a single row.
54
54
 
55
55
  Args:
56
56
  grid_view: A SAP GuiGridView object.
57
57
  row: The zero-based index of the row.
58
58
  scroll_to_row: Whether to scroll to the row before reading the data.
59
- This ensures the data of the row has been loaded before reading.
59
+ This ensures the data of the row has been loaded before reading.
60
60
 
61
61
  Returns:
62
62
  tuple[str]: A tuple of the row's data.
63
- """
63
+ """
64
64
 
65
65
  if scroll_to_row:
66
66
  grid_view.FirstVisibleRow = row
@@ -83,7 +83,7 @@ def iterate_rows(grid_view) -> tuple[str]:
83
83
 
84
84
  Yields:
85
85
  tuple[str]: A tuple of the next row's data.
86
- """
86
+ """
87
87
 
88
88
  row = 0
89
89
  while row < grid_view.RowCount:
@@ -104,12 +104,12 @@ def get_column_titles(grid_view) -> tuple[str]:
104
104
 
105
105
  Returns:
106
106
  tuple[str]: A tuple of the gridview's column titles.
107
- """
107
+ """
108
108
 
109
109
  return tuple(grid_view.GetColumnTitles(c)[0] for c in grid_view.ColumnOrder)
110
110
 
111
111
 
112
- def find_row_index_by_value(grid_view, column:str, value:str) -> int:
112
+ def find_row_index_by_value(grid_view, column: str, value: str) -> int:
113
113
  """Find the index of the first row where the given column's value
114
114
  matches the given value. Returns -1 if no row is found.
115
115
 
@@ -123,7 +123,7 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
123
123
 
124
124
  Returns:
125
125
  int: The index of the first row which column value matches the given value.
126
- """
126
+ """
127
127
 
128
128
  if column not in grid_view.ColumnOrder:
129
129
  raise ValueError(f"Column '{column}' not in grid_view")
@@ -132,14 +132,15 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
132
132
  # Only scroll when row isn't visible
133
133
  if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
134
134
  grid_view.FirstVisibleRow = row
135
-
135
+
136
136
  if grid_view.GetCellValue(row, column) == value:
137
137
  return row
138
-
138
+
139
139
  return -1
140
140
 
141
- def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int]:
142
- """Find all indecies of the rows where the given column's value
141
+
142
+ def find_all_row_indices_by_value(grid_view, column: str, value: str) -> list[int]:
143
+ """Find all indices of the rows where the given column's value
143
144
  match the given value. Returns an empty list if no row is found.
144
145
 
145
146
  Args:
@@ -151,8 +152,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
151
152
  ValueError: If the column name doesn't exist in the grid view.
152
153
 
153
154
  Returns:
154
- list[int]: A list of row indecies where the value matches.
155
- """
155
+ list[int]: A list of row indices where the value matches.
156
+ """
156
157
  if column not in grid_view.ColumnOrder:
157
158
  raise ValueError(f"Column '{column}' not in grid_view")
158
159
 
@@ -162,39 +163,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
162
163
  # Only scroll when row isn't visible
163
164
  if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
164
165
  grid_view.FirstVisibleRow = row
165
-
166
+
166
167
  if grid_view.GetCellValue(row, column) == value:
167
168
  rows.append(row)
168
-
169
- return rows
170
-
171
-
172
-
173
-
174
- if __name__=='__main__':
175
- import win32com.client
176
-
177
- SAP = win32com.client.GetObject("SAPGUI")
178
- app = SAP.GetScriptingEngine
179
- connection = app.Connections(0)
180
- session = connection.Sessions(0)
181
-
182
- table = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
183
-
184
- rows = find_all_row_indecies_by_value(table, "ZZ_PARTNER", '15879880')
185
- print(rows)
186
- table.setCurrentCell(rows[0], "ZZ_PARTNER")
187
-
188
- # print(get_row(table, 1, True))
189
-
190
- # scroll_entire_table(table)
191
-
192
- # data = get_all_rows(table)
193
- # print(len(data), len(data[0]))
194
-
195
- # for r in iterate_rows(table):
196
- # print(r)
197
-
198
- # print(get_column_titles(table))
199
-
200
169
 
170
+ return rows