Beispiel #1
0
    def __init__(self,
                 width=70,
                 indentStep=4,
                 treeDoc=None,
                 footer=None,
                 fileObj=None):
        '''Can specify the width of output, the indent step, the header
        and footer to print, and the destination fileObj. If no destination
        file, then stdout is assumed.'''
        self.__traverser = TopicTreeTraverser(self)

        import sys
        self.__destination = fileObj or sys.stdout
        self.__output = []
        self.__header = _toDocString(treeDoc)
        self.__footer = footer
        self.__lastWasAll = False  # True when last topic done was the ALL_TOPICS

        self.__width = width
        self.__wrapper = TextWrapper(width)
        self.__indentStep = indentStep
        self.__indent = 0

        args = dict(width=width,
                    indentStep=indentStep,
                    treeDoc=treeDoc,
                    footer=footer,
                    fileObj=fileObj)

        def fmItem(argName, argVal):
            if isinstance(argVal, str):
                MIN_OFFSET = 5
                lenAV = width - MIN_OFFSET - len(argName)
                if lenAV > 0:
                    argVal = ` argVal[:lenAV] + '...' `
            elif argName == 'fileObj':
                argVal = fileObj.__class__.__name__
            return '# - %s: %s' % (argName, argVal)

        fmtArgs = [
            fmItem(argName, argVal) for (argName, argVal) in args.iteritems()
        ]
        self.__comment = [
            '# Automatically generated by %s(**kwargs).' %
            self.__class__.__name__,
            '# The kwargs were:',
        ]
        self.__comment.extend(fmtArgs)
        self.__comment.extend([''])  # two empty line after comment
    def __init__(self, rootTopic=None, fileObj=None, width=70, indentStep=4, 
        treeDoc = defaultTopicTreeSpecHeader, footer = defaultTopicTreeSpecFooter):
        '''For formatting, can specify the width of output, the indent step, the 
        header and footer to print to override defaults. The destination is fileObj;
        if none is given, then sys.stdout is used. If rootTopic is given(), calls
        writeAll(rootTopic) at end of __init__.'''
        self.__traverser = TopicTreeTraverser(self)

        import sys
        fileObj = fileObj or sys.stdout

        self.__destination = fileObj
        self.__output = []
        self.__header = _toDocString(treeDoc)
        self.__footer = footer
        self.__lastWasAll = False # True when last topic done was the ALL_TOPICS

        self.__width   = width
        self.__wrapper = TextWrapper(width)
        self.__indentStep = indentStep
        self.__indent  = 0

        args = dict(width=width, indentStep=indentStep, treeDoc=treeDoc,
                    footer=footer, fileObj=fileObj)
        def fmItem(argName, argVal):
            if isinstance(argVal, (str, unicode)):
                MIN_OFFSET = 5
                lenAV = width - MIN_OFFSET - len(argName)
                if lenAV > 0:
                    argVal = `argVal[:lenAV] + '...'`
            elif argName == 'fileObj':
                argVal = fileObj.__class__.__name__
            return '# - %s: %s' % (argName, argVal)
        fmtArgs = [fmItem(argName, argVal) for (argName, argVal) in args.iteritems()]
        self.__comment = [
            '# Automatically generated by %s(**kwargs).' % self.__class__.__name__,
            '# The kwargs were:',
        ]
        self.__comment.extend(fmtArgs)
        self.__comment.extend(['']) # two empty line after comment
        
        if rootTopic is not None:
            self.writeAll(rootTopic)