Use black linting utility on all python scripts

This commit is contained in:
Eric Torres 2019-03-06 23:34:48 -08:00
parent 71264172f2
commit 3ec9989f25
5 changed files with 84 additions and 78 deletions

View File

@ -7,10 +7,7 @@ import subprocess
# ========== Main Script ========== # ========== Main Script ==========
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-b", "--bs", parser.add_argument("-b", "--bs", default=512, help="block size", metavar="bs")
default=512,
help="block size",
metavar="bs")
parser.add_argument("input_file", help="input file to write") parser.add_argument("input_file", help="input file to write")
parser.add_argument("output_file", help="output block device") parser.add_argument("output_file", help="output block device")
args = parser.parse_args() args = parser.parse_args()
@ -28,11 +25,17 @@ print(f"Block device: {block_device}")
print(f"Block size: {block_size}") print(f"Block size: {block_size}")
try: try:
subprocess.run(["dd", f"if={input_file}", subprocess.run(
[
"dd",
f"if={input_file}",
f"of={block_device}", f"of={block_device}",
f"bs={block_size}", f"bs={block_size}",
"status=progress"], check=True) "status=progress",
],
check=True,
)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
exit(1) exit(1)
else: else:
subprocess.run(['sync']) subprocess.run(["sync"])

View File

@ -12,7 +12,7 @@ import shutil
import subprocess import subprocess
# =========== Constants ========== # =========== Constants ==========
YOUTUBE_DL_BIN = shutil.which('youtube-dl') YOUTUBE_DL_BIN = shutil.which("youtube-dl")
DEFAULT_FILENAME = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s" DEFAULT_FILENAME = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s"
# ========== Error Codes ========== # ========== Error Codes ==========
@ -20,25 +20,23 @@ E_NOURLS = 2
# ========== Main Script ========== # ========== Main Script ==========
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-b', '--batchfile', parser.add_argument("-b", "--batchfile", help="provide the links from a text file")
help='provide the links from a text file') parser.add_argument(
parser.add_argument('-f', '--format', "-f", "--format", type=str, default="opus", help="the format to use"
type=str, )
default='opus', parser.add_argument(
help='the format to use') "-n", "--filename", type=str, help="downloaded filename (without extension)"
parser.add_argument('-n', '--filename', )
type=str, parser.add_argument("urls", nargs="*", help="video URLs")
help='downloaded filename (without extension)')
parser.add_argument('urls',
nargs='*',
help='video URLs')
args = parser.parse_args() args = parser.parse_args()
dl_opts = [YOUTUBE_DL_BIN, dl_opts = [
'--no-part', YOUTUBE_DL_BIN,
'--no-continue', "--no-part",
'--extract-audio', "--no-continue",
f"--audio-format={args.format}"] "--extract-audio",
f"--audio-format={args.format}",
]
# filename handling # filename handling
# if -b is used, DEFAULT_FILENAME must take precedence # if -b is used, DEFAULT_FILENAME must take precedence

View File

@ -14,7 +14,7 @@ import pathlib
import subprocess import subprocess
# ========== Constants ========== # ========== Constants ==========
DUMP_CMD = ['skdump', '--temperature'] DUMP_CMD = ["skdump", "--temperature"]
# ========== Functions ========== # ========== Functions ==========
@ -35,9 +35,9 @@ def retrieve_smart_temp(device_node):
:returns: output of skdump in mKelvin :returns: output of skdump in mKelvin
:rtype: float :rtype: float
""" """
temp = subprocess.run(DUMP_CMD + [device_node], temp = subprocess.run(
capture_output=True, DUMP_CMD + [device_node], capture_output=True, text=True
text=True).stdout ).stdout
return float(temp) return float(temp)
@ -53,8 +53,12 @@ def convert_to_celsius(mkel_temp):
# ========== Main Script ========== # ========== Main Script ==========
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('device', help='device node to retrieve\ parser.add_argument(
the temperature for', metavar='dev') "device",
help="device node to retrieve\
the temperature for",
metavar="dev",
)
args = parser.parse_args() args = parser.parse_args()
dev = args.device dev = args.device

View File

