<s> """<STR_LIT>""" <EOL> import importlib <EOL> from functools import partial <EOL> FOREIGN_KEY = "<STR_LIT>" <EOL> def _deferred_url_for ( klass , context , fmt = '<STR_LIT>' , fragment = None ) : <EOL> from sondra . document import Document <EOL> from sondra . suite import Suite <EOL> from sondra . application import Application <EOL> from sondra . collection import Collection <EOL> if isinstance ( klass , str ) : <EOL> if "<STR_LIT:/>" in klass or ( '<STR_LIT:.>' not in klass ) : <EOL> slug = klass <EOL> else : <EOL> modulename , classname = klass . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> klass = getattr ( importlib . import_module ( modulename ) , classname ) <EOL> slug = klass . slug <EOL> else : <EOL> slug = klass . slug <EOL> if isinstance ( context , Suite ) : <EOL> suite = context <EOL> elif isinstance ( context , Application ) : <EOL> suite = context . suite <EOL> elif isinstance ( context , Collection ) : <EOL> suite = context . application . suite <EOL> elif isinstance ( context , Document ) : <EOL> suite = context . collection . application . suite <EOL> elif context is None : <EOL> suite = context <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> ret = "<STR_LIT>" <EOL> if issubclass ( klass , Document ) : <EOL> for app in suite . values ( ) : <EOL> for coll in app . values ( ) : <EOL> if coll . document_class is klass : <EOL> ret = coll . url + '<STR_LIT:;>' + fmt <EOL> if fragment : <EOL> return ret + '<STR_LIT>' + fragment <EOL> else : <EOL> return ret + '<STR_LIT>' <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" . format ( klass ) ) <EOL> elif issubclass ( klass , Collection ) : <EOL> for app in suite . applications . values ( ) : <EOL> if slug in app : <EOL> ret = app [ slug ] . url <EOL> break <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" . format ( klass ) ) <EOL> elif issubclass ( klass , Application ) : <EOL> ret = suite [ slug ] . url + '<STR_LIT:;>' + fmt <EOL> elif issubclass ( klass , Suite ) : <EOL> ret = suite . url <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if fragment : <EOL> return ret + "<STR_LIT>" + fragment <EOL> else : <EOL> return ret + '<STR_LIT>' <EOL> def url_for ( klass , fmt = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return partial ( _deferred_url_for , klass = klass , fmt = fmt ) <EOL> def fk ( klass , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> ret = { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> FOREIGN_KEY : url_for ( klass ) <EOL> } <EOL> if kwargs : <EOL> ret . update ( kwargs ) <EOL> return ret <EOL> def lazy_definition ( klass , name = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> ret = { } <EOL> ret . update ( kwargs ) <EOL> if name : <EOL> ret . update ( { "<STR_LIT>" : partial ( _deferred_url_for , klass = klass , fmt = '<STR_LIT>' , fragment = "<STR_LIT>" + name ) } ) <EOL> else : <EOL> ret . update ( { "<STR_LIT>" : url_for ( klass ) } ) <EOL> return ret <EOL> def ref ( klass = '<STR_LIT>' , name = None , ** kwargs ) : <EOL> if klass != "<STR_LIT>" : <EOL> return lazy_definition ( klass , name , ** kwargs ) <EOL> else : <EOL> ret = { "<STR_LIT>" : ( "<STR_LIT>" + name ) if name else url_for ( klass ) } <EOL> ret . update ( ** kwargs ) <EOL> return ret </s>
