o ¶žâc9ã@sÜdZddlmZmZmZGdd„deƒZGdd„deƒZGdd„deƒZ Gd d „d eƒZ Gd d „d eƒZ Gd d„deƒZ Gdd„deƒZ Gdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZdS)z"A schema system. Yes. Another one!é)Ú iteritemsÚunicodeÚlongc@seZdZdZdS)Ú InvalidErrorz&Raised when invalid input is received.N)Ú__name__Ú __module__Ú __qualname__Ú__doc__©r r ú6/usr/lib/python3/dist-packages/landscape/lib/schema.pyrsrc@ó eZdZdZdd„Zdd„ZdS)ÚConstantz1Something that must be equal to a constant value.cCó ||_dS©N)Úvalue©Úselfrr r r Ú__init__ ó zConstant.__init__cCsVt|jtƒrt|tƒrz| ¡}Wn tyYnw||jkr)td||jfƒ‚|S)Nz%r != %r)Ú isinstancerÚstrÚbytesÚdecodeÚUnicodeDecodeErrorrrr r r Úcoerces  ÿ zConstant.coerceN©rrrr rrr r r r r s r c@r )ÚAnyzsSomething which must apply to any of a number of different schemas. @param schemas: Other schema objects. cGrr)Úschemas)rrr r r r rz Any.__init__c Cs@|jD]}z| |¡WStyYqwtd||jfƒ‚)zˆ The result of the first schema which doesn't raise L{InvalidError} from its C{coerce} method will be returned. z!%r did not match any schema in %s)rrr)rrÚschemar r r r#s  ÿÿz Any.coerceNrr r r r ró rc@óeZdZdZdd„ZdS)ÚBoolz!Something that must be a C{bool}.cCst|tƒs td|fƒ‚|S)Nz%r is not a bool)rÚboolrrr r r r3s z Bool.coerceN©rrrr rr r r r r!1ó r!c@r )ÚIntz,Something that must be an C{int} or C{long}.cCs t|ttfƒstd|fƒ‚|S)Nz%r isn't an int or long)rÚintrrrr r r r;sz Int.coerceNr#r r r r r%9r$r%c@r )ÚFloatz7Something that must be an C{int}, C{long}, or C{float}.cCs"t|tttfƒstd|fƒ‚|S)Nz%r isn't a float)rr&rÚfloatrrr r r rCsz Float.coerceNr#r r r r r'Ar$r'c@r )ÚBytesziA binary string. If the value is a Python3 str (unicode), it will be automatically encoded. cCs,t|tƒr|St|tƒr| ¡Std|ƒ‚)Nz%r isn't a bytestring)rrrÚencoderrr r r rOs   z Bytes.coerceNr#r r r r r)Is r)c@s"eZdZdZddd„Zdd„ZdS) ÚUnicodez³Something that must be a C{unicode}. If the value is a C{str}, it will automatically be decoded. @param encoding: The encoding to automatically decode C{str}s with. úutf-8cCrr)Úencoding)rr-r r r rarzUnicode.__init__c Csdt|tƒr$z| |j¡}Wnty#}z td|t|ƒfƒ‚d}~wwt|tƒs0td|fƒ‚|S)Nz%r can't be decoded: %sz%r isn't a unicode)rrrr-rrrr)rrÚer r r rds €ÿ zUnicode.coerceN)r,rr r r r r+Ys  r+c@r )ÚListznSomething which must be a C{list}. @param schema: The schema that all values of the list must match. cCrr©r©rrr r r rtrz List.__init__c Csvt|tƒs td|fƒ‚t|ƒ}t|ƒD]$\}}z |j |¡||<Wqty8}z td||j|fƒ‚d}~ww|S)Nz%r is not a listz%r could not coerce with %s: %s)rÚlistrÚ enumeraterr)rrÚnew_listÚiÚsubvaluer.r r r rws  ÿÿ€ÿz List.coerceNrr r r r r/orr/c@r )ÚTuplez¤Something which must be a fixed-length tuple. @param schema: A sequence of schemas, which will be applied to each value in the tuple respectively. cGrrr0r1r r r rŒrzTuple.__init__cCsxt|tƒs td|fƒ‚t|ƒt|jƒkr#tdt|jƒt|ƒ|fƒ‚g}t|j|ƒD] \}}| | |¡¡q+t|ƒS)Nz%r is not a tuplezNeed %s items, got %s in %r)rÚtuplerÚlenrÚzipÚappendr)rrÚ new_valuerr r r rs ÿz Tuple.coerceNrr r r r r7…s r7c@s"eZdZdZddd„Zdd„ZdS)ÚKeyDictzéSomething which must be a C{dict} with defined keys. The keys must be constant and the values must match a per-key schema. @param schema: A dict mapping keys to schemas that the values of those keys must match. NcCs |durg}t|ƒ|_||_dSr)ÚsetÚoptionalr)rrr?r r r r£s  zKeyDict.__init__c CsÒi}t|tƒstd|fƒ‚t|ƒD]7\}}||jvr$td||jfƒ‚z |j| |¡||<WqtyI}z td|||j||fƒ‚d}~wwt| ¡ƒ}t|j ¡ƒ|j}||}|rgtd|fƒ‚|S)Nú%r is not a dict.z%r is not a valid key as per %rz7Value of %r key of dict %r could not coerce with %s: %szMissing keys %s) rÚdictrrrrr>Úkeysr?) rrÚnew_dictÚkÚvr.Únew_keysÚ required_keysÚmissingr r r r©s0  ÿÿÿ€ÿ zKeyDict.coercerrr r r r r=›s  r=c@r )ÚDictz°Something which must be a C{dict} with arbitrary keys. @param key_schema: The schema that keys must match. @param value_schema: The schema that values must match. cCs||_||_dSr)Ú key_schemaÚ value_schema)rrJrKr r r rÆs z Dict.__init__cCsJt|tƒs td|fƒ‚i}| ¡D]\}}|j |¡||j |¡<q|S)Nr@)rrArÚitemsrKrrJ)rrrCrDrEr r r rÊs z Dict.coerceNrr r r r rI¿s rIN)r Útwisted.python.compatrrrÚ ExceptionrÚobjectr rr!r%r'r)r+r/r7r=rIr r r r Ús$