Skip to content

Commit f4fc08e

Browse files
authored
Merge pull request #60 from multiscale/ruff-format
Apply ruff format
2 parents 184d5f5 + dba451e commit f4fc08e

57 files changed

Lines changed: 4638 additions & 3873 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.git-blame-ignore-revs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ruff format
2+
c69bbeff1d5171e230eb069112fdb65da1476a82

‎tox.ini‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ commands =
1414
mypy
1515
pytest {posargs}
1616
ruff check
17+
ruff format --check
1718

1819
[gh-actions]
1920
python =

‎ymmsl/__init__.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This package contains all the classes needed to represent a yMMSL file,
44
as well as to read and write yMMSL files.
55
"""
6+
67
from importlib.metadata import version as package_version
78

89
from ymmsl.conversion.converter import DowngradeError, convert_to
@@ -13,10 +14,18 @@
1314
from ymmsl.v0_2 import Operator, Settings
1415

1516
__version__ = package_version("ymmsl")
16-
__author__ = 'Lourens Veen'
17-
__email__ = 'l.veen@esciencecenter.nl'
17+
__author__ = "Lourens Veen"
18+
__email__ = "l.veen@esciencecenter.nl"
1819

1920

2021
__all__ = [
21-
'convert_to', 'Document', 'dump', 'DowngradeError', 'load', 'load_as',
22-
'Operator', 'save', 'Settings']
22+
"convert_to",
23+
"Document",
24+
"dump",
25+
"DowngradeError",
26+
"load",
27+
"load_as",
28+
"Operator",
29+
"save",
30+
"Settings",
31+
]

‎ymmsl/command_line.py‎

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313

1414

1515
def showwarning(
16-
message: Union[Warning, str], category: Type[Warning], filename: str,
17-
lineno: int, file: Optional[TextIO] = None, line: Optional[str] = None) -> None:
18-
print(f'WARNING: {message}', file=file)
16+
message: Union[Warning, str],
17+
category: Type[Warning],
18+
filename: str,
19+
lineno: int,
20+
file: Optional[TextIO] = None,
21+
line: Optional[str] = None,
22+
) -> None:
23+
print(f"WARNING: {message}", file=file)
1924

2025

2126
warnings.showwarning = showwarning
@@ -33,24 +38,37 @@ def ymmsl() -> None:
3338

3439

3540
_version_tag_to_type: Dict[str, Type] = {
36-
'v0.1': v0_1.PartialConfiguration,
37-
'v0.2': v0_2.Configuration,
38-
}
41+
"v0.1": v0_1.PartialConfiguration,
42+
"v0.2": v0_2.Configuration,
43+
}
3944

4045

41-
@ymmsl.command(short_help='Convert a yMMSL file to a newer version')
46+
@ymmsl.command(short_help="Convert a yMMSL file to a newer version")
4247
@click.argument(
43-
'input_file', default='-', type=click.Path(
44-
exists=True, file_okay=True, dir_okay=False, readable=True,
45-
resolve_path=False, allow_dash=True))
48+
"input_file",
49+
default="-",
50+
type=click.Path(
51+
exists=True,
52+
file_okay=True,
53+
dir_okay=False,
54+
readable=True,
55+
resolve_path=False,
56+
allow_dash=True,
57+
),
58+
)
4659
@click.argument(
47-
'output_file', required=False, type=click.Path(
48-
file_okay=True, dir_okay=True, writable=True, resolve_path=False,
49-
allow_dash=True))
50-
@click.option(
51-
'-t', '--to', default='v0.2', help='Version to convert to, e.g. "v0.2".')
52-
def convert(
53-
input_file: str, output_file: Optional[str], to: str) -> None:
60+
"output_file",
61+
required=False,
62+
type=click.Path(
63+
file_okay=True,
64+
dir_okay=True,
65+
writable=True,
66+
resolve_path=False,
67+
allow_dash=True,
68+
),
69+
)
70+
@click.option("-t", "--to", default="v0.2", help='Version to convert to, e.g. "v0.2".')
71+
def convert(input_file: str, output_file: Optional[str], to: str) -> None:
5472
"""Convert a yMMSL file to a later version
5573
5674
When upgrading in place, and/or if an output file is specified and it exists, a
@@ -98,16 +116,17 @@ def convert(
98116
won't work, because the shell will open the file to do the redirect, empty it, and
99117
then run ymmsl convert, which then fails because the input is empty.
100118
"""
101-
if input_file != '-':
102-
print(f'Converting {input_file}')
119+
if input_file != "-":
120+
print(f"Converting {input_file}")
103121

104122
if output_file is None:
105123
output_file = input_file
106124

107125
if to not in _version_tag_to_type:
108126
click.echo(
109-
f'Invalid version {to} specified. Supported versions are v0.1 and v0.2',
110-
err=True)
127+
f"Invalid version {to} specified. Supported versions are v0.1 and v0.2",
128+
err=True,
129+
)
111130
exit(1)
112131

113132
try:
@@ -116,22 +135,24 @@ def convert(
116135
except DowngradeError:
117136
click.echo()
118137
click.echo(
119-
f'It seems that {input_file} is of a newer version than the target'
120-
f' version {to}.')
138+
f"It seems that {input_file} is of a newer version than the target"
139+
f" version {to}."
140+
)
121141
click.echo()
122142
click.echo(
123-
'This tool can only upgrade files, not downgrade them, so that does'
124-
' not work.')
143+
"This tool can only upgrade files, not downgrade them, so that does"
144+
" not work."
145+
)
125146
exit(1)
126147

127-
if output_file != '-' and os.path.exists(output_file):
128-
backup_file = output_file + '.bak'
148+
if output_file != "-" and os.path.exists(output_file):
149+
backup_file = output_file + ".bak"
129150
if not os.path.exists(backup_file):
130151
copyfile(output_file, backup_file)
131-
print(f'Wrote backup file {backup_file}')
152+
print(f"Wrote backup file {backup_file}")
132153

133-
with click.open_file(output_file, 'w') as output_stream:
154+
with click.open_file(output_file, "w") as output_stream:
134155
save(document, output_stream)
135156

136-
if input_file != '-':
137-
print('Conversion complete')
157+
if input_file != "-":
158+
print("Conversion complete")

‎ymmsl/conversion/convert_v0_1_to_v0_2.py‎

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@ def convert_v0_1_to_v0_2(config: v0_1.PartialConfiguration) -> v0_2.Configuratio
1515
Returns:
1616
The corresponding configuration expressed in yMMSL v0.2.
1717
"""
18-
description = 'Please add a description'
18+
description = "Please add a description"
1919
if config.description:
2020
description = config.description
2121
models = [convert_model(config.model)] if config.model is not None else None
2222
settings = deepcopy(config.settings)
2323
programs = [
24-
convert_implementation(impl) for impl in config.implementations.values()]
24+
convert_implementation(impl) for impl in config.implementations.values()
25+
]
2526
if programs:
2627
warnings.warn(
27-
'In yMMSL v0.2 implementations have become programs, and you can now'
28-
' specify the ports of a program in the yMMSL description. If your'
29-
' program has fixed ports then you should do this, because it will make'
30-
' incorrect wiring easier to debug. While there, add a description too!',
28+
"In yMMSL v0.2 implementations have become programs, and you can now"
29+
" specify the ports of a program in the yMMSL description. If your"
30+
" program has fixed ports then you should do this, because it will make"
31+
" incorrect wiring easier to debug. While there, add a description too!",
3132
stacklevel=2,
3233
)
3334

