~eldZddlmZddlmZddlmZGddeZGddZGd d ZGd d Z Gd dZ GddZ GddZ GddZ GddZGddZGddZGddZy)z"A schema system. Yes. Another one!) iteritems)long)unicodeceZdZdZy) InvalidErrorz&Raised when invalid input is received.N)__name__ __module__ __qualname____doc__6/usr/lib/python3/dist-packages/landscape/lib/schema.pyrrs0r rceZdZdZdZdZy)Constantz1Something that must be equal to a constant value.c||_yN)valueselfrs r__init__zConstant.__init__s  r ct|jtr!t|tr |j }||jk7rt |d|j|S#t $rY6wxYw)Nz != ) isinstancerstrbytesdecodeUnicodeDecodeErrorrrs rcoercezConstant.coercesj djj# &:eU+C   DJJ %$tzzn=> > &  sA'' A32A3Nrr r r rrr r rrr s; r rceZdZdZdZdZy)AnyzsSomething which must apply to any of a number of different schemas. @param schemas: Other schema objects. c||_yr)schemas)rr"s rrz Any.__init__%s  r c|jD]} |j|cSt|d|j#t$rY=wxYw)z The result of the first schema which doesn't raise L{InvalidError} from its C{coerce} method will be returned. z did not match any schema in )r"rr)rrschemas rrz Any.coerce(sa ll F }}U++  i4T\\N C     s? A  A Nrr r rr r s   r r ceZdZdZdZy)Boolz!Something that must be a C{bool}.cBt|tst|d|S)Nz is not a bool)rboolrrs rrz Bool.coerce:s#%&%.9: : r Nrr r r rr r rr&r&7s +r r&ceZdZdZdZy)Intz,Something that must be an C{int} or C{long}.cNt|ttfst|d|S)Nz isn't an int or long)rintrrrs rrz Int.coerceCs(%#t-%*?@A A r Nr)r r rr+r+@s 6r r+ceZdZdZdZy)Floatz7Something that must be an C{int}, C{long}, or C{float}.cXt|tttfst |d|S)Nz isn't a float)rr-rfloatrrs rrz Float.coerceLs*%#tU!34%.9: : r Nr)r r rr/r/Is Ar r/ceZdZdZdZy)BytesziA binary string. If the value is a Python3 str (unicode), it will be automatically encoded. ct|tr|St|tr|jSt |d)Nz isn't a bytestring)rrrencoderrs rrz Bytes.coerceYs: eU #L eS !<<> !eY&9:;;r Nr)r r rr3r3Rs  r>{s  r r>ceZdZdZdZdZy)TuplezSomething which must be a fixed-length tuple. @param schema: A sequence of schemas, which will be applied to each value in the tuple respectively. c||_yrr@rAs rrzTuple.__init__rBr cvt|tst|dt|t|jk7r0tdt|jdt|d|g}t |j|D]%\}}|j |j|'t|S)Nz is not a tuplezNeed z items, got z in )rtuplerlenr$zipappendr)rr new_valuer$s rrz Tuple.coerces%'%/:; ; u:T[[) )DKK()*5zl$ui1   e4 3MFE   V]]51 2 3Yr Nrr r rrLrLs   r rLceZdZdZddZdZy)KeyDicta'Something which must be a C{dict} with defined keys. The keys must be constant and the values must match a per-key schema. If strict, extra keys cause an exception during coercion. @param schema: A dict mapping keys to schemas that the values of those keys must match. NcH|g}t||_||_||_yr)setoptionalr$_strict)rr$rXstricts rrzKeyDict.__init__s&  HH    r c Hi}t|tst|dt|D]`\}}||jv}|r&|j rt|d|j|r? |j|j |||<bt|j}t|jj|jz }||z } | rtd| |S#t$r)}td|d|d|j|d|d}~wwxYw)N is not a dict.z is not a valid key as per z Value of z key of dict rDrEz Missing keys ) rdictrrr$rYrrWkeysrX) rrnew_dictkv unknown_keyr<new_keys required_keysmissings rrzKeyDict.coerces;%&%/:; ;e$ DAq4;;.Kt||"e6t{{oF "kk!n33A6  $x}}'DKK,,./$--? (* wi89 9  "uM%; KKN+2aS2 s,!C// D!8$DD!)NTrr r rrUrUsr rUceZdZdZdZdZy)DictzSomething 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. c ||_||_yr) key_schema value_schema)rrirjs rrz Dict.__init__s$(r ct|tst|di}|jD]<\}}|jj |||j j |<>|S)Nr\)rr]ritemsrjrri)rrr_r`ras rrz Dict.coercesm%&%/:; ;KKM NDAq262C2C2J2J12MHT__++A. / Nr Nrr r rrgrgs )r rgN)r twisted.python.compatrrr Exceptionrrr r&r+r/r3r7r>rLrUrgr r rros(+&) 9 $  0<<"0.  0--`r