Skip to content

Commit 01e7b01

Browse files
committed
Linter fix
1 parent 28726fd commit 01e7b01

File tree

11 files changed

+225
-216
lines changed

11 files changed

+225
-216
lines changed

ITK_dev_shared_components/SAP/gridview_util.py

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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."""
22

33
def scroll_entire_table(grid_view, return_to_top=False) -> None:
44
"""This function scrolls through the entire table to load all cells.
@@ -9,13 +9,13 @@ def scroll_entire_table(grid_view, return_to_top=False) -> None:
99
1010
Returns:
1111
_type_: _description_
12-
"""
12+
"""
1313
if grid_view.RowCount == 0 or grid_view.VisibleRowCount == 0:
1414
return
15-
15+
1616
for i in range(0, grid_view.RowCount, grid_view.VisibleRowCount):
1717
grid_view.FirstVisibleRow = i
18-
18+
1919
if return_to_top:
2020
grid_view.FirstVisibleRow = 0
2121

@@ -30,7 +30,7 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
3030
3131
Returns:
3232
tuple[tuple[str]]: A 2D tuple of all cell values in the gridview.
33-
"""
33+
"""
3434

3535
if pre_load:
3636
scroll_entire_table(grid_view, True)
@@ -45,9 +45,9 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
4545
for c in columns:
4646
v = grid_view.GetCellValue(r, c)
4747
row_data.append(v)
48-
48+
4949
output.append(tuple(row_data))
50-
50+
5151
return tuple(output)
5252

5353

@@ -62,7 +62,7 @@ def get_row(grid_view, row:int, scroll_to_row=False) -> tuple[str]:
6262
6363
Returns:
6464
tuple[str]: A tuple of the row's data.
65-
"""
65+
"""
6666

6767
if scroll_to_row:
6868
grid_view.FirstVisibleRow = row
@@ -85,7 +85,7 @@ def iterate_rows(grid_view) -> tuple[str]:
8585
8686
Yields:
8787
tuple[str]: A tuple of the next row's data.
88-
"""
88+
"""
8989

9090
row = 0
9191
while row < grid_view.RowCount:
@@ -106,7 +106,7 @@ def get_column_titles(grid_view) -> tuple[str]:
106106
107107
Returns:
108108
tuple[str]: A tuple of the gridview's column titles.
109-
"""
109+
"""
110110

111111
return tuple(grid_view.GetColumnTitles(c)[0] for c in grid_view.ColumnOrder)
112112

@@ -125,7 +125,7 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
125125
126126
Returns:
127127
int: The index of the first row which column value matches the given value.
128-
"""
128+
"""
129129

130130
if column not in grid_view.ColumnOrder:
131131
raise ValueError(f"Column '{column}' not in grid_view")
@@ -134,14 +134,14 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
134134
# Only scroll when row isn't visible
135135
if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
136136
grid_view.FirstVisibleRow = row
137-
137+
138138
if grid_view.GetCellValue(row, column) == value:
139139
return row
140-
140+
141141
return -1
142142

143-
def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int]:
144-
"""Find all indecies of the rows where the given column's value
143+
def find_all_row_indices_by_value(grid_view, column:str, value:str) -> list[int]:
144+
"""Find all indices of the rows where the given column's value
145145
match the given value. Returns an empty list if no row is found.
146146
147147
Args:
@@ -153,8 +153,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
153153
ValueError: If the column name doesn't exist in the grid view.
154154
155155
Returns:
156-
list[int]: A list of row indecies where the value matches.
157-
"""
156+
list[int]: A list of row indices where the value matches.
157+
"""
158158
if column not in grid_view.ColumnOrder:
159159
raise ValueError(f"Column '{column}' not in grid_view")
160160

@@ -164,39 +164,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
164164
# Only scroll when row isn't visible
165165
if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
166166
grid_view.FirstVisibleRow = row
167-
167+
168168
if grid_view.GetCellValue(row, column) == value:
169169
rows.append(row)
170-
171-
return rows
172-
173-
174-
175-
176-
if __name__=='__main__':
177-
import win32com.client
178-
179-
SAP = win32com.client.GetObject("SAPGUI")
180-
app = SAP.GetScriptingEngine
181-
connection = app.Connections(0)
182-
session = connection.Sessions(0)
183-
184-
table = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
185-
186-
rows = find_all_row_indecies_by_value(table, "ZZ_PARTNER", '15879880')
187-
print(rows)
188-
table.setCurrentCell(rows[0], "ZZ_PARTNER")
189-
190-
# print(get_row(table, 1, True))
191-
192-
# scroll_entire_table(table)
193-
194-
# data = get_all_rows(table)
195-
# print(len(data), len(data[0]))
196-
197-
# for r in iterate_rows(table):
198-
# print(r)
199-
200-
# print(get_column_titles(table))
201-
202170

171+
return rows

ITK_dev_shared_components/SAP/multi_session.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,31 @@
66
import threading
77
import math
88
from typing import Callable
9+
910
import pythoncom
1011
import win32com.client
1112
import win32gui
1213

1314
def run_with_session(session_index:int, func:Callable, args:tuple) -> None:
14-
"""Run a function in a sepcific session based on the sessions index.
15-
This function is meant to be run inside a seperate thread.
15+
"""Run a function in a specific session based on the sessions index.
16+
This function is meant to be run inside a separate thread.
1617
The function must take a session object as its first argument.
1718
Note that this function will not spawn the sessions before running,
1819
use spawn_sessions to do that.
1920
"""
2021

2122
pythoncom.CoInitialize()
2223

23-
SAP = win32com.client.GetObject("SAPGUI")
24-
app = SAP.GetScriptingEngine
24+
sap = win32com.client.GetObject("SAPGUI")
25+
app = sap.GetScriptingEngine
2526
connection = app.Connections(0)
2627
session = connection.Sessions(session_index)
2728

2829
func(session, *args)
2930

3031
pythoncom.CoUninitialize()
3132

33+
3234
def run_batch(func:Callable, args:tuple[tuple], num_sessions=6) -> None:
3335
"""Run a function in parallel sessions.
3436
The function will be run {num_sessions} times with args[i] as input.
@@ -41,7 +43,7 @@ def run_batch(func:Callable, args:tuple[tuple], num_sessions=6) -> None:
4143
for i in range(num_sessions):
4244
t = ExThread(target=run_with_session, args=(i, func, args[i]))
4345
threads.append(t)
44-
46+
4547
for t in threads:
4648
t.start()
4749
for t in threads:
@@ -50,6 +52,7 @@ def run_batch(func:Callable, args:tuple[tuple], num_sessions=6) -> None:
5052
if t.error:
5153
raise t.error
5254

55+
5356
def run_batches(func:Callable, args:tuple[tuple], num_sessions=6):
5457
"""Run a function in parallel batches.
5558
This function runs the input function for each set of arguments in args.
@@ -63,6 +66,7 @@ def run_batches(func:Callable, args:tuple[tuple], num_sessions=6):
6366
batch = args[b:b+num_sessions]
6467
run_batch(func, args, len(batch))
6568

69+
6670
def spawn_sessions(num_sessions=6) -> tuple:
6771
"""A function to spawn multiple sessions of SAP.
6872
This function will attempt to spawn the desired number of sessions.
@@ -78,10 +82,10 @@ def spawn_sessions(num_sessions=6) -> tuple:
7882
7983
Returns:
8084
tuple: A tuple of all currently open sessions.
81-
"""
85+
"""
8286

83-
SAP = win32com.client.GetObject("SAPGUI")
84-
app = SAP.GetScriptingEngine
87+
sap = win32com.client.GetObject("SAPGUI")
88+
app = sap.GetScriptingEngine
8589
connection = app.Connections(0)
8690
session = connection.Sessions(0)
8791

@@ -92,7 +96,7 @@ def spawn_sessions(num_sessions=6) -> tuple:
9296

9397
for _ in range(num_sessions - connection.Sessions.count):
9498
session.CreateSession()
95-
99+
96100
# Wait for the sessions to spawn
97101
while connection.Sessions.count < num_sessions:
98102
time.sleep(0.1)
@@ -113,20 +117,22 @@ def spawn_sessions(num_sessions=6) -> tuple:
113117
x = i % c * w
114118
y = i // c * h
115119
win32gui.MoveWindow(hwnd, x, y, w, h, True)
116-
120+
117121
return sessions
118122

119-
def get_all_SAP_sessions() -> tuple:
123+
124+
def get_all_SAP_sessions() -> tuple: # pylint: disable=invalid-name
120125
"""Returns a tuple of all open SAP sessions (on connection index 0).
121126
122127
Returns:
123128
tuple: A tuple of SAP GuiSession objects.
124129
"""
125-
SAP = win32com.client.GetObject("SAPGUI")
126-
app = SAP.GetScriptingEngine
130+
sap = win32com.client.GetObject("SAPGUI")
131+
app = sap.GetScriptingEngine
127132
connection = app.Connections(0)
128133
return tuple(connection.Sessions)
129134

135+
130136
class ExThread(threading.Thread):
131137
"""A thread with a handle to get an exception raised inside the thread: ExThread.error"""
132138
def __init__(self, *args, **kwargs):
@@ -138,5 +144,3 @@ def run(self):
138144
self._target(*self._args, **self._kwargs)
139145
except Exception as e: # pylint: disable=broad-exception-caught
140146
self.error = e
141-
142-

ITK_dev_shared_components/SAP/opret_kundekontakt.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""This module provides a single function to conviniently peform the action 'opret-kundekontaker' in SAP."""
1+
"""This module provides a single function to conveniently peform the action 'opret-kundekontaker' in SAP."""
22

33
from typing import Literal
44
import win32clipboard
55
from ITK_dev_shared_components.SAP import tree_util
66

77

8-
def opret_kundekontakter(session, fp:str, aftaler:list[str] | None,
9-
art:Literal[' ', 'Automatisk', 'Fakturagrundlag', 'Fuldmagt ifm. værge', 'Konverteret', 'Myndighedshenvend.', 'Orientering', 'Returpost', 'Ringeaktivitet', 'Skriftlig henvend.', 'Telefonisk henvend.'],
8+
def opret_kundekontakter(session, fp:str, aftaler:list[str] | None,
9+
art:Literal[' ', 'Automatisk', 'Fakturagrundlag', 'Fuldmagt ifm. værge', 'Konverteret', 'Myndighedshenvend.', 'Orientering', 'Returpost', 'Ringeaktivitet', 'Skriftlig henvend.', 'Telefonisk henvend.'],
1010
notat:str, lock=None) -> None:
1111
"""Creates a kundekontakt on the given FP and aftaler.
1212
@@ -41,33 +41,25 @@ def opret_kundekontakter(session, fp:str, aftaler:list[str] | None,
4141

4242
# Go to editor and paste (lock if multithreaded)
4343
session.findById("wnd[0]/usr/subNOTICE:SAPLEENO:1002/btnEENO_TEXTE-EDITOR").press()
44-
if lock:
44+
if lock:
4545
lock.acquire()
46-
_setClipboard(notat)
46+
_set_clipboard(notat)
4747
session.findById("wnd[0]/tbar[1]/btn[9]").press()
48-
if lock:
48+
if lock:
4949
lock.release()
5050

5151
# Back and save
5252
session.findById("wnd[0]/tbar[0]/btn[3]").press()
5353
session.findById("wnd[0]/tbar[0]/btn[11]").press()
5454

5555

56-
def _setClipboard(text:str) -> None:
56+
def _set_clipboard(text:str) -> None:
57+
"""Private function to set text to the clipboard.
58+
59+
Args:
60+
text: Text to set to clipboard.
61+
"""
5762
win32clipboard.OpenClipboard()
5863
win32clipboard.EmptyClipboard()
5964
win32clipboard.SetClipboardText(text)
6065
win32clipboard.CloseClipboard()
61-
62-
63-
if __name__ == '__main__':
64-
from ITK_dev_shared_components.SAP import multi_session
65-
from datetime import datetime
66-
67-
session = multi_session.spawn_sessions(1)[0]
68-
fp = '25564617'
69-
aftaler = ['2291987', '2421562', '2311094']
70-
art = 'Orientering'
71-
notat = 'Test '+ str(datetime.now())
72-
73-
opret_kundekontakter(session, fp, aftaler, 'Automatisk', notat)

0 commit comments

Comments
 (0)