snippets

More or less useful code snippets
Log | Files | Refs

commit 1ec1c99232a5e3b1ad990d56dbc5f552c53711c5
parent b6de6edf9ccce43bc156e0419497a2c709063929
Author: Vetle Haflan <vetle.haflan@luxsave.com>
Date:   Fri, 29 Nov 2019 11:15:57 +0100

Modify vget.py so that it downloads by default

This way there's no need to alias vget to 'vget.py -g', instead just add
the (now -l) flag to the completion script. The script can be "installed"
by just copying or symlinking the script to /usr/bin/vget or something.

Diffstat:
Mvget.py | 24+++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/vget.py b/vget.py @@ -1,13 +1,17 @@ #!/usr/bin/python3 # This script is meant as a client for 'python3 -m http.server' -# This script either gets a given resource (flag -g) or returns autocomplete -# suggestions based on the input argument the contents detected on the server +# This script either gets a given resource or returns autocomplete suggestions +# (with flag -l) based on the input argument the contents detected on the server # -# To set up bash completion, run the following shell commands: -# alias vget='/path/to/vget.py -g' -# _vget(){ COMPREPLY=($(./vget.py ${COMP_WORDS[1]}));} -# complete -o nospace -C _vget /path/to/vget.py +# Setup: To install and set up bash completion, run: +# ln -s $PWD/vget.py /usr/bin/vget +# and add the following to /usr/share/bash-completion/completions/vget: +# _vget() +# { +# COMPREPLY=($(vget -l ${COMP_WORDS[1]})) +# } +# complete -o nospace -F _vget vget import re import requests @@ -18,17 +22,18 @@ DEFAULT_HOST='me' HOST_URLS={ 'me': 'http://localhost:8000', 'vetle': 'http://localhost:8000', + 'seb': 'http://10.0.0.103:8000' } parser = argparse.ArgumentParser() parser.add_argument('url', type=str, nargs='?') -parser.add_argument("-g", dest='get', action='store_true', +parser.add_argument("-l", dest='list_only', action='store_true', help="Actually download the resource, don't get list") -parser.set_defaults(get_it=False) +parser.set_defaults(list_only=False) args = parser.parse_args() url = args.url -download = args.get +download = not args.list_only # TODO: Allow no arguments, and return the entire list of aliases if so @@ -57,6 +62,7 @@ if download: r = requests.get(url) filename = url.split('/')[-1] open(filename, 'wb').write(r.content) + print('--> ', filename) exit() # Otherwise download the list for autocompletion