From ba752f4142fc3644a0b6c3fa7ea54e6553766862 Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Mon, 25 Jul 2022 07:37:31 +0200 Subject: [PATCH] fixing lark for js-files and its transformer --- py-helpers/prepare_code/js/debug.py | 12 +- py-helpers/prepare_code/js/grammar.js.lark | 252 +++-- py-helpers/prepare_code/js/transformer.py | 1085 ++++++++++++++------ py-helpers/prepare_code/main.py | 17 +- py-helpers/prepare_code/post_processor.py | 15 +- tsconfig.py.json | 12 +- 6 files changed, 945 insertions(+), 448 deletions(-) diff --git a/py-helpers/prepare_code/js/debug.py b/py-helpers/prepare_code/js/debug.py index f6ebfc2..1db8a3d 100644 --- a/py-helpers/prepare_code/js/debug.py +++ b/py-helpers/prepare_code/js/debug.py @@ -49,16 +49,10 @@ def parse(parser, path_to_file): if __name__ == "__main__": - - # "dist-py\dispatcher\baseServices\connectivy.js" - + # dist-py/dispatcher/InstanceManager/InstanceManager.js input_path = Path(__file__).parent.joinpath( - '..', "..", "dist-py", "helpers") - path_to_file = os.path.join(input_path, "jsonSchemaMethods.js") - - input_path = Path(__file__).parent.joinpath( - '..', "..", "..", "dist-py", "dispatcher", "baseServices") - path_to_file = os.path.join(input_path, "connectivy.js") + '..', "..", "..", "dist-py", "dispatcher", "InstanceManager") + path_to_file = os.path.join(input_path, "InstanceManager.js") parse( get_parser(), diff --git a/py-helpers/prepare_code/js/grammar.js.lark b/py-helpers/prepare_code/js/grammar.js.lark index ec96578..c496f85 100644 --- a/py-helpers/prepare_code/js/grammar.js.lark +++ b/py-helpers/prepare_code/js/grammar.js.lark @@ -25,8 +25,10 @@ STR: /(`.*?`)|(".*?")|(\'.*?\')/ // %ignore "export" "default" /w+/ ";" // %ignore "export" "*" "from" STR ";" // %ignore "export" "*" "as" /w+/ "from" STR ";" -// %ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" "from" STR ";" -// %ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" ";" +// %ignore "export" "{" /(?<=export {)(.|\n)+?(?=})/ "}" "from" STR ";" +// %ignore "export" "{" /(?<=export {)(.|\n)+?(?=})/ "}" ";" + +skip: export "{" (id [","])+ "}" terminator // -------------------------------------------------------------------------- @@ -51,7 +53,6 @@ ret_expr: id | invert | list | dict - | descruct_dict | reassign | await_stmt | delete_stmt @@ -73,7 +74,7 @@ ret_expr: id // Now we ar able to provide this expressions wiht a terminator. -ret_expr_with_terminator: ret_expr terminator +ret_expr_with_terminator.10: ret_expr terminator return_statement: "return" [ret_expr] @@ -87,116 +88,126 @@ statement: ret_expr_with_terminator | do_while | if_statement | switch - | class - | decorated_class + | class_statement + | decorated_class_statement | function | try_catch + | skip // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Default Terminator: -terminator: ";" +terminator.100: ";" // Default ID: -id: /[a-zA-Z_$][a-zA-Z0-9_$]*/ -> identifier +id: /[a-zA-Z_$][a-zA-Z0-9_$]*/ -> identifier // We define valid import statements: -import_stmt: "import" str terminator -> import_stmt_all - | "import" id "from" str terminator -> import_stmt_id - | "import" "*" "as" id "from" str terminator -> import_stmt_as - | "import" "{" import_names "}" "from" str terminator -> import_stmt_from +import_stmt: "import" str terminator -> import_stmt_all + | "import" id "from" str terminator -> import_stmt_id + | "import" "*" "as" id "from" str terminator -> import_stmt_as + | "import" "{" import_names "}" "from" str terminator -> import_stmt_from // we may import multiple items: -import_names: import_name ("," import_name)* [","] +import_names: import_name ("," import_name)* [","] // The import name might include multiple lines -import_name: id - | id "as" id -> import_as_name - | /\n/ -// Lets define a string; -str: /(`.*?`)|(".*?")|(\'.*?\')/ -str_multi_line: /(`(\\`|.|\n)*?`)/ +import_name: id + | id "as" id -> import_as_name + | /\n/ +// Lets define a string; +str: /(`.*?`)|(".*?")|(\'.*?\')/ +str_multi_line: /(`(\\`|.|\n)*?`)/ // Lets define a number; -num: INT ["." INT] | "." INT +num: INT ["." INT] | "." INT // Define a boolean; -bool: "false" | "true" +bool: bool_false | bool_true +bool_false: "false" +bool_true: "true" -null: "null" -undefined: "undefined" +null: "null" +undefined: "undefined" -increment: accessor "++" - | "++" accessor -decrement: accessor "--" - | "--" accessor +increment: accessor "++" + | "++" accessor +decrement: accessor "--" + | "--" accessor -invert: "!" ret_expr +invert: "!" ret_expr -instanceof: id "instanceof" id +instanceof: id "instanceof" id -typeof: "typeof" accessor +typeof: "typeof" accessor -delete_stmt: "delete" accessor +delete_stmt: "delete" accessor -await_stmt: "await" ret_expr +await_stmt: "await" ret_expr -reg_ex: "/" /(?<=\/).+(?=\/\w)/ "/" [/\w/] +reg_ex: "/" /(?<=\/).+(?=\/\w)/ "/" [/\w/] -sum: product - | sum "+" product -> add - | sum "-" product -> sub - | accessor "+=" product -> assigned_add - | accessor "-=" product -> assigned_sub +sum: product + | sum "+" product -> add + | sum "-" product -> sub + | accessor "+=" product -> assigned_add + | accessor "-=" product -> assigned_sub -product: atom - | product "*" atom -> mult - | product "/" atom -> div - | accessor "*=" atom -> assigned_mult - | accessor "/=" atom -> assigned_div +product: atom + | product "*" atom -> mult + | product "/" atom -> div + | accessor "*=" atom -> assigned_mult + | accessor "/=" atom -> assigned_div -boolean_operation: boolean_input boolean_operator boolean_input +boolean_operation: boolean_input boolean_operator boolean_input -boolean_operator: ">" -> bool_op_gt - | "<" -> bool_op_lt - | "<=" -> bool_op_lte - | ">=" -> bool_op_gte - | "==" -> bool_op_eq - | "===" -> bool_op_eq - | "!=" -> bool_op_not_eq - | "!==" -> bool_op_not_eq - | "&&" -> bool_op_and - | "||" -> bool_op_or - | "in" -> bool_op_in +boolean_operator: ">" -> bool_op_gt + | "<" -> bool_op_lt + | "<=" -> bool_op_lte + | ">=" -> bool_op_gte + | "==" -> bool_op_eq + | "===" -> bool_op_eq + | "!=" -> bool_op_not_eq + | "!==" -> bool_op_not_eq + | "&&" -> bool_op_and + | "||" -> bool_op_or + | "in" -> bool_op_in -boolean_input: ret_expr +boolean_input: ret_expr -atom: ret_expr - | id - | "-" atom - | "(" sum ")" +atom: ret_expr + | id + | "-" atom + | "(" sum ")" -// Define Lists. +// Define Lists. list: "[" [list_items] "]" list_items: (list_item [","])+ list_item: ret_expr | "..." ret_expr -> list_item_rest -descruct_list: "[" ((id | (rest_accessor)) [","])* "]" "=" ret_expr +declare_descruct_list_var: declare_var_type "[" (destruct_list_items [","])* "]" "=" ret_expr terminator +destruct_list_items: destruct_list_item | destruct_list_rest +destruct_list_rest: "..." id +destruct_list_item: id // Define Objects dict: "{" [dict_items] "}" dict_items: (dict_item [","] )+ dict_item: (id | num | str) ":" ret_expr -> dict_item_default - | id "(" [func_args] ")" func_body -> dict_item_func + | id "(" [func_args] ")" body -> dict_item_func | "..." ret_expr -> dict_item_rest | id -> dict_item_short -descruct_dict: "{" ((id | (id ":" id) | (rest_accessor)) [","])* "}" "=" ret_expr +declare_descruct_dict_var: declare_var_type "{" (descruct_dict_items [","])* "}" "=" ret_expr terminator +descruct_dict_items: (destruct_dict_single_id | destruct_dict_renamed | destruct_dict_rest) +destruct_dict_single_id: id +destruct_dict_renamed: (id | num | str) ":" id +destruct_dict_rest: "..." id // -------------------------------------------------------------------------- @@ -204,13 +215,12 @@ descruct_dict: "{" ((id | (id ":" id) | (rest_accessor)) [","]) // -------------------------------------------------------------------------- export: "export" -declare_var: [export] declare_var_type id "=" ret_expr_with_terminator +declare_var.10: [export] declare_var_type id "=" ret_expr_with_terminator | [export] declare_var_type id "=" function terminator | [export] declare_var_type id "=" arrow_function terminator -declare_var_not_initialized: [export] declare_var_type id terminator -declare_var_descructed: declare_var_type descruct_dict terminator - | declare_var_type descruct_list terminator +declare_var_not_initialized.10: [export] declare_var_type id terminator +declare_var_descructed.10: declare_descruct_dict_var | declare_descruct_list_var // Valid defintions of variables. @@ -251,10 +261,14 @@ accessor: id | "(" await_stmt ")" | accessor ["?"] ("." accessor)+ -> access_dot | accessor ["?"] ("[" bracket_accessor "]")+ -> access_bracket - | function_call -> simple_access + | function_call + | accessor "." "length" -> access_len + | accessor "." "size" -> access_len + | accessor "." "filter" "(" call_args ")" -> access_filter + | accessor "." "map" "(" call_args ")" -> access_map -rest_accessor: "..." id +rest_accessor: "..." id // -------------------------------------------------------------------------- // Reassignment: @@ -262,7 +276,7 @@ rest_accessor: "..." id // lets define a reassingment: -reassign: accessor "=" ret_expr +reassign: accessor "=" ret_expr // -------------------------------------------------------------------------- @@ -277,11 +291,11 @@ reassign: accessor "=" ret_expr // function body; // -------------------------------------------------------------------------- -function: [export] "function" [id] "(" [func_args] ")" func_body -> function - | [export] "async" "function" [id] "(" [func_args] ")" func_body -> async_function +function: [export] "function" [id] "(" [func_args] ")" body -> function + | [export] "async" "function" [id] "(" [func_args] ")" body -> async_function -arrow_function: "(" [func_args] ")" "=>" func_body -> arrow_function - | "async" "(" [func_args] ")" "=>" func_body -> async_arrow_function +arrow_function: "(" [func_args] ")" "=>" body -> arrow_function + | "async" "(" [func_args] ")" "=>" body -> async_arrow_function // Now we have to define the valid arguments: // The function may receives multiple arguments @@ -297,22 +311,6 @@ func_arg: id -> default_func_arg // we can not parse it more or less :( // | dict | list - - -// Define the Function Body: -// This consists of the brackets and the statements in the function - -func_body: "{" [func_statements] "}" -func_statements: func_statement+ -func_statement: statement - - -// And now we define, which elements are allowed to be included -// in a function. in our case these are more or less all statements -// Additionally, we have ot make shure, that we are able to -// "return" something. - - // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- @@ -333,21 +331,20 @@ call_arg: ret_expr -> call_arg // -------------------------------------------------------------------------- // Define a For - Statement -for: "for" "(" declare_var_type for_iter_var for_iter_type ret_expr ")" iter_body -> default_for - | "for" "(" declare_var_type "[" (for_iter_var [","])+ "]" for_iter_type ret_expr ")" iter_body -> mutli_for - | "for" "(" declare_var_type id "=" ret_expr ";" ret_expr ";" ret_expr ")" iter_body -> ranged_for +for: "for" "(" declare_var_type id for_iter_type accessor ")" body_or_expr_with_terminator -> default_for + | "for" "(" declare_var_type id "=" ret_expr ";" ret_expr ";" ret_expr ")" body_or_expr_with_terminator -> ranged_for + | "for" "(" declare_var_type "[" (id [","])+ "]" for_iter_type ret_expr ")" body_or_expr_with_terminator -> multi_for for_iter_type: "in" | "of" -for_iter_var: id | dict | list +while_statement: "while" "(" ret_expr ")" body_or_expr_with_terminator -while_statement: "while" "(" ret_expr ")" iter_body +do_while: "do" body "while" "(" ret_expr ")" terminator -do_while: "do" iter_body "while" "(" ret_expr ")" terminator +body: "{" statement* "}" -iter_body: "{" iter_statements "}" | iter_statement -iter_statements: iter_statement* -iter_statement: statement +body_or_expr_with_terminator: body + | ret_expr_with_terminator continue_statement: "continue" break_statement: "break" @@ -358,39 +355,37 @@ break_statement: "break" // Define a If - Statement // We have to consider "if" "else if" and "else" -if_statement: "if" "(" ret_expr ")" if_body [else_if_statements] [else_statement] +if_statement: "if" "(" ret_expr ")" body_or_expr_with_terminator [else_if_statements] [else_statement] else_if_statements: else_if_statement+ -else_if_statement: "else" "if" "(" ret_expr ")" if_body -else_statement: "else" if_body +else_if_statement: "else" "if" "(" ret_expr ")" body_or_expr_with_terminator +else_statement: "else" body_or_expr_with_terminator - -if_body: "{" statement* "}" - | ret_expr_with_terminator -> if_body_single - -inline_if: ret_expr "?" ret_expr ":" ret_expr +inline_if: ret_expr "?" ret_expr ":" ret_expr // -------------------------------------------------------------------------- // switch-case // -------------------------------------------------------------------------- switch: "switch" "(" ret_expr ")" switch_body -switch_body: "{" ((switch_case)* [switch_default])* "}" -switch_case: "case" ret_expr ":" [switch_case_body] -switch_default: "default" ":" [switch_case_body] +switch_body: "{" (switch_case* [switch_default])* "}" +switch_case: "case" ret_expr ":" switch_case_body +switch_default: "default" ":" switch_case_body -switch_case_body: (("{" switch_case_statements "}") | switch_case_statements) break_statement +switch_case_body: ("{" switch_case_statements "}") + | switch_case_statements -switch_case_statements: switch_case_statement* -switch_case_statement: statement - +switch_case_statements: statement* switch_case_body_end + +switch_case_body_end: break_statement terminator + | return_statement terminator + | throw_statement terminator + | throw_error_statement terminator // -------------------------------------------------------------------------- // Error Handling // -------------------------------------------------------------------------- -try_catch: "try" try_catch_body "catch" "(" id ")" try_catch_body ["finally" try_catch_body] - -try_catch_body: "{" statement* "}" +try_catch: "try" body "catch" "(" id ")" body ["finally" body] throw_statement: "throw" ret_expr throw_error_statement: "throw" "Error" "(" ret_expr ")" @@ -399,9 +394,9 @@ throw_error_statement: "throw" "Error" "(" ret_expr ")" // -------------------------------------------------------------------------- -class: [export] "class" id ["extends" id] class_body -decorated_class: "@" function_call class - | /let \w+ = (?=class)/ class terminator // Version compiled by tsc +class_statement: [export] "class" id ["extends" id] class_body +decorated_class_statement: "@" function_call class_statement + | /let \w+ = (?=class)/ class_statement terminator // Version compiled by tsc class_body: "{" class_declarations* "}" @@ -413,18 +408,15 @@ class_declarations: constructor | decorated_method | decorated_async_method -constructor: "constructor" "(" constructor_args? ")" func_body -constructor_args: constructor_arg ("," constructor_arg)* -constructor_arg: ["@" function_call] func_arg +constructor: "constructor" "(" [func_args] ")" body +getter: "get" id "(" ")" body +setter: "set" id "(" func_arg ")" body -getter: "get" id "(" ")" func_body -setter: "set" id "(" func_arg ")" func_body - -method: id "(" [func_args] ")" func_body -async_method: "async" id "(" [func_args] ")" func_body +method: id "(" [func_args] ")" body +async_method: "async" id "(" [func_args] ")" body decorated_method: ("@" function_call) method decorated_async_method: ("@" function_call) async_method // Types for a new Class -new_class: "new" id "(" [call_args] ")" \ No newline at end of file +new_class: "new" ret_expr "(" [call_args] ")" \ No newline at end of file diff --git a/py-helpers/prepare_code/js/transformer.py b/py-helpers/prepare_code/js/transformer.py index 577a0be..d94e735 100644 --- a/py-helpers/prepare_code/js/transformer.py +++ b/py-helpers/prepare_code/js/transformer.py @@ -3,35 +3,240 @@ import ast import astor import logging +# from prepare_code import get_logger from ..logger import get_logger from lark import Transformer -def to_snake_case(str): + +def to_snake_case(str): + if str == str.upper(): + return str return ''.join(['_'+i.lower() if i.isupper() - else i for i in str]).lstrip('_') + else i for i in str]).lstrip('_') class CodeTransformeJs(Transformer): - def __init__(self, visit_tokens: bool = True, level=logging.INFO) -> None: + def __init__(self, visit_tokens: bool = True, level=logging.INFO, to_snake_case=False, switch_case_to_if_else=True, **kwargs) -> None: super().__init__(visit_tokens) - self.logger = get_logger("DebugWrapper", level) + self.to_snake_case = to_snake_case + self.switch_case_to_if_else = switch_case_to_if_else + + self._logger = get_logger("DebugWrapper", level) self._callback_counter = 0 self._anonymous_func_to_ids = dict() self._ids_to_anonymous_func = dict() - self._to_snake_case = False - # The Key is allways a ast.Node (the Parent Node.) # The Value is a Set, containing sub-nodes, self._anonymous_func_tree = dict() + self._skip = [ + "terminator", + "export", + "declare_var_type", + "for_iter_type", + "skip" + ] + self._first = [ + "ret_expr", + "ret_expr_with_terminator", + "statement", + "sum", + "bool", + "product", + "boolean_input", + "atom", + "list_item", + "bracket_accessor", + "accessor", + "func_statement", + "func_arg", + "call_arg", + "iter_statement", + "else_statement", + "switch_case_statement", + "switch_case_body_end", + "class_declarations", + "for_iter_var", + "descruct_dict_items", + "destruct_list_items" + ] + self._all = [ + "import_names", + "list_items", + "dict_items", + "func_statements", + "call_args", + "iter_statements", + "else_if_statements", + "else_if_statement", + "switch_body", + "switch_case_statements", + "class_body" + ] + self._contains_body = [ + "for", + "default_for", + "ranged_for", + "multi_for", + "while_statement", + "body", + "if_statement", + "switch", + "try_catch", + "class_statement", + # "method", + # "async_method", + "start" + ] + self._original = [ + "to_snake_case", + "switch_case_to_if_else", + "_logger", + "_callback_counter", + "_anonymous_func_to_ids", + "_ids_to_anonymous_func", + "_anonymous_func_tree", + "_get_func_name", + "_get_name", + "_adapt_items", + "_add_to_tree", + "_add_to_tree_single", + "_adapt_body", + "_log", + "_log_extracted", + "_skip", + "_default", + "_original", + "_contains_body", + "transform", + "start" + ] + + def __getattribute__(self, __name: str): + + own_attribute = list(__class__.__dict__.keys()) + + logger = object.__getattribute__(self, "_logger") + _original = object.__getattribute__(self, "_original") + _skip = object.__getattribute__(self, "_skip") + _first = object.__getattribute__(self, "_first") + _all = object.__getattribute__(self, "_all") + + if __name in _original: + return object.__getattribute__(self, __name) + + elif __name in _skip: + + logger.debug(f"getting skipping function for '{__name}'") + + def ret(items): + self._logger.debug(f"skipping '{__name}'") + self._log(__name, items) + return + + return ret + + elif __name in _first: + + logger.debug(f"getting first function for '{__name}'") + + def ret(items): + + self._log(__name, items) + + try: + self._logger.debug( + f"calling '{__name}' resulted in result={ast.dump(items[0])}") + except: + self._logger.debug( + f"calling '{__name}' resulted in result={items[0]}") + + return items[0] + + return ret + + elif __name in _all: + + logger.debug(f"getting all function for '{__name}'") + + def ret(items): + self._log(__name, items) + self._logger.debug(f"all for '{__name}'") + return items + + return ret + + elif __name not in own_attribute: + + # logger.debug(f"a unkown function named '{__name}' has been requested. Using Super element") + return object.__getattribute__(self, __name) + + else: + attr = object.__getattribute__(self, __name) + + logger.debug(f"using custom function for '{__name}'") + + if callable(attr): + + _contains_body = object.__getattribute__( + self, "_contains_body") + + def ret(*args, **kwargs): + """ A Custom Function, which is used to + register the function if possible. + """ + try: + items = args[0] + + if __name not in _contains_body: + items = self._adapt_items(args[0]) + + self._log(__name, items) + + # Call the original Function + result = attr(items) + + try: + self._logger.debug( + f"calling '{__name}' resulted in result={ast.dump(result)}") + except: + self._logger.debug( + f"calling '{__name}' resulted in result={result}") + + try: + if __name not in _contains_body and type(result) not in (list, tuple, dict): + # Now register some callbacks: + self._add_to_tree(result, items) + except: + pass + + return result + + except Exception as err: + logger.debug( + f"failed calling: '{__name}'. reason: {err}") + result = attr(*args, **kwargs) + + return result + + return ret + + return attr + def _get_func_name(self): name = f"callback_{self._callback_counter}" self._callback_counter += 1 return name + def _get_name(self, str): + if self.to_snake_case: + return to_snake_case(str) + else: + return str + def _adapt_items(self, items): if type(items) is list: @@ -51,25 +256,42 @@ class CodeTransformeJs(Transformer): else: return items - def _add_to_tree(self, parent, items): + def _add_to_tree(self, ret, items): """ Create the required Tree. - Parent = Node + ret = Node Childs = """ - if type(items) is list: + to_check = [] + if type(items) is list: for item in items: - self._add_to_tree_single(parent, item) + if type(item) is list: + to_check += item + else: + to_check.append(item) else: - self._add_to_tree_single(parent, items) + to_check.append(items) + + for item in to_check: + try: + self._add_to_tree_single(ret, item) + except: + pass def _add_to_tree_single(self, parent, item): - if item in self._ids_to_anonymous_func: + id = item + + try: + id = item.name + except: + pass + + if id in self._ids_to_anonymous_func: if parent not in self._anonymous_func_tree: self._anonymous_func_tree[parent] = set() self._anonymous_func_tree[parent].add( - self._ids_to_anonymous_func[item] + self._ids_to_anonymous_func[id] ) elif item in self._anonymous_func_tree: if parent not in self._anonymous_func_tree: @@ -94,9 +316,9 @@ class CodeTransformeJs(Transformer): return body - def log(self, name, items): + def _log(self, name, items): try: - self.logger.debug( + self._logger.debug( f"calling -> '{name}' -> type(items) = {type(items)}; len(items) = {len(items)}") for idx, item in enumerate(items): @@ -104,111 +326,73 @@ class CodeTransformeJs(Transformer): if type(item) is list: for _idx, _item in enumerate(item): try: - self.logger.debug(f"\t\titem[{idx}]->{_idx}: {ast.dump(_item)}") + self._logger.debug( + f"\t\titem[{idx}]->{_idx}: {ast.dump(_item)}") except: - self.logger.debug(f"\t\titem[{idx}]->{_idx}: {_item}") + self._logger.debug( + f"\t\titem[{idx}]->{_idx}: {_item}") else: - self.logger.debug(f"\t\titem[{idx}]: {ast.dump(item)}") + self._logger.debug( + f"\t\titem[{idx}]: {ast.dump(item)}") except: - self.logger.debug( + self._logger.debug( f"\t\titem[{idx}]: {item}") except: - self.logger.debug( + self._logger.debug( f"calling -> '{name}' -> type(items) = {type(items)}") - def log_extracted(self, name, **args): + def _log_extracted(self, name, **args): try: - self.logger.debug(f"calling -> '{name}' -> args = {dict(**args)}") + self._logger.debug(f"calling -> '{name}' -> args = {dict(**args)}") except: pass + def start(self, items): - self.log("start", items) - - body = self._adapt_body(items) - ret = _ast.Module(body=body) - - self._add_to_tree(ret, body) - - return ret - - def ret_expr(self, items): - self.log("ret_expr", items) - return items[0] - - def ret_expr_with_terminator(self, items): - self.log("ret_expr_with_terminator", items) - ret_expr, terminator = items - self.log_extracted("ret_expr_with_terminator", - ret_expr=ret_expr, terminator=terminator) - return ret_expr + self._log("start", items) + body = self.body(items) + return _ast.Module(body=body) def return_statement(self, items): - self.log("ret_expr_with_terminator", items) - - (ret_expr, ) = items - - self.log_extracted("return_statement", ret_expr=ret_expr) - - value = self._adapt_items(ret_expr) - - ret = _ast.Return(value=value) - - self._add_to_tree(ret, value) - - return ret - - def statement(self, items): - return items[0] - - def terminator(self, items): - """ We want to skip the terminators. - """ - self.log("terminator", items) - return + i = items[0] + return _ast.Return(value=items[0]) def identifier(self, items): - self.log("id", items) - - if self._to_snake_case: - return _ast.Name(id = to_snake_case(items[0].value)) - - return _ast.Name(id=items[0].value) + self._log("id", items) + return _ast.Name(id=self._get_name(items[0].value)) def import_stmt_all(self, items): - self.log("import_stmt_all", items) + self._log("import_stmt_all", items) module, terminator = items - self.log_extracted("import_stmt_all", module=module, - terminator=terminator) + self._log_extracted("import_stmt_all", module=module, + terminator=terminator) return _ast.Import(name=_ast.alias(name=module.value)) def import_stmt_id(self, items): - self.log("import_stmt_id", items) + self._log("import_stmt_id", items) return items def import_stmt_as(self, items): - self.log("import_stmt_as", items) + self._log("import_stmt_as", items) (identifier, module, __) = items + names = [] if module.value == identifier: names = [_ast.alias(name=module.value, asname=None)] else: names = [_ast.alias(name=module.value, asname=identifier)] - self.log_extracted("import_stmt_all", - identifier=identifier, module=module) + self._log_extracted("import_stmt_as", + identifier=identifier, module=module) return _ast.Import(names=names) def import_stmt_from(self, items): - self.log("import_stmt_from", items) + self._log("import_stmt_from", items) (import_names, _, __) = items - self.log_extracted("import_stmt_from", import_names=import_names) + self._log_extracted("import_stmt_from", import_names=import_names) # TODO: determine the level properly return _ast.ImportFrom(module=_.value.replace('/', '.'), names=import_names, level=0) - def import_names(self, items): - return items - def import_name(self, items): (items,) = items return _ast.alias(name=items, asname=None) @@ -219,7 +403,7 @@ class CodeTransformeJs(Transformer): def str(self, items): - self.log("str", items) + self._log("str", items) (items,) = items items = items[1:-1] return _ast.Constant(value=items) @@ -230,20 +414,24 @@ class CodeTransformeJs(Transformer): return _ast.Constant(value=items) def num(self, items): - self.log("num", items) + self._log("num", items) dig_01 = items[0].value dig_02 = items[1].value if items[1] is not None else 0 value = float(str(f"{dig_01}.{dig_02}")) + if items[1] is None: + value = int(dig_01) + return _ast.Constant(value=value) - def bool(self, items): - if items.value == "false": - return _ast.Constant(value=False) + def bool_true(self, items): return _ast.Constant(value=True) + def bool_false(self, items): + return _ast.Constant(value=False) + def null(self, items): return _ast.Constant(value=None) @@ -257,14 +445,16 @@ class CodeTransformeJs(Transformer): return self.assigned_sub([items[0], _ast.Constant(value=1)]) def invert(self, items): - return _ast.UnaryOp( + ret = _ast.UnaryOp( op=_ast.Not(), operand=items[0] ) + return ret + def istanceof(self, items): return _ast.Call( - func=_ast.Name(id='isinstance', ctx=ast.Load()), + func=_ast.Name(id='isinstance'), args=[ items[0], items[1] @@ -274,7 +464,7 @@ class CodeTransformeJs(Transformer): def typeof(self, items): return _ast.Call( - func=_ast.Name(id='type', ctx=ast.Load()), + func=_ast.Name(id='type'), args=[ items[0] ], @@ -283,7 +473,7 @@ class CodeTransformeJs(Transformer): def instanceof(self, items): return _ast.Call( - func=_ast.Name(id='type', ctx=ast.Load()), + func=_ast.Name(id='type'), args=[ items[0] ], @@ -304,21 +494,12 @@ class CodeTransformeJs(Transformer): def reg_ex(self, items): return _ast.Call( - func=_ast.Name(id='re.compile', ctx=ast.Load()), + func=_ast.Name(id='re.compile'), args=[ - items[0] + _ast.Constant(value=items[0].value) ], keywords=[] ) - - def atom(self, items): - return items[0] - - def sum(self, items): - return items[0] - - def product(self, items): - return items[0] def add(self, items): return self._op(items, _ast.Add()) @@ -333,7 +514,7 @@ class CodeTransformeJs(Transformer): return self._op(items, _ast.Div()) def _op(self, items, op): - self.log("assigned_add", items) + self._log("assigned_add", items) return _ast.BinOp( left=items[0], @@ -342,7 +523,7 @@ class CodeTransformeJs(Transformer): ) def _assigned_op(self, items, op): - self.log("assigned_add", items) + self._log("assigned_add", items) return self.reassign([ items[0], @@ -362,9 +543,9 @@ class CodeTransformeJs(Transformer): return self._assigned_op(items, _ast.Div()) def boolean_operation(self, items): - self.log("boolean_operation", items) + self._log("boolean_operation", items) - return _ast.Compare( + ret = _ast.Compare( left=items[0], ops=[ items[1] @@ -374,6 +555,8 @@ class CodeTransformeJs(Transformer): ] ) + return ret + def bool_op_gt(self, items): return _ast.Gt() @@ -401,9 +584,6 @@ class CodeTransformeJs(Transformer): def bool_op_in(self, items): return _ast.In() - def boolean_input(self, items): - return items[0] - def list(self, items): if items[0] is not None: @@ -414,8 +594,6 @@ class CodeTransformeJs(Transformer): ctx=ast.Load() ) - self._add_to_tree(ret, _items) - return ret return _ast.List( @@ -423,20 +601,15 @@ class CodeTransformeJs(Transformer): ctx=ast.Load() ) - def list_items(self, items): - self.log("list_items", items) - return items - - def list_item(self, items): - self.log("list_item", items) - return items[0] - def list_item_rest(self, items): - self.log("list_item_rest", items) - return _ast.Starred( + self._log("list_item_rest", items) + + ret = _ast.Starred( value=items[0] ) + return ret + def descruct_list(self, items): ret = [] @@ -461,8 +634,8 @@ class CodeTransformeJs(Transformer): keys = [] values = [] - for item in items: - if item is not None: + if items[0]: + for item in items[0]: keys += item["keys"] values += item["values"] @@ -472,25 +645,28 @@ class CodeTransformeJs(Transformer): ctx=ast.Load() ) - self._add_to_tree(ret, values) - return ret - def dict_items(self, items): - self.log("dict_items", items) - return items[0] - def dict_item_default(self, items): + + id = "" + if type(items[0]) is _ast.Name: + id = items[0].id + elif type(items[0]) is _ast.Constant: + id = items[0].value + else: + raise Exception("unkown type handled... :(") + return { - "keys": [items[0]], + "keys": [_ast.Constant(value=id)], "values": [items[1]] } def dict_item_func(self, items): # TODO: How to Handle. return { - "keys": [items[0].value], - "values": [] + "keys": [_ast.Constant(value=items[0].id)], + "values": [items[0]] } def dict_item_rest(self, items): @@ -505,20 +681,159 @@ class CodeTransformeJs(Transformer): "values": [items[0]] } - def descruct_dict(self, items): - # TODO: Impement - return _ast.Constant(value=f"DICT_DESCRUTION from dict '{items[1].value}'") + def declare_descruct_list_var(self, items): + # Rule = declare_var_type "[" (id [","])* [","] rest_accessor "]" "=" ret_expr terminator - def export(self, items): - return + source = items[-2] + + # Define a new name + id_of_copy = _ast.Name(id=self._get_name(f"tmp_cp")) + + ret = [] + + # We iterate over the items. + for [idx, item] in enumerate(items[1:-2]): + if item is None: + continue + elif item.get("rest", False): + # Our rest item should allways be + ret.insert( + len(ret), + self.reassign( + [ + item.get("rest"), + id_of_copy + ] + ) + ) + else: + ret.insert( + idx, + self.reassign( + [ + item.get("assign_name"), + self.function_call( + [ + self.access_dot([ + id_of_copy, + _ast.Name(id="pop") + ]), + [ + _ast.Constant(value=idx) + ] + ] + ) + ] + ) + ) + + ret.insert(0, + # Firstly, we want to copy our element. + self.reassign([ + id_of_copy, + self.function_call([ + _ast.Name(id="deepcopy"), + [ + source + ] + ]) + ]) + ) + + return ret + + def destruct_list_rest(self, items): + return self.destruct_dict_rest(items) + + def destruct_list_item(self, items): + return { + "assign_name": items[0] + } + + def declare_descruct_dict_var(self, items): + # TODO: Impement + _ = items[0] + source = items[-2] + # Define a new name + id_of_copy = _ast.Name(id=self._get_name(f"tmp_cp")) + + ret = [] + + # We iterate over the items. + for item in items[1:-2]: + if item is None: + continue + elif item.get("rest", False): + # Our rest item should allways be + ret.insert( + len(ret), + self.reassign( + [ + item.get("rest"), + id_of_copy + ] + ) + ) + else: + ret.insert( + -1, + self.reassign( + [ + item.get("assign_name"), + self.function_call( + [ + self.access_dot([ + id_of_copy, + _ast.Name(id="pop") + ]), + [ + item.get("extract_name") + ] + ] + ) + ] + ) + ) + + ret.insert(0, + # Firstly, we want to copy our element. + self.reassign([ + id_of_copy, + self.function_call([ + _ast.Name(id="deepcopy"), + [ + source + ] + ]) + ]) + ) + + return ret + + def destruct_dict_single_id(self, items): + return { + "extract_name": items[0], + "assign_name": items[0] + } + + def destruct_dict_renamed(self, items): + return { + "extract_name": items[0], + "assign_name": items[1] + } + + def destruct_dict_rest(self, items): + return { + "rest": items[0] + } def declare_var(self, items): (_, __, id, value) = items - self.log("declare_var", items) + self._log("declare_var", items) try: - self.logger.debug( + self._logger.debug( f"declare_var: id={ast.dump(id)}; value={ast.dump(value)}") except: pass @@ -530,47 +845,83 @@ class CodeTransformeJs(Transformer): value=_value ) - self._add_to_tree(ret, _value) - return ret def declare_var_not_initialized(self, items): return _ast.Assign( targets=[items[0]], - value=_ast.Assign(value=None) + value=_ast.Constant(value=None) ) def declare_var_descructed(self, items): # Return only the desturcted stuff - return items[1] - - def bracket_accessor(self, items): - return items[0] - - def accessor(self, items): return items[0] def var_based_access(self, items): - return _ast.Name(id=items[0], ctx=ast.Load()) + return _ast.Name(id=items[0]) - def simple_access(self, items): - return items[0] + def access_len(self, items): + """ Convert item.size / item.length to len(...) + """ + + return _ast.Call( + func=_ast.Name(id='len'), + args=[ + items[0], + ], + keywords=[] + ) + + def access_filter(self, items): + """ Convert item.size / item.length to len(...) + """ + + return self.function_call( + [ + _ast.Name(id='filter'), + [ + items[1][0], + items[0] + ] + + ] + ) + + def access_map(self, items): + """ Convert item.size / item.length to len(...) + """ + + return self.function_call( + [ + _ast.Name(id='map'), + [ + items[1][0], + items[0] + ] + + ] + ) def access_dot(self, items): if type(items[1]) is _ast.Name: - return _ast.Attribute( + + ret = _ast.Attribute( value=items[0], attr=items[1].id, ctx=ast.Load() ) - return _ast.Attribute( + return ret + + ret = _ast.Attribute( value=items[0], attr=items[1], ctx=ast.Load() ) + return ret + def access_bracket(self, items): return _ast.Subscript( value=items[0], @@ -579,7 +930,7 @@ class CodeTransformeJs(Transformer): def rest_accessor(self, items): return _ast.Call( - func=_ast.Name(id='deepcopy', ctx=ast.Load()), + func=_ast.Name(id='deepcopy'), args=[ items[0], ], @@ -590,14 +941,29 @@ class CodeTransformeJs(Transformer): return self.declare_var([None, None, items[0], items[1]]) def _function(self, items, type=None): + """ Uses Rule = [export] "function" [id] "(" [func_args] ")" func_body + + Args: + items (tuple|list): The items during tree tranversion. Len must be 4 + type (str, optional): Must be 'async' for an async function / method. Defaults to None. + + Returns: + _type_: _description_ + """ _constructor = _ast.AsyncFunctionDef if type == "async" else _ast.FunctionDef (export, name, func_args, body) = items - func_args = func_args if func_args is not None else _ast.arguments(args=[], defaults=[]) + if name is None: + # TODO: Get name for callback + name = _ast.Name(id=self._get_func_name()) - self.logger.debug(f"_function: name={name}, func_args={func_args}, body={body}") + func_args = func_args if func_args is not None else _ast.arguments( + args=[], defaults=[]) + + self._logger.debug( + f"_function: name={name}, func_args={func_args}, body={body}") _body = self._adapt_body(body) @@ -612,42 +978,37 @@ class CodeTransformeJs(Transformer): # Register the Function. self._anonymous_func_to_ids[ret] = name - self._ids_to_anonymous_func[name] = ret + self._ids_to_anonymous_func[name.id] = ret # self._anonymous_func_tree[ret] = name - self._add_to_tree(ret, _body) - return ret def function(self, items): - self.log("function", items) + self._log("function", items) return self._function(items) def async_function(self, items): - self.log("async_function", items) + self._log("async_function", items) return self._function(items, "async") def _arrow_function(self, items, type=None): - self.log("_arrow_function", items) - - # TODO: Get name for callback - name = _ast.Name(id=self._get_func_name()) - return self._function([None, name, *items], type) + self._log("_arrow_function", items) + return self._function([None, None, *items], type) def arrow_function(self, items): - self.log("arrow_function", items) + self._log("arrow_function", items) return self._arrow_function(items) def arrow_function_one_arg(self, items): - self.log("arrow_function", items) + self._log("arrow_function", items) return self._arrow_function(items) def async_arrow_function(self, items): - self.log("async_arrow_function", items) + self._log("async_arrow_function", items) return self._arrow_function(items, "async") @@ -673,12 +1034,9 @@ class CodeTransformeJs(Transformer): return _ast.arguments(**ret) - def func_arg(self, items): - return items - def default_func_arg(self, items): - self.log("default_func_arg", items) + self._log("default_func_arg", items) # TODO: Implement return { "args": [_ast.arg(arg=items[0].id)], @@ -687,7 +1045,7 @@ class CodeTransformeJs(Transformer): } def rest_func_arg(self, items): - self.log("rest_func_arg", items) + self._log("rest_func_arg", items) return { "vararg": [_ast.arg(arg=items[0].id)], "args": [], @@ -696,92 +1054,149 @@ class CodeTransformeJs(Transformer): def assigend_func_arg(self, items): - self.log("assigend_func_arg", items) + self._log("assigend_func_arg", items) return { "args": [_ast.arg(arg=items[0].id)], "defaults": [items[1]], "vararg": [] } - def implicit_or_typed(self, items): - return items[0] - - def func_body(self, items): - + def body_or_expr_with_terminator(self, items): ret = [] - elements = items[0] - self.log("func_body", elements) - - for element in elements: - if type(element) is list: - ret += element - else: - ret.append(element) + if type(items[0]) is list: + ret += items[0] + else: + ret.append(items[0]) return ret - def func_statements(self, items): - return items + def body(self, items): + ret = [] - def func_statement(self, items): - return items + self._log("func_body", items) + + for item in items: + if item is not None: + if type(item) is list: + ret += item + else: + ret.append(item) + try: + ret = self._adapt_body(ret) + except: + pass + + return ret def function_call(self, items): + """ Uses Rule = accessor "(" [call_args] ")" - self.log("function_call", items) + Args: + items (_type_): _description_ + + Returns: + _type_: _description_ + """ + + self._log("function_call", items) id = items[0] args = items[1] if items[1] is not None else [] - # Adapt the ID: - if type(id) is _ast.Name: - id = id - args = self._adapt_items(args) ret = _ast.Call( - func = id, - args = args, + func=id, + args=args, keywords=[] ) - self._add_to_tree(ret, args) - return ret - def call_args(self, items): - self.log("call_args", items) - return items - - def call_arg(self, items): - self.log("call_arg", items) - return items[0] - def rest_call_arg(self, items): return _ast.Starred( value=items[0] ) def default_for(self, items): - pass + # Rule = "for" "(" declare_var_type id for_iter_type ret_expr ")" body_or_expr_with_terminator + (_, id, __, expr, body) = items + return _ast.For( + target=id, + iter=expr, + body=body, + orelse=[] + ) def multi_for(self, items): - pass + + names = items[1:-3] + source = items[-2] + body = items[-1] + + # Define a new name + iter_item = _ast.Name(id=self._get_name(f"iter_item")) + + # We extend the body. + for idx, name in enumerate(names): + body.insert( + idx, + self.reassign( + [ + name, + self.access_bracket( + [ + iter_item, + _ast.Constant(value=idx) + ] + ) + ] + ) + ) + + # Now we will extract our Loop + return self.default_for( + [ + None, + iter_item, + None, + source, + body + ] + ) def ranged_for(self, items): - pass + """ We will convert a for (i, ...) to a while operation. + Therefore we use the initial assignment, before we + create the while loop. Inside of the loop, we will + add the "loop"-operation. It will be executed allways. + - def for_iter_type(self, items): - pass + Args: + items: The Tree, which is used to create the wole statement. + """ + # Rule = "for" "(" declare_var_type id "=" ret_expr ";" ret_expr ";" ret_expr ")" body_or_expr_with_terminator - def for_iter_var(self, items): - pass + (_, name, init_value, comp, loop_operation, body) = items + + # We have to insert our loop_operation at the end: + body.insert(-1,loop_operation) + + ret = [ + self.reassign([name, init_value]), + self.while_statement([ + comp, + body + ]) + ] + + return ret def while_statement(self, items): - self.log("while_statement", items) - self.logger.debug(items[0]) - self.logger.debug(items[1]) + self._log("while_statement", items) + self._logger.debug(items[0]) + self._logger.debug(items[1]) return _ast.While( test=items[0], body=self._adapt_body( @@ -790,20 +1205,6 @@ class CodeTransformeJs(Transformer): orelse=[] ) - def iter_body(self, items): - self.log("iter_body", items) - - if items[0] is None: - return [] - - return items[0] - - def iter_statements(self, items): - return items - - def iter_statement(self, items): - return items[0] - def continue_statement(self, items): return _ast.Continue() @@ -853,21 +1254,6 @@ class CodeTransformeJs(Transformer): orelse=else_body ) - def else_if_statements(self, items): - return items - - def else_if_statement(self, items): - return items - - def else_statement(self, items): - return items[0] - - def if_body(self, items): - return items - - def if_body_single(self, items): - return items - def inline_if(self, items): _items = self._adapt_items(items) @@ -878,8 +1264,6 @@ class CodeTransformeJs(Transformer): orelse=_items[2] ) - self._add_to_tree(ret, _items) - return ret def new_class(self, items): @@ -888,23 +1272,79 @@ class CodeTransformeJs(Transformer): return _ast.Call(func=_ast.Name(id=identifier), args=args, keywords=[]) def switch(self, items): - raise Exception("!!SWITCH_CASE!!") - return _ast.Match() - def switch_body(self, items): - return _ast.Break() + subject = items[0] + + if self.switch_case_to_if_else: + + else_body = None + elifs = [] + + for item in items[1]: + if item is None: + continue + if item.get("else", False): + else_body = item.get("body") + else: + right = item.get("condition") + left = subject + + _test = self.boolean_operation([left, _ast.Eq(), right]) + _body = item.get("body") + + elifs.append((_test, _body)) + + if len(elifs) == 0: + raise Exception("Can not convert switch case.") + + test, body = elifs.pop(0) + + if len(elifs) == 0: + elifs = None + + return self.if_statement((test, body, elifs, else_body)) + + return _ast.Match( + subject=items[0], + cases=items[1] + ) def switch_case(self, items): - return _ast.Break() + + body = self._adapt_body( + items[1] + ) + + if self.switch_case_to_if_else: + return { + "body": body, + "condition": items[0] + } + else: + + ret = _ast.match_case( + pattern=_ast.MatchValue(value=items[0]), + body=body + ) + + return ret def switch_default(self, items): - return _ast.Break() - def switch_case_statements(self, items): - return _ast.Break() + body = self._adapt_body( + items[0] + ) - def switch_case_statement(self, items): - return _ast.Break() + if self.switch_case_to_if_else: + return { + "body": body, + "else": True + } + + return self.switch_case([_ast.Name(id="_"), body]) + + def switch_case_body(self, items): + return items[0] def try_catch(self, items): @@ -912,31 +1352,25 @@ class CodeTransformeJs(Transformer): _try_body = self._adapt_body(try_body) _catch_body = self._adapt_body(catch_body) - _finally_body = self._adapt_body(finally_body if finally_body is not None else []) + _finally_body = self._adapt_body( + finally_body if finally_body is not None else []) ret = _ast.Try( - body = _try_body, - handlers = [ + body=_try_body, + handlers=[ _ast.ExceptHandler( - type =_ast.Name(id='Exception'), - name = err_id.id, - body = _catch_body, + type=_ast.Name(id='Exception'), + name=err_id.id, + body=_catch_body, ) ], - orelse = [], - finalbody = _finally_body + orelse=[], + finalbody=_finally_body ) - self._add_to_tree(ret, _try_body + _catch_body + _finally_body) - - - return ret - def try_catch_body(self, items): - return items - def throw_statement(self, items): return _ast.Raise( exc=items[0] @@ -947,50 +1381,103 @@ class CodeTransformeJs(Transformer): exc=items[0] ) + def class_statement(self, items): + (_, name, base, body) = items -class DebuggedCodeTransformeJs(Transformer): - def __init__(self, visit_tokens: bool = True) -> None: - super().__init__(visit_tokens) + bases = [] - self.logger = get_logger("DebugWrapper") + if base is not None: + bases = [base] + + return _ast.ClassDef(name=name.id, body=body, + decorator_list=[], bases=bases) - def __getattribute__(self, __name: str): - logger = object.__getattribute__(self, "logger") + def decorated_class_statement(self, items): + return class_statement(self, items[0]) - try: - func = object.__getattribute__(self, __name) + def constructor(self, items): + # Rule = "constructor" "(" [func_args] ")" func_body + # -> args = _ast.arguments + (args, body) = items - if callable(func): + # Add the Self Symbol + args = args if args is not None else _ast.arguments( + args=[], defaults=[]) + args.args.insert(0, _ast.arg(arg='self', annotation=None)) - def cb(items): - logger.info(f"Calling function '{__name}'") - try: - logger.info(f"received parameters => {len(items)}") - except: - pass - return func(items) + return _ast.FunctionDef(name='__init__', args=args, body=body, decorator_list=[]) - return cb + def getter(self, items): + # Rule = id "(" [func_args] ")" func_body - return func - except: - pass + # Adapt the ID to "get_{id}" + id = items[0] + body = items[1] - logger.warn(f"'{__name}' has not been found!") - raise KeyError(f"'{__name}' has not been found!") + ret = self.method([id, None, body]) + ret.decorator_list.append( + self.function_call([ + _ast.Name(id="property"), + None + ]) + ) + + return ret + + def setter(self, items): + # Rule = "set" id "(" func_arg ")" func_body + id = items[0] + args = items[1]["args"] + body = items[2] + + ret = self.method([id, _ast.arguments(args=args, defaults=[]), body]) + ret.decorator_list.append( + self.function_call([ + _ast.Name(id=id.id+".setter"), + None + ]) + ) + + return ret + + def method(self, items): + # Rule = id "(" [func_args] ")" func_body + ret = self._function([None, *items]) + + method_args = [_ast.arg(arg='self', annotation=None)] + method_args.extend(ret.args.args) + ret.args.args = method_args + + return ret + + def async_method(self, items): + # Rule = "async" id "(" [func_args] ")" func_body + ret = self._function([None, *items], "async") + + method_args = [_ast.arg(arg='self', annotation=None)] + method_args.extend(ret.args.args) + ret.args.args = method_args + + return ret def transform(tree, debug): transformer = CodeTransformeJs( True, logging.DEBUG if debug else logging.INFO) - + program = transformer.transform(tree) if debug: print(ast.dump(program, indent=2)) + l = 80 + + print("\n"*2) + print("-"*l) + print("| Code Below :" + " "*(l-15) + "|") + print("-"*l) + print("\n"*2) + code = astor.to_source(program) - if debug: - print(code) return code diff --git a/py-helpers/prepare_code/main.py b/py-helpers/prepare_code/main.py index 3d98146..cc9d0b0 100644 --- a/py-helpers/prepare_code/main.py +++ b/py-helpers/prepare_code/main.py @@ -1,5 +1,4 @@ import argparse -from msilib.schema import Error import os import re import multiprocessing as mp @@ -194,9 +193,9 @@ def main(): typescript_files.append((file_name, path_to_file, dir_path)) else: - raise Error("Failed to load the file") - + raise Exception("Failed to load the file") + typescript_files = sorted(typescript_files,key=lambda item: item[1]) # Define the Destination output_path = os.path.join(os.getcwd(), args.outputFolder) @@ -238,6 +237,12 @@ def main(): else: failed.append((org_file_name, err)) + + if len(success): + print("\n"*2) + print(f"Created the following files ({len(success)}):") + for file_name in success: + print("\t- ", file_name) if len(failed): logger.warn(f"The following files failed ({len(failed)}):") @@ -245,11 +250,7 @@ def main(): print("\t", idx+1, ".\t", file_name) print("\t\t\t->",str(err).split("\n")[0]) - if (args.debug): - print("\n"*2) - print(f"Created the following files ({len(success)}):") - for file_name in success: - print("\t- ", file_name) + print("\n"*2) logger.info(f"Parsed {len(success)} of {len(typescript_files)} files ({(len(success)/len(typescript_files))*100:.2f} %).") diff --git a/py-helpers/prepare_code/post_processor.py b/py-helpers/prepare_code/post_processor.py index 22474e3..6dab253 100644 --- a/py-helpers/prepare_code/post_processor.py +++ b/py-helpers/prepare_code/post_processor.py @@ -7,7 +7,20 @@ replacers = { "true": "True", "false": "False", "JSON.stringify": "json.dumps", - "JSON.parse": "json.loads" + "JSON.parse": "json.loads", + "const _this = this;": "", + "_this": "self", + "this": "self", + "Set": "set", + "Map": "dict", + "toLowerCase": "lower", + "toUpperCase": "upper", + ".push(": ".append(", + "Array.from": "list", + "null": "None", + '"null"': "None", + '"undefined"': "None" + } def post_process(code: str) -> str: diff --git a/tsconfig.py.json b/tsconfig.py.json index 8298913..a794239 100644 --- a/tsconfig.py.json +++ b/tsconfig.py.json @@ -21,7 +21,17 @@ "declaration": true }, "include": [ - "lib", + "lib/communication", + "lib/decorators", + "lib/dispatcher", + "lib/eventEmitter", + "lib/helpers", + "lib/loader", + "lib/logger", + "lib/module", + "lib/observables", + "lib/promise", + "lib/pubSub" ], "exclude": [ "node_modules",