-
-
Save zed/8aad60cf66392f13e3e27c9ffd2c67a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Find docs link — Pythonista for iOS app extension. | |
1. take stdlib name from appex(share)/clipboard/command-line | |
2. copy the link to its doc into clipboard. | |
""" | |
import sys | |
import webbrowser | |
try: | |
import appex | |
import clipboard | |
import console | |
clipboard_get = clipboard.get | |
clipboard_set = clipboard.set | |
console_hud_alert = console.hud_alert | |
except ImportError: # not on Pythonista for iOS | |
appex = None | |
import pyperclip # pip install pyperclip | |
clipboard_get = pyperclip.paste | |
clipboard_set = pyperclip.copy | |
console_hud_alert = print | |
def find_docs_url(fqname): | |
if fqname in vars(__builtins__): # builtin function | |
package = "functions" | |
else: | |
package = fqname.rpartition('.')[0] or fqname | |
return 'https://docs.python.org/3/library/{package}.html#{fqname}'.format_map(vars()) | |
def main(): | |
if not (appex and appex.is_running_extension()): | |
if len(sys.argv) == 2: | |
name = sys.argv[1] | |
else: | |
name = clipboard_get() | |
else: | |
name = appex.get_text() | |
if name: | |
print('Input name:', name) | |
url = find_docs_url(name) | |
clipboard_set(url) | |
message = "Copied to clipboard: " + url | |
print(message) | |
console_hud_alert(message, 'success') | |
webbrowser.open(url) | |
else: | |
console_hud_alert('No input name found.') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment