Commits

Benoit Chesneau  committed 80cfcec

fix some bugs.

  • Participants
  • Parent commits e5d391b

Comments (0)

Files changed (1)

File contrib/friendpastepy/friendpastepy.py

         }
         return self._make_request("PUT", "/%s" % pasteid, content)
         
+    def update_from_url(self, url, title, snippet, lang=None):
+        """Update a paste from url
+        
+        :param url: full url of paste
+        :param title: new paste title
+        :param snippet: new paste content
+        :param lang: optionnal, the lang of the paste
+        :return: response dict
+        """
+        pasteid = self.get_id_fro_url(url)
+        return self.update(pasteid, title, snippet, lang)
+        
     def changeset(self, pasteid, rev):
         """Get a changeset
         
         clipboard.CloseClipboard()
     except: # we arent on windows
         if sys.platform == 'darwin': # osx platform
-            os.system("echo %s | pbcopy" % (data))
+            data = re.escape(data)
+            os.system(r"echo %s | pbcopy" % data)
+            print "paste in clip"
+            return 0
         else:  # posix platform
             if os.environ.get('DISPLAY'):
                 try:
                         data = os.popen("echo %s | xclip -i" % (data))
                     except:
                         pass
+                    return 0
                                    
     raise ClipboardNotAvailable     
 
     webbrowser.open(url)
     
     
-def save_last_snippet_url(url):
+def save_paste_url(url):
     fd = file(IDFILE_PATH, 'w')
     fd.write(url)
     fd.close
 
-def read_last_snippet_url():
+def read_last_paste_url():
     fd = file(IDFILE_PATH, 'r')
     url = fd.read()
     fd.close
     return url
+    
+
+def utf8(text):
+    if isinstance(text, unicode):
+        return text.encode('utf-8')
+    elif isinstance(text, str):
+        return text
+    else:
+        return str(text)
 
 if __name__ == "__main__":
     # parse command line
     parser.add_option('-l', '--language', action='store',
             dest="language",
             default=DEFAULT_LANG, help='language used for syntax coloration')
-    parser.add_option('-f', '--fetch', action='store',
+    parser.add_option('-f', '--fetch', action='store_true',
             dest="fetch",
-            default=False, help='fetch snippet with url passed in stdin/clipboard')
+            default='', help='fetch snippet with url passed in stdin/clipboard')
     parser.add_option('-u', '--update', action='store',
             dest="update",
-            default=None, help='update the snippet with this id')
+            default=False, help='update the snippet with this id')
     parser.add_option('--update-last', action='store_true',
             dest="update_last",
             default=False, help='update your last snippet')
                           action='store_false',
                           default=False,
                           help="Don't copy the url into the clipboard")
-    
-    parser.add_option('-o', '--output-paste', dest='output_paste',
-                          action='store_true',
-                          default=None,
-                          help='Open the paste in a web browser')     
+
     
     (options, args) = parser.parse_args()
     
     
     url = None
     if options.fetch: # get url from commandline or clipboard
-        if args[0]:
+        if len(args) > 0 and not options.fetch:
             url = sys.stdin.read()
         else:
             try:
             print >> sys.stderr, "Can't get url ", url
             sys.exit(1)
     
-        paste = None           
+        paste = None   
         try:
             data = friendpaste.get_from_url(url)
             paste = data["snippet"]
             pass
         
         if paste is not None:
-            if options.output_paste == 'clipboard':
-                try:
-                    writeto_clipboard(paste)
-                except:
-                    print >> sys.stderr, "Can't write paste to the clipboard"
-                    sys.exit(1)
-            elif options.output_paste is not None:
-                fd = file(options.output_paste, 'w')
-                fd.write(paste)
-                fd.close
-            else:
-                print paste
+            try:
+                writeto_clipboard(utf8(paste))
+            except:
+                print >> sys.stderr, "Can't write paste to the clipboard"
+                sys.exit(1)
+        
+            print paste
             sys.exit(0)
         print >> sys.stderr, "Can't get paste at url ", url
         sys.exit(1)
         except Exception, e:
             print >> sys.stderr, "Something wrong happened", e
             
+    if paste.strip() == "" or not paste:
+        sys.exit(1)
+    
     title = options.title
     lang = options.language
     
     ret = {}
     if options.update:
         url = options.update
-        if options.update is None or options.update_last:
+        if not options.update or options.update_last:
             try:
                 url = read_last_paste_url()
             except:
                 pass
-        ret = friendpaste.update(url, title, paste, lang)
-        
-    ret = friendpaste.post(title, paste, lang)
+        ret = friendpaste.update_from_url(url, title, paste, lang)
+    else:   
+        ret = friendpaste.post(title, paste, lang)
+    
     ok = ret.get('ok', False)
     if ok:
         if not options.noclipboard:
                 writeto_clipboard(ret['url'])
             except:
                 pass
+        
         try:
             save_paste_url(ret['url'])
         except: