<s> """<STR_LIT>""" <EOL> from google . appengine . api import datastore_types <EOL> from google . appengine . ext import db <EOL> class BaseMetadata ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> KIND_NAME = '<STR_LIT>' <EOL> @ classmethod <EOL> def kind ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . KIND_NAME <EOL> class Namespace ( BaseMetadata ) : <EOL> """<STR_LIT>""" <EOL> KIND_NAME = '<STR_LIT>' <EOL> EMPTY_NAMESPACE_ID = datastore_types . _EMPTY_NAMESPACE_ID <EOL> @ property <EOL> def namespace_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key_to_namespace ( self . key ( ) ) <EOL> @ classmethod <EOL> def key_for_namespace ( cls , namespace ) : <EOL> """<STR_LIT>""" <EOL> if namespace : <EOL> return db . Key . from_path ( cls . KIND_NAME , namespace ) <EOL> else : <EOL> return db . Key . from_path ( cls . KIND_NAME , cls . EMPTY_NAMESPACE_ID ) <EOL> @ classmethod <EOL> def key_to_namespace ( cls , key ) : <EOL> """<STR_LIT>""" <EOL> return key . name ( ) or '<STR_LIT>' <EOL> class Kind ( BaseMetadata ) : <EOL> """<STR_LIT>""" <EOL> KIND_NAME = '<STR_LIT>' <EOL> @ property <EOL> def kind_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key_to_kind ( self . key ( ) ) <EOL> @ classmethod <EOL> def key_for_kind ( cls , kind ) : <EOL> """<STR_LIT>""" <EOL> return db . Key . from_path ( cls . KIND_NAME , kind ) <EOL> @ classmethod <EOL> def key_to_kind ( cls , key ) : <EOL> """<STR_LIT>""" <EOL> return key . name ( ) <EOL> class Property ( BaseMetadata ) : <EOL> """<STR_LIT>""" <EOL> KIND_NAME = '<STR_LIT>' <EOL> @ property <EOL> def property_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key_to_property ( self . key ( ) ) <EOL> @ property <EOL> def kind_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key_to_kind ( self . key ( ) ) <EOL> property_representation = db . StringListProperty ( ) <EOL> @ classmethod <EOL> def key_for_kind ( cls , kind ) : <EOL> """<STR_LIT>""" <EOL> return db . Key . from_path ( Kind . KIND_NAME , kind ) <EOL> @ classmethod <EOL> def key_for_property ( cls , kind , property ) : <EOL> """<STR_LIT>""" <EOL> return db . Key . from_path ( Kind . KIND_NAME , kind , Property . KIND_NAME , property ) <EOL> @ classmethod <EOL> def key_to_kind ( cls , key ) : <EOL> """<STR_LIT>""" <EOL> if key . kind ( ) == Kind . KIND_NAME : <EOL> return key . name ( ) <EOL> else : <EOL> return key . parent ( ) . name ( ) <EOL> @ classmethod <EOL> def key_to_property ( cls , key ) : <EOL> """<STR_LIT>""" <EOL> if key . kind ( ) == Kind . KIND_NAME : <EOL> return None <EOL> else : <EOL> return key . name ( ) <EOL> class EntityGroup ( BaseMetadata ) : <EOL> """<STR_LIT>""" <EOL> KIND_NAME = '<STR_LIT>' <EOL> ID = <NUM_LIT:1> <EOL> version = db . IntegerProperty ( name = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def key_for_entity ( cls , entity_or_key ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( entity_or_key , db . Model ) : <EOL> key = entity_or_key . key ( ) <EOL> else : <EOL> key = entity_or_key <EOL> while key . parent ( ) : <EOL> key = key . parent ( ) <EOL> return db . Key . from_path ( cls . KIND_NAME , cls . ID , parent = key ) <EOL> def get_namespaces ( start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> q = Namespace . all ( ) <EOL> if start is not None : <EOL> q . filter ( '<STR_LIT>' , Namespace . key_for_namespace ( start ) ) <EOL> if end is not None : <EOL> q . filter ( '<STR_LIT>' , Namespace . key_for_namespace ( end ) ) <EOL> return [ x . namespace_name for x in q . run ( ) ] <EOL> def get_kinds ( start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> q = Kind . all ( ) <EOL> if start is not None and start != '<STR_LIT>' : <EOL> q . filter ( '<STR_LIT>' , Kind . key_for_kind ( start ) ) <EOL> if end is not None : <EOL> if end == '<STR_LIT>' : <EOL> return [ ] <EOL> q . filter ( '<STR_LIT>' , Kind . key_for_kind ( end ) ) <EOL> return [ x . kind_name for x in q . run ( ) ] <EOL> def get_properties_of_kind ( kind , start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> q = Property . all ( keys_only = True ) <EOL> q . ancestor ( Property . key_for_kind ( kind ) ) <EOL> if start is not None and start != '<STR_LIT>' : <EOL> q . filter ( '<STR_LIT>' , Property . key_for_property ( kind , start ) ) <EOL> if end is not None : <EOL> if end == '<STR_LIT>' : <EOL> return [ ] <EOL> q . filter ( '<STR_LIT>' , Property . key_for_property ( kind , end ) ) <EOL> return [ Property . key_to_property ( x ) for x in q . run ( ) ] <EOL> def get_representations_of_kind ( kind , start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> q = Property . all ( ) <EOL> q . ancestor ( Property . key_for_kind ( kind ) ) <EOL> if start is not None and start != '<STR_LIT>' : <EOL> q . filter ( '<STR_LIT>' , Property . key_for_property ( kind , start ) ) <EOL> if end is not None : <EOL> if end == '<STR_LIT>' : <EOL> return { } <EOL> q . filter ( '<STR_LIT>' , Property . key_for_property ( kind , end ) ) <EOL> result = { } <EOL> for property in q . run ( ) : <EOL> result [ property . property_name ] = property . property_representation <EOL> return result <EOL> def get_entity_group_version ( entity_or_key ) : <EOL> """<STR_LIT>""" <EOL> eg = db . get ( EntityGroup . key_for_entity ( entity_or_key ) ) <EOL> if eg : <EOL> return eg . version <EOL> else : <EOL> return None </s>
