Simple Sublime Text 3 plugin that adds support for uploading the selected regions of a file, or the whole file if nothing is selected, to an npaste server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

npaste-upload.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import sublime
  2. import sublime_plugin
  3. import os
  4. import uuid
  5. import string
  6. import random
  7. import base64
  8. import requests
  9. from subprocess import *
  10. from requests.auth import HTTPBasicAuth
  11. class NpasteUploadCommand(sublime_plugin.TextCommand):
  12. def run(self, edit):
  13. settings = sublime.load_settings('npaste.sublime-settings')
  14. selection = self.view.sel()
  15. selections = "\n".join(list(map(lambda s: self.view.substr(s), selection)))
  16. if selections == "":
  17. npaste_data = self.view.substr(sublime.Region(0, self.view.size()))
  18. else:
  19. npaste_data = selections
  20. data = ""
  21. passphrase = None
  22. payload = {'age': settings.get('age') }
  23. # Encrypt paste using GPG
  24. if (settings.get('encrypt') == True):
  25. passphrase = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(settings.get('encryption_key_length')))
  26. process = Popen(['gpg', '--armor', '--batch', '--passphrase', passphrase, '--symmetric'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
  27. d = bytes(npaste_data, 'utf-8')
  28. process.stdin.write(base64.b64encode(bytes(npaste_data, 'utf-8')))
  29. paste, errs = process.communicate()
  30. if (not errs):
  31. data = paste.decode("utf-8")
  32. payload["encrypted"] = 1
  33. else:
  34. sublime.message_dialog(errs.decode("utf-8"))
  35. else:
  36. data = npaste_data
  37. if settings.get('archive') == True:
  38. payload["archive"] = 1
  39. # Upload paste to npaste
  40. files = {'paste': ('paste.txt', data, 'text/plain')}
  41. r = requests.post(settings.get('url'), files=files, data=payload, auth=HTTPBasicAuth(settings.get('username'), settings.get('password')))
  42. url = r.text
  43. if (passphrase is not None):
  44. url = url + "#" + passphrase
  45. sublime.set_clipboard(url)
  46. sublime.message_dialog('Copied to clipboard!')