B wÏ1`,*ã@sVddlmZddlZddlmZmZddlmZddlmZdd„Z Gd d „d ƒZ dS) é)Ú OrderedDictNé)ÚJsonSchemaValueExceptionÚJsonSchemaDefinitionException)Úindent)Ú RefResolvercCst|tƒr|S|gS)N)Ú isinstanceÚlist)Úvariable©r úgsz3CodeGenerator.global_state_code..z import rezREGEX_PATTERNS = {z z, Ú})r%rr&rÚitems)r Zregexsr r r Úglobal_state_codeYs   zCodeGenerator.global_state_codecCs|js| ¡dS)N)rÚgenerate_func_code)r r r r r%tsz!CodeGenerator._generate_func_codecCs4| d¡x$|jr.|j ¡\}}| ||¡q WdS)zt Creates base code of validation function and calls helper for creating code by definition. zNoneType = type(None)N)ÚlrÚpopitemÚgenerate_validation_function)r ÚuriÚnamer r r r5xs z CodeGenerator.generate_func_codec Csf|j |¡| d¡|j |¡:}| d|¡"|j|dddd| d¡WdQRXWdQRXdS)zL Generate validation function for given uri with given name r+z def {}(data):ÚdataT)Úclear_variablesz return dataN)rÚaddr6rZ resolvingÚgenerate_func_code_block)r r9r:r!r r r r8†s   z*CodeGenerator.generate_validation_functionFcCs`|j|j|jf}||||_|_|_|r8|j}tƒ|_| |¡|\|_|_|_|r\||_dS)zB Creates validation rules for current definition. N)rrrrrÚ_generate_func_code_block)r r!r Ú variable_namer<ÚbackupZbackup_variablesr r r r>‘s z&CodeGenerator.generate_func_code_blockcCs2t|tƒstdƒ‚d|kr$| ¡n | |¡dS)Nzdefinition must be an objectz$ref)rr)rÚ generate_refÚrun_generate_functions)r r!r r r r?¡s   z'CodeGenerator._generate_func_code_blockcCs*x$|j ¡D]\}}||kr |ƒq WdS)N)rr3)r r!r/Úfuncr r r rCªsz$CodeGenerator.run_generate_functionsc CsV|j |jd¡:|j ¡}|j ¡}||jkr<||j|<| d|¡WdQRXdS)aW Ref can be link to remote or local definition. .. code-block:: python {'$ref': 'http://json-schema.org/draft-04/schema#'} { 'properties': { 'foo': {'type': 'integer'}, 'bar': {'$ref': '#/properties/foo'} } } z$refz{}({variable})N)rZin_scoperrrrrr6)r r:r9r r r rB¯s     zCodeGenerator.generate_refcOs€d|j|j}|j}|r.d|kr.d |j¡}t|jp8if|j|dœ|—Ž}|j||Ž}| dd¡ dd¡}|j  ||¡|S) a… Short-cut of line. Used for inserting line. It's formated with parameters ``variable``, ``variable_name`` (as ``name`` for short-cut), all keys from current JSON schema ``definition`` and also passed arguments in ``args`` and named ``kwds``. .. code-block:: python self.l('if {variable} not in {enum}: raise JsonSchemaValueException("Wrong!")') When you want to indent block, use it as context manager. For example: .. code-block:: python with self.l('if {variable} not in {enum}:'): self.l('raise JsonSchemaValueException("Wrong!")') ú Ú{z"+"{}".format(**locals())+")r r:r$z\nú z\r) ÚINDENTrrr,r)rrÚreplacerÚappend)r ÚlineÚargsÚkwdsÚspacesr:Úcontextr r r r6Çs    zCodeGenerator.lcCst|ƒ dd¡S)zÔ Short-cut of escape. Used for inserting user values into a string message. .. code-block:: python self.l('raise JsonSchemaValueException("Variable: {}")', self.e(variable)) ú"z\")ÚstrrI)r Ústringr r r ÚeëszCodeGenerator.e)ÚrulecGsVd|d}| t|jtƒr(|j |¡nd¡}|j|f|žt|jƒt|ƒ|dœŽdS)zG Short-cut for creating raising exception in the code. z raise JsonSchemaValueException("zI", value={variable}, name="{name}", definition={definition}, rule={rule})N)r!rTÚdefinition_rule)rSrrr)Úgetr6Úrepr)r ÚmsgrTrLrUr r r Úexcõs "zCodeGenerator.exccCs4d |j¡}||jkrdS|j |¡| d¡dS)a Append code for creating variable with length of that variable (for example length of list or dictionary) with name ``{variable}_len``. It can be called several times and always it's done only when that variable still does not exists. z{}_lenNz {variable}_len = len({variable}))r,rrr=r6)r r@r r r Úcreate_variable_with_lengthýs    z)CodeGenerator.create_variable_with_lengthcCs4d |j¡}||jkrdS|j |¡| d¡dS)z® Append code for creating variable with keys of that variable (dictionary) with a name ``{variable}_keys``. Similar to `create_variable_with_length`. z{}_keysNz({variable}_keys = set({variable}.keys()))r,rrr=r6)r r@r r r Úcreate_variable_keys s    z"CodeGenerator.create_variable_keyscCs4d |j¡}||jkrdS|j |¡| d¡dS)z¬ Append code for creating variable with bool if it's instance of list with a name ``{variable}_is_list``. Similar to `create_variable_with_length`. z {}_is_listNz:{variable}_is_list = isinstance({variable}, (list, tuple)))r,rrr=r6)r r@r r r Úcreate_variable_is_lists    z%CodeGenerator.create_variable_is_listcCs4d |j¡}||jkrdS|j |¡| d¡dS)z¬ Append code for creating variable with bool if it's instance of list with a name ``{variable}_is_dict``. Similar to `create_variable_with_length`. z {}_is_dictNz1{variable}_is_dict = isinstance({variable}, dict))r,rrr=r6)r r@r r r Úcreate_variable_is_dict s    z%CodeGenerator.create_variable_is_dict)N)F)Ú__name__Ú __module__Ú __qualname__Ú__doc__rHr#Úpropertyr'r*r4r%r5r8r>r?rCrBrr6rSrYrZr[r\r]r r r r rs(  !     $    r) Ú collectionsrr(Ú exceptionsrrrZ ref_resolverrr rr r r r Ús