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)
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)
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
class TopicTreeSpecPrinter: ''' Function class to print the topic tree using the Python class syntax. If printed to a module, it can then be imported, given to pub.addTopicDefnProvider(), etc. The printout can be sent to any file object (object that has a write() method). ''' INDENT_CH = ' ' #INDENT_CH = '.' 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) def getOutput(self): '''Each line that was sent to fileObj was saved in a list; returns a string which is '\n'.join(list).''' return '\n'.join( self.__output ) def writeAll(self, topicObj): '''Traverse each topic of topic tree, starting at topicObj, printing each topic definition as the tree gets traversed. ''' self.__traverser.traverse(topicObj) def _accept(self, topicObj): # accept every topic return True def _startTraversal(self): # output comment self.__wrapper.initial_indent = '# ' self.__wrapper.subsequent_indent = self.__wrapper.initial_indent self.__output.extend( self.__comment ) # output header: if self.__header: self.__output.extend(['']) self.__output.append(self.__header) self.__output.extend(['']) def _doneTraversal(self): if self.__footer: self.__output.append('') self.__output.append('') self.__output.append(self.__footer) if self.__destination is not None: self.__destination.write(self.getOutput()) def _onTopic(self, topicObj): '''This gets called for each topic. Print as per specified content.''' # don't print root of tree, it is the ALL_TOPICS builtin topic if topicObj.isAll(): self.__lastWasAll = True return self.__lastWasAll = False self.__output.append( '' ) # empty line # topic name self.__wrapper.width = self.__width head = 'class %s:' % topicObj.getNodeName() self.__formatItem(head) # each extra content (assume constructor verified that chars are valid) self.__printTopicDescription(topicObj) if policies.msgDataProtocol != 'arg1': self.__printTopicArgSpec(topicObj) def _startChildren(self): '''Increase the indent''' if not self.__lastWasAll: self.__indent += self.__indentStep def _endChildren(self): '''Decrease the indent''' if not self.__lastWasAll: self.__indent -= self.__indentStep def __printTopicDescription(self, topicObj): if topicObj.getDescription(): extraIndent = self.__indentStep self.__formatItem("'''", extraIndent) self.__formatItem( topicObj.getDescription(), extraIndent ) self.__formatItem("'''", extraIndent) def __printTopicArgSpec(self, topicObj): extraIndent = self.__indentStep # generate the listener protocol reqdArgs, optArgs = topicObj.getArgs() argsStr = [] if reqdArgs: argsStr.append( ", ".join(reqdArgs) ) if optArgs: optStr = ', '.join([('%s=None' % arg) for arg in optArgs]) argsStr.append(optStr) argsStr = ', '.join(argsStr) # print it only if there are args; ie if listener() don't print it if argsStr: # output a blank line and protocol self.__formatItem('\n', extraIndent) protoListener = 'def %s(%s):' % (SPEC_METHOD_NAME, argsStr) self.__formatItem(protoListener, extraIndent) # and finally, the args docs extraIndent += self.__indentStep self.__formatItem("'''", extraIndent) # but ignore the arg keys that are in parent args docs: parentMsgKeys = () if topicObj.getParent() is not None: parentMsgKeys = topicObj.getParent().getArgDescriptions().keys() argsDocs = topicObj.getArgDescriptions() for key, argDesc in argsDocs.iteritems(): if key not in parentMsgKeys: msg = "- %s: %s" % (key, argDesc) self.__formatItem(msg, extraIndent) self.__formatItem("'''", extraIndent) def __formatItem(self, item, extraIndent=0): indent = extraIndent + self.__indent indentStr = self.INDENT_CH * indent lines = item.splitlines() for line in lines: self.__output.append( '%s%s' % (indentStr, line) ) def __formatBlock(self, text, extraIndent=0): self.__wrapper.initial_indent = self.INDENT_CH * (self.__indent + extraIndent) self.__wrapper.subsequent_indent = self.__wrapper.initial_indent self.__output.append( self.__wrapper.fill(text) )
class TopicTreeAsSpec: ''' Prints the class representation of topic tree, as Python code that can be imported and given to pub.addTopicDefnProvider(). The printout can be sent to any file object (object that has a write() method). Example:: from StringIO import StringIO capture = StringIO() printer = TopicTreeAsSpec(fileObj=capture) printer.traverse(someTopic) ''' INDENT_CH = ' ' #INDENT_CH = '.' 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 getOutput(self): return '\n'.join( self.__output ) def traverse(self, topicObj): self.__traverser.traverse(topicObj) def _accept(self, topicObj): # accept every topic return True def _startTraversal(self): # output comment self.__wrapper.initial_indent = '# ' self.__wrapper.subsequent_indent = self.__wrapper.initial_indent self.__output.extend( self.__comment ) # output header: if self.__header: self.__output.extend(['']) self.__output.append(self.__header) self.__output.extend(['']) def _doneTraversal(self): if self.__footer: self.__output.append('') self.__output.append('') self.__output.append(self.__footer) if self.__destination is not None: self.__destination.write(self.getOutput()) def _onTopic(self, topicObj): '''This gets called for each topic. Print as per specified content.''' # don't print root of tree, it is the ALL_TOPICS builtin topic if topicObj.isAll(): self.__lastWasAll = True return self.__lastWasAll = False self.__output.append( '' ) # empty line # topic name self.__wrapper.width = self.__width head = 'class %s:' % topicObj.getTailName() self.__formatItem(head) # each extra content (assume constructor verified that chars are valid) self.__printTopicDescription(topicObj) if policies.msgDataProtocol != 'arg1': self.__printTopicArgSpec(topicObj) def _startChildren(self): '''Increase the indent''' if not self.__lastWasAll: self.__indent += self.__indentStep def _endChildren(self): '''Decrease the indent''' if not self.__lastWasAll: self.__indent -= self.__indentStep def __printTopicDescription(self, topicObj): if topicObj.getDescription(): extraIndent = self.__indentStep self.__formatItem("'''", extraIndent) self.__formatItem( topicObj.getDescription(), extraIndent ) self.__formatItem("'''", extraIndent) def __printTopicArgSpec(self, topicObj): extraIndent = self.__indentStep # generate the listener protocol reqdArgs, optArgs = topicObj.getArgs() argsStr = [] if reqdArgs: argsStr.append( ", ".join(reqdArgs) ) if optArgs: optStr = ', '.join([('%s=None' % arg) for arg in optArgs]) argsStr.append(optStr) argsStr = ', '.join(argsStr) # print it only if there are args; ie if listener() don't print it if argsStr: # output a blank line and protocol self.__formatItem('\n', extraIndent) protoListener = 'def %s(%s):' % (SPEC_METHOD_NAME, argsStr) self.__formatItem(protoListener, extraIndent) # and finally, the args docs extraIndent += self.__indentStep self.__formatItem("'''", extraIndent) # but ignore the arg keys that are in parent args docs: parentMsgKeys = () if topicObj.getParent() is not None: parentMsgKeys = topicObj.getParent().getArgDescriptions().keys() argsDocs = topicObj.getArgDescriptions() for key, argDesc in argsDocs.iteritems(): if key not in parentMsgKeys: msg = "- %s: %s" % (key, argDesc) self.__formatItem(msg, extraIndent) self.__formatItem("'''", extraIndent) def __formatItem(self, item, extraIndent=0): indent = extraIndent + self.__indent indentStr = self.INDENT_CH * indent lines = item.splitlines() for line in lines: self.__output.append( '%s%s' % (indentStr, line) ) def __formatBlock(self, text, extraIndent=0): self.__wrapper.initial_indent = self.INDENT_CH * (self.__indent + extraIndent) self.__wrapper.subsequent_indent = self.__wrapper.initial_indent self.__output.append( self.__wrapper.fill(text) )
class TopicTreeSpecPrinter: """ Function class to print the topic tree using the Python class syntax. If printed to a module, it can then be imported, given to pub.addTopicDefnProvider(), etc. The printout can be sent to any file object (object that has a write() method). """ INDENT_CH = " " # INDENT_CH = '.' 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) def getOutput(self): """Each line that was sent to fileObj was saved in a list; returns a string which is '\n'.join(list).""" return "\n".join(self.__output) def writeAll(self, topicObj): """Traverse each topic of topic tree, starting at topicObj, printing each topic definition as the tree gets traversed. """ self.__traverser.traverse(topicObj) def _accept(self, topicObj): # accept every topic return True def _startTraversal(self): # output comment self.__wrapper.initial_indent = "# " self.__wrapper.subsequent_indent = self.__wrapper.initial_indent self.__output.extend(self.__comment) # output header: if self.__header: self.__output.extend([""]) self.__output.append(self.__header) self.__output.extend([""]) def _doneTraversal(self): if self.__footer: self.__output.append("") self.__output.append("") self.__output.append(self.__footer) if self.__destination is not None: self.__destination.write(self.getOutput()) def _onTopic(self, topicObj): """This gets called for each topic. Print as per specified content.""" # don't print root of tree, it is the ALL_TOPICS builtin topic if topicObj.isAll(): self.__lastWasAll = True return self.__lastWasAll = False self.__output.append("") # empty line # topic name self.__wrapper.width = self.__width head = "class %s:" % topicObj.getNodeName() self.__formatItem(head) # each extra content (assume constructor verified that chars are valid) self.__printTopicDescription(topicObj) if policies.msgDataProtocol != "arg1": self.__printTopicArgSpec(topicObj) def _startChildren(self): """Increase the indent""" if not self.__lastWasAll: self.__indent += self.__indentStep def _endChildren(self): """Decrease the indent""" if not self.__lastWasAll: self.__indent -= self.__indentStep def __printTopicDescription(self, topicObj): if topicObj.getDescription(): extraIndent = self.__indentStep self.__formatItem("'''", extraIndent) self.__formatItem(topicObj.getDescription(), extraIndent) self.__formatItem("'''", extraIndent) def __printTopicArgSpec(self, topicObj): extraIndent = self.__indentStep # generate the listener protocol reqdArgs, optArgs = topicObj.getArgs() argsStr = [] if reqdArgs: argsStr.append(", ".join(reqdArgs)) if optArgs: optStr = ", ".join([("%s=None" % arg) for arg in optArgs]) argsStr.append(optStr) argsStr = ", ".join(argsStr) # print it only if there are args; ie if listener() don't print it if argsStr: # output a blank line and protocol self.__formatItem("\n", extraIndent) protoListener = "def %s(%s):" % (SPEC_METHOD_NAME, argsStr) self.__formatItem(protoListener, extraIndent) # and finally, the args docs extraIndent += self.__indentStep self.__formatItem("'''", extraIndent) # but ignore the arg keys that are in parent args docs: parentMsgKeys = () if topicObj.getParent() is not None: parentMsgKeys = topicObj.getParent().getArgDescriptions().keys() argsDocs = topicObj.getArgDescriptions() for key, argDesc in argsDocs.iteritems(): if key not in parentMsgKeys: msg = "- %s: %s" % (key, argDesc) self.__formatItem(msg, extraIndent) self.__formatItem("'''", extraIndent) def __formatItem(self, item, extraIndent=0): indent = extraIndent + self.__indent indentStr = self.INDENT_CH * indent lines = item.splitlines() for line in lines: self.__output.append("%s%s" % (indentStr, line)) def __formatBlock(self, text, extraIndent=0): self.__wrapper.initial_indent = self.INDENT_CH * (self.__indent + extraIndent) self.__wrapper.subsequent_indent = self.__wrapper.initial_indent self.__output.append(self.__wrapper.fill(text))