1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought util package component>
#------------------------------------------------------------------------------
""" A generic instance factory. """
# Enthought library imports.
from enthought.traits.api import Any, Dict, HasTraits, List
class _Factory(HasTraits):
""" A generic instance.
A factory is a factory that creates instances of a specified class.
"""
# The class that we are a factory for.
klass = Any
# Any non-keyword arguments to pass to our klass constructor.
args = List()
# Any keyword arguments to pass to our klass constructor.
kw = Dict()
###########################################################################
# object interface.
###########################################################################
def __call__(self, *args, **kw):
""" Creates an instance of the 'klass'.
This allows the caller to create an instance using a different set
of arguments to those passed into the factory constructor, and in
turn makes the factory behave like a normal class (which is simply
a factory after all).
"""
return self._create_instance(self.klass, args, kw)
###########################################################################
# 'Factory' interface.
###########################################################################
def generate(self):
""" Creates an instance of this 'klass'. """
return self._create_instance(self.klass, self._get_args(),
self._get_kw())
###########################################################################
# Protected interface.
###########################################################################
def _get_args(self):
return self.args
def _get_kw(self):
return self.kw
###########################################################################
# Private interface.
###########################################################################
def _create_instance(self, klass, args, kw):
""" Creates an instance of 'klass'. """
return klass(*args, **kw)
# make clones of arguments. this is a temporary fix for Converge ticket #676.
# fixme: this is a bit hacky. the correct thing to do is to
# figure out an intelligent way to intelligently indicate when we
# should be cloning these arguments, but for now we're going to do it
# any time we can.
class Factory(_Factory):
def _get_args(self):
args = []
for arg in self.args:
if hasattr(arg, 'clone'):
args.append(arg.clone())
else:
args.append(arg)
return args
def _get_kw(self):
kw = {}
for key, value in self.kw.items():
if hasattr(value, 'clone'):
kw[key] = value.clone()
else:
kw[key] = value
return kw
#### EOF ######################################################################
|