Extending Swoop¶
Swoop is extensible via two mechanisms. Both of them modify the set of classes
that Swoop creates as it parses a file and creates new EagleFilePart
objects.
Mixins¶
The first mechanism is mixins. A mixin is as class that provides some
additional functionality for each EagleFilePart
that Swoop creates.
When you extend Swoop using a mixin, you create a new subclass of each
EagleFilePart
class that Swoop uses (e.g., Library
,
Attribute
, and Symbol
). The new subclass inherits both from
original class and the mixin in class.
The Swoop.Mixin()
function generates all theses classes automatically.
For instance, to add a set of arbitrary attributes to each object, you could do this:
class AttrMixin(object):
def __init__(self):
self.attrs = {}
def set_attr(self, n, v):
self.attrs[n] = v
return self
def get_attr(self, n):
return self.attrs.get(n)
AttrEagleFile = Swoop.Mixin(AttrMixin, "Attr")
sch = AttrEagleFile.open("test.sch")
sch.get_library("A_LIBARRY").get_symbol("GOOD_SYMBOL").set_attr("good?", "yes!")
This creates a bunch of new classes including AttrPackage
,
AttrLibraryFile
, and AttrWire
. The AttrEagleFile
object that AttrEagleFile.open()
returns is made of solely of these new
Attr*
objects.
You can compose extensions as well:
class Walker(object):
def do_it(self):
return "walk"
WalkerAttrEagleFile = Swoop.Mixin(Walker, "Walker",base=AttrEagleFile)
sch2 = WalkerAttrEagleFile.open("test.sch")
sch2.get_library("A_LIBARRY").get_symbol("GOOD_SYMBOL").do_it()
sch2.get_library("A_LIBARRY").get_symbol("GOOD_SYMBOL").set_attr("good?", "yes!")
-
Swoop.
Mixin
(mixin, prefix, base=<class 'Swoop.Swoop.EagleFile'>)[source]¶ Extend Swoop by adding a mixin to every class.
This function creates a new subclasses of every class Swoop uses to represent an Eagle file (including the file types). The names of the new classes are prefixed with
prefix
and they all inherit from the original class andmixin
.Parameters: - mixin – Mixin class. Its constructor should take no arguments.
- prefix – Prefix for the class name. It’ll be prepended to the names of all the Swoop classes.
- base – Starting point for the extension. This should either be
Swoop.EagleFile
or a class returned by a previous call to this function.
Returns: A new subclass of
base
that can be used just likeEagleFile
Return type: A class.
The Class Map¶
You can take finer-grain control over which classes Swoop uses by modifying the
class_map
member of EagleFile
(or a derived class returned by
Swoop.Mixin()
).
The class_map
defines the mapping between tag names (as they appear in
Eagle file) and EagleFilePart
subclasses.
For instance if you wanted to add a set_location()
method to
Element
you could write the following:
from Swoop import *
class MyElement(Element):
def __init__(self):
Element.__init__(self)
def set_location(x,y):
self.set_x(x)
self.set_y(y)
EagleFile.class_map["element"] = MyElement
If you later extend EagleFile
using Swoop.Mixin()
, the mixin will be applied to MyElement
.