Skip to content

Commit 4b1b84b

Browse files
committed
refactor: ruff checks
1 parent e1395bd commit 4b1b84b

35 files changed

Lines changed: 502 additions & 484 deletions

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/core.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
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
@@ -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+
}

geos-trame/geos_trame/app/deck/file.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717

1818

1919
class DeckFile( object ):
20-
"""
21-
Holds the information of a deck file.
22-
Can be empty.
23-
"""
20+
"""Holds the information of a deck file. Can be empty."""
21+
22+
def __init__( self, filename: str, **kwargs: Any ) -> None:
23+
"""Constructor.
2424
25-
def __init__( self, filename: str, **kwargs ) -> None:
26-
"""
27-
Constructor.
2825
Input:
2926
filename: file name of the deck file
3027
"""
@@ -44,16 +41,15 @@ def __init__( self, filename: str, **kwargs ) -> None:
4441
self.path = os.path.dirname( self.filename )
4542

4643
def open_deck_file( self, filename: str ) -> None:
47-
"""
48-
Opens a file and parses it.
44+
"""Opens a file and parses it.
45+
4946
Input:
5047
filename: file name of the input file
5148
Signals:
5249
input_file_changed: On success
5350
Raises:
5451
GeosTrameException: On invalid input file
5552
"""
56-
5753
self.changed = False
5854
self.root_node = None
5955

@@ -81,18 +77,19 @@ def open_deck_file( self, filename: str ) -> None:
8177
attribute_name_generator=text.camel_case,
8278
)
8379
parser = XmlParser( context=context, config=ParserConfig(
84-
) ) # fail_on_unknown_properties=True, fail_on_unknown_attributes=True, fail_on_converter_warnings=True
80+
) )
8581
try:
8682
self.problem = parser.parse( simulation_deck, Problem )
8783
except ElementTree.XMLSyntaxError as e:
8884
msg = "Failed to parse input file %s:\n%s\n" % ( filename, e )
89-
raise GeosTrameException( msg )
85+
raise GeosTrameException( msg ) from e
9086

9187
encoder = DictEncoder( context=context, config=SerializerConfig( indent=" " ) )
9288
self.pb_dict = { "Problem": encoder.encode( self.problem ) }
9389
self.inspect_tree = build_inspect_tree( encoder.encode( self.problem ) )
9490

9591
def to_str( self ) -> str:
92+
"""Get the problem as a string."""
9693
config = SerializerConfig( indent=" ", xml_declaration=False )
9794
context = XmlContext(
9895
element_name_generator=text.pascal_case,
@@ -102,9 +99,8 @@ def to_str( self ) -> str:
10299
return serializer.render( self.problem )
103100

104101

105-
def build_inspect_tree( obj ) -> dict:
106-
"""Return the fields of a dataclass instance as a new dictionary mapping
107-
field names to field values.
102+
def build_inspect_tree( obj: dict ) -> dict:
103+
"""Return the fields of a dataclass instance as a new dictionary mapping field names to field values.
108104
109105
Example usage::
110106
@@ -121,25 +117,22 @@ class C:
121117
dataclass instances. This will also look into built-in containers:
122118
tuples, lists, and dicts. Other objects are copied with 'copy.deepcopy()'.
123119
"""
124-
125120
return _build_inspect_tree_inner( "Problem", obj, [] )
126121

127122

128-
def _build_inspect_tree_inner( key, obj, path ) -> dict:
129-
sub_node = dict()
130-
if "name" in obj:
131-
sub_node[ "title" ] = obj[ "name" ]
132-
else:
133-
sub_node[ "title" ] = key
134-
sub_node[ "children" ] = list()
135-
sub_node[ "is_drawable" ] = key in [
136-
"VTKMesh",
137-
"InternalMesh",
138-
"InternalWell",
139-
"VTKWell",
140-
"Perforation",
141-
]
142-
sub_node[ "drawn" ] = False
123+
def _build_inspect_tree_inner( key: str, obj: dict, path: list ) -> dict:
124+
sub_node = {
125+
"title": obj.get( "name", key ),
126+
"children": [],
127+
"is_drawable": key in [
128+
"VTKMesh",
129+
"InternalMesh",
130+
"InternalWell",
131+
"VTKWell",
132+
"Perforation",
133+
],
134+
"drawn": False,
135+
}
143136

144137
for key, value in obj.items():
145138

0 commit comments

Comments
 (0)