Created
March 18, 2017 06:43
-
-
Save kaeza/e822e6f95693c3703ffeb7da013375e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# :coding=utf-8: | |
from __future__ import unicode_literals, print_function | |
import os | |
from Tkinter import * | |
from Tix import * | |
from tkMessageBox import showerror, showinfo | |
from tkFileDialog import askopenfilename | |
from subprocess import Popen, PIPE | |
from webbrowser import open_new_tab | |
def paste(data, filename=None, description=None): | |
args = [ "gist" ] | |
if filename: args.extend(("-f", filename)) | |
if description: args.extend(("-d", description)) | |
p = Popen(args, stdin=PIPE, stdout=PIPE) | |
p.stdin.write(data.encode("utf-8")) | |
r = p.communicate()[0] | |
return r | |
class MainFrame(Frame): | |
def __init__(self, master, *args, **kw): | |
Frame.__init__(self, master, *args, **kw) | |
f = Frame(self) | |
l = Label(f, text="Description") | |
l.pack(side=LEFT, fill=BOTH) | |
self.desc_var = v = StringVar() | |
e = Entry(f, textvariable=v) | |
e.pack(side=LEFT, fill=BOTH, expand=True) | |
l = Label(f, text="Filename") | |
l.pack(side=LEFT, fill=BOTH) | |
self.name_var = v = StringVar() | |
e = Entry(f, textvariable=v) | |
e.pack(side=LEFT, fill=BOTH) | |
b = Button(f, text="...", command=self._browse) | |
b.pack(side=LEFT, fill=BOTH) | |
f.pack(side=TOP, fill=BOTH) | |
f = Frame(self) | |
b = Button(f, text="Paste!", command=self._paste) | |
b.pack(side=LEFT, fill=BOTH) | |
l = Label(f, text="URL") | |
l.pack(side=LEFT, fill=BOTH) | |
self.url_var = v = StringVar() | |
e = Entry(f, textvariable=v, state="readonly") | |
e.pack(side=LEFT, fill=BOTH, expand=True) | |
b = Button(f, text="Copy", command=self._copy) | |
b.pack(side=LEFT, fill=BOTH) | |
b = Button(f, text="Open", command=self._open) | |
b.pack(side=LEFT, fill=BOTH) | |
f.pack(side=TOP, fill=BOTH) | |
t = ScrolledText(self) | |
t.pack(side=TOP, fill=BOTH, expand=True) | |
self.text = t.text | |
def _paste(self): | |
data = self.text.get("0.0", "end") | |
if (not data) or data == "\n": | |
showerror("Error", "Nothing to paste!") | |
return | |
desc = self.desc_var.get() | |
name = self.name_var.get() | |
try: | |
self.url_var.set(paste(data, name, desc).strip()) | |
self._copy() | |
except OSError as e: | |
showerror("Error", "Error pasting: "+str(e)) | |
def _copy(self): | |
url = self.url_var.get() | |
if not url: return | |
self.clipboard_clear() | |
self.clipboard_append(url) | |
showinfo("Information", "URL copied to clipboard.") | |
def _open(self): | |
url = self.url_var.get() | |
if not url: return | |
open_new_tab(url) | |
def _browse(self): | |
name = askopenfilename(initialfile=self.name_var.get()) | |
if name: | |
try: | |
f = open(name, "rb") | |
self.text.delete("0.0", "end") | |
while True: | |
block = f.read(1024*1024) | |
if not block: break | |
self.text.insert("end", block) | |
self.name_var.set(os.path.basename(name)) | |
except IOError as e: | |
showerror("Error", "Error reading file: "+str(e)) | |
if __name__ == "__main__": | |
root = Tk() | |
root.title("Gist GUI") | |
mf = MainFrame(root) | |
mf.pack(side=TOP, fill=BOTH, expand=True) | |
root.mainloop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can we update this, 08.12.2016 is more than two years,
and now for the checking and updating for 5.0.0 would be nice to see which should be fixed more early as often used.