Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: XVicarious on October 16, 2011, 04:36:44 pm

Title: urllib is not being nice :(
Post by: XVicarious on October 16, 2011, 04:36:44 pm
So I am writing a plugin for XChat and I have come to the conclusion that urllib is somehow preventing greenText() from being executed... What is going on :(?

Here is the code:
Code: [Select]
__module_name__ = "IrmageBoard"
__module_version__ = "0.2.0"
__module_description__ = "Liek a real imageboard guies (without the images!!!1)"

import xchat, string, re, random, yaml
from urllib import urlopen

global myFilter
ymlFilter = urlopen("http://xvicario.us/irmageboard/filters.yml")
myFilter = yaml.load(ymlFilter)
ymlFilter.close()

print "\0034",__module_name__, __module_version__, "has been loaded\003"

def greenText(word, word_eol, userdata):
    print "EXECUTING greenText()"
    if word_eol[0].find('>'):
        return xchat.EAT_NONE
        print "FOUND NOTHING!"
    else:
        xchat.command(" ".join(["msg", xchat.get_info("channel"), ''.join(["3",word_eol[0]])]))
        print "FOUND SOMETIHNG!"
    return xchat.EAT_ALL

def filters(word, word_eol, userdata):
    newString = word_eol[0]
    for k, v in myFilter.iteritems():
        if k in newString:
            tempColor = random.randrange(0,16,1)
            tempBack = random.randrange(0,16,1)
            tempReplace = re.compile(k, re.IGNORECASE)
            newString = tempReplace.sub(v, newString)
            newString = "".join(["\003",str(tempColor),",",str(tempBack),newString,"\003"])
    xchat.command(" ".join(["msg", xchat.get_info("channel"), newString]))
    return xchat.EAT_ALL

xchat.hook_command("", greenText)
xchat.hook_command("", filters)

edit: that is everything :/
Title: Re: urllib is not being nice :(
Post by: Munchor on October 16, 2011, 04:44:35 pm
Have you tried using urllib2? You can find information on it here (http://docs.python.org/library/urllib2.html).

Can you try this?

Code: [Select]
__module_name__ = "IrmageBoard"
__module_version__ = "0.2.0"
__module_description__ = "Like a real imageboard guies (without the images!!1)"

import string
import re
import random
import yaml

import xchat
import urllib2

global myFilter #Do you really need to use globals? They ain't recommended
ymlFilter = urllib2.urlopen("http://xvicario.us/irmageboard/filters.yml")
myFilter = yaml.load(ymlFilter)
ymlFilter.close()

print "\0034 %s %s has been loaded\003" % (__module_name__, __module_version__)

def greenText(word, word_eol, userdata):
    """TODO Docstring for greenText()"""
    print "EXECUTING greenText()"
    if word_eol[0].find('>'):
        return xchat.EAT_NONE
        print "FOUND NOTHING!"
    else:
        xchat.command(" ".join(["msg", xchat.get_info("channel"),
                      ''.join(["3",word_eol[0]])]))
        print "FOUND SOMETHING!"
    return xchat.EAT_ALL

def filters(word, word_eol, userdata):
    """Filters a word"""
    newString = word_eol[0]
    for k, v in myFilter.iteritems():
        if k in newString:
            tempColor = random.randrange(0, 16, 1)
            tempBack = random.randrange(0, 16, 1)
            tempReplace = re.compile(k, re.IGNORECASE)
            newString = tempReplace.sub(v, newString)
            newString = "".join(["\003", str(tempColor), ", ", str(tempBack),
                                newString,"\003"])
    xchat.command(" ".join(["msg", xchat.get_info("channel"), newString]))
    return xchat.EAT_ALL

All my changes were minimum, but I think I changed a few things with urllib2 that might make a difference.
Title: Re: urllib is not being nice :(
Post by: XVicarious on October 16, 2011, 04:57:17 pm
It does the same thing :(

edit: you can actually see my debugging code :P "EXECUTING greenText()" etc lol that stuff never shows up, so i can safely say it doesn't execute. I also disabled the urlopen and the code then worked...
Title: Re: urllib is not being nice :(
Post by: alberthrocks on October 16, 2011, 08:42:20 pm
It does the same thing :(

edit: you can actually see my debugging code :P "EXECUTING greenText()" etc lol that stuff never shows up, so i can safely say it doesn't execute. I also disabled the urlopen and the code then worked...
I'm assuming that you know that any print output is redirected to XChat's window, right?
(I haven't tested this, sorry! :()

EDIT: Oh wait, I think you're doing it wrong... :P
xchat.hook_command("", greenText) may not work because the initial argument is blank. I don't know how XChat2 responds to this though... but I would suggest trying other hooks too, found here: http://xchat.org/docs/xchatpython.html#head-36ddb85a7e96550366a7165d2aed583da9ea01f5

This is, of course, if you weren't looking at the XChat window and all the output was there. ;)
Title: Re: urllib is not being nice :(
Post by: XVicarious on October 16, 2011, 09:02:54 pm
That is how you are supposed to intercept outgoing messages...  See the filters() function.  that does ALMOST the same thing.

edit: Somehow it was in my last two lines.  I switched the order of them (so the filter got hooked first, then the greenText) and it works... Idk why...
Title: Re: urllib is not being nice :(
Post by: alberthrocks on October 16, 2011, 10:06:03 pm
That is how you are supposed to intercept outgoing messages...  See the filters() function.  that does ALMOST the same thing.

edit: Somehow it was in my last two lines.  I switched the order of them (so the filter got hooked first, then the greenText) and it works... Idk why...
I think I might know why... again, this is all theory, but I have an idea now: since you have the same first argument, the 2nd line will replace the hook established in the first. To prove it, add a print statement to the filters function, and if I'm right, you won't see that get printed at all! ;)

Assuming I'm right about the issue... it seems that you're simply playing around with the code, since I found your code snipplet online, which is probably a plugin example. However, in the future, if you are going to parse like this (with two functions), you should either:
1) Merge the two functions into one; or
2) If you prefer having two functions handing the hook, call the 2nd function from the first. That is...
Code: [Select]
__module_name__ = "IrmageBoard"
__module_version__ = "0.2.0"
__module_description__ = "Liek a real imageboard guies (without the images!!!1)"

import xchat, string, re, random, yaml
from urllib import urlopen

global myFilter
ymlFilter = urlopen("http://xvicario.us/irmageboard/filters.yml")
myFilter = yaml.load(ymlFilter)
ymlFilter.close()

print "\0034",__module_name__, __module_version__, "has been loaded\003"

def greenText(word, word_eol, userdata):
    print "EXECUTING greenText()"
    if word_eol[0].find('>'):
        return xchat.EAT_NONE
        print "FOUND NOTHING!"
    else:
        xchat.command(" ".join(["msg", xchat.get_info("channel"), ''.join(["3",word_eol[0]])]))
        print "FOUND SOMETIHNG!"
    return xchat.EAT_ALL

def filters(word, word_eol, userdata):
    newString = word_eol[0]
    for k, v in myFilter.iteritems():
        if k in newString:
            tempColor = random.randrange(0,16,1)
            tempBack = random.randrange(0,16,1)
            tempReplace = re.compile(k, re.IGNORECASE)
            newString = tempReplace.sub(v, newString)
            newString = "".join(["\003",str(tempColor),",",str(tempBack),newString,"\003"])
    xchat.command(" ".join(["msg", xchat.get_info("channel"), newString]))
    greenText(word, word_eol, userdata) # Call that 2nd function!
    return xchat.EAT_ALL # May not be necessary, since the greenText function also returns this too... although...
    # Maybe I'm a bit wrong in how it returns. If the 2nd function doesn't kick out of the plugin, simply do
    # ret = greenText(word, word_eol, userdata)
    # return ret

xchat.hook_command("", filters)
Be sure to read my included code comments for more info.

Have fun learning how to write XChat plugins! :D (They're really, really fun when they work! :))
Title: Re: urllib is not being nice :(
Post by: XVicarious on October 16, 2011, 10:46:49 pm
Its kinda funny because just switching them fixed it.  Both things work now.  This is my third script for XChat.  Its really fun doing this lol.
Title: Re: urllib is not being nice :(
Post by: alberthrocks on October 16, 2011, 10:59:27 pm
Its kinda funny because just switching them fixed it.  Both things work now.  This is my third script for XChat.  Its really fun doing this lol.
Oh actually... I might know why, but then I don't know why it occurs. A lovely programming term called race condition (http://en.wikipedia.org/wiki/Race_condition).

If they are both being called, the first function returns xchat.EAT_ALL... which tells XChat to withhold any of the output from the user and any other plugins that might want to parse the data. So in a sense, your code still has some errors, it's just that the switch doesn't "EAT_ALL". ;) (My code above fixes that!)

It might do a stack like system - filters is first, greenText is last when added in the opposite way. When you switched it, greenText is first, filters is last in order of execution. Again, this is speculation, since XChat-WDK (Windows version) doesn't particularly do Python scripting unless you install some obscure version of it... :/
Title: Re: urllib is not being nice :(
Post by: XVicarious on October 16, 2011, 11:02:15 pm
Actually that isn't true about installing an obscure version.  I have 2.7.  IDK if its Active Python or just vanilla but they operate almost the same.
The problem with your code is that filters() will be optional in the future (the function to change this is commented out in hte full script) so disabling filters() would disable the whole thing.  I can just use another return.