Exemplo n.º 1
0
    def __init__(self, formatters, variables, separator=' ', formatStr=None):
        """Initialize a MOFormatter object with a list of formatters
        to be merged and the variables list.

        @raise FormatterError: no Formatters specified.
        """

        # sanity check
        if not formatters:
            raise FormatterError, "no Formatters specified."

        # initialize the variables
        # keeps the order that the formatters were defined as
        self.order      = []
        # keyed by base format class name, value is a Formatter object
        # for that class type
        self.formatters = {}
        # keyed by base format class name, value is an object of that
        # type
        self.objects    = {}

        for f in formatters:
            # create a Formatter object with no variables to print
            fobj = f([])
            # get the classname of the object being formatted
            classname = fobj.className.__class__.__name__
            # add everything to the list
            self.order.append(classname)
            self.formatters[classname] = fobj
            self.objects[classname] = fobj.className

        # cached used to identify a variable name with a class name
        # if it has already been referenced.
        self.varcache = {}

        # since we have overloaded the appropriate functions, it doesn't
        # matter that we aren't passing a valid class name to Formatter.
        Formatter.__init__(self, None, variables, separator=separator,
                           formatStr=formatStr)

        # now, as if what we've done isn't crazy enough, point the widths
        # data back to all the formatters so they know it too
        for val in self.formatters.values():
            val.widths = self.widths
            val.lines  = self.lines
            val.colors = self.colors
Exemplo n.º 2
0
    def getString(self, objects, fitString='squeeze'):
        """
        Returns a string based on the format string set in __init__
        and uses the objects defined in the list 'objects' as the input.
        The order of the objects does not matter, and you also need not
        pass in an object that won't be used.  For example, if your
        format string doesn't require any variables from the Slot object,
        then one need not be passed.

        """

        # get rid of the old values
        self.objects.clear()
        
        for obj in objects:
            self.objects[obj.__class__.__name__] = obj

        # since we have overloaded _getValue we need not pass a valid
        # object.
        return Formatter.getString(self, None, fitString=fitString)