Commits

Ronny Pfannschmidt  committed 9148226 Merge

Merged in caio/lodgeit-main (pull request #6) - paste from clipboard

  • Participants
  • Parent commits 952b419, 6612561

Comments (0)

Files changed (1)

File scripts/lodgeit.py

         win32clipboard.CloseClipboard()
 
 
+def read_clipboard():
+    try:
+        import win32clipboard
+    except ImportError:
+        from subprocess import Popen, PIPE
+        for prog in ['pbpaste'], ['xclip', '-o']:
+            try:
+                client = Popen(prog, stdout=PIPE)
+            except OSError:
+                continue
+            else:
+                stdout, _ = client.communicate()
+                return stdout
+        # Try GTK now
+        try:
+            import pygtk
+            pygtk.require('2.0')
+            import gtk
+            import gobject
+        except ImportError:
+            fail("Unable to read from clipboard", 6)
+        data = ""
+        def callback(clipboard, text, userdata):
+            global data
+            data = text
+        clipboard = gtk.Clipboard(display=gtk.gdk.display_get_default(),
+                                  selection='CLIPBOARD')
+        clipboard.request_text(callback)
+        gobject.timeout_add(1,gtk.main_quit)
+        gtk.main()
+        return data
+    else:
+        win32clipboard.OpenClipboard()
+        data = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
+        win32clipboard.CloseClipboard()
+        return data
+
+
 def open_webbrowser(url):
     """Open a new browser window."""
     import webbrowser
     return rv
 
 
-def compile_paste(filenames, langopt):
+def compile_paste(filenames, langopt, clipboard=False):
     """Create a single paste out of zero, one or multiple files."""
     def read_file(f):
         try:
     mime = ''
     lang = langopt or ''
     if not filenames:
-        data = read_file(sys.stdin)
+        if clipboard:
+            data = read_clipboard()
+        else:
+            data = read_file(sys.stdin)
         print 'Pasting...'
         if not langopt:
             mime = get_mimetype(data, '') or ''
     parser.add_option('-e', '--encoding', default=settings['encoding'],
                       help='Specify the encoding of a file (default is '
                            'utf-8 or guessing if available)')
+    parser.add_option('-x', '--from-clipboard', action='store_true',
+                      default=False, dest='from_clipboard',
+                      help='Read data to be pasted from the clipboard')
     parser.add_option('-b', '--open-browser', dest='open_browser',
                       action='store_true',
                       default=settings['open_browser'],
 
     # load file(s)
     try:
-        data, language, filename, mimetype = compile_paste(args, opts.language)
+        data, language, filename, mimetype = compile_paste(args,
+                                                           opts.language,
+                                                           opts.from_clipboard)
     except Exception, err:
         fail('Error while reading the file(s): %s' % err, 2)
     if not data: