From 6c755906d7f235a005286025cde1efbfc905d6cc Mon Sep 17 00:00:00 2001 From: Krytarik Raido Date: Sat, 4 Dec 2021 04:04:04 +0100 Subject: [PATCH] Random: Various updates and improvements. --- Random/README.rst | 50 ++++++++++++++++++++++ Random/README.txt | 1 - Random/__init__.py | 32 +++++++------- Random/config.py | 9 ++-- Random/plugin.py | 104 +++++++++++---------------------------------- Random/test.py | 7 +-- requirements.txt | 4 ++ 7 files changed, 101 insertions(+), 106 deletions(-) create mode 100644 Random/README.rst delete mode 100644 Random/README.txt diff --git a/Random/README.rst b/Random/README.rst new file mode 100644 index 0000000..af01dad --- /dev/null +++ b/Random/README.rst @@ -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 ... + Picks a phrase at random. Use quotes to enclose multiple words in a phrase. + +.. _command-random-randrange: + +randrange + Picks a random number in range - + +.. _command-random-seed: + +seed [] + Seeds the random number generator with the given , 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. + diff --git a/Random/README.txt b/Random/README.txt deleted file mode 100644 index d60b47a..0000000 --- a/Random/README.txt +++ /dev/null @@ -1 +0,0 @@ -Insert a description of your plugin here, with any notes, etc. about using it. diff --git a/Random/__init__.py b/Random/__init__.py index d5b8797..c509133 100644 --- a/Random/__init__.py +++ b/Random/__init__.py @@ -1,5 +1,6 @@ ### -# Copyright (c) 2010, Terence Simpson +# 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: diff --git a/Random/config.py b/Random/config.py index 1029278..665efd2 100644 --- a/Random/config.py +++ b/Random/config.py @@ -1,5 +1,6 @@ ### -# Copyright (c) 2010, Terence Simpson +# 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: diff --git a/Random/plugin.py b/Random/plugin.py index 9af553d..01fd12f 100644 --- a/Random/plugin.py +++ b/Random/plugin.py @@ -1,5 +1,6 @@ ### -# Copyright (c) 2010, Terence Simpson +# 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): """[] @@ -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): + """ ... + + 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): """ + Picks a random number in range - """ - if max <= min: + if maxn <= minn: irc.error(" must be less than ") 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): - """ [] - - Registers with the bot and, optionally, assigns the given - 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: diff --git a/Random/test.py b/Random/test.py index 9d2ea9a..aa1ebe7 100644 --- a/Random/test.py +++ b/Random/test.py @@ -1,5 +1,6 @@ ### -# Copyright (c) 2010, Terence Simpson +# 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: diff --git a/requirements.txt b/requirements.txt index 46f7765..7676cb9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,7 @@ PackageInfo =========== apt-file python3-apt + +Random +====== +(none)