@@ -36,27 +37,40 @@ def convert_v0_1_to_v0_2(config: v0_1.PartialConfiguration) -> v0_2.Configuratio
3637
resume = deepcopy(config.resume)
3738

3839
warnings.warn(
39-
'Comments can unfortunately not be read by this converter, and so have been'
40-
' ignored. Please copy them into an appropriate description field.',
40+
"Comments can unfortunately not be read by this converter, and so have been"
41+
" ignored. Please copy them into an appropriate description field.",
4142
stacklevel=2,
4243
)
4344

4445
return v0_2.Configuration(
45-
description, None, models, None, settings, programs, resources, checkpoints,
46-
resume)
46+
description,
47+
None,
48+
models,
49+
None,
50+
settings,
51+
programs,
52+
resources,
53+
checkpoints,
54+
resume,
55+
)
4756

4857

4958
def convert_component(component: v0_1.Component) -> v0_2.Component:
5059
"""Convert a v0.1 Component object to a v0.2 Component."""
5160
ports = component.ports if component.ports else v0_1.Ports()
52-
description = 'Please add a description'
61+
description = "Please add a description"
5362
implementation: Optional[str] = None
5463
if component.implementation is not None:
5564
implementation = str(component.implementation)
5665

5766
return v0_2.Component(
58-
str(component.name), convert_ports(ports), description, implementation,
59-
False, component.multiplicity)
67+
str(component.name),
68+
convert_ports(ports),
69+
description,
70+
implementation,
71+
False,
72+
component.multiplicity,
73+
)
6074

6175

6276
def convert_conduit(conduit: v0_1.Conduit) -> v0_2.Conduit:
@@ -77,27 +91,31 @@ def infer_ports(components: List[v0_2.Component], conduits: List[v0_2.Conduit])
7791
for conduit in conduits:
7892
if conduit.sending_component() == component.name:
7993
component.ports[conduit.sending_port()] = v0_2.Port(
80-
conduit.sending_port(), v0_2.Operator.O_F,
81-
v0_2.Timeline(''))
94+
conduit.sending_port(), v0_2.Operator.O_F, v0_2.Timeline("")
95+
)
8296
if component.name not in changed_components:
8397
changed_components.append(component.name)
8498

8599
if conduit.receiving_component() == component.name:
86100
component.ports[conduit.receiving_port()] = v0_2.Port(
87-
conduit.receiving_port(), v0_2.Operator.F_INIT,
88-
v0_2.Timeline(''))
101+
conduit.receiving_port(),
102+
v0_2.Operator.F_INIT,
103+
v0_2.Timeline(""),
104+
)
89105
if component.name not in changed_components:
90106
changed_components.append(component.name)
91107

