# pypdevsbbl.generic.io module

Set of PythonPDEVS I/O blocks for user interaction.

The classes in this file will allow for logging values and messages to files for analysis and tracing.

Execute this file to test the `Sound` block. You should hear a melody being played on the piano. If this is not the case, something is wrong with the audio on your system. See the block itself for more info.

Note

In order to obtain the `syslog` messages, a `syslog` server needs to be setup. You can write your own server or you can install one on your system. PySysLogQt is a tiny, graphical server that was made by the author for this specific purpose. It accepts both RFC 5424-formatted messages as well as the default Python format.

Warning

In order for the `Logger` block and all its subclasses to work, the `setLogger()` function needs to be called.

`pypdevsbbl.generic.io.``setLogger`(loc, address=('localhost', 1024), loglevel=0, rfc5424=True)

  Sets the logger to a syslog logger that might follow RFC 5424.

  Note

  The `pypdevs.logger.setLogger()` creates a logger that’s based on the standard implementation of the `SysLogHandler`. Alas, this handler does not follow the full syslog specification as described in RFC 5424 and therefore Python(P)DEVS doesn’t either. This function replaces `pypdevs.logger.setLogger()` and provides the correct RFC.

  Parameters:

  - loc (any) – Location of the server, to prepend to every logged message.
  - address (tuple) – The address of the syslog server in the form of (ip-address, port). Defaults to `('localhost', 1024)`.
  - loglevel (numeric) – Minimal log level to support. All messages with a lower level will be discarded. Should be one from in the above list. Defaults to 0.
  - rfc5424 (bool) – Whether or not to follow `RFC 5424` as a logging format. Defaults to `True`. Note that this requires rfc5424-logging-handler to be installed.

class `pypdevsbbl.generic.io.``Logger`(name: str, level: str = 'info')

  Bases: `pypdevs.DEVS.AtomicDEVS`

  Generic `syslog`-logger.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters:

  - name (str) – The name of the block.
  - level (str) – Logging level, in string format. This is a function that is called on the logger object.

  Input Ports: | input (str) – The message to be logged.

  `extTransition`(inputs)

class `pypdevsbbl.generic.io.``Emergency`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs emergency messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``Alert`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs alert messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``Critical`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs critical messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``Error`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs error messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``Warn`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs warning messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``Notice`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs notice messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``Info`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs informational messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``Debug`(name: str)

  Bases: `pypdevsbbl.generic.io.Logger`

  Logs debug messages.

  Before this class can be used, `setLogger()` needs to be called.

  Parameters: | name (str) – The name of the block.
  Input Ports: | input (str) – The message to be logged.

class `pypdevsbbl.generic.io.``FileWriter`(blockname: str, filename: str, binary: bool = False, append: bool = True)

  Bases: `pypdevs.DEVS.AtomicDEVS`

  Writes data to a file.

  Parameters:

  - blockname (str) – The name of the block.
  - filename (str) – The name of the file to write to.
  - binary (bool) – Whether or not to open the file in binary mode. Defaults to `False`.
  - append (bool) – Whether or not to append instead of overwriting. Note that this requires the file to already exist. Defaults to `True`.

  Input Ports: | input (str/bytes) – When in binary mode, it expects a `bytes`-object, otherwise a `str`. This is the data that needs to be written to the file.

  `extTransition`(inputs)

class `pypdevsbbl.generic.io.``FileReader`(name: str, binary: bool = False)

  Bases: `pypdevs.DEVS.AtomicDEVS`

  Reads contents of a file.

  Every time a filename arrives at the block, its contents will be outputted in the same time instance.

  Parameters:

  - name (str) – The name of the block.
  - binary (bool) – Whether or not to read the file in binary mode. Defaults to `False`.

  State: | contents (str) – The contents of the just-read file or `None` if no file has been read.
  Input Ports: | filename (str) – The name of the file to read.
  Output Ports: | contents (str/bytes) – The contents of the file.

  `timeAdvance`()
  `intTransition`()
  `extTransition`(inputs)
  `outputFnc`()

class `pypdevsbbl.generic.io.``Listener`(name: str, dt=inf, fp=<function Listener.<lambda>>, *args)

  Bases: `pypdevs.DEVS.AtomicDEVS`

  Listens to an extrenal source.

  The provided function is executed every `dt` time and its return value is outputted on the polled port. Alternatively, every time a message arrives on the dt port, the `dt` value will change.

  Note

  The block will poll at the beginning of the simulation.

  Parameters:

  - dt (numeric) – The delay between messages. Cannot be negative or zero. Defaults to `INFINITY`.
  - fp (def) – The polling function to be executed. Takes at least the current time as first input. When returning `None`, the `Listener` will not fire an output event. Defaults to a do nothing function: `lambda x: None`.
  - *args – Additional arguments used to call the function.

  State:

  - dt (numeric) – The time between polls. Cannot be negative or zero.
  - elapsed (numeric) – The currently elapsed time.
  - value (any) – The return value.

  Input Ports: | dt (any) – A new `dt` value.
  Output Ports: | listen (any) – The polled result.

  `timeAdvance`()
  `outputFnc`()
  `intTransition`()
  `extTransition`(inputs)