Skip to content

Commit ebcc5c1

Browse files
committed
Do not replace X$start/X$end markers for TWISS, RANGE=... command
Resolves: #142
1 parent 92ed319 commit ebcc5c1

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/cpymad/util.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def normalize_range_name(name: str) -> str:
165165
'echo', 'title', 'text', 'format', 'dir'}
166166

167167

168-
def format_param(key: str, value) -> str:
168+
def format_param(key: str, value, cmd: str = None) -> str:
169169
"""
170170
Format a single MAD-X command parameter.
171171
@@ -189,7 +189,7 @@ def format_param(key: str, value) -> str:
189189
elif isinstance(value, bool):
190190
return key + '=' + str(value).lower()
191191
elif key == 'range' or isinstance(value, Range):
192-
return key + '=' + _format_range(value)
192+
return key + '=' + _format_range(value, cmd)
193193
# check for basestrings before abc.Sequence, because every
194194
# string is also a Sequence:
195195
elif isinstance(value, str):
@@ -210,14 +210,32 @@ def format_param(key: str, value) -> str:
210210
return key + '=' + str(value)
211211

212212

213-
def _format_range(value) -> str:
213+
def _format_range(value, cmd: str = None) -> str:
214+
# NOTE: To avoid falsely overriding user input, it is preferred to not
215+
# replace X$start/X$end markers by #s/#e if the corresponding command
216+
# can handle the markers. See #142.
217+
# - TWISS: Seems to handle $start/$end markers appropriately.
218+
# - CONSTRAINT: Seems to not handle these markers
219+
# - others: TBD
220+
# For now, replace it for all commands except twiss.. This may change in
221+
# the future.
222+
start_end_markers_are_supported = (
223+
cmd is not None and
224+
cmd.lower() == 'twiss')
214225
if isinstance(value, str):
215-
return normalize_range_name(value)
226+
if start_end_markers_are_supported:
227+
return value.lower()
228+
else:
229+
return normalize_range_name(value)
216230
elif isinstance(value, Range):
217231
begin, end = value.first, value.last
218232
else:
219233
begin, end = value
220-
begin, end = normalize_range_name((str(begin), str(end)))
234+
if start_end_markers_are_supported:
235+
begin = str(begin).lower()
236+
end = str(end).lower()
237+
else:
238+
begin, end = normalize_range_name((str(begin), str(end)))
221239
return begin + '/' + end
222240

223241

@@ -285,16 +303,19 @@ def format_str(value):
285303
return mad_quote(value.lower())
286304
if dtype == PARAM_TYPE_STRING:
287305
if key == 'range' or isinstance(value, Range):
288-
return key + '=' + _format_range(value)
306+
return key + '=' + _format_range(value, cmd.name)
289307
if isinstance(value, str):
290308
return key + '=' + format_str(value)
291309
if dtype == PARAM_TYPE_STRING_ARRAY:
292310
# NOTE: allowing single scalar value to STRING_ARRAY, mainly useful
293311
# for `match`, see above.
294312
if key == 'range' or isinstance(value, Range):
295313
if isinstance(value, list):
296-
return key + '={' + ','.join(map(_format_range, value)) + '}'
297-
return key + '=' + _format_range(value)
314+
return key + '={' + ','.join([
315+
_format_range(v, cmd.name)
316+
for v in value
317+
]) + '}'
318+
return key + '=' + _format_range(value, cmd.name)
298319
if isinstance(value, str):
299320
return key + '=' + format_str(value)
300321
if isinstance(value, abc.Sequence):
@@ -328,7 +349,7 @@ def format_command(*args, **kwargs) -> str:
328349
if isinstance(cmd, str):
329350
_args = [cmd] + list(args)
330351
_keys = ordered_keys(kwargs)
331-
_args += [format_param(k, kwargs[k]) for k in _keys]
352+
_args += [format_param(k, kwargs[k], cmd) for k in _keys]
332353
else:
333354
_args = [cmd.name] + list(args)
334355
_keys = ordered_keys(kwargs)

test/test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_format_cmdpar(mad):
143143

144144
assert fmt(mult, 'knl', None) == ''
145145

146-
assert fmt(twiss, 'range', 'h$start/h$end') == 'range=#s/#e'
146+
assert fmt(twiss, 'range', 'h$start/h$end') == 'range=h$start/h$end'
147147
assert fmt(twiss, 'range', ('a', 'b')) == 'range=a/b'
148148
assert fmt(match, 'range', '#s/#e') == 'range=#s/#e'
149149
assert fmt(match, 'range', ['#s/#e', 's/e']) == 'range={#s/#e,s/e}'

0 commit comments

Comments
 (0)