-
-
Save Arxcis/ce95a4aff4ff3decc93bad538809d394 to your computer and use it in GitHub Desktop.
Proposal of how to refactor `the_real_goto.py`
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 python | |
# code: utf-8 | |
'Goto - the magic project that takes you where you need to be, now.' | |
import sys | |
from gotomagic.handlers import * | |
from gotomagic.magic import GotoMagic | |
import gotomagic.text as text | |
from gotomagic.text import print_text | |
from gotomagic.githubmagic import GitHub | |
def main(): | |
"""Application entry-point. | |
Every command launches a miniprogram.""" | |
if len(sys.argv) == 2: | |
print(usage()) | |
return 0 | |
jfile = sys.argv[1] | |
command = sys.argv[2] | |
magic = GotoMagic(jfile) | |
if command in ['help', '-h', '/?', '--help']: | |
return help() | |
if command == 'add': | |
return add(magic) | |
if command == 'update': | |
return update(magic) | |
if command == 'rm': | |
return remove(magic) | |
if command == 'show': | |
return show(magic) | |
if command == 'copy': | |
return copy(magic) | |
if command == 'list': | |
return _list(magic) | |
if command == 'subl': | |
return subl(magic) | |
if command == '--github-list': | |
return github_list(magic) | |
if command == 'github': | |
return github(magic) | |
if command == 'cd': | |
return cd(magic) | |
if '-o' in sys.argv or '--open' in sys.argv or 'open' in sys.argv: | |
return open(magic) | |
# default | |
open_link(magic[command]) | |
return 0 | |
def usage(): | |
return """ | |
Goto - the magic traveler, how may I help you? | |
Wondering how to change project? | |
- consult my brother in command by typing: | |
project help | |
The basics | |
- Adding a shortcut: goto add <magicword> <url or path> | |
- Updating a shortcut: goto update <magicword> <url or path> | |
- Removing a shortcut: goto rm <magicword> | |
- Show url of shortcut: goto show <magicword> | |
- Listing all shortcuts: goto list | |
- With the urls printed: goto list -v | |
Working with folders and files: | |
- cd in terminal: goto cd <magicword> | |
- open folder in Finder: goto open <magicword> | |
goto -o <magicword> | |
code - A specially magic magic word: | |
If you add a shortcut | |
to a folder,and name the | |
shortcut "code"; goto add code <path to folder with code> | |
- you may then do this: goto subl | |
and it opens | |
Sublime Text in your folder. | |
""" | |
def help(): | |
print(usage()) | |
return 0 | |
def add(magic): | |
try: | |
magic.add_shortcut(sys.argv[3], sys.argv[4]) | |
magic.save() | |
print('Added magic word %s' % sys.argv[3]) | |
return 0 | |
except IndexError: | |
if len(sys.argv) > 3: | |
print_text( | |
text.warning["missing_uri"], | |
magicword=sys.argv[3] | |
) | |
else: | |
print_text( | |
text.warning["missing_magicword_and_uri"] | |
) | |
return 1 | |
def update(magic): | |
magic.update_shortcut(sys.argv[3], sys.argv[4]) | |
magic.save() | |
print('Updated magic word %s' % sys.argv[3]) | |
return 0 | |
def remove(magic): | |
try: | |
magic.remove_shortcut(sys.argv[3]) | |
magic.save() | |
print('Removed magic word %s' % sys.argv[3]) | |
return 0 | |
except Exception: | |
print('Failed to remove magic word %s' % sys.argv[3]) | |
return 1 | |
def show(magic): | |
magic.show_shortcut(sys.argv[3]) | |
return 0 | |
def copy(magic): | |
copy_to_clipboard(str(magic.get_uri(sys.argv[3]))) | |
return 0 | |
def _list(magic): | |
magic.list_shortcuts(verbose=('-v' in sys.argv)) | |
return 0 | |
def subl(magic): | |
try: | |
open_sublime(magic['code']) | |
except KeyError: | |
print(text.warning["no_magicword_named_code"]) | |
return 0 | |
def github_list(magic): | |
""" Generating dynamic auto completion list | |
used by start_goto""" | |
try: | |
api = GitHub(magic['github']) | |
query = "" | |
if len(sys.argv) == 4: | |
query = sys.argv[3] | |
keys = api.search_for_urls(query, return_keys=True) | |
print("\n".join(keys)) | |
return 0 | |
except KeyError: | |
return 1 | |
def open(magic): | |
open_folder(magic.get_uri(sys.argv[3])) | |
return 0 | |
def github(magic): | |
""" Going to sub-url in github """ | |
api = GitHub(magic.get_uri('github')) | |
if len(sys.argv) == 4: | |
url = api.url_for(endpoint=sys.argv[3]) | |
open_link(url) | |
else: | |
open_link(magic.get_uri('github')) | |
return 0 | |
def cd(magic): | |
open_terminal(magic.get_uri(sys.argv[3])) | |
return 0 | |
if __name__ == "__main__": | |
exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
VERY good refactoring :) 👍 - also we could just put the command handlers in a separate file or folder.
So the file is not too long.
Lets test it somehow - I think I will start creating unit tests.