Add store_sources option that stores rendered source in a __megasniff_sources__ property

This commit is contained in:
2025-08-20 00:33:09 +03:00
parent c11a63c8a5
commit b11266990b
2 changed files with 8 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
[project] [project]
name = "megasniff" name = "megasniff"
version = "0.2.2" version = "0.2.3"
description = "Library for in-time codegened type validation" description = "Library for in-time codegened type validation"
authors = [ authors = [
{ name = "nikto_b", email = "niktob560@yandex.ru" } { name = "nikto_b", email = "niktob560@yandex.ru" }

View File

@@ -48,17 +48,20 @@ class SchemaInflatorGenerator:
object_template: jinja2.Template object_template: jinja2.Template
tuple_template: jinja2.Template tuple_template: jinja2.Template
_store_sources: bool
_strict_mode: bool _strict_mode: bool
def __init__(self, def __init__(self,
loader: Optional[jinja2.BaseLoader] = None, loader: Optional[jinja2.BaseLoader] = None,
strict_mode: bool = False, strict_mode: bool = False,
store_sources: bool = False,
*, *,
object_template_filename: str = 'inflator.jinja2', object_template_filename: str = 'inflator.jinja2',
tuple_template_filename: str = 'inflator_tuple.jinja2', tuple_template_filename: str = 'inflator_tuple.jinja2',
): ):
self._strict_mode = strict_mode self._strict_mode = strict_mode
self._store_sources = store_sources
if loader is None: if loader is None:
template_path = importlib.resources.files('megasniff.templates') template_path = importlib.resources.files('megasniff.templates')
@@ -85,7 +88,10 @@ class SchemaInflatorGenerator:
'from megasniff.exceptions import MissingFieldException, FieldValidationException\n') 'from megasniff.exceptions import MissingFieldException, FieldValidationException\n')
txt = imports + '\n' + txt txt = imports + '\n' + txt
exec(txt, namespace) 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: def _unwrap_typeref(self, t: type, strict_mode: bool) -> TypeRenderData:
type_origin = get_origin(t) 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(txt_segments) + '\n\n' + convertor_functext
convertor_functext = '\n'.join(list(filter(lambda x: len(x.strip()), convertor_functext.split('\n')))) convertor_functext = '\n'.join(list(filter(lambda x: len(x.strip()), convertor_functext.split('\n'))))
convertor_functext = convertor_functext.replace(', )', ')')
return convertor_functext, namespace return convertor_functext, namespace