ITK-dev-shared-components 0.0.5__tar.gz → 0.0.6__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 (20) hide show
  1. ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/gridview_util.py +200 -0
  2. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/SAP/multi_session.py +11 -0
  3. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/SAP/sap_login.py +31 -5
  4. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/PKG-INFO +1 -1
  5. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/SOURCES.txt +2 -1
  6. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/PKG-INFO +1 -1
  7. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/pyproject.toml +1 -1
  8. ITK_dev_shared_components-0.0.6/tests/test_gridview_util.py +75 -0
  9. ITK_dev_shared_components-0.0.5/ITK_dev_shared_components/SAP/gridview_util.py +0 -126
  10. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/SAP/__init__.py +0 -0
  11. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/SAP/opret_kundekontakt.py +0 -0
  12. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/SAP/sap_util.py +0 -0
  13. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/SAP/tree_util.py +0 -0
  14. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/__init__.py +0 -0
  15. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/dependency_links.txt +0 -0
  16. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/requires.txt +0 -0
  17. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/top_level.txt +0 -0
  18. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/LICENSE +0 -0
  19. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/README.md +0 -0
  20. {ITK_dev_shared_components-0.0.5 → ITK_dev_shared_components-0.0.6}/setup.cfg +0 -0
@@ -0,0 +1,200 @@
1
+ """This module provides static functions to peform common tasks with SAP GuiGridView COM objects."""
2
+
3
+ def scroll_entire_table(grid_view, return_to_top=False) -> None:
4
+ """This function scrolls through the entire table to load all cells.
5
+
6
+ Args:
7
+ grid_view: A SAP GuiGridView object.
8
+ return_to_top: Whether to return the table to the first row after scrolling. Defaults to False.
9
+
10
+ Returns:
11
+ _type_: _description_
12
+ """
13
+
14
+ for i in range(0, grid_view.RowCount, grid_view.VisibleRowCount):
15
+ grid_view.FirstVisibleRow = i
16
+
17
+ if return_to_top:
18
+ grid_view.FirstVisibleRow = 0
19
+
20
+
21
+ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
22
+ """Get all values from a table and return them in a 2D tuple.
23
+
24
+ Args:
25
+ grid_view: A SAP GuiGridView object.
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.
28
+
29
+ Returns:
30
+ tuple[tuple[str]]: A 2D tuple of all cell values in the gridview.
31
+ """
32
+
33
+ if pre_load:
34
+ scroll_entire_table(grid_view, True)
35
+
36
+ columns = grid_view.ColumnOrder
37
+ row_count = grid_view.RowCount
38
+
39
+ output = []
40
+
41
+ for r in range(row_count):
42
+ row_data = []
43
+ for c in columns:
44
+ v = grid_view.GetCellValue(r, c)
45
+ row_data.append(v)
46
+
47
+ output.append(tuple(row_data))
48
+
49
+ return tuple(output)
50
+
51
+
52
+ def get_row(grid_view, row:int, scroll_to_row=False) -> tuple[str]:
53
+ """Returns the data of a single row.
54
+
55
+ Args:
56
+ grid_view: A SAP GuiGridView object.
57
+ row: The zero-based index of the row.
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.
60
+
61
+ Returns:
62
+ tuple[str]: A tuple of the row's data.
63
+ """
64
+
65
+ if scroll_to_row:
66
+ grid_view.FirstVisibleRow = row
67
+
68
+ row_data = []
69
+ for c in grid_view.ColumnOrder:
70
+ v = grid_view.GetCellValue(row, c)
71
+ row_data.append(v)
72
+ return tuple(row_data)
73
+
74
+
75
+ def iterate_rows(grid_view) -> tuple[str]:
76
+ """This generator yields each row of the table in order.
77
+ This is preferable to loading all rows as this will only load a row when it's needed.
78
+ As a side effect this function scrolls the yielded row into view, so the
79
+ table doesn't need to be pre-loaded before calling this function.
80
+
81
+ Args:
82
+ grid_view: A SAP GuiGridView object.
83
+
84
+ Yields:
85
+ tuple[str]: A tuple of the next row's data.
86
+ """
87
+
88
+ row = 0
89
+ while row < grid_view.RowCount:
90
+ # Only scroll when row isn't visible
91
+ if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
92
+ grid_view.FirstVisibleRow = row
93
+
94
+ yield get_row(grid_view, row)
95
+ row += 1
96
+
97
+
98
+ def get_column_titles(grid_view) -> tuple[str]:
99
+ """Get the column titles of the table instead of the column ids.
100
+ To get the column ids use grid_view.ColumnOrder.
101
+
102
+ Args:
103
+ grid_view: A SAP GuiGridView object.
104
+
105
+ Returns:
106
+ tuple[str]: A tuple of the gridview's column titles.
107
+ """
108
+
109
+ return tuple(grid_view.GetColumnTitles(c)[0] for c in grid_view.ColumnOrder)
110
+
111
+
112
+ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
113
+ """Find the index of the first row where the given column's value
114
+ matches the given value. Returns -1 if no row is found.
115
+
116
+ Args:
117
+ grid_view: A SAP GuiGridView object.
118
+ column: The id of the column.
119
+ value: The value to search for.
120
+
121
+ Raises:
122
+ ValueError: If the gridview doesn't contain a column with the given id.
123
+
124
+ Returns:
125
+ int: The index of the first row which column value matches the given value.
126
+ """
127
+
128
+ if column not in grid_view.ColumnOrder:
129
+ raise ValueError(f"Column '{column}' not in grid_view")
130
+
131
+ for row in range(grid_view.RowCount):
132
+ # Only scroll when row isn't visible
133
+ if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
134
+ grid_view.FirstVisibleRow = row
135
+
136
+ if grid_view.GetCellValue(row, column) == value:
137
+ return row
138
+
139
+ return -1
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
143
+ match the given value. Returns an empty list if no row is found.
144
+
145
+ Args:
146
+ grid_view (_type_): A SAP GuiGridView object.
147
+ column (str): The name of the column whose value to check.
148
+ value (str): The value to search for.
149
+
150
+ Raises:
151
+ ValueError: If the column name doesn't exist in the grid view.
152
+
153
+ Returns:
154
+ list[int]: A list of row indecies where the value matches.
155
+ """
156
+ if column not in grid_view.ColumnOrder:
157
+ raise ValueError(f"Column '{column}' not in grid_view")
158
+
159
+ rows = []
160
+
161
+ for row in range(grid_view.RowCount):
162
+ # Only scroll when row isn't visible
163
+ if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
164
+ grid_view.FirstVisibleRow = row
165
+
166
+ if grid_view.GetCellValue(row, column) == value:
167
+ 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
+
@@ -114,6 +114,17 @@ def spawn_sessions(num_sessions=6) -> list:
114
114
 
115
115
  return sessions
116
116
 
117
+ def get_all_SAP_sessions() -> tuple:
118
+ """Returns a tuple of all open SAP sessions (on connection index 0).
119
+
120
+ Returns:
121
+ tuple: A tuple of SAP GuiSession objects.
122
+ """
123
+ SAP = win32com.client.GetObject("SAPGUI")
124
+ app = SAP.GetScriptingEngine
125
+ connection = app.Connections(0)
126
+ return tuple(connection.Sessions)
127
+
117
128
  class ExThread(threading.Thread):
118
129
  """A thread with a handle to get an exception raised inside the thread: ExThread.error"""
119
130
  def __init__(self, *args, **kwargs):
@@ -6,6 +6,8 @@ import subprocess
6
6
  import time
7
7
  from selenium import webdriver
8
8
  from selenium.webdriver.common.by import By
9
+ import pywintypes
10
+ from ITK_dev_shared_components.SAP import multi_session
9
11
 
10
12
 
11
13
  def login_using_portal(username:str, password:str):
@@ -42,7 +44,7 @@ def login_using_portal(username:str, password:str):
42
44
 
43
45
  driver.quit()
44
46
 
45
- #TODO: Wait for if SAP has opened
47
+ _wait_for_sap_to_open()
46
48
 
47
49
 
48
50
  def _wait_for_download():
@@ -83,11 +85,35 @@ def login_using_cli(username: str, password: str, client='751', system='P02') ->
83
85
 
84
86
  subprocess.run(command_args, check=False)
85
87
 
88
+ _wait_for_sap_to_open()
89
+
90
+
91
+ def _wait_for_sap_to_open() -> None:
92
+ """Check every second for 10 seconds if the SAP Gui scripting engine is available.
93
+
94
+ Raises:
95
+ TimeoutError: If SAP doesn't start within 10 seconds.
96
+ """
97
+ for _ in range(10):
98
+ time.sleep(1)
99
+ try:
100
+ sessions = multi_session.get_all_SAP_sessions()
101
+ if len(sessions) > 0:
102
+ return
103
+ except pywintypes.com_error:
104
+ pass
105
+
106
+ raise TimeoutError("SAP didn't respond within 10 seconds.")
107
+
108
+ def kill_sap():
109
+ """Kills all SAP processes currently running."""
110
+ os.system("taskkill /F /IM saplogon.exe")
86
111
 
87
112
  if __name__=="__main__":
88
- user = "az12345"
89
- password = "Hunter2"
90
- login_using_portal(user, password)
91
- login_using_cli(user, password)
113
+ # user = "az12345"
114
+ # password = "Hunter2"
115
+ # login_using_portal(user, password)
116
+ # login_using_cli(user, password)
117
+ kill_sap()
92
118
 
93
119
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ITK-dev-shared-components
3
- Version: 0.0.5
3
+ Version: 0.0.6
4
4
  Summary: Shared components to use in RPA projects
5
5
  Author-email: ITK Development <itk-rpa@mkb.aarhus.dk>
6
6
  Project-URL: Homepage, https://github.com/itk-dev-rpa/itk-dev-shared-components
@@ -13,4 +13,5 @@ ITK_dev_shared_components/SAP/multi_session.py
13
13
  ITK_dev_shared_components/SAP/opret_kundekontakt.py
14
14
  ITK_dev_shared_components/SAP/sap_login.py
15
15
  ITK_dev_shared_components/SAP/sap_util.py
16
- ITK_dev_shared_components/SAP/tree_util.py
16
+ ITK_dev_shared_components/SAP/tree_util.py
17
+ tests/test_gridview_util.py
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ITK_dev_shared_components
3
- Version: 0.0.5
3
+ Version: 0.0.6
4
4
  Summary: Shared components to use in RPA projects
5
5
  Author-email: ITK Development <itk-rpa@mkb.aarhus.dk>
6
6
  Project-URL: Homepage, https://github.com/itk-dev-rpa/itk-dev-shared-components
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ITK_dev_shared_components"
7
- version = "0.0.5"
7
+ version = "0.0.6"
8
8
  authors = [
9
9
  { name="ITK Development", email="itk-rpa@mkb.aarhus.dk" },
10
10
  ]
@@ -0,0 +1,75 @@
1
+ import unittest
2
+ import os
3
+ from ITK_dev_shared_components.SAP import gridview_util, sap_login, multi_session
4
+
5
+ class test_gridview_util(unittest.TestCase):
6
+ @classmethod
7
+ def setUpClass(cls):
8
+ user, password = os.environ['SAP Login'].split(';')
9
+ sap_login.login_using_cli(user, password)
10
+ session = multi_session.get_all_SAP_sessions()[0]
11
+ session.startTransaction("fmcacov")
12
+ session.findById("wnd[0]/usr/ctxtGPART_DYN").text = "25564617"
13
+ session.findById("wnd[0]").sendVKey(0)
14
+
15
+ @classmethod
16
+ def tearDownClass(cls):
17
+ sap_login.kill_sap()
18
+
19
+ def setUp(self) -> None:
20
+ session = multi_session.get_all_SAP_sessions()[0]
21
+ self.table = session.findById("wnd[0]/usr/tabsDATA_DISP/tabpDATA_DISP_FC1/ssubDATA_DISP_SCA:RFMCA_COV:0202/cntlRFMCA_COV_0100_CONT5/shellcont/shell")
22
+
23
+ def test_scroll_entire_table(self):
24
+ gridview_util.scroll_entire_table(self.table)
25
+ gridview_util.scroll_entire_table(self.table, True)
26
+
27
+ def test_get_all_rows(self):
28
+ result = gridview_util.get_all_rows(self.table)
29
+ self.assertGreater(len(result), 0)
30
+ self.assertGreater(len(result[0]), 0)
31
+
32
+ def test_get_row(self):
33
+ result = gridview_util.get_row(self.table, 0, False)
34
+ self.assertGreater(len(result), 0)
35
+
36
+ result = gridview_util.get_row(self.table, 0, True)
37
+ self.assertGreater(len(result), 0)
38
+
39
+ def test_iterate_rows(self):
40
+ for row in gridview_util.iterate_rows(self.table):
41
+ self.assertGreater(len(row), 0)
42
+
43
+ def test_get_column_titles(self):
44
+ result = gridview_util.get_column_titles(self.table)
45
+ self.assertGreater(len(result), 0)
46
+
47
+ def test_find_row_index_by_value(self):
48
+ result = gridview_util.find_row_index_by_value(self.table, "TXTU2", "Test Deltrans")
49
+ self.assertNotEqual(result, -1)
50
+
51
+ result = gridview_util.find_row_index_by_value(self.table, "TXTU2", "Foo")
52
+ self.assertEqual(result, -1)
53
+
54
+ with self.assertRaises(ValueError):
55
+ gridview_util.find_row_index_by_value(self.table, "Foo", "Bar")
56
+
57
+
58
+ def test_find_all_row_indecies_by_value(self):
59
+ result = gridview_util.find_all_row_indecies_by_value(self.table, "TXTU2", "Gebyr")
60
+ self.assertGreater(len(result), 0)
61
+
62
+ result = gridview_util.find_all_row_indecies_by_value(self.table, "TXTU2", "Foo")
63
+ self.assertEqual(len(result), 0)
64
+
65
+ with self.assertRaises(ValueError):
66
+ gridview_util.find_all_row_indecies_by_value(self.table, "Foo", "Bar")
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+ if __name__ == '__main__':
75
+ unittest.main()
@@ -1,126 +0,0 @@
1
- """This module provides static functions to peform common tasks with SAP GuiGridView COM objects."""
2
-
3
- def scroll_entire_table(grid_view, return_to_top=False) -> None:
4
- """This function scrolls through the entire table to load all cells.
5
- grid_view: A SAP GuiGridView object.
6
- return_to_top: Whether to return the table to the first row after scrolling.
7
- """
8
- for i in range(0, grid_view.RowCount, grid_view.VisibleRowCount):
9
- grid_view.FirstVisibleRow = i
10
-
11
- if return_to_top:
12
- grid_view.FirstVisibleRow = 0
13
-
14
-
15
- def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
16
- """Get all values from a table and return them in a 2D tuple.
17
- grid_view: A SAP GuiGridView object.
18
- pre_load: Whether to first scroll through the table to load all values.
19
- If a row hasn't been loaded before reading, the row data will be empty.
20
- """
21
- if pre_load:
22
- scroll_entire_table(grid_view, True)
23
-
24
- columns = grid_view.ColumnOrder
25
- row_count = grid_view.RowCount
26
-
27
- output = []
28
-
29
- for r in range(row_count):
30
- row_data = []
31
- for c in columns:
32
- v = grid_view.GetCellValue(r, c)
33
- row_data.append(v)
34
-
35
- output.append(tuple(row_data))
36
-
37
- return tuple(output)
38
-
39
-
40
- def get_row(grid_view, row, scroll_to_row=False) -> tuple[str]:
41
- """Return the data of a single row
42
- grid_view: A SAP GuiGridView object.
43
- row: The zero-based index of the row.
44
- scroll_to_row: Whether to scroll to the row before reading the data.
45
- This ensures the data of the row has been loaded before reading.
46
- """
47
- if scroll_to_row:
48
- grid_view.FirstVisibleRow = row
49
-
50
- row_data = []
51
- for c in grid_view.ColumnOrder:
52
- v = grid_view.GetCellValue(row, c)
53
- row_data.append(v)
54
- return tuple(row_data)
55
-
56
-
57
- def iterate_rows(grid_view) -> tuple[str]:
58
- """This generator yields each row of the table in order.
59
- This is preferable to loading all rows as this will only load a row when it's needed.
60
- As a side effect this function scrolls the yielded row into view, so the
61
- table doesn't need to be pre-loaded before calling this function.
62
- grid_view: A SAP GuiGridView object.
63
- """
64
- row = 0
65
- while row < grid_view.RowCount:
66
- # Only scroll when row isn't visible
67
- if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
68
- grid_view.FirstVisibleRow = row
69
-
70
- yield get_row(grid_view, row)
71
- row += 1
72
-
73
-
74
- def get_column_titles(grid_view) -> tuple[str]:
75
- """Get the column titles of the table instead of the column ids.
76
- grid_view: A SAP GuiGridView object.
77
- """
78
- return tuple(grid_view.GetColumnTitles(c)[0] for c in grid_view.ColumnOrder)
79
-
80
-
81
- def find_row_index_by_value(grid_view, column:str, value:str) -> int:
82
- """Find the index of the first row where the given column's value
83
- match the given value. Returns -1 if no row is found.
84
- grid_view: A SAP GuiGridView object.
85
- column: The name of the column whose value to check.
86
- value: The value to search for.
87
- """
88
- if column not in grid_view.ColumOrder:
89
- raise ValueError(f"Column '{column}' not in grid_view")
90
-
91
- for row in range(grid_view.RowCount):
92
- # Only scroll when row isn't visible
93
- if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
94
- grid_view.FirstVisibleRow = row
95
-
96
- if grid_view.GetCellValue(row, column) == value:
97
- return row
98
-
99
- return -1
100
-
101
-
102
-
103
-
104
- if __name__=='__main__':
105
- import win32com.client
106
-
107
- SAP = win32com.client.GetObject("SAPGUI")
108
- app = SAP.GetScriptingEngine
109
- connection = app.Connections(0)
110
- session = connection.Sessions(0)
111
-
112
- table = session.findById("wnd[0]/usr/tabsDATA_DISP/tabpDATA_DISP_FC1/ssubDATA_DISP_SCA:RFMCA_COV:0202/cntlRFMCA_COV_0100_CONT5/shellcont/shell")
113
-
114
- # print(get_row(table, 1, True))
115
-
116
- # scroll_entire_table(table)
117
-
118
- # data = get_all_rows(table)
119
- # print(len(data), len(data[0]))
120
-
121
- # for r in iterate_rows(table):
122
- # print(r)
123
-
124
- # print(get_column_titles(table))
125
-
126
-