Skip to content
This repository has been archived by the owner on May 14, 2018. It is now read-only.

Commit

Permalink
Pass pep8 code style check
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jun 11, 2014
1 parent b4e4deb commit 0220a2c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
18 changes: 12 additions & 6 deletions extender/__init__.py
Expand Up @@ -3,7 +3,7 @@
from .plugin import Plugin
from .manager import PluginManager

__version__ = '0.0.7'
__version__ = '0.0.8'

logging.basicConfig()

Expand All @@ -17,10 +17,16 @@ def safe_execute(func, *args, **kwargs):
else:
cls = func.__class__
logger = logging.getLogger('extender.errors.plugins')
logger.error('Error processing %r on %r: %s', func.__name__, cls.__name__, e, extra={
'func_module': cls.__module__,
'func_args': args,
'func_kwargs': kwargs,
}, exc_info=True)
logger.error('Error processing %r on %r: %s',
func.__name__,
cls.__name__,
e,
extra={
'func_module': cls.__module__,
'func_args': args,
'func_kwargs': kwargs,
},
exc_info=True
)
else:
return result
17 changes: 11 additions & 6 deletions extender/manager.py
Expand Up @@ -58,7 +58,7 @@ def all(self):
else:
results.append(cls)
except Exception:
logger.exception('Unable to import %s' % cls_path)
logger.exception('Unable to import {cls}'.format(cls=cls_path))
continue
self.cache = results

Expand Down Expand Up @@ -160,7 +160,10 @@ def register(self, cls):
if not hasattr(cls, '__name__'):
# class instance
cls = cls.__class__
self.add('%s.%s' % (cls.__module__, cls.__name__))
self.add('{module}.{name}'.format(
module=cls.__module__,
name=cls.__name__
))
return cls

def unregister(self, cls):
Expand All @@ -170,12 +173,15 @@ def unregister(self, cls):
try:
cls = self.get(slug)
except KeyError:
logger.error('No plugin named %s' % slug)
logger.error('No plugin named %s.', slug)
return
if not hasattr(cls, '__name__'):
# class instance
cls = cls.__class__
self.remove('%s.%s' % (cls.__module__, cls.__name__))
self.remove('{module}.{name}'.format(
module=cls.__module__,
name=cls.__name__
))
return cls

def install(self, entry_points):
Expand All @@ -184,7 +190,6 @@ def install(self, entry_points):
plugin = ep.load()
except Exception:
import traceback
sys.stderr.write("Failed to load plugin %r:\n%s\n" % (ep.name, traceback.format_exc()))
logger.exception("Failed to load plugin %r:\n" % ep.name)
logger.exception("Failed to load plugin %r.", ep.name)
else:
self.register(plugin)
11 changes: 9 additions & 2 deletions extender/plugin.py
Expand Up @@ -4,6 +4,7 @@


class IPlugin(local):

"""
Plugin interface. Should not be inherited from directly.
Expand Down Expand Up @@ -41,18 +42,24 @@ def is_enabled(self):


class PluginMount(type):

def __new__(mcs, name, bases, attrs):
new_cls = type.__new__(mcs, name, bases, attrs)
if IPlugin in bases:
return new_cls
if not new_cls.title:
if not hasattr(new_cls, 'title'):
new_cls.title = new_cls.__name__
if not new_cls.slug:
if not hasattr(new_cls, 'slug'):
new_cls.slug = new_cls.title.replace(' ', '-').lower()
if not hasattr(new_cls, 'priority'):
new_cls.priority = 0
if not hasattr(new_cls, 'is_enabled'):
new_cls.is_enabled = lambda s: True
return new_cls


class Plugin(six.with_metaclass(PluginMount, IPlugin)):

"""
A plugin should be treated as if it were a singleton. The owner does not
control when or how the plugin gets instantiated, nor is it guaranteed that
Expand Down
9 changes: 5 additions & 4 deletions release.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, with_statement
import os
import sys

Expand All @@ -17,9 +18,9 @@
pandoc.core.PANDOC_PATH = pandoc_path

