Skip to content

Commit ffe81fb

Browse files
committed
refactor: ruff checks
1 parent f5c8661 commit ffe81fb

36 files changed

+528
-491
lines changed

geos-trame/geos_trame/app/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
33
# SPDX-FileContributor: Lionel Untereiner
44
from pathlib import Path
5+
from typing import Any
56

67
from trame.app import get_server
8+
from trame_server import Server
79

810
from geos_trame.app.core import GeosTrame
911

1012

11-
def main( server=None, **kwargs ):
13+
def main( server: Server = None, **kwargs: Any ) -> None:
14+
"""Main function."""
1215
# Get or create server
1316
if server is None:
1417
server = get_server()

geos-trame/geos_trame/app/components/alertHandler.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88

99
class AlertHandler( vuetify3.VContainer ):
10-
"""
11-
Vuetify component used to display an alert status.
10+
"""Vuetify component used to display an alert status.
1211
1312
This alert will be displayed in the bottom right corner of the screen.
1413
It will be displayed until closed by the user or after 10 seconds if it is a success or warning.
1514
"""
1615

17-
def __init__( self ):
16+
def __init__( self ) -> None:
17+
"""Constructor."""
1818
super().__init__(
1919
fluid=True,
2020
classes="pa-0 ma-0",
@@ -31,31 +31,32 @@ def __init__( self ):
3131

3232
self.generate_alert_ui()
3333

34-
def generate_alert_ui( self ):
35-
"""
36-
Generate the alert UI.
34+
def generate_alert_ui( self ) -> None:
35+
"""Generate the alert UI.
3736
3837
The alert will be displayed in the bottom right corner of the screen.
3938
4039
Use an abritary z-index value to put the alert on top of the other components.
4140
"""
42-
with self:
43-
with vuetify3.VCol( style="width: 40%; position: fixed; right: 50px; bottom: 50px; z-index: 100;", ):
44-
vuetify3.VAlert(
45-
style="max-height: 20vh; overflow-y: auto",
46-
classes="ma-2",
47-
v_for=( "(status, index) in alerts", ),
48-
key="status",
49-
type=( "status.type", "info" ),
50-
text=( "status.message", "" ),
51-
title=( "status.title", "" ),
52-
closable=True,
53-
click_close=( self.on_close, f"[status.id]" ),
54-
)
55-
56-
def add_alert( self, type: str, title: str, message: str ):
57-
"""
58-
Add a status to the stack with a unique id.
41+
with (
42+
self,
43+
vuetify3.VCol( style="width: 40%; position: fixed; right: 50px; bottom: 50px; z-index: 100;", ),
44+
):
45+
vuetify3.VAlert(
46+
style="max-height: 20vh; overflow-y: auto",
47+
classes="ma-2",
48+
v_for=( "(status, index) in alerts", ),
49+
key="status",
50+
type=( "status.type", "info" ),
51+
text=( "status.message", "" ),
52+
title=( "status.title", "" ),
53+
closable=True,
54+
click_close=( self.on_close, "[status.id]" ),
55+
)
56+
57+
def add_alert( self, type: str, title: str, message: str ) -> None:
58+
"""Add a status to the stack with a unique id.
59+
5960
If there are more than 5 alerts displayed, remove the oldest.
6061
A warning will be automatically closed after 10 seconds.
6162
"""
@@ -77,21 +78,15 @@ def add_alert( self, type: str, title: str, message: str ):
7778
if type == "warning":
7879
asyncio.get_event_loop().call_later( self.__lifetime_of_alert, self.on_close, alert_id )
7980

80-
async def add_warning( self, title: str, message: str ):
81-
"""
82-
Add an alert of type "warning"
83-
"""
81+
async def add_warning( self, title: str, message: str ) -> None:
82+
"""Add an alert of type 'warning'."""
8483
self.add_alert( "warning", title, message )
8584

86-
async def add_error( self, title: str, message: str ):
87-
"""
88-
Add an alert of type "error"
89-
"""
85+
async def add_error( self, title: str, message: str ) -> None:
86+
"""Add an alert of type 'error'."""
9087
self.add_alert( "error", title, message )
9188

92-
def on_close( self, alert_id ):
93-
"""
94-
Remove in the state the alert associated to the given id.
95-
"""
89+
def on_close( self, alert_id: int ) -> None:
90+
"""Remove in the state the alert associated to the given id."""
9691
self.state.alerts = list( filter( lambda i: i[ "id" ] != alert_id, self.state.alerts ) )
9792
self.state.flush()

geos-trame/geos_trame/app/utils/properties_checker.py renamed to geos-trame/geos_trame/app/components/properties_checker.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,69 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
33
# SPDX-FileContributor: Kitware
4+
from typing import Any
45

56
from trame_client.widgets.core import AbstractElement
67
from trame_simput import get_simput_manager
78

89
from geos_trame.app.data_types.field_status import FieldStatus
910
from geos_trame.app.deck.tree import DeckTree
1011
from geos_trame.app.ui.viewer.regionViewer import RegionViewer
12+
from geos_trame.app.utils.geos_utils import group_name_ref_array_to_list
1113

12-
attributes_to_check = [ "region_attribute", "fields_to_import", "surfacicFieldsToImport" ]
14+
# Doc reference: https://geosx-geosx.readthedocs-hosted.com/en/latest/docs/sphinx/datastructure/CompleteXMLSchema.html
15+
attributes_to_check = [ ( "region_attribute", str ), ( "fields_to_import", list ), ( "surfacicFieldsToImport", list ) ]
1316

1417

1518
class PropertiesChecker( AbstractElement ):
16-
"""
17-
Class to check the validity of properties within a deck tree.
18-
"""
19+
"""Validity checker of properties within a deck tree."""
1920

20-
def __init__( self, tree: DeckTree, region_viewer: RegionViewer, **kwargs ):
21+
def __init__( self, tree: DeckTree, region_viewer: RegionViewer, **kwargs: Any ) -> None:
22+
"""Constructor."""
2123
super().__init__( "div", **kwargs )
2224

2325
self.tree = tree
2426
self.region_viewer = region_viewer
2527
self.simput_manager = get_simput_manager( id=self.state.sm_id )
2628

27-
def check_fields( self ):
28-
"""
29-
Get the names of all the cell data arrays from the input of the region viewer, then check that all the attributes
30-
in `attributes_to_check` have a value corresponding to one of the array names.
29+
def check_fields( self ) -> None:
30+
"""Check all the fields in the deck_tree.
31+
32+
Get the names of all the cell data arrays from the input of the region viewer, then check that
33+
all the attributes in `attributes_to_check` have a value corresponding to one of the array names.
3134
"""
3235
cellData = self.region_viewer.input.GetCellData()
3336
arrayNames = [ cellData.GetArrayName( i ) for i in range( cellData.GetNumberOfArrays() ) ]
34-
arrayNames.extend( [ "", "{}" ] ) # default values
3537
for field in self.state.deck_tree:
3638
self.check_field( field, arrayNames )
3739
self.state.dirty( "deck_tree" )
3840
self.state.flush()
3941

