OnPluginPartialLine from python

Posted by Worlo on Wed 05 Dec 2007 02:20 PM — 6 posts, 27,131 views.

Netherlands #0
I am unable to get OnPluginPartialLine to work from python.

This is what I looked at first:
http://www.gammon.com.au/forum/bbshowpost.php?id=3774&page=2

But my VBScript knowledge just isn't good enough to get what I want.

I want to do something like:

import re

def OnPluginPartialLine(txt):
    if re.match("^([0-9]+)h, ([0-9]+)m, ([0-9]+)e, ([0-9]+)w (.*)-", txt):
        <do some work>


Putting a world.Note("Plugin Match Found") there causes errors in my plugin. I get a pop-up saying Type Mismatch. But i have no idea what line the type mismatch is on. My hunch is that this is not a function in VB, so it must be something different to use in Python, but I have no clue.

Here is the code for my plugin at the moment:

<?xml version="1.0" encoding="US-ASCII"?>

<!DOCTYPE muclient>

<!-- Plugin "Status_Bar_Prompt" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Prompt_Handler"
   author="Worlo"
   id="ff9331b06c15ab21046be001"
   language="Python"
   purpose="Get prompt info in variables as it arrives."
   date_written="2007-12-05 16:00:00"
   requires="4.14"
   version="1.0"
   >

</plugin>


<!--  Script  -->


<script>
<![CDATA[
import re

def OnPluginPartialLine(txt):
    if re.match("^([0-9]+)h, ([0-9]+)m, ([0-9]+)e, ([0-9]+)w (.*)-", txt):
        world.Note("Plugin Match Found")

]]>
</script>

</muclient>


Who can enlighten me?

Warm regards, Worlo
Australia Forum Administrator #1
See:

http://www.gammon.com.au/forum/bbshowpost.php?id=5110

I think it is something to do with the default return type that Python must use.

As far as I can tell, you must either return nothing - that is, the empty variant (however you do that in Python) or something that can be converted to a number. However that number is not used in this case.
#2
Omitting the return at the end of the function is the same as specifying "return None". (Which should be the translation of 'empty variant'.)

Netherlands #3
Thanks, the return True is what you need.


So the updated script function that is matching all prompts right when they arrive perfectly is:


<script>
<![CDATA[
import re
def OnPluginPartialLine(txt):
    if re.match("^([0-9]+)h, ([0-9]+)m, ([0-9]+)e, ([0-9]+)w (.*)-", txt):
        world.Note("Plugin Match Found")
    return True
]]>
</script>
Netherlands #4
Here is my finalized plugin that captures prompt information into variables for Achaea, use it as you please.


<?xml version="1.0" encoding="US-ASCII"?>

<!DOCTYPE muclient>

<!-- Plugin "Status_Bar_Prompt" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Prompt_Handler"
   author="Worlo"
   id="ff9331b06c15ab21046be001"
   language="Python"
   purpose="Get prompt info in variables as it arrives."
   date_written="2007-12-05 16:00:00"
   requires="4.14"
   version="1.0"
   >

</plugin>


<!--  Script  -->


<script>
<![CDATA[
import re
def OnPluginPartialLine(txt):
    matchObject = re.match("^([0-9]+)h, ([0-9]+)m, ([0-9]+)e, ([0-9]+)w (.*)-", txt)
    if matchObject:
        groups = matchObject.groups()
        # uncomment the following to see if you are matching
        # world.Note("H: " + groups[0])
        health = groups[0]
        mana = groups[1]
        endurance = groups[2]
        willpower = groups[3]
        statusFlags = groups[4]
        world.SetVariable("health", health)
        world.SetVariable("mana", mana)
        world.SetVariable("endurance", endurance)
        world.SetVariable("willpower", willpower)
        world.SetVariable("statusFlags", statusFlags)
    return True
]]>
</script>

</muclient>
Netherlands #5
So I noticed world variables are contained in the plugin...

I want reliable information sharing across tools so I did a database thing.

This is my plugin now.

<?xml version="1.0" encoding="US-ASCII"?>

<!DOCTYPE muclient>

<!-- Plugin "Status_Bar_Prompt" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Prompt_Handler"
   author="Worlo"
   id="ff9331b06c15ab21046be001"
   language="Python"
   purpose="Get prompt info in variables as it arrives."
   date_written="2007-12-05 16:00:00"
   requires="4.14"
   version="1.0"
   >

</plugin>


<!--  Script  -->


<script>
<![CDATA[
import re
from pysqlite2 import dbapi2 as sqlite

def setStatusValues(health, mana, endurance, willpower, statusFlags):
    con = sqlite.connect("c:\achaea\achaea.db")
    cursor = con.cursor()
    array = {'health': health,
             'mana': mana,
             'endurance': endurance,
             'willpower': willpower,
             'statusFlags': statusFlags}
    for key, value in array.iteritems():
        cursor.execute("update status set statusValue = '%s' where statusName = '%s'" % (value, key))
    cursor.close()
    con.commit()
    con.close()

def OnPluginPartialLine(txt):
    matchObject = re.match("^([0-9]+)h, ([0-9]+)m, ([0-9]+)e, ([0-9]+)w (.*)-", txt)
    if matchObject:
        groups = matchObject.groups()
        # uncomment the following to see if you are matching
        # world.Note("H: " + groups[0])
        health = groups[0]
        mana = groups[1]
        endurance = groups[2]
        willpower = groups[3]
        statusFlags = groups[4]
        setStatusValues(health, mana, endurance, willpower, statusFlags)
    return True
]]>
</script>

</muclient>