From 7ba51b40254b8431d6a5328e09d13236fc0551b2 Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Sun, 22 Nov 2020 09:35:24 -0500 Subject: [PATCH 1/5] [ADD] TechnoLibre code generator repo branch 12.0_dev --- manifest/default.dev.xml | 2 ++ script/install_locally.sh | 1 + source_repo_addons.csv | 1 + 3 files changed, 4 insertions(+) diff --git a/manifest/default.dev.xml b/manifest/default.dev.xml index b074a79..13b0544 100644 --- a/manifest/default.dev.xml +++ b/manifest/default.dev.xml @@ -6,6 +6,7 @@ + @@ -90,6 +91,7 @@ + diff --git a/script/install_locally.sh b/script/install_locally.sh index 7d49bc1..0951fd1 100755 --- a/script/install_locally.sh +++ b/script/install_locally.sh @@ -154,6 +154,7 @@ if [[ ${EL_MINIMAL_ADDONS} = "False" ]]; then printf "${EL_HOME}/doc/itpp-labs_odoo-test-docs," >> ${EL_CONFIG_FILE} printf "${EL_HOME}/doc/odoo_documentation-user," >> ${EL_CONFIG_FILE} printf "${EL_HOME}/script/OCA_odoo-module-migrator," >> ${EL_CONFIG_FILE} + printf "${EL_HOME}/addons/TechnoLibre_odoo-code-generator," >> ${EL_CONFIG_FILE} fi printf "\n" >> ${EL_CONFIG_FILE} diff --git a/source_repo_addons.csv b/source_repo_addons.csv index 9af2f29..efa6147 100644 --- a/source_repo_addons.csv +++ b/source_repo_addons.csv @@ -125,3 +125,4 @@ https://github.com/itpp-labs/odoo-port-docs.git,doc,master, https://github.com/itpp-labs/odoo-test-docs.git,doc,master, https://github.com/odoo/documentation-user.git,doc,, https://github.com/OCA/odoo-module-migrator.git,script,master, +https://github.com/TechnoLibre/odoo-code-generator.git,addons,, From 662bc864c00ddd9dc62034894184929f171301bd Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Sat, 19 Dec 2020 01:23:19 -0500 Subject: [PATCH 2/5] [ADD] pip dependency pymysql for code generator db servers --- poetry.lock | 18 +++++++++++++++++- pyproject.toml | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index 8eeab1d..9c215ea 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1523,6 +1523,18 @@ category = "main" optional = false python-versions = "*" +[[package]] +name = "pymysql" +version = "0.10.1" +description = "Pure Python MySQL Driver" +category = "main" +optional = false +python-versions = "*" + +[package.extras] +ed25519 = ["PyNaCl (>=1.4.0)"] +rsa = ["cryptography"] + [[package]] name = "pynacl" version = "1.4.0" @@ -2618,7 +2630,7 @@ python-versions = "*" [metadata] lock-version = "1.0" python-versions = "^3.7" -content-hash = "add98a5142cb63404b0f61132b791c65be6ca03aa14f5e09497a0b5f36878668" +content-hash = "149f70cf43e8a2cb392838c304f1227ba799258f5f4999f3ad89316cd8a7e455" [metadata.files] agithub = [ @@ -3553,6 +3565,10 @@ pymssql = [ {file = "pymssql-2.1.5-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:70a5c67759254e982368c5b9ccfe076447a7fd545b8376eb62d60c3b85e3b94d"}, {file = "pymssql-2.1.5.tar.gz", hash = "sha256:d60f5f90337399668e10ab6a23a1657f190c9585401eb96a5456261f7c414864"}, ] +pymysql = [ + {file = "PyMySQL-0.10.1-py2.py3-none-any.whl", hash = "sha256:44f47128dda8676e021c8d2dbb49a82be9e4ab158b9f03e897152a3a287c69ea"}, + {file = "PyMySQL-0.10.1.tar.gz", hash = "sha256:263040d2779a3b84930f7ac9da5132be0fefcd6f453a885756656103f8ee1fdd"}, +] pynacl = [ {file = "PyNaCl-1.4.0-cp27-cp27m-macosx_10_10_x86_64.whl", hash = "sha256:ea6841bc3a76fa4942ce00f3bda7d436fda21e2d91602b9e21b7ca9ecab8f3ff"}, {file = "PyNaCl-1.4.0-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:d452a6746f0a7e11121e64625109bc4468fc3100452817001dbe018bb8b08514"}, diff --git a/pyproject.toml b/pyproject.toml index 9418095..835ce64 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -148,6 +148,7 @@ xlwt = "1.3" xmltodict = "^0.12.0" zeep = "^3.4.0" zxcvbn = "^4.4.28" +pymysql = "^0.10.1" [tool.poetry.dev-dependencies] websocket_client = "^0.57.0" From 789ae2b9ad7e17f43966a6783e3db94edaaaf08d Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Wed, 2 Dec 2020 06:39:58 -0500 Subject: [PATCH 3/5] [ADD] script to transform Python code with code_writer --- .../transform_python_to_code_writer.py | 763 ++++++++++++++++++ 1 file changed, 763 insertions(+) create mode 100644 script/code_generator/transform_python_to_code_writer.py diff --git a/script/code_generator/transform_python_to_code_writer.py b/script/code_generator/transform_python_to_code_writer.py new file mode 100644 index 0000000..6030507 --- /dev/null +++ b/script/code_generator/transform_python_to_code_writer.py @@ -0,0 +1,763 @@ +#!./.venv/bin/python +import os +import sys +import argparse +import logging +import subprocess +from code_writer import CodeWriter +# import tokenize + +from script.git_tool import GitTool + + +def get_config(): + """Parse command line arguments, extracting the config file name, + returning the union of config file and command line arguments + + :return: dict of config file settings and command line arguments + """ + config = GitTool.get_project_config() + + # TODO update description + parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, + description='''\ + Transform a python file in code writer format python file. +''', + epilog='''\ +''' + ) + parser.add_argument('-f', '--file', dest="file", required=True, + help="Path of file to transform to code_writer.") + parser.add_argument('-o', '--output', dest="output", + help="The output file.") + args = parser.parse_args() + return args + + +def count_space_tab(word, group_space=4): + """ + + :param word: + :param group_space: count tab for number of group_space + :return: number of tab, or -1 if ignore line or empty, nb_space of over + """ + nb_tab = 0 + nb_space = 0 + for s_char in word: + if s_char in ["\n", "\r"]: + return -1, 0 + elif s_char in ["\t"]: + nb_tab += 1 + elif s_char == " ": + nb_space += 1 + if nb_space == group_space: + nb_space = 0 + nb_tab += 1 + else: + # Finish to count + break + return nb_tab, nb_space + + +def main(): + config = get_config() + cw = CodeWriter() + last_nb_tab = 0 + nb_tab = 0 + no_tab = 0 + + # Validate file format + out = subprocess.check_output(f"python -m tabnanny {config.file}", stderr=subprocess.STDOUT, shell=True) + if out: + print(out) + sys.exit(1) + + # with tokenize.open(config.file) as f: + # tokens = tokenize.generate_tokens(f.readline) + # for token in tokens: + # print(token) + + cw.emit("from code_writer import CodeWriter") + cw.emit("cw = CodeWriter()") + + no_line = 1 + with open(config.file, 'r') as file: + for line in file.readlines(): + nb_tab, nb_space = count_space_tab(line) + diff_tab = nb_tab - last_nb_tab + new_line = line.strip() + new_line = new_line.replace("\"", "\\\"") + + status_no_tab = add_line(cw, new_line, no_line, nb_tab, no_tab, no_tab, nb_space) + if status_no_tab >= 0: + no_tab = status_no_tab + + last_nb_tab = nb_tab + no_line += 1 + cw.emit("print(cw.render())") + + output = cw.render() + if config.output: + with open(config.output, 'w') as file: + file.write(output) + else: + print(output) + + +def add_line(cw, line, no_line, nb_indent, no_indent, init_no_intend, nb_space): + """ + Recursive check indent and write line + :param cw: code_writer module + :param line: line to write + :param line: actual position of line, useful for debug + :param nb_indent: number of indent to support + :param no_indent: actual indentation + :param init_no_intend: initial indentation, can detect if new indent or use the last + :param nb_space: more space to add + :return: the actual no_indent + """ + max_indent = 19 + if nb_indent > max_indent: + print(f"Do not support more than {max_indent} indent.") + sys.exit(-1) + + if nb_indent == -1: + cw.emit("cw.emit(\"\")") + return 0 + elif nb_indent == no_indent: + if nb_indent == 0: + cw.emit(f"cw.emit(\"{line}\")") + return 0 + elif nb_indent == 1: + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 2: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 3: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 4: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 5: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 6: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 7: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 8: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 9: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 10: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 11: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 12: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 13: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 14: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 15: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 16: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 17: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 18: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 19: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + with cw.indent(): + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + return nb_indent + else: + if no_indent > nb_indent: + # deindent + return add_line(cw, line, no_line, nb_indent, no_indent - 1, init_no_intend, nb_space) + else: + # indent + return add_line(cw, line, no_line, nb_indent, no_indent + 1, init_no_intend, nb_space) + print("BUG") + return -1 + + +if __name__ == '__main__': + main() From a1c98338776c657910b81e8846c81ffa6bad1a9a Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Sun, 27 Dec 2020 05:19:26 -0500 Subject: [PATCH 4/5] [ADD] pip dependency code-writer --- poetry.lock | 14 +++++++++++++- pyproject.toml | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index 9c215ea..5c98f46 100644 --- a/poetry.lock +++ b/poetry.lock @@ -260,6 +260,14 @@ category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +[[package]] +name = "code-writer" +version = "1.1.1" +description = "Library with convenience functions for generating code." +category = "main" +optional = false +python-versions = "*" + [[package]] name = "colorama" version = "0.4.3" @@ -2630,7 +2638,7 @@ python-versions = "*" [metadata] lock-version = "1.0" python-versions = "^3.7" -content-hash = "149f70cf43e8a2cb392838c304f1227ba799258f5f4999f3ad89316cd8a7e455" +content-hash = "a15f4c49602bdab8cb5bbd10878c78ea9662854c11aff31f6a2319d2edd193e9" [metadata.files] agithub = [ @@ -2766,6 +2774,10 @@ click = [ {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"}, ] +code-writer = [ + {file = "code-writer-1.1.1.tar.gz", hash = "sha256:485260d0444707876596c499bd9b1ef9a11dc720d741ac57c884216de9fa3f40"}, + {file = "code_writer-1.1.1-py3-none-any.whl", hash = "sha256:65bbf4616cd4f206a2e3fc5a9933a002d053d60ebdcc1dac66acc1a58cddd6b8"}, +] colorama = [ {file = "colorama-0.4.3-py2.py3-none-any.whl", hash = "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff"}, {file = "colorama-0.4.3.tar.gz", hash = "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"}, diff --git a/pyproject.toml b/pyproject.toml index 835ce64..f16ff7d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -149,6 +149,7 @@ xmltodict = "^0.12.0" zeep = "^3.4.0" zxcvbn = "^4.4.28" pymysql = "^0.10.1" +code-writer = "^1.1.1" [tool.poetry.dev-dependencies] websocket_client = "^0.57.0" From c889982935340378247a139d178e3b1b65ec5457 Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Mon, 7 Dec 2020 23:06:04 -0500 Subject: [PATCH 5/5] [ADD] code_generator xml to XML DOM --- .../transform_xml_to_code_writer.py | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 script/code_generator/transform_xml_to_code_writer.py diff --git a/script/code_generator/transform_xml_to_code_writer.py b/script/code_generator/transform_xml_to_code_writer.py new file mode 100644 index 0000000..46c8e3b --- /dev/null +++ b/script/code_generator/transform_xml_to_code_writer.py @@ -0,0 +1,205 @@ +#!./.venv/bin/python +import os +import sys +import argparse +import logging +from code_writer import CodeWriter +from xml.dom import minidom, Node + +from script.git_tool import GitTool + + +def get_config(): + """Parse command line arguments, extracting the config file name, + returning the union of config file and command line arguments + + :return: dict of config file settings and command line arguments + """ + config = GitTool.get_project_config() + + # TODO update description + parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, + description='''\ + Transform a xml file in code writer format xml file. +''', + epilog='''\ +''' + ) + parser.add_argument('-f', '--file', dest="file", required=True, + help="Path of file to transform to code_writer.") + parser.add_argument('-o', '--output', dest="output", + help="The output file.") + args = parser.parse_args() + return args + + +def transform_string_to_list(str_info): + # lst_info = [a.strip() for a in str_info.split("\n") if a.strip()] + lst_info = [a for a in str_info.split("\n") if a.strip()] + return lst_info + + +def code_writer_deep_xml(nodes): + if not nodes: + return + size_nodes = len(nodes.childNodes) + lst_result = [] + for node in nodes.childNodes: + if node.nodeType == Node.ELEMENT_NODE: + value = code_writer_deep_xml(node) + comment_str = node.toprettyxml() + comment = "" + if comment_str: + lst_comment = comment_str.split("\n") + if lst_comment: + comment = f"# {lst_comment[0]}" + + code_line = (f"E.{node.tagName}({str(dict(node.attributes.items()))})", value) + lst_result.append((comment, code_line)) + elif node.nodeType == Node.TEXT_NODE: + if size_nodes == 1: + return node.data + return lst_result + + +class GenerateCode: + def __init__(self): + self._result = "" + + @property + def result(self): + return self._result + + def generate_code(self, lst_in): + if type(lst_in) is list: + i = 1 + max = len(lst_in) + for tpl_in in lst_in: + self._result += f"\n{tpl_in[0]}\n" + self.generate_code(tpl_in[1]) + if i != max: + self._result += "," + i += 1 + elif type(lst_in) is tuple: + if lst_in[1]: + self._result += f"{lst_in[0][:-1]}, " + if type(lst_in[1]) is str: + self._result += f"'{lst_in[1].strip()}'" + else: + self.generate_code(lst_in[1]) + self._result += ")" + else: + self._result += f"{lst_in[0][:-1]})" + + +def main(): + config = get_config() + cw = CodeWriter() + + mydoc = minidom.parse(config.file) + if not mydoc: + print(f"Error, cannot parse {config.file}") + sys.exit(1) + + cw.emit('from lxml.builder import E') + cw.emit('from lxml import etree as ET') + cw.emit('from code_writer import CodeWriter') + cw.emit('') + cw.emit('print(\'\')') + cw.emit('print("")') + + lst_function = [] + comment_for_next_group = None + + for odoo in mydoc.getElementsByTagName('odoo'): + for ir_view_item in odoo.childNodes: + if ir_view_item.nodeType == Node.ELEMENT_NODE: + # Show part of xml + fct_name = "ma_fonction" + lst_function.append(fct_name) + cw.emit(f'def {fct_name}():') + with cw.indent(): + cw.emit('"""') + for line in transform_string_to_list(ir_view_item.toprettyxml()): + cw.emit(line) + cw.emit('"""') + # Show comment + if comment_for_next_group: + cw.emit(f'print(\'\')') + comment_for_next_group = None + attributes_root = dict(ir_view_item.attributes.items()) + + lst_out = code_writer_deep_xml(ir_view_item) + + generate_code = GenerateCode() + generate_code.generate_code(lst_out) + child_root = generate_code.result + + code = f'root = E.{ir_view_item.tagName}({str(attributes_root)}, {child_root})' + + for line in code.split("\n"): + cw.emit(line) + + cw.emit('content = ET.tostring(root, pretty_print=True)') + cw.emit() + cw.emit("cw = CodeWriter()") + cw.emit('for line in content.decode("utf-8").split("\\n"):') + with cw.indent(): + cw.emit("with cw.indent():") + with cw.indent(): + cw.emit("cw.emit(line)") + cw.emit("print(cw.render())") + cw.emit(f"{fct_name}()") + elif ir_view_item.nodeType == Node.COMMENT_NODE: + comment_for_next_group = ir_view_item.data + else: + # print(ir_view_item) + pass + + cw.emit('print("")') + + output = cw.render() + if config.output: + with open(config.output, 'w') as file: + file.write(output) + else: + print(output) + + +def add_line(cw, line, no_line, nb_indent, no_indent, init_no_intend, nb_space): + """ + Recursive check indent and write line + :param cw: code_writer module + :param line: line to write + :param line: actual position of line, useful for debug + :param nb_indent: number of indent to support + :param no_indent: actual indentation + :param init_no_intend: initial indentation, can detect if new indent or use the last + :param nb_space: more space to add + :return: the actual no_indent + """ + max_indent = 19 + if nb_indent > max_indent: + print(f"Do not support more than {max_indent} indent.") + sys.exit(-1) + + if nb_indent == -1: + cw.emit("cw.emit(\"\")") + return 0 + elif nb_indent == no_indent: + if nb_indent == 0: + cw.emit(f"cw.emit(\"{line}\")") + return 0 + elif nb_indent == 1: + if no_indent != init_no_intend: + cw.emit(f"with cw.indent({4 + nb_space if nb_space else ''}):") + with cw.indent(): + cw.emit(f"cw.emit(\"{line}\")") + elif nb_indent == 2: + if nb_indent - 1 > init_no_intend: + cw.emit(f"with cw.indent():") + + +if __name__ == '__main__': + main()