92108
if changed_components:
93-
ch_comp_list = '\n - ' + '\n - '.join(map(str, changed_components))
109+
ch_comp_list = "\n - " + "\n - ".join(map(str, changed_components))
94110
warnings.warn(
95-
'In yMMSL v0.2 components are required to declare their ports. The'
96-
' following components did not have a ports declaration, so one has'
97-
' been added based on the connected conduits. THIS MAY BE WRONG,'
98-
' because the operators have all been set to F_INIT and O_F, while they'
99-
' may really be O_I or S. Please check these components and adjust'
100-
f' as needed: {ch_comp_list}', stacklevel=4)
111+
"In yMMSL v0.2 components are required to declare their ports. The"
112+
" following components did not have a ports declaration, so one has"
113+
" been added based on the connected conduits. THIS MAY BE WRONG,"
114+
" because the operators have all been set to F_INIT and O_F, while they"
115+
" may really be O_I or S. Please check these components and adjust"
116+
f" as needed: {ch_comp_list}",
117+
stacklevel=4,
118+
)
101119

102120

103121
def convert_model(model: v0_1.ModelReference) -> v0_2.Model:
@@ -109,14 +127,15 @@ def convert_model(model: v0_1.ModelReference) -> v0_2.Model:
109127
Returns:
110128
The corresponding configuration expressed in yMMSL v0.2.
111129
"""
112-
description = 'Please add a description'
130+
description = "Please add a description"
113131

114132
if isinstance(model, v0_1.Model):
115133
components = list(map(convert_component, model.components))
116134
conduits = list(map(convert_conduit, model.conduits))
117135
infer_ports(components, conduits)
118136
return v0_2.Model(
119-
str(model.name), None, description, None, components, conduits)
137+
str(model.name), None, description, None, components, conduits
138+
)
120139
else:
121140
return v0_2.Model(str(model.name), None, description, None, [], [])
122141

@@ -133,7 +152,7 @@ def convert_implementation(impl: v0_1.Implementation) -> v0_2.Program:
133152
Returns:
134153
The corresponding program expressed in yMMSL v0.2.
135154
"""
136-
description = 'Please add a description'
155+
description = "Please add a description"
137156
base_env: Optional[v0_1.BaseEnv] = impl.base_env
138157
env: Optional[Dict[str, str]] = impl.env
139158

@@ -146,9 +165,21 @@ def convert_implementation(impl: v0_1.Implementation) -> v0_2.Program:
146165
env = None
147166

148167
return v0_2.Program(
149-
str(impl.name), None, description, None, base_env, impl.modules,
150-
impl.virtual_env, env, execution_model, impl.executable, impl.args,
151-
impl.script, impl.can_share_resources, impl.keeps_state_for_next_use)
168+
str(impl.name),
169+
None,
170+
description,
171+
None,
172+
base_env,
173+
impl.modules,
174+
impl.virtual_env,
175+
env,
176+
execution_model,
177+
impl.executable,
178+
impl.args,
179+
impl.script,
180+
impl.can_share_resources,
181+
impl.keeps_state_for_next_use,
182+
)
152183

153184

154185
def convert_ports(ports: v0_1.Ports) -> v0_2.Ports:
@@ -161,23 +192,26 @@ def convert_ports(ports: v0_1.Ports) -> v0_2.Ports:
161192
The corresponding ports expressed in yMMSL v0.2.
162193
"""
163194
return v0_2.Ports(
164-
list(map(str, ports.f_init)),
165-
list(map(str, ports.o_i)),
166-
list(map(str, ports.s)),
167-
list(map(str, ports.o_f)))
195+
list(map(str, ports.f_init)),
196+
list(map(str, ports.o_i)),
197+
list(map(str, ports.s)),
198+
list(map(str, ports.o_f)),
199+
)
168200

169201

170202
def convert_resources(
171-
resources: MutableMapping[v0_1.Reference, v0_1.ResourceRequirements],
172-
models: Optional[List[v0_2.Model]]
173-
) -> MutableMapping[v0_2.Reference, v0_2.ResourceRequirements]:
203+
resources: MutableMapping[v0_1.Reference, v0_1.ResourceRequirements],
204+
models: Optional[List[v0_2.Model]],
205+
) -> MutableMapping[v0_2.Reference, v0_2.ResourceRequirements]:
174206
if not models:
175207
warnings.warn(
176-
'When specifying resources in yMMSL v0.2, component names must be'
177-
' prefixed with the name of the top (outermost) model, e.g. as'
178-
' my_model.my_component rather than just my_component. This file'
179-
' does not contain a model, so its name cannot be added automatically.'
180-
' Please add the model name yourself.', stacklevel=3)
208+
"When specifying resources in yMMSL v0.2, component names must be"
209+
" prefixed with the name of the top (outermost) model, e.g. as"
210+
" my_model.my_component rather than just my_component. This file"
211+
" does not contain a model, so its name cannot be added automatically."
212+
" Please add the model name yourself.",
213+
stacklevel=3,
214+
)
181215
return deepcopy(resources)
182216
else:
183217
mname = models[0].name

0 commit comments

Comments
 (0)