Browse Source

add notext option, fix paste without text handling

Now text is taken if no `--notext (-q)` argument is passed.

If that flag used, paste will be created with empty text,
but file for send must be specified

If `notext` arg is not used, text will be taken in next order:
* from `--text (-t)` argument
* from stdin (forcibly)

Here's no ability create paste without any text.
pull/16/head
R4SAS 6 years ago
parent
commit
487def2b45
  1. 37
      pbincli/actions.py
  2. 1
      pbincli/cli.py

37
pbincli/actions.py

@ -22,15 +22,16 @@ def compress(s): @@ -22,15 +22,16 @@ def compress(s):
return b64encode(''.join(map(chr, b)).encode('utf-8'))
def send(args, api_client):
if args.stdin:
text = args.stdin.read()
elif args.text:
text = args.text
elif args.file:
text = "Sending a file to you!"
else:
if not args.notext:
if args.text:
text = args.text
elif args.stdin:
text = args.stdin.read()
elif not args.file:
print("Nothing to send!")
sys.exit(1)
else:
text = ""
# Formatting request
request = {'expire':args.expire,'formatter':args.format,'burnafterreading':int(args.burn),'opendiscussion':int(args.discus)}
@ -61,8 +62,12 @@ def send(args, api_client): @@ -61,8 +62,12 @@ def send(args, api_client):
with open(args.file, "rb") as f:
contents = f.read()
f.close()
mime = guess_type(args.file)
if args.debug: print("Filename:\t{}\nMIME-type:\t{}".format(path_leaf(args.file), mime[0]))
mime = guess_type(args.file, strict=False)[0]
# MIME fallback
if not mime: mime = "application/octet-stream"
if args.debug: print("Filename:\t{}\nMIME-type:\t{}".format(path_leaf(args.file), mime))
file = "data:" + mime[0] + ";base64," + b64encode(contents).decode()
filename = path_leaf(args.file)
@ -135,12 +140,16 @@ def get(args, api_client): @@ -135,12 +140,16 @@ def get(args, api_client):
if args.debug: print("Text:\t{}\n".format(data))
text = SJCL().decrypt(data, password)
print("{}\n".format(decompress(text.decode())))
check_writable("paste.txt")
with open("paste.txt", "wb") as f:
f.write(decompress(text.decode()))
f.close
if args.debug: print("Decoded text size: {}\n".format(len(text)))
if len(text):
print("{}\n".format(decompress(text.decode())))
check_writable("paste.txt")
with open("paste.txt", "wb") as f:
f.write(decompress(text.decode()))
f.close
if 'attachment' in result and 'attachmentname' in result:
print("Found file, attached to paste. Decoding it and saving")

1
pbincli/cli.py

@ -34,6 +34,7 @@ def main(): @@ -34,6 +34,7 @@ def main():
send_parser.add_argument("-F", "--format", default="plaintext", action="store",
choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)")
send_parser.add_argument("-t", "--text", help="comment in quotes. Ignored if used stdin")
send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="don't send text in paste")
send_parser.add_argument("-p", "--password", help="password for encrypting paste")
send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run")

Loading…
Cancel
Save