doc = pandoc.Document()
doc.markdown = open('README.md').read()
f = open('README.rst', 'w+')
f.write(doc.rst)
f.close()
with open('README.md') as f:
doc.markdown = f.read()
with open('README.rst', 'w+') as f:
f.write(doc.rst)
os.system("python setup.py release")
os.remove('README.rst')
12 changes: 7 additions & 5 deletions setup.py
@@ -1,16 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
import os
from setuptools import setup

readme = 'README.md'
if os.path.exists('README.rst'):
long_description = open('README.rst').read()
else:
long_description = open('README.md').read()
readme = 'README.rst'
with open(readme) as f:
long_description = f.read()

setup(
name='extender',
version='0.0.7',
version='0.0.8',
author='messense',
author_email='messense@icloud.com',
url='https://github.com/messense/extender',
Expand Down Expand Up @@ -40,4 +42,4 @@
],
tests_require=['nose'],
test_suite='nose.collector',
)
)
2 changes: 1 addition & 1 deletion tests/plugin1/setup.py
Expand Up @@ -20,4 +20,4 @@
'plugin1 = plugin1.plugin:TestPlugin1',
]
},
)
)
2 changes: 1 addition & 1 deletion tests/plugin2/setup.py
Expand Up @@ -20,4 +20,4 @@
'plugin2 = plugin2.plugin:TestPlugin2',
]
},
)
)
2 changes: 1 addition & 1 deletion tests/plugin3/setup.py
Expand Up @@ -20,4 +20,4 @@
'plugin3 = plugin3.plugin:TestPlugin3',
]
},
)
)
31 changes: 23 additions & 8 deletions tests/test_pluginmanager.py
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
import nose
import six
from nose.tools import raises
from extender import PluginManager, Plugin
from extender.plugin import PluginMount


def test_plugins_install():
Expand All @@ -15,15 +17,15 @@ def test_plugins_install_with_entry_points():
assert len(plugins) == 3


def test_plugin_get():
def test_plugins_get():
plugins = PluginManager()
plugins.install('extender.plugins')

assert plugins.get('plugin1') is not None


@raises(KeyError)
def test_plugin_get_with_nonexists_slug():
def test_plugins_get_with_nonexists_slug():
plugins = PluginManager()
plugins.install('extender.plugins')
plugins.get('test-plugin')
Expand Down Expand Up @@ -112,19 +114,32 @@ def test(self):
return 'test'


def test_plugin_register_with_class():
class TestGenericPlugin(six.with_metaclass(PluginMount, object)):
title = 'TestGenericPlugin'

def test(self):
return 'test'


def test_plugins_register_with_class():
plugins = PluginManager()
plugins.register(TestPlugin)
assert plugins.first('test') == 'test'


def test_plugin_register_with_instance():
def test_plugins_register_with_instance():
plugins = PluginManager()
plugins.register(TestPlugin())
assert plugins.first('test') == 'test'


def test_plugin_unregister_with_slug():
def test_plugins_register_generic_plugin_with_class():
plugins = PluginManager()
plugins.register(TestGenericPlugin)
assert plugins.first('test') == 'test'


def test_plugins_unregister_with_slug():
plugins = PluginManager()
plugins.install('extender.plugins')

Expand All @@ -133,7 +148,7 @@ def test_plugin_unregister_with_slug():
assert len(plugins) == 2


def test_plugin_unregister_with_class():
def test_plugins_unregister_with_class():
plugins = PluginManager()
plugins.register(TestPlugin)

Expand All @@ -142,7 +157,7 @@ def test_plugin_unregister_with_class():
assert len(plugins) == 0


def test_plugin_unregister_with_instance():
def test_plugins_unregister_with_instance():
plugins = PluginManager()
plugins.register(TestPlugin)

Expand All @@ -151,4 +166,4 @@ def test_plugin_unregister_with_instance():
assert len(plugins) == 0

if __name__ == '__main__':
nose.runmodule()
nose.runmodule()

0 comments on commit 0220a2c

Please sign in to comment.