Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trans and autocomplete. #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions python/jittor/utils/autocomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# usage: PYTHONPATH=/PATH/TO/JITTOR python autocomplete.py
import os
import re
from jittor import ops
jittor_path = os.environ.get("PYTHONPATH")

def get_pyi():
# for __init__.py functions
os.system(f"PYTHONPATH={jittor_path} stubgen -m jittor -o autoacompleteout")
f = open(os.path.join("autoacompleteout","jittor","__init__.pyi"),"a")
# for c++ ops
var_text = "class Var:\n"
for func_name,func in ops.__dict__.items():
if func_name == "__doc__" or func_name == "__name__" or func_name == "__loader__" or func_name == "__spec__" or func_name == "__package__":
continue
text = func.__doc__
declarations = re.findall(r"Declaration:\n(.+)\n",text)
for decl in declarations:
f.write(f"def {func_name}(")
params = re.findall(r".+ [a-zA-Z_0-9]+\((.+)", decl)
print(func_name,params)
get_var = 0
for param in params:
para = param.split(",")
for i,p in enumerate(para):
pa = p.strip().split(" ")[1]
if i == 0 and p.strip().split(" ")[0] == "VarHolder*":
get_var = 1
var_text += f"\tdef {func_name}(self,"
pf = pa.split("=")[0]
f.write(pf)
if get_var == 1:
var_text += pf
if i != len(para) - 1:
f.write(",")
if get_var == 1:
var_text += ","
else:
if len(pa.split("=")) > 1:
f.write("):...\n")
if get_var == 1:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: return value type annotation
TODO: group multiple c python binding

var_text += "):...\n"
else:
f.write(":...\n")
if get_var == 1:
var_text += ":...\n"
f.write(var_text)
f.close()
# mv to jittor folder and delete buffer
abs_path = os.path.join(jittor_path,"jittor","__init__.pyi")
pyi_path = os.path.join("autoacompleteout","jittor","__init__.pyi")
os.system(f"mv {pyi_path} {abs_path}")
os.system("rm -rf autoacompleteout")
os.system("rm -rf .mypy_cache")

if __name__ == "__main__":
get_pyi()
29 changes: 29 additions & 0 deletions python/jittor/utils/tran.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# usage: PYTHONPATH=/PATH/TO/JITTOR python tran.py
import re
import os

path = os.environ.get("PYTHONPATH")

def walk_dir(dir_name):
for abname in os.listdir(dir_name):
nfn = os.path.join(dir_name,abname)
if os.is_dir(nfn):
walk_dir(nfn)
continue
if not nfn.endswith(".py"): continue
f = open(nfn,"r+",encoding="utf-8")
startline = f.tell()
content = f.read()
loc_lang = locale.getdefaultlocale()
if loc_lang[0] == "zh-CN":
content = re.sub('\'\'\'(.*?)@@@', '\'\'\'', content, flags=re.DOTALL)
elif loc_lang[0] == "en-US":
content = re.sub('@@@(.*?)\'\'\'', '\'\'\'', content, flags=re.DOTALL)
f.seek(startline)
f.truncate(0)
f.write(content)
f.close()


if __name__ == "__main__":
walk_dir(path)