main.py 445 B

123456789101112131415161718192021
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import re
  4. _find_unsafe = re.compile(r'[a-zA-Z0-9_^@%+=:,./-]').search
  5. def quote(s):
  6. """Return a shell-escaped version of the string *s*."""
  7. if not s:
  8. return "''"
  9. if _find_unsafe(s) is None:
  10. return s
  11. # use single quotes, and put single quotes into double quotes
  12. # the string $'b is then quoted as '$'"'"'b'
  13. return "'" + s.replace("'", "'\"'\"'") + "'"