Skip to content

Instantly share code, notes, and snippets.

@trn1ty
Last active January 27, 2021 18:02
Show Gist options
  • Save trn1ty/b4915adab899a6a2055a204f71a1078b to your computer and use it in GitHub Desktop.
Save trn1ty/b4915adab899a6a2055a204f71a1078b to your computer and use it in GitHub Desktop.
img2pdf wrapper
#!/usr/bin/python3
import glob
import os
# img2pdf's all files with extension [extension] in [directory], with the output name [output].
# configuration =====
directory = "./"
extension = ".png"
output = "output.pdf"
# ===================
# stolen from github.com/devenblake/ytfeed.py
def query(default, *args, **kwargs):
message = args[0] + " " if len(args) > 0 else ""
yAnswers = kwargs["yAnswers"] if "yAnswers" in list(kwargs) else [
"Y", "YES", "1"
]
nAnswers = kwargs["nAnswers"] if "nAnswers" in list(kwargs) else [
"N", "NO", "0"
]
answer = input(
message + "(" + ("Y" if default else "y") + "/"
+ ("N" if not(default) else "n") + "): "
).upper()
return (
(0 if default else 1) if (
(default == 0 and answer in yAnswers)
or (default == 1 and answer in nAnswers)
) else default
)
# also stolen from ytfeed
def unparsecommand(parsed_command):
settings = {
"casesensitive": True,
"escape": "\\",
"quotes": ["'", '"'],
"space": [" "]
}
escape, quotes, space = \
settings["escape"], settings["quotes"], settings["space"]
command = ""
for i in range(len(parsed_command)): # i = word
for j in range(len(parsed_command[i])): # j = char
if parsed_command[i][j] in [escape]+quotes+space:
command += escape
command += parsed_command[i][j]
if i != len(parsed_command)-1: command += space[0]
return command
files = []
for file in glob.glob(directory + "*" + extension): files.append(file)
if os.system("which img2pdf"):
print('''\
This utility needs img2pdf present. See https://pypi.org/project/img2pdf/.
''')
command = "img2pdf " + unparsecommand(files) + " -o " + output
print(command)
if query(0,"Run?"): os.system(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment