fork download
  1. #!/usr/bin/env python
  2. """
  3. ideone.com paster
  4. """
  5.  
  6. from sys import stdin, stderr
  7. from SOAPpy import WSDL
  8. import sys, getopt
  9.  
  10.  
  11. #settings
  12. IDEONE_SERVICE = 'http://i...content-available-to-author-only...e.com/api/1/service.wsdl'
  13. USER = 'test'
  14. PASS = 'test'
  15. LANG = 'text'
  16.  
  17.  
  18. #language code tables
  19. LANGS_CODE = {
  20. 'ada' : 7,
  21. 'assembler' : 13,
  22. 'awk' : 104,
  23. 'bash' : 28,
  24. 'c' : 11,
  25. 'c++' : 1,
  26. 'clojure' : 111,
  27. 'clisp' : 32,
  28. 'erlang' : 36,
  29. 'haskell' : 21,
  30. 'java' : 10,
  31. 'lua' : 26,
  32. 'ocaml' : 8,
  33. 'perl' : 3,
  34. 'swipl' : 15,
  35. 'python' : 4,
  36. 'scala' : 39,
  37. 'text' : 62,
  38. 'default' : 62
  39. }
  40.  
  41. def getLangCode(lang):
  42. if LANGS_CODE.has_key(lang):
  43. return LANGS_CODE[lang]
  44. return LANGS_CODE['default']
  45.  
  46.  
  47. # error handling
  48. ERROR_MESSAGES = {
  49. 'AUTH_ERROR' : "User name or user's password are invalid.",
  50. 'PASTE_NOT_FOUND' : "Paste with specified link could not be found.",
  51. 'WRONG_LANG_ID' : "Language with specified id does not exist."
  52. }
  53.  
  54. def printErrorAndDie(error):
  55. msg = 'unknown error'
  56. if ERROR_MESSAGES.has_key(error):
  57. msg = ERROR_MESSAGES[error]
  58. stderr.print_line(msg)
  59. exit(1)
  60.  
  61. def getErrorCode(resp):
  62. return resp['item']['value']['item'][0]['value']
  63.  
  64. def isError(resp):
  65. return resp['item'][0] == 'error'
  66.  
  67.  
  68. #options handling
  69. USAGE = """
  70. cat somefile | ideone.py [KEY]...
  71. or
  72. ideone.py file1 file2 file3
  73.  
  74. KEYS:
  75. -h - this usage
  76. -u 'user' - set ideone's username to 'user'
  77. -p 'pass' - set ideone's password to 'pass'
  78. -l 'lang' - set code's language to 'lang'
  79. """
  80.  
  81. def usage():
  82. print USAGE
  83. exit(0)
  84.  
  85. def changeUser(newUser):
  86. global USER
  87. USER = newUser
  88.  
  89. def changePass(newPass):
  90. global PASS
  91. PASS = newPass
  92.  
  93. def changeLang(newLang):
  94. global LANG
  95. LANG = newLang
  96.  
  97. CHANGE_ARGS = {
  98. '-u': changeUser,
  99. '-p': changePass,
  100. '-l': changeLang
  101. }
  102.  
  103. FLAG_ARGS = {
  104. '-h': usage
  105. }
  106.  
  107. def parseArgs(args):
  108. copts = CHANGE_ARGS.keys()
  109. fopts = FLAG_ARGS.keys()
  110. return getopt.getopt(args,
  111. ''.join(map(lambda arg: arg.replace('-', '') + ':', copts)) +
  112. ''.join(map(lambda arg: arg.replace('-', ''), fopts)))
  113.  
  114. def applyOpts(optlist):
  115. for opt in optlist:
  116. if opt[1] != '':
  117. CHANGE_ARGS[opt[0]](opt[1])
  118. else:
  119. FLAG_ARGS[opt[0]]()
  120.  
  121. #ideone wsdl client
  122. IDEONE = WSDL.Proxy(IDEONE_SERVICE)
  123.  
  124. def paste(file):
  125. source = file.read()
  126. response = IDEONE.createSubmission(USER, PASS, source, getLangCode(LANG), '', False, False)
  127. res = ''
  128. if isError(response):
  129. printErrorAndDie(getErrorCode(response))
  130. else:
  131. return "http://i...content-available-to-author-only...e.com/%s" % (response['item'][1]['value'])
  132.  
  133.  
  134. optlist, filelist = parseArgs(sys.argv[1:])
  135. applyOpts(optlist)
  136.  
  137. if filelist == []:
  138. print paste(stdin)
  139. else:
  140. for filename in filelist:
  141. file = open(filename, "r")
  142. print paste(file)
  143. file.close()
  144.  
  145.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty