/Tools/framer/framer/util.py

http://unladen-swallow.googlecode.com/ · Python · 35 lines · 21 code · 4 blank · 10 comment · 7 complexity · b58f42ffe15ed1b4bf81574e8ea02a83 MD5 · raw file

  1. def cstring(s, width=70):
  2. """Return C string representation of a Python string.
  3. width specifies the maximum width of any line of the C string.
  4. """
  5. L = []
  6. for l in s.split("\n"):
  7. if len(l) < width:
  8. L.append(r'"%s\n"' % l)
  9. return "\n".join(L)
  10. def unindent(s, skipfirst=True):
  11. """Return an unindented version of a docstring.
  12. Removes indentation on lines following the first one, using the
  13. leading whitespace of the first indented line that is not blank
  14. to determine the indentation.
  15. """
  16. lines = s.split("\n")
  17. if skipfirst:
  18. first = lines.pop(0)
  19. L = [first]
  20. else:
  21. L = []
  22. indent = None
  23. for l in lines:
  24. ls = l.strip()
  25. if ls:
  26. indent = len(l) - len(ls)
  27. break
  28. L += [l[indent:] for l in lines]
  29. return "\n".join(L)