Listing 5. Detecting and Installing Mix-ins Named after Their Classes
from MixIn import MixIn
from glob import glob
import os
def MixInDir(dir):
''' Installs mix-ins found in the given directory.
Each mix-in is expected to match the name
of a class in our domain classes. '''
path = os.path.join(basePath, '*.py')
for filename in glob(path):
dirname, name = os.path.split(filename)
name = os.path.splitext(name)[0]
# Get the real class
module = __import__(name)
pyClass = getattr(module, name)
# Get the mix-in class
results = {}
exec open(filename) in results
mixInClass = results[name]
# Mix it in
MixIn(pyClass, mixInClass)
Copyright © 1994 - 2018 Linux Journal. All rights reserved.