simplify command line scripts with click

This commit is contained in:
Sam Schott 2019-03-02 13:41:22 +00:00
parent fea5c017ef
commit 5fc81196c3
4 changed files with 97 additions and 85 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
source activate python3
maestral --gui
maestral gui
conda deactivate

View File

@ -6,89 +6,99 @@ Created on Fri Nov 30 13:51:32 2018
@author: samschott
"""
def run():
import sys
# generate sys.argv dictionary
if len(sys.argv) > 1:
parameters = sys.argv[2:]
wtd = sys.argv[1]
else:
wtd = "--sync"
if wtd == "--client":
from maestral.client import MaestralClient
print("""Maestral
(c) Sam Schott, 2018
made with Dropbox SDK from https://www.dropbox.com/developers/reference/sdk \n""")
client = MaestralClient()
if parameters[0] == "get":
client.download(parameters[1], parameters[2])
elif parameters[0] == "put":
client.upload(parameters[1], parameters[2])
elif parameters[0] == "mv":
client.move(parameters[1], parameters[2])
elif parameters[0] == "rm":
client.remove(parameters[1])
elif parameters[0] == "ls":
res = client.list_folder(parameters[1], recursive=False)
print("\t".join(res.keys()))
elif parameters[0] == "mkdir":
client.make_dir(parameters[1])
elif parameters[0] == "account-info":
res = client.get_account_info()
print("%s, %s" % (res.email, res.account_type))
elif wtd == "--help":
print("""
Syntax: maestral [<OPTION>] [<PARAMETERS>]
Starts the Maestral syncing app in the command line.
--help - displays this text
--gui - runs Maestral with status bar based GUI
--sync - runs Maestral as command line tool
--configuration - runs configuration wizard
--unlink - unlinks Maestral from your Dropbox account but keeps
your downloaded files in place
--client - runs Maestral API Client
syntax: maestral --client [parameter1] [parameter2] [parameter3]
get [from_path] [to_path] - downloads file
put [from_path] [to_path] - uploads file
mv [from_path] [to_path] - moves and renames file
rm [path] - removes a file
ls [<path>] - creates a list of files in (root) directory
mkdir [path] - creates a directory
account-info - gets Dropbox account info
""")
elif wtd == "--configuration":
from maestral.main import Maestral
m = Maestral(run=False)
m.set_dropbox_directory()
m.select_excluded_folders()
elif wtd == "--sync":
from maestral.main import Maestral
m = Maestral()
elif wtd == "--gui":
from maestral.gui.main import run
run()
elif wtd == "--unlink":
from maestral.main import Maestral
m = Maestral(run=False)
m.unlink()
else:
print("Invalid syntax. Type maestral --help for more information.")
import click
if __name__ == "__main__":
@click.group()
def main():
"""Maestral Dropbox Client for Linux and macOS."""
pass
@main.command()
def sync():
"""Runs Maestral as a command line tool."""
from maestral.main import Maestral
Maestral()
@main.command()
def gui():
"""Runs Maestral with a status bar based GUI."""
from maestral.gui.main import run
run()
@main.command()
def configure():
"""Runs the command line configuration wizard."""
from maestral.main import Maestral
m = Maestral(run=False)
m.set_dropbox_directory()
m.select_excluded_folders()
@main.command()
def unlink():
"""Unlinks your Dropbox account."""
import os
from maestral.client import OAuth2Session
os.unlink(OAuth2Session.TOKEN_FILE)
@main.command()
@click.argument('dropbox_path')
@click.argument('local_path')
def download(dropbox_path: str, local_path: str):
"""Downloads a file from Dropbox."""
from maestral.client import MaestralClient
client = MaestralClient()
client.download(dropbox_path, local_path)
@main.command()
@click.argument('local_path')
@click.argument('dropbox_path')
def upload(local_path: str, dropbox_path: str):
"""Uploads a file to Dropbox."""
from maestral.client import MaestralClient
client = MaestralClient()
client.upload(local_path, dropbox_path)
@main.command()
@click.argument('old_path')
@click.argument('new_path')
def move(old_path: str, new_path: str):
"""Moves or renames a file or folder on Dropbox."""
from maestral.client import MaestralClient
client = MaestralClient()
client.move(old_path, new_path)
@main.command()
@click.argument('dropbox_path')
def ls(dropbox_path: str):
"""Lists contents of a folder on Dropbox."""
from maestral.client import MaestralClient
client = MaestralClient()
res = client.list_folder(dropbox_path, recursive=False)
print("\t".join(res.keys()))
@main.command()
@click.argument('dropbox_path')
def mkdir(dropbox_path: str):
"""Creates a new directory on Dropbox."""
from maestral.client import MaestralClient
client = MaestralClient()
client.make_dir(dropbox_path)
@main.command()
def account_info():
"""Prints Dropbox account info."""
from maestral.client import MaestralClient
client = MaestralClient()
res = client.get_account_info()
print("%s, %s" % (res.email, res.account_type))

View File

@ -1,3 +1,4 @@
click
dropbox
watchdog
blinker

View File

@ -51,6 +51,7 @@ setup(
],
},
install_requires=[
"click",
"dropbox",
"watchdog",
"blinker",
@ -60,7 +61,7 @@ setup(
],
zip_safe=False,
entry_points={
"console_scripts": ["maestral=maestral.console_script:run"],
"console_scripts": ["maestral=maestral.console_script:main"],
},
scripts=['bin/maestral-gui'],
python_requires='>=3.6',