Author: bugman Date: Thu May 3 19:34:45 2012 New Revision: 15911 URL: http://svn.gna.org/viewcvs/relax?rev=15911&view=rev Log: Created the Uf_info singleton class for holding all of user function data. Modified: branches/uf_redesign/user_functions/__init__.py Modified: branches/uf_redesign/user_functions/__init__.py URL: http://svn.gna.org/viewcvs/relax/branches/uf_redesign/user_functions/__init__.py?rev=15911&r1=15910&r2=15911&view=diff ============================================================================== --- branches/uf_redesign/user_functions/__init__.py (original) +++ branches/uf_redesign/user_functions/__init__.py Thu May 3 19:34:45 2012 @@ -26,4 +26,81 @@ This package contains all information and details about user functions, from documentation to icons to be used in the GUI. This package contains a special data structure which will be used by the different UIs to automatically generate their interfaces to the user functions. """ +# The __all__ package list. __all__ = [''] + + +# Python module imports. +from re import search +from string import split + +# relax module imports. +from relax_errors import RelaxError +from user_functions.objects import Class_container, Uf_container + + +class Uf_info(object): + """The user function data singleton class.""" + + # Class variable for storing the class instance (for the singleton). + _instance = None + + def __new__(self, *args, **kargs): + """Replacement method for implementing the singleton design pattern.""" + + # First instantiation. + if self._instance is None: + # Instantiate. + self._instance = object.__new__(self, *args, **kargs) + + # Initialise a number of class variables. + self._uf_names = [] + self._uf = [] + self._class_names = [] + self._classes = [] + + # Already instantiated, so return the instance. + return self._instance + + + def add_class(self, name): + """Add a new user function class. + + @param name: The name of the user function class. + @type name: str + @return: The user function class data object. + @rtype: user_functions.objects.Class_container instance + """ + + # Store the name and initialise a new object. + self._class_names.append(name) + self._classes.append(Class_container()) + + # Return the object. + return self._classes[-1] + + + def add_uf(self, name): + """Add the user function to the object. + + @param name: The name of the user function. + @type name: str + @return: The user function data object. + @rtype: user_functions.objects.Uf_container instance + """ + + # First check if the user function class has been set up. + if search('.', name): + # Split up the name. + class_name, fn_name = split(name, '.') + + # Check for the class name. + if class_name not in self._class_names: + raise RelaxError("The user function class '%s' has not been set up yet." % class_name) + + # Store the name and initialise a new object. + self._uf_names.append(name) + self._uf.append(Uf_container()) + + # Return the object. + return self._uf[-1]