From b11266990b9078c6f8716318419ee3bc471597f4 Mon Sep 17 00:00:00 2001 From: nikto_b Date: Wed, 20 Aug 2025 00:33:09 +0300 Subject: [PATCH] Add `store_sources` option that stores rendered source in a `__megasniff_sources__` property --- pyproject.toml | 2 +- src/megasniff/inflator.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bf2821e..00aec3d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "megasniff" -version = "0.2.2" +version = "0.2.3" description = "Library for in-time codegened type validation" authors = [ { name = "nikto_b", email = "niktob560@yandex.ru" } diff --git a/src/megasniff/inflator.py b/src/megasniff/inflator.py index 2ed478d..001f69f 100644 --- a/src/megasniff/inflator.py +++ b/src/megasniff/inflator.py @@ -48,17 +48,20 @@ class SchemaInflatorGenerator: object_template: jinja2.Template tuple_template: jinja2.Template + _store_sources: bool _strict_mode: bool def __init__(self, loader: Optional[jinja2.BaseLoader] = None, strict_mode: bool = False, + store_sources: bool = False, *, object_template_filename: str = 'inflator.jinja2', tuple_template_filename: str = 'inflator_tuple.jinja2', ): self._strict_mode = strict_mode + self._store_sources = store_sources if loader is None: template_path = importlib.resources.files('megasniff.templates') @@ -85,7 +88,10 @@ class SchemaInflatorGenerator: 'from megasniff.exceptions import MissingFieldException, FieldValidationException\n') txt = imports + '\n' + txt exec(txt, namespace) - return namespace['inflate'] + fn = namespace['inflate'] + if self._store_sources: + setattr(fn, '__megasniff_sources__', txt) + return fn def _unwrap_typeref(self, t: type, strict_mode: bool) -> TypeRenderData: type_origin = get_origin(t) @@ -245,6 +251,5 @@ class SchemaInflatorGenerator: convertor_functext = '\n'.join(txt_segments) + '\n\n' + convertor_functext convertor_functext = '\n'.join(list(filter(lambda x: len(x.strip()), convertor_functext.split('\n')))) - convertor_functext = convertor_functext.replace(', )', ')') return convertor_functext, namespace