Merge pull request #23 from ERPLibre/code_generator

Code generator from TechnoLibre
This commit is contained in:
Mathieu Benoit 2021-01-05 18:06:58 -05:00 committed by GitHub
commit 7ba604ba8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1003 additions and 1 deletions

View file

@ -6,6 +6,7 @@
<remote name="Numigi" fetch="https://github.com/ERPLibre/"/>
<remote name="OCA" fetch="https://github.com/ERPLibre/"/>
<remote name="Smile-SA" fetch="https://github.com/ERPLibre/"/>
<remote name="TechnoLibre" fetch="https://github.com/ERPLibre/"/>
<remote name="camptocamp" fetch="https://github.com/ERPLibre/"/>
<remote name="dhongu" fetch="https://github.com/ERPLibre/"/>
<remote name="it-projects-llc" fetch="https://github.com/ERPLibre/"/>
@ -90,6 +91,7 @@
<project name="odoo-base-addons.git" path="addons/Numigi_odoo-base-addons" remote="Numigi" revision="12.0_dev" groups="addons"/>
<project name="odoo-business-spending-management-quebec-canada.git" path="addons/MathBenTech_odoo-business-spending-management-quebec-canada" remote="MathBenTech" revision="12.0_sale_order_line_timesheet_attribution" groups="addons"/>
<project name="odoo-cloud-platform.git" path="addons/camptocamp_odoo-cloud-platform" remote="camptocamp" groups="addons"/>
<project name="odoo-code-generator.git" path="addons/TechnoLibre_odoo-code-generator" remote="TechnoLibre" revision="12.0" groups="addons"/>
<project name="odoo-development.git" path="doc/itpp-labs_odoo-development" remote="itpp-labs" revision="master" groups="odoo"/>
<project name="odoo-entertainment-addons.git" path="addons/Numigi_odoo-entertainment-addons" remote="Numigi" revision="12.0_dev" groups="addons"/>
<project name="odoo-git-addons.git" path="addons/Numigi_odoo-git-addons" remote="Numigi" revision="12.0_dev" groups="addons"/>

30
poetry.lock generated
View file

@ -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"
@ -1523,6 +1531,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 +2638,7 @@ python-versions = "*"
[metadata]
lock-version = "1.0"
python-versions = "^3.7"
content-hash = "add98a5142cb63404b0f61132b791c65be6ca03aa14f5e09497a0b5f36878668"
content-hash = "a15f4c49602bdab8cb5bbd10878c78ea9662854c11aff31f6a2319d2edd193e9"
[metadata.files]
agithub = [
@ -2754,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"},
@ -3553,6 +3577,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"},

View file

@ -148,6 +148,8 @@ xlwt = "1.3"
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"

View file

@ -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()

View file

@ -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(\'<?xml version="1.0" encoding="utf-8"?>\')')
cw.emit('print("<odoo>")')
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.strip()} -->\')')
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("</odoo>")')
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()

View file

@ -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}

View file

@ -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,,

1 url path revision clone-depth
125 https://github.com/itpp-labs/odoo-test-docs.git doc master
126 https://github.com/odoo/documentation-user.git doc
127 https://github.com/OCA/odoo-module-migrator.git script master
128 https://github.com/TechnoLibre/odoo-code-generator.git addons