File: main.py

package info (click to toggle)
python-shellescape 3.4.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 92 kB
  • sloc: python: 64; makefile: 4
file content (21 lines) | stat: -rw-r--r-- 445 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re


_find_unsafe = re.compile(r'[a-zA-Z0-9_^@%+=:,./-]').search


def quote(s):
    """Return a shell-escaped version of the string *s*."""
    if not s:
        return "''"

    if _find_unsafe(s) is None:
        return s

    # use single quotes, and put single quotes into double quotes
    # the string $'b is then quoted as '$'"'"'b'

    return "'" + s.replace("'", "'\"'\"'") + "'"