Skip to content

Instantly share code, notes, and snippets.

@mfekadu
Last active February 26, 2020 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfekadu/8b35a73776e0297e317fdca6382bd898 to your computer and use it in GitHub Desktop.
Save mfekadu/8b35a73776e0297e317fdca6382bd898 to your computer and use it in GitHub Desktop.
All the useful invokes that I like https://www.pyinvoke.org
from invoke import task
import os
import webbrowser
try:
from StringIO import StringIO ## for Python 2
except ImportError:
from io import StringIO ## for Python 3
"""
HELPERS
"""
def go_to_website(URL, verbose=True):
"""
given a URL, opens the browser
"""
print("Opening...", URL) if verbose else None
webbrowser.open(URL)
"""
TASKS
"""
@task(aliases=("list", "lsit", "ist", "-list", "lis", "li", "slit", "slist"))
def _dash_dash_list(c):
"""
because i forget --list often and fixz my ttypos
"""
c.run("invoke --list")
@task(aliases=("gh", "repo", "remote", "origin"))
def github(c, username="INSERT_USERNAME", repo="INSERT_REPOSITORY_NAME"):
"""
opens the GitHub website for this project in default browser
"""
# optionally just hard code this
# TODO: look into how to read the .git/ folder to redirect based on that.
SITE = f"https://github.com/{username}/{repo}"
go_to_website(SITE)
@task(aliases=("gsit", "gst", "sgit", "gis", "gsi", "giat", "gisr", "gsot", "gost"))
def gist(c, edit=False, username="mfekadu", gist_hash="8b35a73776e0297e317fdca6382bd898"):
"""
opens the gist.GitHub.com website for this task.py source code
"""
SITE = f"https://gist.github.com/{username}/{gist_hash}"
SITE = f"{SITE}/edit" if edit else SITE
go_to_website(SITE)
@task(aliases=("ghd", "desktop"))
def github_desktop(c):
"""
opens the GitHub Desktop app <yes i am *that* lazy>. macOS only.
"""
c.run("open -a 'GitHub Desktop'")
@task(aliases=("invoke", "wtf", "huh", "what", "umm", "uhh", "idk"))
def go_to_invoke_docs(c):
"""
opens the docs for the PyInvoke project in default browser
"""
SITE = "https://www.pyinvoke.org"
go_to_website(SITE)
@task(help={"name": "Name of the person to say hi to."})
def hi(c, name, help=False):
"""
Say hi to someone.
"""
print("Hi {}!".format(name))
@task(aliases=("format", "black", "lint"))
def black_auto_format(c, verbose=True):
"""
Make the code look nice.
"""
print("Formatting!")
cwd = os.getcwd()
# move up to the directory that contains ".git"
# which often is the root of a repository
print("current directory: ", cwd)
while cwd != "/" and ".git" not in os.listdir(cwd):
if ".git" not in os.listdir(cwd):
os.chdir("..")
cwd = os.getcwd()
print("current directory: ", cwd)
else:
break
cmd = "black ."
print("running command: {}".format(cmd))
c.run("black .")
@task(aliases=("sc", "scala", "hi-scala", "hiscala", "helloscala"))
def hello_scala(c, verbose=True, name="hello_scala"):
"""
create a hello_world scala file
"""
filename = f"{name}.sc"
print(f"Creating {filename}")
file_content = """import scala.io._
object HelloApp {
def main(args: Array[String]): Unit = {
val coder = "Python"
val num = 21
println(s"Hello Scala from ${coder}!");
println(s"${num + num} is a cool num");
}
}
"""
with open(filename, "w") as f:
f.write(file_content)
cmd = f"cat {filename}"
print(f"$ {cmd}")
c.run(cmd)
cmd = f"scala {filename}"
print(f"$ {cmd}")
c.run(cmd)
@task(aliases=("copy", "pbcopy"))
def copy_tasks_py_to_clipboard(c):
"""
"""
cmd = "cat tasks.py | pbcopy"
print(f"$ {cmd}")
c.run(cmd)
@task(aliases=("ssh",))
def copy_ssh(c):
"""
"""
# https://askubuntu.com/a/811236
cmd = "ls -p ~/.ssh/ | grep -v /"
print(f"$ {cmd}")
c.run(cmd)
choice = input("\n\nWhich one? (enter the name): ")
print("\n\n")
cmd = f"cat ~/.ssh/{choice} | pbcopy"
print(f"$ {cmd}")
c.run(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment