Merge lp:~divmod-dev/divmod.org/simplify-metaASC-3030 into lp:divmod.org

Proposed by Tristan Seligmann
Status: Merged
Approved by: Jonathan Jacobs
Approved revision: 2676
Merged at revision: 2681
Proposed branch: lp:~divmod-dev/divmod.org/simplify-metaASC-3030
Merge into: lp:divmod.org
Diff against target: 79 lines (+30/-9)
1 file modified
Axiom/axiom/scripts/axiomatic.py (+30/-9)
To merge this branch: bzr merge lp:~divmod-dev/divmod.org/simplify-metaASC-3030
Reviewer Review Type Date Requested Status
Jonathan Jacobs Approve
Review via email: mp+72321@code.launchpad.net

Description of the change

Simplify the metaclass trickery that makes subclasses of AxiomaticCommand automatically provide IPlugin and IAxiomaticCommand.

To post a comment you must log in.
Revision history for this message
Jonathan Jacobs (jjacobs) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Axiom/axiom/scripts/axiomatic.py'
2--- Axiom/axiom/scripts/axiomatic.py 2009-06-22 20:54:01 +0000
3+++ Axiom/axiom/scripts/axiomatic.py 2011-08-21 02:40:49 +0000
4@@ -1,5 +1,5 @@
5 # -*- test-case-name: axiomatic.test.test_axiomatic -*-
6-from zope.interface import directlyProvides
7+from zope.interface import alsoProvides, noLongerProvides
8
9 import os
10 import sys
11@@ -7,12 +7,12 @@
12 import errno
13 import signal
14
15-from twisted import plugin
16+from twisted.plugin import IPlugin, getPlugins
17 from twisted.python import usage
18 from twisted.python.runtime import platform
19 from twisted.scripts import twistd
20
21-from axiom import iaxiom
22+from axiom.iaxiom import IAxiomaticCommand
23
24 class AxiomaticSubCommandMixin(object):
25 store = property(lambda self: self.parent.getStore())
26@@ -23,18 +23,39 @@
27 codec = getattr(sys.stdin, 'encoding', None) or sys.getdefaultencoding()
28 return unicode(cmdline, codec)
29
30-class _metaASC(type):
31+
32+
33+class _AxiomaticCommandMeta(type):
34+ """
35+ Metaclass for L{AxiomaticCommand}.
36+
37+ This serves to make subclasses provide L{IPlugin} and L{IAxiomaticCommand}.
38+ """
39 def __new__(cls, name, bases, attrs):
40 newcls = type.__new__(cls, name, bases, attrs)
41- if not (newcls.__name__ == 'AxiomaticCommand' and newcls.__module__ == _metaASC.__module__):
42- directlyProvides(newcls, plugin.IPlugin, iaxiom.IAxiomaticCommand)
43+ alsoProvides(newcls, IPlugin, IAxiomaticCommand)
44 return newcls
45
46+
47+
48 class AxiomaticSubCommand(usage.Options, AxiomaticSubCommandMixin):
49- pass
50+ """
51+ L{twisted.python.usage.Options} subclass for Axiomatic sub commands.
52+ """
53+
54+
55
56 class AxiomaticCommand(usage.Options, AxiomaticSubCommandMixin):
57- __metaclass__ = _metaASC
58+ """
59+ L{twisted.python.usage.Options} subclass for Axiomatic plugin commands.
60+
61+ Subclass this to have your class automatically provide the necessary
62+ interfaces to be picked up by axiomatic.
63+ """
64+ __metaclass__ = _AxiomaticCommandMeta
65+
66+noLongerProvides(AxiomaticCommand, IPlugin)
67+noLongerProvides(AxiomaticCommand, IAxiomaticCommand)
68
69
70
71@@ -119,7 +140,7 @@
72 yield ('status', None, Status, 'Report whether a server is running from the given Axiom database')
73
74 from axiom import plugins
75- for plg in plugin.getPlugins(iaxiom.IAxiomaticCommand, plugins):
76+ for plg in getPlugins(IAxiomaticCommand, plugins):
77 try:
78 yield (plg.name, None, plg, plg.description)
79 except AttributeError:

Subscribers

People subscribed via source and target branches

to all changes: