Random: Various updates and improvements.

This commit is contained in:
Krytarik Raido 2021-12-04 04:04:04 +01:00
parent 2dfd320ddc
commit 6c755906d7
7 changed files with 101 additions and 106 deletions

50
Random/README.rst Normal file
View File

@ -0,0 +1,50 @@
.. _plugin-Random:
Documentation for the Random plugin for Supybot
===============================================
Purpose
-------
This plugin returns a random phrase or number out of a given
set of phrases or range of numbers.
Usage
-----
Return a random phrase or number out of a given
set of phrases or range of numbers.
.. _commands-Random:
Commands
--------
.. _command-random-random:
random <phrase1> <phrase2> ...<phraseN>
Picks a phrase at random. Use quotes to enclose multiple words in a phrase.
.. _command-random-randrange:
randrange <min> <max>
Picks a random number in range <min>-<max>
.. _command-random-seed:
seed [<number>]
Seeds the random number generator with the given <number>, which can either be an integer or a floating-point number.
When no argument is given, the random number generator is seeded with the current time.
.. _conf-Random:
Configuration
-------------
.. _conf-supybot.plugins.Random.public:
supybot.plugins.Random.public
This config variable defaults to "True", is not network-specific, and is not channel-specific.
Determines whether this plugin is publicly visible.

View File

@ -1 +0,0 @@
Insert a description of your plugin here, with any notes, etc. about using it.

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com>
# Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -25,12 +26,11 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Add a description of the plugin (to be presented to the user inside the wizard)
here. This should describe *what* the plugin does.
This plugin returns a random phrase or number out of a given
set of phrases or range of numbers.
"""
import supybot
@ -38,29 +38,31 @@ import supybot.world as world
# Use this for the version of this plugin. You may wish to put a CVS keyword
# in here if you're keeping the plugin in CVS or some similar system.
__version__ = ""
__version__ = "1.0.0"
# XXX Replace this with an appropriate author or supybot.Author instance.
__author__ = supybot.authors.unknown
__author__ = supybot.Author("Krytarik Raido", "krytarik", "krytarik@gmail.com")
# This is a dictionary mapping supybot.Author instances to lists of
# contributions.
__contributors__ = {}
__contributors__ = {
supybot.Author("Terence Simpson", "tsimpson", "tsimpson@ubuntu.com"): ['Original Author']
}
# This is a url where the most recent plugin package can be downloaded.
__url__ = '' # 'http://supybot.com/Members/yourname/Random/download'
__url__ = 'https://launchpad.net/ubuntu-bots'
import config
import plugin
reload(plugin) # In case we're being reloaded.
from . import config
from . import plugin
from importlib import reload
# In case we're being reloaded.
reload(config)
reload(plugin)
# Add more reloads here if you add third-party modules and want them to be
# reloaded when this plugin is reloaded. Don't forget to import them as well!
if world.testing:
import test
from . import test
Class = plugin.Class
configure = config.configure
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com>
# Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -25,12 +26,12 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import supybot.conf as conf
import supybot.registry as registry
def configure(advanced):
# This will be called by supybot to configure this module. advanced is
# a bool that specifies whether the user identified himself as an advanced
@ -41,9 +42,7 @@ def configure(advanced):
Random = conf.registerPlugin('Random')
# This is where your configuration variables (if any) should go. For example:
# conf.registerGlobalValue(Random, 'someConfigVariableName',
# registry.Boolean(False, """Help for someConfigVariableName."""))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com>
# Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -25,38 +26,23 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.ircdb as ircdb
import random as rdm
import string
import time
class Random(callbacks.Plugin):
"""Add the help for "@plugin help Random" here
This should describe *how* to use this plugin."""
"""Return a random phrase or number out of a given
set of phrases or range of numbers."""
threaded = True
def __init__(self, irc):
callbacks.Plugin.__init__(self, irc)
self.__parent = super(Random, self)
self.__parent.__init__(irc)
self.rdm = rdm.Random(time.time())
self.pwchars = string.ascii_letters + string.digits + string.punctuation
def _seed(self, num=None):
if num is None:
num = time.time()
self.rdm.seed(num)
def makePassword(self, pwlen=16):
self._seed()
return ''.join(self.rdm.sample(self.pwchars, pwlen))
def seed(self, irc, msg, args, num):
"""[<number>]
@ -65,80 +51,38 @@ class Random(callbacks.Plugin):
either be an integer or a floating-point number. When no argument is
given, the random number generator is seeded with the current time.
"""
self._seed(num)
irc.replySuccess()
if num is None:
num = time.time()
self.rdm.seed(num)
irc.replySuccess()
seed = wrap(seed, [optional(first('long', 'float'))])
def random(self, irc, msg, args, words):
"""word1 word2 ...wordN
Picks a word at random.
def random(self, irc, msg, args, phrases):
"""<phrase1> <phrase2> ...<phraseN>
Picks a phrase at random. Use quotes to enclose multiple words
in a phrase.
"""
wordl = [x for x in words.split(' ') if x]
wordl = [_ for _ in wordl if not ('tsimpson',).__contains__(_.lower())]
if len(wordl) == 1:
irc.reply(wordl[0]);
if len(phrases) == 1:
irc.reply(phrases[0]);
return
if self.rdm.randrange(0, 2):
self.rdm.seed(time.time())
irc.reply(self.rdm.choice(wordl))
random = wrap(random, ['text'])
irc.reply(self.rdm.choice(phrases))
random = wrap(random, [many('something')])
def randrange(self, irc, msg, args, min, max):
def randrange(self, irc, msg, args, minn, maxn):
"""<min> <max>
Picks a random number in range <min>-<max>
"""
if max <= min:
if maxn <= minn:
irc.error("<min> must be less than <max>")
return
max += 1
irc.reply(str(rdm.randrange(min, max)))
randrange = wrap(randrange, ["nonNegativeInt", "nonNegativeInt"])
irc.reply(str(rdm.randrange(minn, maxn+1)))
randrange = wrap(randrange, ['nonNegativeInt', 'nonNegativeInt'])
def adduser(self, irc, msg, args, name, hostmask):
"""<name> [<hostmask>]
Registers <name> with the bot and, optionally, assigns the given
<hostmask> to the new account.
"""
# This try-block checks if the user calling this command is an admin (or owner)
try:
u = ircdb.users.getUser(msg.prefix)
if not u._checkCapability('admin'):
irc.errorNoCapability('admin', Raise=True)
except KeyError:
pass
if ircutils.isUserHostmask(name):
irc.errorInvalid('username', name, "Hostmasks are not valid usernames", Raise=True)
# This try-block checks if the username is already registered
try:
ircdb.users.getUserId(name)
irc.error("That name is already registered", Raise=True)
except KeyError:
pass
if hostmask:
# This try-block checks if the hostmask given is already assigned to another user
try:
u = ircdb.users.getUser(hostmask)
irc.error("That hostmask is already registered to %s" % u.name)
return
except KeyError:
pass
user = ircdb.users.newUser()
user.name = name
user.setPassword(self.makePassword())
if hostmask:
user.addHostmask(hostmask)
ircdb.users.setUser(user)
irc.replySuccess()
# adduser = wrap(adduser, ['something', additional('hostmask')])
Class = Random
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com>
# Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -25,13 +26,9 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
from supybot.test import *
class RandomTestCase(PluginTestCase):
plugins = ('Random',)
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -17,3 +17,7 @@ PackageInfo
===========
apt-file
python3-apt
Random
======
(none)