Create a new paste using the bpaste.net service. :contents: Paste contents string. :returns: URL to the pasted contents, or an error message.
(contents: Union[str, bytes])
| 67 | |
| 68 | |
| 69 | def create_new_paste(contents: Union[str, bytes]) -> str: |
| 70 | """Create a new paste using the bpaste.net service. |
| 71 | |
| 72 | :contents: Paste contents string. |
| 73 | :returns: URL to the pasted contents, or an error message. |
| 74 | """ |
| 75 | import re |
| 76 | from urllib.request import urlopen |
| 77 | from urllib.parse import urlencode |
| 78 | |
| 79 | params = {"code": contents, "lexer": "text", "expiry": "1week"} |
| 80 | url = "https://bpa.st" |
| 81 | try: |
| 82 | response: str = ( |
| 83 | urlopen(url, data=urlencode(params).encode("ascii")).read().decode("utf-8") |
| 84 | ) |
| 85 | except OSError as exc_info: # urllib errors |
| 86 | return "bad response: %s" % exc_info |
| 87 | m = re.search(r'href="/raw/(\w+)"', response) |
| 88 | if m: |
| 89 | return f"{url}/show/{m.group(1)}" |
| 90 | else: |
| 91 | return "bad response: invalid format ('" + response + "')" |
| 92 | |
| 93 | |
| 94 | def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None: |
no test coverage detected