[Zope3-checkins] SVN: Zope3/branches/tlotze/src/zope/interface/document.py simplifications, coding style, only equivalent changes

Thomas Lotze tl at gocept.com
Mon Aug 22 18:09:57 EDT 2005


Log message for revision 38029:
  simplifications, coding style, only equivalent changes

Changed:
  U   Zope3/branches/tlotze/src/zope/interface/document.py

-=-
Modified: Zope3/branches/tlotze/src/zope/interface/document.py
===================================================================
--- Zope3/branches/tlotze/src/zope/interface/document.py	2005-08-22 21:56:16 UTC (rev 38028)
+++ Zope3/branches/tlotze/src/zope/interface/document.py	2005-08-22 22:09:56 UTC (rev 38029)
@@ -18,104 +18,90 @@
 
 $Id$
 """
-from string import maketrans
 import zope.interface
 
 def asStructuredText(I, munge=0):
-    """ Output structured text format.  Note, this will wack any existing
+    """ Output structured text format.  Note, this will whack any existing
     'structured' format of the text.  """
 
-
-    r = ["%s\n\n" % I.getName()]
+    r = [I.getName()]
     outp = r.append
     level = 1
 
     if I.getDoc():
-        outp(_justify_and_indent(_trim_doc_string(I.getDoc()), level)+ "\n\n")
+        outp(_justify_and_indent(_trim_doc_string(I.getDoc()), level))
 
     bases = [base
              for base in I.__bases__
              if base is not zope.interface.Interface
              ]
     if bases:
-        outp((" " * level) + "This interface extends:\n\n")
-        level = level + 1
+        outp(_justify_and_indent("This interface extends:", level, munge))
+        level += 1
         for b in bases:
             item = "o %s" % b.getName()
-            outp(_justify_and_indent(_trim_doc_string(item), level, munge)
-                 + "\n\n")
+            outp(_justify_and_indent(_trim_doc_string(item), level, munge))
+        level -= 1
 
-        level = level - 1
-
-    outp(_justify_and_indent("Attributes:", level, munge)+'\n\n')
-    level = level + 1
-
     namesAndDescriptions = I.namesAndDescriptions()
     namesAndDescriptions.sort()
 
+    outp(_justify_and_indent("Attributes:", level, munge))
+    level += 1
     for name, desc in namesAndDescriptions:
         if not hasattr(desc, 'getSignatureString'):   # ugh...
             item = "%s -- %s" % (desc.getName(),
                                  desc.getDoc() or 'no documentation')
-            outp(_justify_and_indent(_trim_doc_string(item), level, munge)
-                 + "\n\n")
-    level = level - 1
+            outp(_justify_and_indent(_trim_doc_string(item), level, munge))
+    level -= 1
 
-    outp(_justify_and_indent("Methods:", level, munge)+'\n\n')
-    level = level + 1
+    outp(_justify_and_indent("Methods:", level, munge))
+    level += 1
     for name, desc in namesAndDescriptions:
         if hasattr(desc, 'getSignatureString'):   # ugh...
             item = "%s%s -- %s" % (desc.getName(),
                                    desc.getSignatureString(),
                                    desc.getDoc() or 'no documentation')
-            outp(_justify_and_indent(_trim_doc_string(item), level, munge)
-                 + "\n\n")
+            outp(_justify_and_indent(_trim_doc_string(item), level, munge))
 
-    return "".join(r)
+    return "\n\n".join(r) + "\n\n"
 
+
 def _trim_doc_string(text):
-    """
-    Trims a doc string to make it format
-    correctly with structured text.
-    """
-    text = text.strip().replace('\r\n', '\n')
-    lines = text.split('\n')
-    nlines = [lines[0]]
-    if len(lines) > 1:
-        min_indent=None
-        for line in lines[1:]:
-            indent=len(line) - len(line.lstrip())
-            if indent < min_indent or min_indent is None:
-                min_indent=indent
-        for line in lines[1:]:
+    """ Trims a doc string to make it format
+    correctly with structured text. """
+
+    lines = text.replace('\r\n', '\n').split('\n')
+    nlines = [lines.pop(0)]
+    if lines:
+        min_indent = min([len(line) - len(line.lstrip())
+                          for line in lines])
+        for line in lines:
             nlines.append(line[min_indent:])
+
     return '\n'.join(nlines)
 
 
-_trans = maketrans("\r\n", "  ")
 def _justify_and_indent(text, level, munge=0, width=72):
     """ indent and justify text, rejustify (munge) if specified """
 
-    lines = []
+    indent = " " * level
 
     if munge:
-        line = " " * level
-        text = text.translate(text, _trans).strip().split()
+        lines = []
+        line = indent
+        text = text.split()
 
         for word in text:
             line = ' '.join([line, word])
             if len(line) > width:
                 lines.append(line)
-                line = " " * level
+                line = indent
         else:
             lines.append(line)
 
-        return "\n".join(lines)
+        return '\n'.join(lines)
 
     else:
-        text = text.replace("\r\n", "\n").split("\n")
-
-        for line in text:
-            lines.append((" " * level) + line)
-
-        return '\n'.join(lines)
+        return indent + \
+            text.strip().replace("\r\n", "\n") .replace("\n", "\n" + indent)



More information about the Zope3-Checkins mailing list