regex search in FactFile
This commit is contained in:
parent
6e86096f2b
commit
cb70310616
1
TODO
1
TODO
@ -19,4 +19,3 @@ dr.botzo --- TODO
|
|||||||
* obligatory info command
|
* obligatory info command
|
||||||
* similar to FactFile module, a generic Trigger module
|
* similar to FactFile module, a generic Trigger module
|
||||||
* do splitting in DrBotServerConnection.privmsg
|
* do splitting in DrBotServerConnection.privmsg
|
||||||
* regex search in FactFile
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
from ConfigParser import NoOptionError
|
from ConfigParser import NoOptionError
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
|
|
||||||
from irclib import irclib
|
from irclib import irclib
|
||||||
|
|
||||||
@ -47,11 +48,20 @@ class FactFile(Module):
|
|||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
|
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
|
||||||
with open(filename, 'r') as file:
|
with open(filename, 'r') as file:
|
||||||
facts = file.readlines()
|
facts = []
|
||||||
to_print = facts[random.randint(1, len(facts))-1]
|
if len(whats) == 1:
|
||||||
|
facts = file.readlines()
|
||||||
|
else:
|
||||||
|
lines = file.readlines()
|
||||||
|
for line in lines:
|
||||||
|
# check if line matches provided regex
|
||||||
|
if re.search(' '.join(whats[1:]), line, re.IGNORECASE) is not None:
|
||||||
|
facts.append(line)
|
||||||
|
if len(facts) > 0:
|
||||||
|
to_print = facts[random.randint(1, len(facts))-1]
|
||||||
|
|
||||||
# return text
|
# return text
|
||||||
return self.reply(connection, replypath, to_print.rstrip())
|
return self.reply(connection, replypath, to_print.rstrip())
|
||||||
else:
|
else:
|
||||||
print('filename in config file for \'' + whats[0] + '\' is wrong')
|
print('filename in config file for \'' + whats[0] + '\' is wrong')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user