diff --git a/pyproject.toml b/pyproject.toml index 07337e4..0471bc1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ts2py" -version = "0.0.1" +version = "0.0.2" description = "Python-Interoperability for Typescript-Interfaces" license = "Apache-2.0" diff --git a/ts2py/__init__.py b/ts2py/__init__.py index a3c86e3..ad13606 100644 --- a/ts2py/__init__.py +++ b/ts2py/__init__.py @@ -1,3 +1,3 @@ __title__ = "ts2py" -__version__ = "0.0.1" -__description__ = "Python-Interoperability for Typescript-Interfaces" \ No newline at end of file +__version__ = "0.0.2" +__description__ = "Python-Interoperability for Typescript-Interfaces" diff --git a/ts2py/main.py b/ts2py/main.py index 537df82..1a50953 100644 --- a/ts2py/main.py +++ b/ts2py/main.py @@ -20,7 +20,7 @@ """ import os -from typing import List, Tuple, Any +from typing import List, Tuple, Optional, Any import typer from DHParser import ( compile_source, @@ -76,6 +76,7 @@ def process_file(source: str, target: str) -> None: returned in the terminal output. """ if os.path.isfile(target): + Logger().info(f"Target file '{target}' already exists, deleting it...") os.remove(target) result, errors = compile_src(source) if not has_errors(errors, FATAL): @@ -83,6 +84,8 @@ def process_file(source: str, target: str) -> None: results_file.write(serialize_result(result)) if errors: Logger().error("\n".join(canonical_error_strings(errors))) + else: + Logger().success(f"Conversion for file '{source}' completed succesfully") @app.command() @@ -100,10 +103,10 @@ def convert( False, "--verbose", "-v", help="Enable verbose output" ), peps: List[types.args.PepArg] = typer.Option( - ["655"], "--peps", "-p", help="Assume Python-PEPs, e.g. 655" + ["655"], "--pep", "-p", help="Assume Python-PEPs, e.g. 655" ), - decorator: str = typer.Option( - "", "--decorator", "-d", help="Add the given decorator" + decorator: Optional[str] = typer.Option( + None, "--decorator", help="Add the given decorator" ), debug: bool = typer.Option(False, "--debug", "-d", help="Enable debug mode"), ): @@ -132,7 +135,7 @@ def convert( if helper.use_type_union(compatibility): set_preset_value("ts2py.UseTypeUnion", True, allow_new_key=True) # Set decorator - if len(decorator) > 0: + if decorator: set_preset_value("ts2py.ClassDecorator", decorator) # Set debug mode if debug: diff --git a/ts2py/syntax/compiler.py b/ts2py/syntax/compiler.py index 8011c20..e7d8124 100644 --- a/ts2py/syntax/compiler.py +++ b/ts2py/syntax/compiler.py @@ -23,9 +23,31 @@ def source_hash(source_text: str) -> str: return " ".join([md5(source_text), script_hash]) -DEFAULT_IMPORTS = """ -from typing import TypedDict, GenericTypedDict, NotRequired, Literal, Union, Optional, Any, Generic, TypeVar, Callable, Coroutine, List, Tuple, Dict -""" +TYPING_TYPES = [ + "NotRequired", + "Literal", + "Union", + "Optional", + "Any", + "Generic", + "TypeVar", + "Callable", + "Coroutine", + "List", + "Tuple", + "Dict", +] + + +def get_typing_imports(python_code: Any): + initial_import_line = "from typing import" + typing_types_to_add = ["TypedDict"] + for typing_type in TYPING_TYPES: + typing_match = re.search(rf"\b({typing_type})\b", python_code) + if typing_match: + typing_types_to_add.append(typing_type) + typing_types_str = ", ".join(typing_types_to_add) + return f"{initial_import_line} {typing_types_str}" def to_typename(varname: str) -> str: @@ -152,7 +174,7 @@ def prepare(self, root: Node) -> None: def finalize(self, python_code: Any) -> Any: code_blocks = [] if self.tree.name == "document": - code_blocks.append(DEFAULT_IMPORTS) + code_blocks.append(get_typing_imports(python_code)) code_blocks.append(python_code) cooked = "\n\n".join(code_blocks) cooked = re.sub(" +(?=\n)", "", cooked)