40-
def check_field( self, field, array_names: list[ str ] ):
41-
"""
42-
Check that all the attributes in `attributes_to_check` have a value corresponding to one of the array names.
42+
def check_field( self, field: dict, array_names: list[ str ] ) -> None:
43+
"""Check that all the attributes in `attributes_to_check` have a value corresponding to one of the array names.
44+
4345
Set the `valid` property to the result of this check, and if necessary, indicate which properties are invalid.
4446
"""
4547
field[ "valid" ] = FieldStatus.VALID.value
4648
field[ "invalid_properties" ] = []
4749

4850
proxy = self.simput_manager.proxymanager.get( field[ "id" ] )
4951
if proxy is not None:
50-
for attr in attributes_to_check:
51-
if attr in proxy.definition and proxy[ attr ] not in array_names:
52-
field[ "invalid_properties" ].append( attr )
52+
for attr, expected_type in attributes_to_check:
53+
if attr in proxy.definition:
54+
if ( expected_type is str and proxy[ attr ] # value is not empty (valid)
55+
and proxy[ attr ] not in array_names # value is not in the expected names
56+
):
57+
field[ "invalid_properties" ].append( attr )
58+
elif expected_type is list:
59+
arrays: list[ str ] | None = group_name_ref_array_to_list( proxy[ attr ] )
60+
if arrays is None:
61+
field[ "invalid_properties" ].append( attr )
62+
continue
63+
for array_name in arrays:
64+
if array_name not in array_names:
65+
field[ "invalid_properties" ].append( attr )
66+
break
5367

5468
if len( field[ "invalid_properties" ] ) != 0:
5569
field[ "valid" ] = FieldStatus.INVALID.value

geos-trame/geos_trame/app/core.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
from trame.decorators import TrameApp
77
from trame.widgets import html, simput
88
from trame.widgets import vuetify3 as vuetify
9+
from trame_server import Server
10+
from trame_server.controller import Controller
11+
from trame_server.state import State
912
from trame_simput import get_simput_manager
1013

1114
from geos_trame import module
1215
from geos_trame.app.deck.tree import DeckTree
1316
from geos_trame.app.io.data_loader import DataLoader
1417
from geos_trame.app.ui.viewer.regionViewer import RegionViewer
1518
from geos_trame.app.ui.viewer.wellViewer import WellViewer
16-
from geos_trame.app.utils.properties_checker import PropertiesChecker
19+
from geos_trame.app.components.properties_checker import PropertiesChecker
1720
from geos_trame.app.ui.editor import DeckEditor
1821
from geos_trame.app.ui.inspector import DeckInspector
1922
from geos_trame.app.ui.plotting import DeckPlotting
@@ -27,8 +30,8 @@
2730
@TrameApp()
2831
class GeosTrame:
2932

30-
def __init__( self, server, file_name: str ):
31-
33+
def __init__( self, server: Server, file_name: str ) -> None:
34+
"""Constructor."""
3235
self.alertHandler: AlertHandler | None = None
3336
self.deckPlotting: DeckPlotting | None = None
3437
self.deckViewer: DeckViewer | None = None
@@ -77,26 +80,32 @@ def __init__( self, server, file_name: str ):
7780
self.build_ui()
7881

7982
@property
80-
def state( self ):
83+
def state( self ) -> State:
84+
"""Getter for the state."""
8185
return self.server.state
8286

8387
@property
84-
def ctrl( self ):
88+
def ctrl( self ) -> Controller:
89+
"""Getter for the controller."""
8590
return self.server.controller
8691

87-
def set_input_file( self, file_name ):
88-
"""sets the input file of the InputTree object and populates simput/ui"""
92+
def set_input_file( self, file_name: str ) -> None:
93+
"""Sets the input file of the InputTree object and populates simput/ui."""
8994
self.tree.set_input_file( file_name )
9095

91-
def deck_ui( self ):
92-
"""Generates the UI for the deck edition / visualization tab"""
96+
def deck_ui( self ) -> None:
97+
"""Generates the UI for the deck edition / visualization tab."""
9398
with vuetify.VRow( classes="mb-6 fill-height" ):
9499
with vuetify.VCol(
95100
cols=2,
96101
order=1,
97102
):
98103
self.deckInspector = DeckInspector( source=self.tree, classes="fit-content" )
99-
vuetify.VBtn( text="Check fields", classes="ma-4", click=( self.properties_checker.check_fields, ) )
104+
vuetify.VBtn(
105+
text="Check fields",
106+
classes="ma-4",
107+
click=( self.properties_checker.check_fields, ),
108+
)
100109

101110
with vuetify.VCol(
102111
cols=10,
@@ -133,9 +142,8 @@ def deck_ui( self ):
133142
style="flex: 1; height: 40%; width: 100%;",
134143
)
135144

136-
def build_ui( self ):
137-
"""Generates the full UI for the GEOS Trame Application"""
138-
145+
def build_ui( self ) -> None:
146+
"""Generates the full UI for the GEOS Trame Application."""
139147
with VAppLayout( self.server ) as layout:
140148
self.simput_widget.register_layout( layout )
141149

@@ -154,18 +162,20 @@ def build_ui( self ):
154162
style=
155163
"position: absolute; top: 0; left: 0; height: 100%; width: 100%; display: flex; align-items: center; justify-content: center;",
156164
):
157-
with html.Div(
158-
v_if=( "tab_idx == 0", ),
159-
style=
160-
"height: 100%; width: 100%; display: flex; align-items: center; justify-content: flex-end;",
161-
):
162-
with vuetify.VBtn(
165+
with (
166+
html.Div(
167+
v_if=( "tab_idx == 0", ),
168+
style=
169+
"height: 100%; width: 100%; display: flex; align-items: center; justify-content: flex-end;",
170+
),
171+
vuetify.VBtn(
163172
click=self.tree.write_files,
164173
icon=True,
165174
style="z-index: 1;",
166175
id="save-button",
167-
):
168-
vuetify.VIcon( "mdi-content-save-outline" )
176+
),
177+
):
178+
vuetify.VIcon( "mdi-content-save-outline" )
169179

170180
with html.Div(
171181
style=

geos-trame/geos_trame/app/data_types/renderable.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
class Renderable( Enum ):
8+
"""Enum class for renderable types and their ids."""
89
VTKMESH = "VTKMesh"
910
INTERNALMESH = "InternalMesh"
1011
INTERNALWELL = "InternalWell"

geos-trame/geos_trame/app/data_types/tree_node.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
# SPDX-FileContributor: Kitware
44
from dataclasses import dataclass
55

6-
from geos_trame.app.data_types.field_status import FieldStatus
7-
86

97
@dataclass
108
class TreeNode:
9+
"""Single element of the tree, used by `DeckTree`.
10+
11+
`valid` has to be an int for serialization purposes, but is actually a FieldStatus so only possibles values are:
12+
- 0 (UNCHECKED): Validity check has not been performed.
13+
- 1 (VALID): TreeNode is checked and valid.
14+
- 2 (INVALID): TreeNode is checked and invalid.
1115
"""
12-
Single element of the tree, used by `DeckTree`.
13-
"""
16+
1417
id: str
1518
title: str
1619
children: list
@@ -21,22 +24,13 @@ class TreeNode:
2124

2225
@property
2326
def json( self ) -> dict:
24-
if self.children:
25-
return dict(
26-
id=self.id,
27-
title=self.title,
28-
is_drawable=self.is_drawable,
29-
drawn=self.drawn,
30-
valid=self.valid,
31-
children=[ c.json for c in self.children ],
32-
hidden_children=[ c.json for c in self.hidden_children ],
33-
)
34-
return dict(
35-
id=self.id,
36-
title=self.title,
37-
is_drawable=self.is_drawable,
38-
drawn=self.drawn,
39-
valid=self.valid,
40-
children=None,
41-
hidden_children=[],
42-
)
27+
"""Get the tree node as json."""
28+
return {
29+
"id": self.id,
30+
"title": self.title,
31+
"is_drawable": self.is_drawable,
32+
"drawn": self.drawn,
33+
"valid": self.valid,
34+
"children": [ c.json for c in self.children ] if self.children else None,
35+
"hidden_children": ( [ c.json for c in self.hidden_children ] if self.hidden_children else [] ),
36+
}

0 commit comments

Comments
 (0)