Reference parsers

references

class momotor.options.parser.reference.Reference(provider, id, name, _source)

A reference

id: Optional[str]

The id of the reference

name: Optional[str]

The name of the reference, including the class or domain parts

provider: Optional[str]

The provider of the references

class momotor.options.parser.reference.ReferenceMatch(provider, values)

A reference match

provider: Union[momotor.bundles.recipe.RecipeBundle, momotor.bundles.config.ConfigBundle, momotor.bundles.product.ProductBundle, momotor.bundles.elements.steps.Step, momotor.bundles.elements.result.Result]

The provider containing the elements referenced

values: Tuple[Union[momotor.bundles.elements.properties.Property, momotor.bundles.elements.files.File, momotor.bundles.elements.options.Option, momotor.bundles.elements.result.Result]]

A tuple with the referenced elements

momotor.options.parser.reference.generate_reference(type_, refs, bundles)

A generator producing reference matches.

Each ReferenceMatch object generated is a single reference resolved.

Parameters
Return type

Generator[ReferenceMatch, None, None]

momotor.options.parser.reference.parse_reference(reference)

Parse a reference into its parts.

Parameters

reference (str) – the reference to parse

Return type

Tuple[str, Tuple[Reference], str]

Returns

a 3-tuple containing the type, a tuple of Reference objects, and a string with the rest of the reference string remaining after parsing.

Raises

ValueError – the reference cannot be parsed

Examples:

>>> parse_reference("type")
('type', (Reference(provider=None, id=None, name=None, _source=''),), '')
>>> parse_reference("type rest")
('type', (Reference(provider=None, id=None, name=None, _source=''),), ' rest')
>>> parse_reference("type[] rest")
('type', (Reference(provider=None, id=None, name=None, _source=''),), ' rest')
>>> parse_reference("type[@provider] rest")
('type', (Reference(provider='provider', id=None, name=None, _source='@provider'),), ' rest')
>>> parse_reference("type[@provider#id:class#name] rest")
('type', (Reference(provider='provider', id='id', name='class#name', _source='@provider#id:class#name'),), ' rest')
>>> parse_reference("type[@provider#id,id2:class#name] rest")
('type', (Reference(provider='provider', id='id,id2', name='class#name', _source='@provider#id,id2:class#name'),), ' rest')
momotor.options.parser.reference.resolve_reference_value(value_reference, bundles, *, default_mod='join')

Resolve a reference value string into the value

Parameters
Return type

Tuple[Union[str, int, float, bool, None], str]

Returns

The resolved value

momotor.options.parser.reference.select_by_file_reference(reference, bundles)

Parse a file reference string and collect the referenced files.

This is similar to the file[...] reference syntax, but does not require the file nor the square brackets.

Parameters
  • reference (str) – The reference to parse

  • bundles (Providers) – The bundles to resolve the references too

Return type

Tuple[Tuple[ReferenceMatch], str]

Returns

a 2-tuple containing a tuple of ReferenceMatch objects, and a string with the rest of the reference string remaining after parsing.

momotor.options.parser.reference.select_by_opt_reference(reference, bundles)

Parse an option reference string and collect the referenced options.

This is similar to the opt[...] reference syntax, but does not require the opt nor the square brackets.

Parameters
  • reference (str) –

  • bundles (Providers) – The bundles to resolve the references too

Return type

Tuple[Tuple[ReferenceMatch], str]

Returns

a 2-tuple containing a tuple of ReferenceMatch objects, and a string with the rest of the reference string remaining after parsing.

momotor.options.parser.reference.select_by_prop_reference(reference, results=None, task_id=None)

Parse a property reference string and collect the referenced properties.

This is similar to the prop[...] reference syntax, but does not require the prop nor the square brackets.

Parameters
Return type

Tuple[Tuple[ReferenceMatch], str]

Returns

a 2-tuple containing a tuple of ReferenceMatch objects, and a string with the rest of the reference string remaining after parsing.

momotor.options.parser.reference.select_by_reference(reference, bundles)

Parse a reference string and collect the referenced items

Parameters
  • reference (str) – The reference string to parse

  • bundles (Providers) – The providers providing the objects that are referenced

Return type

Tuple[Optional[str], Tuple[ReferenceMatch], str]

Returns

a 3-tuple containing the type, a tuple of ReferenceMatch objects, and a string with the rest of the reference string remaining after parsing.

Raises

ValueError – the reference cannot be parsed

selectors

momotor.options.parser.selector.filter_by_selector(selector, bundles)

Filter the elements selected by selector from the bundles

Parameters
  • selector (str) – the selector to parse

  • bundles (Providers) – The bundles to resolve the references too

Return type

Tuple[Tuple[Element], str]

Returns

a 2-tuple containing a tuple with the selected elements, and a string with the rest of the selector string remaining after parsing.

Raises

ValueError – if the selector is not valid

momotor.options.parser.selector.match_by_selector(selector, bundles, *, default_mod='all')

Match the elements selected by match selector from the bundles

Parameters
  • selector (str) – the match selector to parse

  • bundles (Providers) – The bundles to resolve the references too

  • default_mod (str) – Default ‘mod’

Return type

Tuple[bool, str]

Returns

a 2-tuple containing a boolean indicating if there was a match, and a string with the rest of the selector string remaining after parsing.

Raises

ValueError – if the selector is not valid

momotor.options.parser.selector.parse_selector(selector)

Parse a selector into its parts.

Parameters

selector (str) – the selector to parse

Returns

a 5-tuple containing the type, a tuple of Reference objects, the operator, the value, and a string with the rest of the selector string remaining after parsing.

Raises

ValueError – the selector cannot be parsed

Examples:

>>> parse_selector('pass')
('pass', (Reference(provider=None, id=None, name=None, _source=''),), None, None, '')
>>> parse_selector('prop[#test]?')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '?', None, '')
>>> parse_selector('prop[#test]?123')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '?', None, '123')
>>> parse_selector('prop[#test]>0')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 0, '')
>>> parse_selector('prop[#test]>1_000')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 1000, '')
>>> parse_selector('prop[#test]>1.5')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 1.5, '')
>>> parse_selector('prop[#test]>-2')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', -2, '')
>>> parse_selector('prop[#test]>-2e2')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', -200.0, '')
>>> parse_selector('prop[#test]>2e-2')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 0.02, '')
>>> parse_selector('prop[#test]=="test string"')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '==', 'test string', '')
>>> parse_selector('prop[#test]>0 123')
('prop', (Reference(provider=None, id='test', name=None, _source='#test'),), '>', 0, ' 123')
Return type

Tuple[str, Tuple[Reference], Optional[str], Union[None, str, int, float], str]

Returns

momotor.options.parser.selector.resolve_selector(selector, bundles)

Resolve all parts of a selector

Parameters
  • selector (str) – the selector to parse

  • bundles (Providers) – The bundles to resolve the references too

Return type

Tuple[str, Iterable[ReferenceMatch], Callable, Union[None, str, int, float], str]

Returns

A 5-tuple containing the attribute to get the reference value from (based on type), an iterator for ReferenceMatch objects, the operator, the value, and a string with the rest of the selector string remaining after parsing.

Raises

ValueError – if the selector is not valid

placeholders

momotor.options.parser.placeholders.replace_placeholders(value, bundles, *, value_processor=None)

Replace all placeholders in value with their resolved values. Placeholders are resolved recursively, i.e. if a resolved value contains more placeholders, these will be resolved as well.

Parameters
  • value (~VT) – the string containing placeholders to resolve. If value is not a string, no processing is done and value is returned unmodified.

  • bundles (Providers) – the bundles to resolve the references too

  • value_processor (Optional[Callable[[Optional[str]], str]]) – a callable that is called with every resolved value, can be used to modify placeholders. If not supplied, uses str to cast the returned value to a string.

Return type

~VT

Returns

the value with all placeholders resolved