From 50768cfd539ebd376bbcd88b37191800b549a05e Mon Sep 17 00:00:00 2001 From: GLolol Date: Sat, 22 Nov 2014 18:15:49 -0800 Subject: [PATCH] Namegen: make number of syllables configurable & update gitignore --- .gitignore | 2 ++ Namegen/config.py | 6 +++--- Namegen/plugin.py | 21 ++++++++++++++------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 12e1cf7..3fe3382 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ __pycache__/ **/test-conf/ **/test-logs/ backup/ +web/ +tmp/ # emacs/vi *.swo diff --git a/Namegen/config.py b/Namegen/config.py index 219d90f..66e87c3 100644 --- a/Namegen/config.py +++ b/Namegen/config.py @@ -48,9 +48,9 @@ def configure(advanced): Namegen = conf.registerPlugin('Namegen') -# This is where your configuration variables (if any) should go. For example: -# conf.registerGlobalValue(Namegen, 'someConfigVariableName', -# registry.Boolean(False, _("""Help for someConfigVariableName."""))) +conf.registerGlobalValue(Namegen, 'syllables', + registry.PositiveInteger(2, _("""Specifies the maximum amount of syllables + a name can have."""))) # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/Namegen/plugin.py b/Namegen/plugin.py index 5767f35..1aa837d 100644 --- a/Namegen/plugin.py +++ b/Namegen/plugin.py @@ -56,26 +56,33 @@ class Namegen(callbacks.Plugin): with open(os.path.join(os.path.dirname(__file__), '%s.txt') % fn) as f: self.names[fn] = f.read().splitlines() - def _namegen(self): + def _namegen(self, syl): """Generates a random name.""" - numSyl = random.randint(0, 3) + numSyl = random.randint(0, syl) name = "{}{}{}".format(random.choice(self.names['starts']), \ ''.join(random.sample(self.names['middles'], numSyl)), \ random.choice(self.names['ends'])) return name - def namegen(self, irc, msg, args, count): - """[] + def namegen(self, irc, msg, args, count, syl): + """[] [] - Generates random names. If not specified, [] defaults to 10.""" + Generates random names. If not specified, [] defaults to 10. + [] specifies the maximum number of syllables a name can have, + and defaults to the value set in 'config plugins.namegen.syllables'.""" + confsyl = self.registryValue("syllables") + maxsyl = max(confsyl, 10) if not count: count = 10 elif count > 100: irc.error("Too many names to count!", Raise=True) + elif syl and syl > maxsyl: + irc.error("Too many syllables specified.", Raise=True) + syl = syl or confsyl r = range if version_info[0] >= 3 else xrange - s = ', '.join([self._namegen() for _ in r(count)]) + s = ', '.join([self._namegen(syl) for _ in r(count)]) irc.reply(s) - namegen = wrap(namegen, [optional('int')]) + namegen = wrap(namegen, [optional('positiveInt'), optional('positiveInt')]) Class = Namegen