Skip to content

Commit

Permalink
add debug mode (#65)
Browse files Browse the repository at this point in the history
* add debug mode
  • Loading branch information
koxudaxi authored Sep 26, 2019
1 parent a1533a1 commit c211f46
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions datamodel_code_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions datamodel_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion datamodel_code_generator/parser/openapi.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +17,7 @@
from ..model.base import DataModel, DataModelField


@snooper_to_methods(max_variable_length=None)
class OpenAPIParser(Parser):
def __init__(
self,
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c211f46

Please sign in to comment.