@ -15,22 +15,22 @@ import subprocess
# ========== Constants ========== # ========== Constants ==========
# Paths # Paths
BOOT_DIR = '/boot' BOOT_DIR = "/boot"
ETC_DIR = '/etc' ETC_DIR = "/etc"
# Exit Codes # Exit Codes
E_NOEDITORFOUND = 2 E_NOEDITORFOUND = 2
E_NOFILESELECTED = 3 E_NOFILESELECTED = 3
# Commands # Commands
FIND_CMD = '/usr/bin/fd' FIND_CMD = "/usr/bin/fd"
FIND_OPTS = ['--hidden', '--print0', '--type', 'f', '--no-ignore-vcs'] FIND_OPTS = ["--hidden", "--print0", "--type", "f", "--no-ignore-vcs"]
FZF_CMD = '/usr/bin/fzf' FZF_CMD = "/usr/bin/fzf"
FZF_OPTS = ['--read0', '--select-1', '--exit-0', '--print0'] FZF_OPTS = ["--read0", "--select-1", "--exit-0", "--print0"]
LOCATE_CMD = '/usr/bin/locate' LOCATE_CMD = "/usr/bin/locate"
LOCATE_OPTS = ['--all', '--ignore-case', '--null'] LOCATE_OPTS = ["--all", "--ignore-case", "--null"]
LOCALE = 'utf-8' LOCALE = "utf-8"
# ========== Functions ========== # ========== Functions ==========
@ -54,13 +54,13 @@ def select_editor(editor_override=None):
if editor_override is not None: if editor_override is not None:
editor = shutil.which(editor_override) editor = shutil.which(editor_override)
elif 'EDITOR' in os.environ: elif "EDITOR" in os.environ:
editor = shutil.which(os.environ.get('EDITOR')) editor = shutil.which(os.environ.get("EDITOR"))
elif shutil.which('vim') is not None: elif shutil.which("vim") is not None:
editor = shutil.which('vim') editor = shutil.which("vim")
if editor is None: if editor is None:
raise FileNotFoundError('An editor could not be resolved') raise FileNotFoundError("An editor could not be resolved")
return editor return editor
@ -78,7 +78,7 @@ def gen_editor_cmd(filename):
if os.access(filename, os.W_OK): if os.access(filename, os.W_OK):
return [editor, filename] return [editor, filename]
else: else:
return ['sudo', '--edit', filename] return ["sudo", "--edit", filename]
def run_fzf(files): def run_fzf(files):
@ -89,11 +89,11 @@ def run_fzf(files):
:returns: selected file :returns: selected file
:rtype: str :rtype: str
""" """
selected_file = subprocess.run([FZF_CMD] + FZF_OPTS, selected_file = subprocess.run(
input=files, [FZF_CMD] + FZF_OPTS, input=files, stdout=subprocess.PIPE
stdout=subprocess.PIPE).stdout ).stdout
return selected_file.decode(LOCALE).strip('\x00') return selected_file.decode(LOCALE).strip("\x00")
def find_files(directory=None): def find_files(directory=None):
@ -106,7 +106,7 @@ def find_files(directory=None):
""" """
cmd = [FIND_CMD] + FIND_OPTS cmd = [FIND_CMD] + FIND_OPTS
if directory is not None: if directory is not None:
cmd.extend(['--', '.', directory]) cmd.extend(["--", ".", directory])
return subprocess.run(cmd, capture_output=True).stdout return subprocess.run(cmd, capture_output=True).stdout
@ -127,31 +127,32 @@ def locate_files(patterns):
# ========== Main Script ========== # ========== Main Script ==========
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-b', '--boot', parser.add_argument(
action='store_const', "-b",
"--boot",
action="store_const",
const=BOOT_DIR, const=BOOT_DIR,
dest='dir', dest="dir",
help='edit a file in /boot') help="edit a file in /boot",
parser.add_argument('-d', '--dir', )
dest='dir', parser.add_argument(
type=str, "-d", "--dir", dest="dir", type=str, help="edit a file in a given directory"
help='edit a file in a given directory') )
parser.add_argument('-E', '--etc', parser.add_argument(
action='store_const', "-E",
"--etc",
action="store_const",
const=ETC_DIR, const=ETC_DIR,
dest='dir', dest="dir",
help='edit a file in /etc') help="edit a file in /etc",
parser.add_argument('-e', '--editor', )
help='use a given editor') parser.add_argument("-e", "--editor", help="use a given editor")
parser.add_argument('patterns', parser.add_argument("patterns", type=str, nargs="*", help="patterns to pass to locate")
type=str,
nargs='*',
help='patterns to pass to locate')
args = parser.parse_args() args = parser.parse_args()
final_find_cmd = [FIND_CMD] + FIND_OPTS final_find_cmd = [FIND_CMD] + FIND_OPTS
editor = '' editor = ""
try: try:
editor = select_editor(args.editor) editor = select_editor(args.editor)
@ -168,7 +169,7 @@ else:
selected_file = run_fzf(files) selected_file = run_fzf(files)
if not selected_file == '': if not selected_file == "":
cmd = gen_editor_cmd(selected_file) cmd = gen_editor_cmd(selected_file)
subprocess.run(cmd) subprocess.run(cmd)
else: else:

View File

@ -5,11 +5,11 @@ import argparse
import requests import requests
# ========== Constants ========== # ========== Constants ==========
WTTR_URI = 'http://wttr.in' WTTR_URI = "http://wttr.in"
# ========== Main Script ========== # ========== Main Script ==========
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('location') parser.add_argument("location")
args = parser.parse_args() args = parser.parse_args()
location = args.location location = args.location