Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. def run(assignments="assignments.txt", open_from="peoples.txt",
  2. to_write=True, to_exec=True):
  3.  
  4. def run(assignments="assignments.txt", open_from="peoples.txt",
  5. to_write=True, to_exec=True):
  6.  
  7. def get_args():
  8. argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python",
  9. add_help=True)
  10. argparser.add_argument('--assignments', required=False, type=str, dest='assignments',
  11. default="assignments.txt", help="Assignments file for names to Python builtins. Default is "
  12. "'assignments.txt'")
  13. argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt",
  14. help="People's Python file, for conversion. Default is 'peoples.txt'.")
  15. argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false",
  16. default=False, help="Do not write output from conversion to standard python to a file, "
  17. "if this argument is provided.")
  18. argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false",
  19. default=True, help="Do not execute output from conversion and do whatever it is set to do, "
  20. "if this argument is provided.")
  21.  
  22. return argparser.parse_args()
  23.  
  24. if __name__ == "__main__":
  25. token_names = None
  26. args = get_args()
  27. try:
  28. run(args.assignment, args.peoples, args.write, args.execute)
  29. except Exception as e:
  30. print "An exception has occurred:n%s" % str(e)
  31.  
  32. def run(assignments, open_from, to_write, to_exec):
  33.  
  34. writepath = open_from[:-4]+"-output.txt"
  35.  
  36. if to_write:
  37. with open(writepath, 'w') as outfile:
  38. # Write to the output file
  39. outfile.write(output)
  40. outfile.close()
  41.  
  42. if to_exec:
  43. # ...
  44.  
  45. # At the beginning with your imports:
  46. import subprocess as sp
  47.  
  48. # Later on in the code...
  49. if to_exec:
  50. if not to_write:
  51. writepath = '/tmp/' + writepath
  52. with open(writepath, 'w') as outfile:
  53. # Write to the output file
  54. outfile.write(output)
  55. outfile.close()
  56.  
  57. (execout, execerr) = sp.Popen(str('python %s' % writepath).split(),
  58. stdout=sp.PIPE, stdin=sp.PIPE).communicate()
  59.  
  60. if not to_write:
  61. os.remove(writepath)
  62.  
  63. if execerr and execerr != '':
  64. raise RuntimeError("An issue occurred running the converted code:n%s" % str(execerr))
  65.  
  66. print execout
  67.  
  68. #!/usr/bin/env python
  69.  
  70. import tokenize
  71. import json
  72. import os
  73. import argparse
  74. import subprocess as sp
  75.  
  76. def get_args():
  77. argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python",
  78. add_help=True)
  79. argparser.add_argument('--assignments', required=False, type=str, dest='assignments',
  80. default="assignments.txt", help="Assignments file for names to Python builtins. Default is "
  81. "'assignments.txt'")
  82. argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt",
  83. help="People's Python file, for conversion. Default is 'peoples.txt'.")
  84. argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false",
  85. default=False, help="Do not write output from conversion to standard python to a file, "
  86. "if this argument is provided.")
  87. argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false",
  88. default=True, help="Do not execute output from conversion and do whatever it is set to do, "
  89. "if this argument is provided.")
  90.  
  91. return argparser.parse_args()
  92.  
  93. def handle_token(type_, token, (srow, scol), (erow, ecol), line):
  94. # Return the info about the tokens, if it's a NAME token then replace it
  95.  
  96. if tokenize.tok_name[type_] == "NAME":
  97. token = token_names.get(token, token)
  98. return type_, token, (srow, scol), (erow, ecol), line
  99.  
  100.  
  101. def run(assignments, open_from, to_write, to_exec):
  102. """
  103. - `assignments` is the file to open the list of names from
  104. - `open_from` is the file to get the input code from
  105. - `to_write` is for toggling writing the compiled code to a file
  106. - `to_exec` is for toggling executing the code
  107.  
  108. Both `to_write` and `to_exec` are for using this code in another
  109. file by way of importing it.
  110. """
  111.  
  112. with open(assignments, "r") as f:
  113. # Read the replacements into token_names
  114. global token_names
  115. token_names = json.load(f)
  116.  
  117. with open(open_from) as source:
  118. # Get the tokenized version of the input, replace it, and untokenize into pretty output
  119. tokens = tokenize.generate_tokens(source.readline)
  120. handled_tokens = (handle_token(*token) for token in tokens)
  121.  
  122. output = tokenize.untokenize(handled_tokens)
  123.  
  124. writepath = open_from[:-4]+"-output.txt"
  125.  
  126. if to_write:
  127. with open(writepath, 'w') as outfile:
  128. # Write to the output file
  129. outfile.write(output)
  130.  
  131. if to_exec:
  132. if not to_write:
  133. writepath = 'tmp.' + writepath
  134. with open(writepath, 'w') as outfile:
  135. # Write to the output file
  136. outfile.write(output)
  137. outfile.close()
  138.  
  139. (execout, execerr) = sp.Popen(str('python %s' % writepath).split(),
  140. stdout=sp.PIPE, stdin=sp.PIPE).communicate()
  141.  
  142. if not to_write:
  143. os.remove(writepath)
  144.  
  145. if execerr and execerr != '':
  146. raise RuntimeError("An issue occurred running the converted code:n%s" % str(execerr))
  147.  
  148. print execout
  149.  
  150. return output
  151.  
  152.  
  153. if __name__ == "__main__":
  154. token_names = None
  155. args = get_args()
  156. try:
  157. run(args.assignments, args.peoples, args.write, args.execute)
  158. except Exception as e:
  159. print "An exception has occurred:n%s" % str(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement