Skip to content

Commit

Permalink
Code cleanup to cardinal/plugins.py
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmaguire committed Dec 30, 2015
1 parent 3e7c13b commit 2527599
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions cardinal/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def next(self):

return self.plugins[keys[self.iteration_counter - 1]]

def _import_module(self, module, type='plugin'):
@staticmethod
def _import_module(module, type='plugin'):
"""Given a plugin name, will import it from its directory or reload it.
If we are passing in a module, we can safely assume at this point that
Expand Down Expand Up @@ -308,7 +309,8 @@ def _load_plugin_config(self, plugin):
# Return JSON config, since YAML config wasn't found
return json_config

def _get_plugin_commands(self, instance):
@staticmethod
def _get_plugin_commands(instance):
"""Find the commands in a plugin and return them as callables.
Keyword arguments:
Expand Down Expand Up @@ -351,7 +353,7 @@ def itercommands(self, channel=None):
for command in plugin['commands']:
yield command

def log_failure(self, message):
def _log_failure(self, message):
def errback(failure):
try:
failure.raiseException()
Expand All @@ -365,7 +367,7 @@ def errback(failure):
return errback

@staticmethod
def ignore_failure(failure):
def _ignore_failure(failure):
return None

def load(self, plugins):
Expand Down Expand Up @@ -417,10 +419,10 @@ def load(self, plugins):
d.addCallback(self._close_plugin_instance)

# Log and ignore failures closing plugin
d.addErrback(self.log_failure(
d.addErrback(self._log_failure(
"Didn't close plugin cleanly: %s" % plugin
))
d.addErrback(self.ignore_failure)
d.addErrback(self._ignore_failure)

# And use the existing module object for our _import_module()
# call below.
Expand All @@ -430,7 +432,7 @@ def return_plugin_module(_):

# Now really import/reload the module
d.addCallback(self._import_module)
d.addErrback(self.log_failure(
d.addErrback(self._log_failure(
"Could not load plugin module: %s" % plugin
))

Expand Down Expand Up @@ -459,6 +461,7 @@ def load_plugin(module):
self.logger.exception(
"Could not instantiate plugin: %s" % plugin
)

raise

commands = self._get_plugin_commands(instance)
Expand Down Expand Up @@ -533,10 +536,10 @@ def unload(self, plugins, keep_entry=False):
d = defer.maybeDeferred(self._close_plugin_instance, plugin)

# Log and ignore failures closing plugin
d.addErrback(self.log_failure(
d.addErrback(self._log_failure(
"Didn't close plugin cleanly: %s" % plugin
))
d.addErrback(self.ignore_failure)
d.addErrback(self._ignore_failure)

# Once all references of the plugin have been removed, Python will
# eventually do garbage collection. We only saved it in one
Expand Down Expand Up @@ -925,11 +928,12 @@ def _add_callback(self, event_name, callback):

return callback_id

@staticmethod
def _generate_id(size=6, chars=string.ascii_uppercase + string.digits):
"""
Thank you StackOverflow: http://stackoverflow.com/a/2257449/242129
Generates a random, 6 character string of letters and numbers (by
default.)
"""
return ''.join(random.choice(chars) for _ in range(6))
return ''.join(random.choice(chars) for _ in range(size))

0 comments on commit 2527599

Please sign in to comment.