<s> import collections <EOL> import re <EOL> class Schema ( collections . Mapping ) : <EOL> def __init__ ( self , schema ) : <EOL> if isinstance ( schema , Schema ) : <EOL> schema = schema . _schema <EOL> self . _schema = schema or { } <EOL> @ property <EOL> def type ( self ) : <EOL> try : <EOL> type = self . _schema [ '<STR_LIT:type>' ] <EOL> if isinstance ( type , ( list , tuple ) ) : <EOL> return tuple ( type ) <EOL> return ( type , ) <EOL> except KeyError : <EOL> return None <EOL> @ property <EOL> def readonly_properties ( self ) : <EOL> if self . type != '<STR_LIT:object>' : <EOL> return ( ) <EOL> properties = [ ] <EOL> for name , schema in self . _schema . get ( '<STR_LIT>' , { } ) . items ( ) : <EOL> if schema . get ( '<STR_LIT>' , False ) : <EOL> properties . append ( name ) <EOL> return tuple ( properties ) <EOL> @ property <EOL> def required_properties ( self ) : <EOL> if '<STR_LIT:object>' not in self . type : <EOL> return ( ) <EOL> return tuple ( self . _schema . get ( '<STR_LIT>' , [ ] ) ) <EOL> def can_include_property ( self , name ) : <EOL> if not self . _schema : <EOL> return True <EOL> if '<STR_LIT:object>' not in self . type : <EOL> return False <EOL> try : <EOL> property_schema = self . _schema [ '<STR_LIT>' ] [ name ] <EOL> if property_schema . get ( '<STR_LIT>' , False ) : <EOL> return False <EOL> return True <EOL> except KeyError : <EOL> if self . _schema . get ( '<STR_LIT>' , True ) : <EOL> return True <EOL> for pattern in self . _schema . get ( '<STR_LIT>' , { } ) . values ( ) : <EOL> if re . match ( pattern , name ) : <EOL> return True <EOL> return False <EOL> def __contains__ ( self , item ) : <EOL> return item in self . _schema <EOL> def __getitem__ ( self , item ) : <EOL> return self . _schema [ item ] <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _schema ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _schema ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , repr ( self . _schema ) ) </s>
