<s> import inspect <EOL> import json <EOL> from oslo_utils import strutils <EOL> from oslo_utils import uuidutils <EOL> import six <EOL> import wsme <EOL> from wsme import types as wtypes <EOL> from ironic . api . controllers . v1 import utils as v1_utils <EOL> from ironic . common import exception <EOL> from ironic . common . i18n import _ <EOL> from ironic . common import utils <EOL> class MacAddressType ( wtypes . UserType ) : <EOL> """<STR_LIT>""" <EOL> basetype = wtypes . text <EOL> name = '<STR_LIT>' <EOL> @ staticmethod <EOL> def validate ( value ) : <EOL> return utils . validate_and_normalize_mac ( value ) <EOL> @ staticmethod <EOL> def frombasetype ( value ) : <EOL> if value is None : <EOL> return None <EOL> return MacAddressType . validate ( value ) <EOL> class UuidOrNameType ( wtypes . UserType ) : <EOL> """<STR_LIT>""" <EOL> basetype = wtypes . text <EOL> name = '<STR_LIT>' <EOL> @ staticmethod <EOL> def validate ( value ) : <EOL> if not ( uuidutils . is_uuid_like ( value ) <EOL> or v1_utils . is_valid_logical_name ( value ) ) : <EOL> raise exception . InvalidUuidOrName ( name = value ) <EOL> return value <EOL> @ staticmethod <EOL> def frombasetype ( value ) : <EOL> if value is None : <EOL> return None <EOL> return UuidOrNameType . validate ( value ) <EOL> class NameType ( wtypes . UserType ) : <EOL> """<STR_LIT>""" <EOL> basetype = wtypes . text <EOL> name = '<STR_LIT:name>' <EOL> @ staticmethod <EOL> def validate ( value ) : <EOL> if not v1_utils . is_valid_logical_name ( value ) : <EOL> raise exception . InvalidName ( name = value ) <EOL> return value <EOL> @ staticmethod <EOL> def frombasetype ( value ) : <EOL> if value is None : <EOL> return None <EOL> return NameType . validate ( value ) <EOL> class UuidType ( wtypes . UserType ) : <EOL> """<STR_LIT>""" <EOL> basetype = wtypes . text <EOL> name = '<STR_LIT>' <EOL> @ staticmethod <EOL> def validate ( value ) : <EOL> if not uuidutils . is_uuid_like ( value ) : <EOL> raise exception . InvalidUUID ( uuid = value ) <EOL> return value <EOL> @ staticmethod <EOL> def frombasetype ( value ) : <EOL> if value is None : <EOL> return None <EOL> return UuidType . validate ( value ) <EOL> class BooleanType ( wtypes . UserType ) : <EOL> """<STR_LIT>""" <EOL> basetype = wtypes . text <EOL> name = '<STR_LIT>' <EOL> @ staticmethod <EOL> def validate ( value ) : <EOL> try : <EOL> return strutils . bool_from_string ( value , strict = True ) <EOL> except ValueError as e : <EOL> raise exception . Invalid ( e ) <EOL> @ staticmethod <EOL> def frombasetype ( value ) : <EOL> if value is None : <EOL> return None <EOL> return BooleanType . validate ( value ) <EOL> class JsonType ( wtypes . UserType ) : <EOL> """<STR_LIT>""" <EOL> basetype = wtypes . text <EOL> name = '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . join ( map ( str , ( wtypes . text , six . integer_types , float , <EOL> BooleanType , list , dict , None ) ) ) <EOL> @ staticmethod <EOL> def validate ( value ) : <EOL> try : <EOL> json . dumps ( value ) <EOL> except TypeError : <EOL> raise exception . Invalid ( _ ( '<STR_LIT>' ) % value ) <EOL> else : <EOL> return value <EOL> @ staticmethod <EOL> def frombasetype ( value ) : <EOL> return JsonType . validate ( value ) <EOL> class ListType ( wtypes . UserType ) : <EOL> """<STR_LIT>""" <EOL> basetype = wtypes . text <EOL> name = '<STR_LIT:list>' <EOL> @ staticmethod <EOL> def validate ( value ) : <EOL> """<STR_LIT>""" <EOL> items = [ v . strip ( ) . lower ( ) for v in six . text_type ( value ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> return list ( set ( filter ( None , items ) ) ) <EOL> @ staticmethod <EOL> def frombasetype ( value ) : <EOL> if value is None : <EOL> return None <EOL> return ListType . validate ( value ) <EOL> macaddress = MacAddressType ( ) <EOL> uuid_or_name = UuidOrNameType ( ) <EOL> name = NameType ( ) <EOL> uuid = UuidType ( ) <EOL> boolean = BooleanType ( ) <EOL> listtype = ListType ( ) <EOL> jsontype = JsonType ( ) <EOL> class JsonPatchType ( wtypes . Base ) : <EOL> """<STR_LIT>""" <EOL> path = wtypes . wsattr ( wtypes . StringType ( pattern = '<STR_LIT>' ) , <EOL> mandatory = True ) <EOL> op = wtypes . wsattr ( wtypes . Enum ( str , '<STR_LIT>' , '<STR_LIT:replace>' , '<STR_LIT>' ) , <EOL> mandatory = True ) <EOL> value = wsme . wsattr ( jsontype , default = wtypes . Unset ) <EOL> _api_base = None <EOL> _extra_non_removable_attrs = set ( ) <EOL> _non_removable_attrs = None <EOL> @ staticmethod <EOL> def internal_attrs ( ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ classmethod <EOL> def non_removable_attrs ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls . _non_removable_attrs is None : <EOL> cls . _non_removable_attrs = cls . _extra_non_removable_attrs . copy ( ) <EOL> if cls . _api_base : <EOL> fields = inspect . getmembers ( cls . _api_base , <EOL> lambda a : not inspect . isroutine ( a ) ) <EOL> for name , field in fields : <EOL> if getattr ( field , '<STR_LIT>' , False ) : <EOL> cls . _non_removable_attrs . add ( '<STR_LIT>' % name ) <EOL> return cls . _non_removable_attrs <EOL> @ staticmethod <EOL> def validate ( patch ) : <EOL> _path = '<STR_LIT:/>' + patch . path . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> ] <EOL> if _path in patch . internal_attrs ( ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise wsme . exc . ClientSideError ( msg % patch . path ) <EOL> if patch . path in patch . non_removable_attrs ( ) and patch . op == '<STR_LIT>' : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise wsme . exc . ClientSideError ( msg % patch . path ) <EOL> if patch . op != '<STR_LIT>' : <EOL> if patch . value is wsme . Unset : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise wsme . exc . ClientSideError ( msg ) <EOL> ret = { '<STR_LIT:path>' : patch . path , '<STR_LIT>' : patch . op } <EOL> if patch . value is not wsme . Unset : <EOL> ret [ '<STR_LIT:value>' ] = patch . value <EOL> return ret </s>
