Search code examples
pythonclipsclipspy

clipspy pendant for pyclips clips.ErrorStream.Read()


Is it possible to read the ErrorStream from clips using clipspy? With pyclips, it worked this way: clips.ErrorStream.Read().

Does it even make sense to do it with clipspy, or are all errors thrown as exceptions and can be caught with clips.CLIPSError ?


Solution

  • When clipspy detects an error, it raises a CLIPSError exception embedding the output of werror as message. Therefore, you can mostly rely on that.

    Nevertheless, as CLIPS C APIs are not entirely consistent you might find some corner case. Most commonly, the CLIPSError exception might not contain any error message as CLIPS does not always print a message in case of error.

    If you want to read CLIPS output you can do that by using the routing facilities which are documented in CLIPS Advanced Programming Guide. The clipspy router APIs are very close to the C ones.

    You can also use the LoggingRouter to manage CLIPS output with Python logging. This comes handy when you want to integrate CLIPS in a python service.

    import clips
    import logging
    
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s')
    
    env = clips.Environment()
    router = clips.LoggingRouter()
    
    router.add_to_environment(env)
    
    env.eval('(printout t "Hello World!" crlf)')
    

    Output

    2018-10-16 17:29:01,829 - INFO - Hello World!