/usr/lib/python3/dist-packages/pyutilib/misc/factory.py is in python3-pyutilib 5.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # _________________________________________________________________________
#
# PyUtilib: A Python utility library.
# Copyright (c) 2008 Sandia Corporation.
# This software is distributed under the BSD License.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
# _________________________________________________________________________
class Factory(object):
"""
Class that provides a generic constructor mechanism.
Adapted from code developed by Andres Tuells and submitted to
the ActiveState Programmer Network http://aspn.activestate.com
"""
def __init__(self):
""" Constructor """
self.Keys = []
self.constructors = {}
def register(self, methodName, constructor, *args, **kargs):
""" Register a constructor """
self.Keys = self.Keys + [methodName]
_args = [constructor]
_args.extend(args)
mname = "construct_" + methodName
self.constructors[methodName] = _Functor(*_args,**kargs)
#self.constructors[methodName] = apply(_Functor,_args, kargs)
setattr(self, methodName,_Functor(*_args, **kargs))
#setattr(self, methodName,apply(_Functor,_args, kargs))
def construct(self,name):
"""
Use the string name to create an object.
"""
try:
method = getattr(self,str(name))
except AttributeError:
return None
return method()
def __call__(self,name):
"""
Use the string name to create an object.
"""
return self.construct(name)
def unregister(self, methodName):
""" Unregister a constructor """
delattr(self, methodName)
self.Keys.remove(methodName)
def keys(self):
"""
Returns the list of names that can are registered in this factory
"""
return self.Keys
def __len__(self):
"""
Returns the number of names in the factory
"""
return len(self.Keys)
def __iter__(self):
""" Returns an iterator for the factory keys """
return self.Keys.__iter__()
def __getitem__(self,index):
""" Allows an index into the list of factory keys """
return self.Keys[index]
class _Functor:
"""
A class that wraps a function to act like the function.
"""
def __init__(self, function, *args, **kargs):
""" Constructor """
assert hasattr(function,'__call__'), "function should be a callable obj"
self._function = function
self._args = args
self._kargs = kargs
def __call__(self, *args, **kargs):
""" Call function """
_args = list(self._args)
_args.extend(args)
_kargs = self._kargs.copy()
_kargs.update(kargs)
return self._function(*_args,**_kargs)
|