From c211f466d08f1b4b828ed2037ade9cff5c6d7c69 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Fri, 27 Sep 2019 02:02:35 +0900 Subject: [PATCH] add debug mode (#65) * add debug mode --- README.md | 5 ++- datamodel_code_generator/__init__.py | 43 ++++++++++++++++++++++ datamodel_code_generator/__main__.py | 9 ++++- datamodel_code_generator/parser/openapi.py | 3 +- setup.cfg | 1 + 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 090072599..f78872a28 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,8 @@ The `datamodel-codegen` command: ``` datamodel-code-generator[10:03:22] usage: datamodel-codegen [-h] [--input INPUT] [--output OUTPUT] [--base-class BASE_CLASS] - [--target-python-version {3.6,3.7}] + [--target-python-version {3.6,3.7}] [--debug] + optional arguments: -h, --help show this help message and exit @@ -62,6 +63,8 @@ optional arguments: Base Class (default: pydantic.BaseModel) --target-python-version {3.6,3.7} target python version (default: 3.7) + --debug show debug message + ``` ## Example diff --git a/datamodel_code_generator/__init__.py b/datamodel_code_generator/__init__.py index 5ed48a79d..500b43d92 100644 --- a/datamodel_code_generator/__init__.py +++ b/datamodel_code_generator/__init__.py @@ -1,7 +1,50 @@ +import inspect from enum import Enum +from typing import Callable, Type, TypeVar + +import pysnooper + +T = TypeVar('T') + +pysnooper.tracer.DISABLED = True + + +def enable_debug_message() -> None: # pragma: no cover + pysnooper.tracer.DISABLED = False class PythonVersion(Enum): PY_36 = '3.6' PY_37 = '3.7' PY_38 = '3.8' + + +def snooper_to_methods( # type: ignore + output=None, + watch=(), + watch_explode=(), + depth=1, + prefix='', + overwrite=False, + thread_info=False, + custom_repr=(), + max_variable_length=100, +) -> Callable: + def inner(cls: Type[T]) -> Type[T]: + methods = inspect.getmembers(cls, predicate=inspect.isfunction) + for name, method in methods: + snooper_method = pysnooper.snoop( + output, + watch, + watch_explode, + depth, + prefix, + overwrite, + thread_info, + custom_repr, + max_variable_length, + )(method) + setattr(cls, name, snooper_method) + return cls + + return inner diff --git a/datamodel_code_generator/__main__.py b/datamodel_code_generator/__main__.py index 3a163755a..ea545e1d0 100755 --- a/datamodel_code_generator/__main__.py +++ b/datamodel_code_generator/__main__.py @@ -12,13 +12,12 @@ from typing import Optional, Sequence, Union import argcomplete -from datamodel_code_generator import PythonVersion +from datamodel_code_generator import PythonVersion, enable_debug_message from datamodel_code_generator.model.pydantic import ( BaseModel, CustomRootType, dump_resolve_reference_action, ) -from datamodel_code_generator.parser.openapi import OpenAPIParser class Exit(IntEnum): @@ -53,6 +52,7 @@ class Exit(IntEnum): choices=['3.6', '3.7'], default='3.7', ) +arg_parser.add_argument('--debug', help='show debug message', action='store_true') def main(args: Optional[Sequence[str]] = None) -> Exit: @@ -66,6 +66,11 @@ def main(args: Optional[Sequence[str]] = None) -> Exit: namespace: Namespace = arg_parser.parse_args(args) + if namespace.debug: # pragma: no cover + enable_debug_message() + + from datamodel_code_generator.parser.openapi import OpenAPIParser + parser = OpenAPIParser( BaseModel, CustomRootType, diff --git a/datamodel_code_generator/parser/openapi.py b/datamodel_code_generator/parser/openapi.py index 5c82df932..26affc096 100644 --- a/datamodel_code_generator/parser/openapi.py +++ b/datamodel_code_generator/parser/openapi.py @@ -1,6 +1,6 @@ from typing import Callable, Dict, List, Optional, Set, Tuple, Type -from datamodel_code_generator import PythonVersion +from datamodel_code_generator import PythonVersion, snooper_to_methods from datamodel_code_generator.format import format_code from datamodel_code_generator.imports import IMPORT_ANNOTATIONS from datamodel_code_generator.model.enum import Enum @@ -17,6 +17,7 @@ from ..model.base import DataModel, DataModelField +@snooper_to_methods(max_variable_length=None) class OpenAPIParser(Parser): def __init__( self, diff --git a/setup.cfg b/setup.cfg index 2d28ed633..8cc91c992 100644 --- a/setup.cfg +++ b/setup.cfg @@ -25,6 +25,7 @@ install_requires = pydantic[email,ujson] == 0.32.0 black == 19.3b0 isort == 4.3.21 + PySnooper == 0.2.8 tests_require = pytest