lewis.utils.command_builder
A fluent command builder for lewis.
Members
Build a command for the stream adapter. |
- class lewis.utils.command_builder.CmdBuilder(target_method, arg_sep='', ignore='', ignore_case=False)[source]
Bases:
object
Build a command for the stream adapter.
Do this by creating this object, adding the values and then building it (this uses a fluent interface).
For example to read a pressure the ioc might send “pres?” and when that happens this should call get_pres command would be: >>> CmdBuilder(“get_pres”).escape(“pres?”).build() This will generate the regex needed by Lewis. The escape is just making sure none of the characters are special reg ex characters. If you wanted to set a pressure the ioc might send “pres <pressure>” where <pressure> is a floating point number, the interface should call set_pres with that number. Now use: >>> CmdBuilder(“set_pres”).escape(“pres “).float().build() this add float as a regularly expression capture group for your argument. It is equivalent to: >>> Cmd(“set_pres”, “pres ([+-]?d+.?d*)”) There are various arguments like int and digit. Finally some special characters are included so if your protocol uses enquirey character ascii 5 you can match is using >>> CmdBuilder(“set_pres”).escape(“pres?”).enq().build()
Create a builder. Use build to create the final object
- Parameters:
target_method – name of the method target to call when the reg ex matches
arg_sep – separators between arguments which are next to each other
ignore – set of characters to ignore between text and arguments
ignore_case – ignore the case when matching command
- ack() CmdBuilder [source]
Add the ACK character (0x6) to the string.
- Returns:
builder
- add_ascii_character(char_number: int) CmdBuilder [source]
Add a single character based on its integer value, e.g. 49 is ‘a’.
- Parameters:
char_number – character number
- Returns:
self
- any() CmdBuilder [source]
Add an argument that matches anything.
- Returns:
builder
- any_except(char: str) CmdBuilder [source]
Adds an argument that matches anything other than a specified character (useful for commands containing delimiters)
- Parameters:
char – the character not to match
- Returns:
builder
- arg(arg_regex, argument_mapping: ~functools.partial = functools.partial(<class 'str'>, encoding='utf-8')) CmdBuilder [source]
Add an argument to the command.
- Parameters:
arg_regex – regex for the argument (capture group will be added)
argument_mapping – the type mapping for the argument (default is str)
- Returns:
builder
- build(*args, **kwargs) Cmd [source]
Builds the CMd object based on the target and regular expression.
- Parameters:
args – arguments to pass to Cmd constructor
kwargs – key word arguments to pass to Cmd constructor
- Returns:
Cmd object
- char(not_chars: None | list[str] | str = None, ignore=False) CmdBuilder [source]
Add a single character argument.
- Parameters:
not_chars – characters that the character can not be; None for can be anything
ignore – True to match with a char but ignore the returned value (default: False)
- Returns:
builder
- digit(mapping: type = <class 'int'>, ignore: bool = False) CmdBuilder [source]
Add a single digit argument.
- Parameters:
mapping – The type to cast the response to (default: int)
ignore – True to match with a digit but ignore the returned value (default: False)
- Returns:
builder
- enq() CmdBuilder [source]
Add the ENQ character (0x5) to the string.
- Returns:
builder
- enum(*allowed_values: AnyStr) CmdBuilder [source]
Matches one of a set of specified strings.
- Parameters:
allowed_values – the values this function is allowed to match
- Returns:
builder
- eos() CmdBuilder [source]
Adds the regex end-of-string character to a command.
- Returns:
builder
- eot() CmdBuilder [source]
Add the EOT character (0x4) to the string.
- Returns:
builder
- escape(text) CmdBuilder [source]
Add some text to the regex which is escaped.
- Parameters:
text – text to add
- Returns:
builder
- etx() CmdBuilder [source]
Add the ETX character (0x3) to the string.
- Returns:
builder
- float(mapping: type = <class 'float'>, ignore: bool = False) CmdBuilder [source]
Add a float argument.
- Parameters:
mapping – The type to cast the response to (default: float)
ignore – True to match with a float but ignore the returned value (default: False)
- Returns:
builder
- get_multicommands(command_separator: AnyStr) CmdBuilder [source]
Allows emulator to split multiple commands separated by a defined command separator, e.g. “;”. Must be accompanied by stream device methods. See Keithley 2700 for examples
- Parameters:
command_separator – Character(s) that separate commands
- Returns:
builder
- int(mapping: type = <class 'int'>, ignore: bool = False) CmdBuilder [source]
Add an integer argument.
- Parameters:
mapping – The type to cast the response to (default: int)
ignore – True to match with a int but ignore the returned value (default: False)
- Returns:
builder
- optional(text) CmdBuilder [source]
Add some escaped text which does not necessarily need to be there. For commands with optional parameters :param text: Text to add :return: builder
- regex(new_regex: str) CmdBuilder [source]
Add a regex to match but not as an argument.
- Parameters:
new_regex – regex to add
- Returns:
builder
- spaces(at_least_one: bool = False) CmdBuilder [source]
Add a regex for any number of spaces
- Parameters:
at_least_one – true there must be at least one space; false there can be any number including zero
- Returns:
builder
- string(length: None | int = None) CmdBuilder [source]
Add an argument which is a string of a given length (if blank string is any length)
- Parameters:
length – length of string; None for any length
- Returns:
builder
- stx() CmdBuilder [source]
Add the STX character (0x2) to the string.
- Returns:
builder