Skip to content

Commit

Permalink
Merge pull request #9 from zopefoundation/pep8
Browse files Browse the repository at this point in the history
Fix the code to be pep 8 compliant.
  • Loading branch information
thefunny42 committed May 9, 2018
2 parents 7092898 + 4dce998 commit ff41a91
Show file tree
Hide file tree
Showing 38 changed files with 272 additions and 147 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGES

martian.ignore('Example')

- Fix the code to be pep 8 compliant.

1.1 (2018-01-25)
================
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()


long_description = (
read('README.rst')
+ '\n' +
Expand Down
24 changes: 12 additions & 12 deletions src/martian/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ Let's look at the framework::
... return self.template.substitute(**kw)
...
... # the registry, we plug in the two templating systems right away
... extension_handlers = { '.txt': InterpolationTemplate,
... extension_handlers = { '.txt': InterpolationTemplate,
... '.tmpl': TemplateStringTemplate }
...
... def render(data, extension, **kw):
... """Render the template at filepath with arguments.
...
...
... data - the data in the file
... extension - the extension of the file
... keyword arguments - variables to interpolate
Expand Down Expand Up @@ -176,7 +176,7 @@ works now::
Above we plug into our ``extension_handler`` registry using Python
code. Using separate code to manually hook components into registries
can get rather cumbersome - each time you write a plugin, you also
need to remember you need to register it.
need to remember you need to register it.

Doing template registration in Python code also poses a maintenance
risk. It is tempting to start doing fancy things in Python code such
Expand All @@ -195,7 +195,7 @@ what to configure where can often be deduced from the structure of
Python code itself, especially when it can be annotated with
additional declarations. The idea is to make it so easy to write and
register a plugin so that even extensive configuration does not overly
burden the developer.
burden the developer.

Configuration actions are executed during a separate phase ("grok
time"), not at import time, which makes it easier to reason about and
Expand Down Expand Up @@ -271,7 +271,7 @@ template languages? Now we can use Martian. We define a *grokker* for
``Template`` that registers the template classes in the
``extension_handlers`` registry::

>>> class meta(FakeModule):
>>> class meta(FakeModule):
... class TemplateGrokker(martian.ClassGrokker):
... martian.component(Template)
... martian.directive(extension)
Expand All @@ -284,7 +284,7 @@ What does this do? A ``ClassGrokker`` has its ``execute`` method
called for subclasses of what's indicated by the ``martian.component``
directive. You can also declare what directives a ``ClassGrokker``
expects on this component by using ``martian.directive()`` (the
``directive`` directive!) one or more times.
``directive`` directive!) one or more times.

The ``execute`` method takes the class to be grokked as the first
argument, and the values of the directives used will be passed in as
Expand Down Expand Up @@ -390,10 +390,10 @@ class, and we want to track instances of it::
... lion = Animal('lion')
... animals = {}
>>> from martiantest.fake import zoo

We define an ``InstanceGrokker`` subclass to grok ``Animal`` instances::

>>> class meta(FakeModule):
>>> class meta(FakeModule):
... class AnimalGrokker(martian.InstanceGrokker):
... martian.component(Animal)
... def execute(self, instance, **kw):
Expand All @@ -402,7 +402,7 @@ We define an ``InstanceGrokker`` subclass to grok ``Animal`` instances::
>>> from martiantest.fake import meta

Let's create a new registry with the ``AnimalGrokker`` in it::

>>> reg = martian.GrokkerRegistry()
>>> reg.grok('meta', meta)
True
Expand All @@ -415,9 +415,9 @@ We can now grok the ``zoo`` module::
The animals will now be in the ``animals`` dictionary::

>>> sorted(zoo.animals.items())
[('chicken', <Animal object at ...>),
('elephant', <Animal object at ...>),
('horse', <Animal object at ...>),
[('chicken', <Animal object at ...>),
('elephant', <Animal object at ...>),
('horse', <Animal object at ...>),
('lion', <Animal object at ...>)]

More information
Expand Down
37 changes: 27 additions & 10 deletions src/martian/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
from martian.core import (
ModuleGrokker, MultiGrokker, MetaMultiGrokker, grok_dotted_name,
grok_package, grok_module, GrokkerRegistry)
from martian.core import ModuleGrokker, MultiGrokker, MetaMultiGrokker
from martian.core import grok_dotted_name, grok_package, grok_module
from martian.core import GrokkerRegistry
from martian.components import GlobalGrokker, ClassGrokker, InstanceGrokker
from martian.components import MethodGrokker
from martian.util import scan_for_classes
from martian.directive import Directive, MarkerDirective, MultipleTimesDirective
from martian.directive import (ONCE, ONCE_NOBASE, ONCE_IFACE,
MULTIPLE, MULTIPLE_NOBASE, DICT)
from martian.directive import CLASS, CLASS_OR_MODULE, MODULE
from martian.directive import UNKNOWN, UnknownError
from martian.directive import (
validateText, validateInterface, validateClass, validateInterfaceOrClass)
from martian.directive import Directive, MarkerDirective
from martian.directive import MultipleTimesDirective
from martian.directive import ONCE, ONCE_NOBASE, ONCE_IFACE
from martian.directive import MULTIPLE, MULTIPLE_NOBASE, DICT
from martian.directive import CLASS, CLASS_OR_MODULE, MODULE, UNKNOWN
from martian.directive import UnknownError
from martian.directive import validateText, validateInterface
from martian.directive import validateClass, validateInterfaceOrClass
from martian.martiandirective import component, directive, priority, baseclass
from martian.martiandirective import ignore
from martian.context import GetDefaultComponentFactory


__all__ = [
'ModuleGrokker', 'MultiGrokker', 'MetaMultiGrokker', 'grok_dotted_name',
'grok_package', 'grok_module', 'GrokkerRegistry',
'GlobalGrokker', 'ClassGrokker', 'InstanceGrokker',
'MethodGrokker', 'scan_for_classes',
'Directive', 'MarkerDirective', 'MultipleTimesDirective',
'ONCE', 'ONCE_NOBASE', 'ONCE_IFACE', 'MULTIPLE', 'MULTIPLE_NOBASE', 'DICT',
'CLASS', 'CLASS_OR_MODULE', 'MODULE', 'UNKNOWN', 'UnknownError',
'validateText', 'validateInterface', 'validateClass',
'validateInterfaceOrClass',
'component', 'directive', 'priority', 'baseclass', 'ignore',
'GetDefaultComponentFactory'

]
2 changes: 1 addition & 1 deletion src/martian/compat3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@


if sys.version_info[0] < 3:
str = unicode
str = unicode # NOQA
else:
str = str
11 changes: 6 additions & 5 deletions src/martian/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
from martian.interfaces import IGrokker, IComponentGrokker
from martian.martiandirective import directive, component


@implementer(IGrokker)
class GrokkerBase(object):

def grok(self, name, obj, **kw):
raise NotImplementedError


class GlobalGrokker(GrokkerBase):
"""Grokker that groks once per module.
"""

def grok(self, name, obj, **kw):
raise NotImplementedError


@implementer(IComponentGrokker)
class ClassGrokker(GrokkerBase):
Expand Down Expand Up @@ -92,14 +93,14 @@ def grok(self, name, class_, module_info=None, **kw):
def execute(self, class_, method, **data):
raise NotImplementedError


@implementer(IComponentGrokker)
class InstanceGrokker(GrokkerBase):
"""Grokker that groks instances in a module.
"""

def grok(self, name, class_, **kw):
def grok(self, name, class_, **kw):
return self.execute(class_, **kw)

def execute(self, class_, **kw):
raise NotImplementedError

8 changes: 5 additions & 3 deletions src/martian/context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from martian.directive import UnknownError
from martian.util import scan_for_classes


class GetDefaultComponentFactory(object):

def __init__(self, iface, component_name, directive_name):
"""Create a get_default_component function.
Expand All @@ -15,17 +17,17 @@ def __init__(self, iface, component_name, directive_name):
self.iface = iface
self.component_name = component_name
self.directive_name = directive_name

def __call__(self, component, module, **data):
"""Determine module-level component.
Look for components in module.
iface determines the kind of module-level component to look for
(it will implement iface).
If there is no module-level component, raise an error.
If there is one module-level component, it is returned.
If there are more than one module-level component, raise
Expand Down
4 changes: 2 additions & 2 deletions src/martian/context.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ It has the following rules:
Let's implement a context directive with this behavior::

>>> import martian
>>> class context(martian.Directive):
>>> class context(martian.Directive):
... scope = martian.CLASS_OR_MODULE
... store = martian.ONCE

Expand Down Expand Up @@ -126,7 +126,7 @@ There are too many possible contexts::
>>> from martiantest.fake import ambiguouscontext
>>> context.bind(get_default=get_default_context).get(ambiguouscontext.B)
Traceback (most recent call last):
...
...
GrokError: Multiple possible contexts for <class 'martiantest.fake.ambiguouscontext.B'>, please use the 'context' directive.

Let's try this with inheritance, where an implicit context is provided
Expand Down
37 changes: 30 additions & 7 deletions src/martian/core.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import types, inspect
import types
import inspect

from zope.interface import implementer

from martian.interfaces import IGrokker, IMultiGrokker
from martian.interfaces import IMultiGrokker
from martian import util, scan
from martian.components import (GrokkerBase, ClassGrokker, InstanceGrokker,
GlobalGrokker)
from martian.components import GrokkerBase, ClassGrokker, InstanceGrokker
from martian.components import GlobalGrokker
from martian.error import GrokError
from martian.martiandirective import component, priority
from martian.compat3 import CLASS_TYPES


@implementer(IMultiGrokker)
class MultiGrokkerBase(GrokkerBase):

Expand All @@ -36,12 +38,14 @@ def clear(self):
def grokkers(self, name, obj):
raise NotImplementedError


def _grokker_sort_key(args):
"""Helper function to calculate sort order of grokker.
"""
grokker, name, obj = args
return priority.bind().get(grokker)


class ModuleGrokker(MultiGrokkerBase):

def __init__(self, grokker=None, prepare=None, finalize=None):
Expand Down Expand Up @@ -107,6 +111,7 @@ def grokkers(self, name, module):
for t in grokker.grokkers(name, obj):
yield t


class MultiInstanceOrClassGrokkerBase(MultiGrokkerBase):

def __init__(self):
Expand Down Expand Up @@ -134,16 +139,21 @@ def grokkers(self, name, obj):
yield grokker, name, obj
used_grokkers.add(grokker)


class MultiInstanceGrokker(MultiInstanceOrClassGrokkerBase):

def get_bases(self, obj):
return inspect.getmro(obj.__class__)


class MultiClassGrokker(MultiInstanceOrClassGrokkerBase):

def get_bases(self, obj):
if type(obj) is types.ModuleType:
if isinstance(obj, types.ModuleType):
return []
return inspect.getmro(obj)


class MultiGlobalGrokker(MultiGrokkerBase):

def __init__(self):
Expand All @@ -162,6 +172,7 @@ def grokkers(self, name, module):
for grokker in self._grokkers:
yield grokker, name, module


class MultiGrokker(MultiGrokkerBase):

def __init__(self):
Expand Down Expand Up @@ -190,34 +201,41 @@ def grokkers(self, name, obj):
else:
return self._multi_instance_grokker.grokkers(name, obj)


class MetaMultiGrokker(MultiGrokker):
"""Multi grokker which comes pre-registered with meta-grokkers.
"""

def clear(self):
super(MetaMultiGrokker, self).clear()
# bootstrap the meta-grokkers
self.register(ClassMetaGrokker(self))
self.register(InstanceMetaGrokker(self))
self.register(GlobalMetaGrokker(self))


def grok_dotted_name(dotted_name, grokker, exclude_filter=None,
ignore_nonsource=True, **kw):
module_info = scan.module_info_from_dotted_name(dotted_name, exclude_filter,
ignore_nonsource)
module_info = scan.module_info_from_dotted_name(
dotted_name, exclude_filter, ignore_nonsource)
grok_package(module_info, grokker, **kw)


def grok_package(module_info, grokker, **kw):
grok_module(module_info, grokker, **kw)
for sub_module_info in module_info.getSubModuleInfos():
grok_package(sub_module_info, grokker, **kw)


def grok_module(module_info, grokker, **kw):
grokker.grok(module_info.dotted_name, module_info.getModule(),
module_info=module_info, **kw)


# deep meta mode here - we define grokkers that can pick up the
# three kinds of grokker: ClassGrokker, InstanceGrokker and ModuleGrokker
class MetaGrokker(ClassGrokker):

def __init__(self, multi_grokker):
"""multi_grokker - the grokker to register grokkers with.
"""
Expand All @@ -227,15 +245,20 @@ def grok(self, name, obj, **kw):
self.multi_grokker.register(obj())
return True


class ClassMetaGrokker(MetaGrokker):
component(ClassGrokker)


class InstanceMetaGrokker(MetaGrokker):
component(InstanceGrokker)


class GlobalMetaGrokker(MetaGrokker):
component(GlobalGrokker)


class GrokkerRegistry(ModuleGrokker):

def __init__(self):
super(GrokkerRegistry, self).__init__(MetaMultiGrokker())
Loading

0 comments on commit ff41a91

Please sign in to comment.