utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from __future__ import annotations
  2. import re
  3. # stolen from python docs
  4. def trim(docstring):
  5. if not docstring:
  6. return ""
  7. # Convert tabs to spaces (following the normal Python rules)
  8. # and split into a list of lines:
  9. lines = docstring.expandtabs().splitlines()
  10. # Determine minimum indentation (first line doesn't count):
  11. indent = 232323
  12. for line in lines[1:]:
  13. stripped = line.lstrip()
  14. if stripped:
  15. indent = min(indent, len(line) - len(stripped))
  16. # Remove indentation (first line is special):
  17. trimmed = [lines[0].strip()]
  18. if indent < 232323:
  19. for line in lines[1:]:
  20. trimmed.append(line[indent:].rstrip())
  21. # Strip off trailing and leading blank lines:
  22. while trimmed and not trimmed[-1]:
  23. trimmed.pop()
  24. while trimmed and not trimmed[0]:
  25. trimmed.pop(0)
  26. # Return a single string:
  27. return "\n".join(trimmed)
  28. def env_name_format(str):
  29. # KeyCorridorEnv
  30. split = re.findall(r"[A-Z](?:[a-z]+[0-9]*|[A-Z]*[0-9]*(?=[A-Z]|$))", str)
  31. if not split:
  32. split.append(str)
  33. # ['Key', 'Corridor', 'Env']
  34. split = filter(lambda x: x.upper() != "ENV", split)
  35. return " ".join(split)