Last active
          June 19, 2019 16:36 
        
      - 
      
- 
        Save villares/1b14997beef52de34069b91752faef40 to your computer and use it in GitHub Desktop. 
    Prova de conceito
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # Needs a tab nammed "in.py" with the source to be converted! | |
| # TODO: deal with `with pushMatrix():` and other Python Mode contexts | |
| import re | |
| has_import = False | |
| has_future_division = False | |
| has_print_statement = False | |
| globals_list = [] | |
| def rename_stuff(line): | |
| global has_print_statement | |
| line = line.replace("P3D", "WEBGL") | |
| line = line.replace("size", "createCanvas") | |
| line = line.replace("PVector", "p5.Vector") | |
| line = line.replace("pushMatrix", "push") | |
| line = line.replace("pushStyle", "push") | |
| line = line.replace("popMatrix", "pop") | |
| line = line.replace("popStyle", "pop") | |
| line = line.replace("mousePressed (", "mousePressed(") # people wouldn't write `method<space><space>(` I guess | |
| line = line.replace("keyPressed (", "keyPressed(") | |
| line = line.replace("mousePressed ", "mouseIsPressed ") | |
| line = line.replace("keyPressed ", "keyIsPressed ") | |
| line = line.replace("colorMode(HSB)", "colorMode(HSB, 255, 255, 255)") | |
| if line.strip().startswith("print "): | |
| has_print_statement = True | |
| line = line.replace("print ","# print ") | |
| line = re.sub("print\s(.*)", 'print(\\1)', line) | |
| return line | |
| with open("in.py", 'r') as file_in: | |
| lines = file_in.readlines() | |
| with open("out.py", 'w') as file_out: | |
| # scan code | |
| for one_line in lines: | |
| one_line = rename_stuff(one_line) | |
| if "__future__" in one_line and "division" in one_line: | |
| has_future_division = True | |
| one_line = "# no need in Python 3... " + one_line | |
| if "from pytop5js import" in one_line: # sem o * de proposito! | |
| has_import = True | |
| if one_line.strip().startswith("global"): | |
| names = one_line.split(",") | |
| names[0] = names[0].split()[1] | |
| globals_list += [n.strip() for n in names] | |
| no_need_to_add = set() | |
| for one_line in lines: | |
| for name in globals_list: | |
| if one_line.startswith(name): | |
| no_need_to_add.add(name) | |
| division_found = any(("/" in l for l in lines)) | |
| globals_list = list(set(globals_list) - no_need_to_add) | |
| # prefix code | |
| if not has_import: | |
| file_out.write("from pyp5js import *\n") # or "from pytop5js... depending on version | |
| # print globals_list | |
| for name in globals_list: | |
| file_out.write(name + " = None\n") | |
| # rewrite main code | |
| for one_line in lines: | |
| file_out.write(one_line) | |
| if division_found and not has_future_division: | |
| print("Warning: Mind your Python 2 divisions!") | |
| if has_print_statement: | |
| print("Warning: Mind your Python 2 print statements!") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment