DontBmad

converters.dontbmad.assign_property(line: str, context: dict) dict

Assign a property of an element to the context.

Parameters:
  • line – Line of a property assignment to be parsed.

  • context – Dictionary of variables to assign the property to and from which to read variables.

Returns:

Updated context.

converters.dontbmad.assign_variable(line: str, context: dict) dict

Assign a variable to the context.

Parameters:
  • line – Line of a variable assignment to be parsed.

  • context – Dictionary of variables to assign the variable to and from which to read variables.

Returns:

Updated context.

converters.dontbmad.convert_bmad_lattice(bmad_lattice_file_path: Path, environment_variables: dict | None = None) Element

Convert a Bmad lattice file to a Cheetah Segment.

NOTE: This function was designed at the example of the LCLS lattice. While this

lattice is extensive, this function might not properly convert all features of a Bmad lattice. If you find that this function does not work for your lattice, please open an issue on GitHub.

Parameters:
  • bmad_lattice_file_path – Path to the Bmad lattice file.

  • environment_variables – Dictionary of environment variables to use when parsing the lattice file.

Returns:

Cheetah Segment representing the Bmad lattice.

converters.dontbmad.convert_element(name: str, context: dict) Element

Convert a parsed Bmad element dict to a cheetah Element.

Parameters:
  • name – Name of the (top-level) element to convert.

  • context – Context dictionary parsed from Bmad lattice file(s).

Returns:

Converted cheetah Element. If you are calling this function yourself as a user of Cheetah, this is most likely a Segment.

converters.dontbmad.define_element(line: str, context: dict) dict

Define an element in the context.

Parameters:
  • line – Line of an element definition to be parsed.

  • context – Dictionary of variables to define the element in and from which to read variables.

Returns:

Updated context.

converters.dontbmad.define_line(line: str, context: dict) dict

Define a beam line in the context.

Parameters:
  • line – Line of a beam line definition to be parsed.

  • context – Dictionary of variables to define the beam line in and from which to read variables.

Returns:

Updated context.

converters.dontbmad.define_overlay(line: str, context: dict) dict

Define an overlay in the context.

Parameters:
  • line – Line of an overlay definition to be parsed.

  • context – Dictionary of variables to define the overlay in and from which to read variables.

Returns:

Updated context.

converters.dontbmad.evaluate_expression(expression: str, context: dict) Any

Evaluate an expression in the context of a dictionary of variables.

Parameters:
  • expression – Expression to evaluate.

  • context – Dictionary of variables to evaluate the expression in the context of.

Returns:

Result of evaluating the expression.

converters.dontbmad.merge_delimiter_continued_lines(lines: list[str], delimiter: str, remove_delimiter: bool = False) list[str]

Merge lines ending with some character as a delimitter with the following line.

Parameters:
  • lines – List of lines to merge.

  • delimitter – Character to use as a delimitter.

  • remove_delimitter – Whether to remove the delimitter from the merged line.

Returns:

List of lines with ampersand-continued lines merged.

converters.dontbmad.parse_lines(lines: str) dict

Parse a list of lines from a Bmad lattice file. They should be cleaned and merged before being passed to this function.

Parameters:

lines – List of lines to parse.

Returns:

Dictionary of variables defined in the lattice file.

converters.dontbmad.parse_use_line(line: str, context: dict) dict

Parse a use line.

Parameters:
  • line – Line of a use statement to be parsed.

  • context – Dictionary of variables to define the overlay in and from which to read variables.

Returns:

Updated context.

converters.dontbmad.read_clean_lines(lattice_file_path: Path) list[str]

Recursevely read lines from Bmad lattice files, removing comments and empty lines, and replacing lines calling external files with the lines of the external file.

Parameters:

lattice_file_path – Path to the root Bmad lattice file.

Returns:

List of lines from the root Bmad lattice file and all external files.

converters.dontbmad.resolve_object_name_wildcard(wildcard_pattern: str, context: dict) list

Return a list of object names that match the given wildcard pattern.

Parameters:
  • wildcard_pattern – Wildcard pattern to match.

  • context – Dictionary of variables among which to search for matching object.

Returns:

List of object names that match the given wildcard pattern, both in terms of name and element type.

converters.dontbmad.validate_understood_properties(understood: list[str], properties: dict) None

Validate that all properties are understood. This function primarily ensures that properties not understood by Cheetah are not ignored silently.

Raises an AssertionError if a property is found that is not understood.

Parameters:
  • understood – List of properties understood (or purpusefully ignored) by Cheetah.

  • properties – Dictionary of properties to validate.

Returns:

None