<s> import re <EOL> from django . template . loader import render_to_string <EOL> from django . template import Context , RequestContext <EOL> from django . db . models . base import ModelBase <EOL> from django . conf import settings <EOL> INLINE_SPLITTER = re . compile ( r"""<STR_LIT>""" , re . VERBOSE ) <EOL> INLINE_KWARG_PARSER = re . compile ( r"""<STR_LIT>""" , re . VERBOSE ) <EOL> class InlineUnrenderableError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class InlineInputError ( InlineUnrenderableError ) : <EOL> pass <EOL> class InlineValueError ( InlineUnrenderableError ) : <EOL> pass <EOL> class InlineAttributeError ( InlineUnrenderableError ) : <EOL> pass <EOL> class InlineNotRegisteredError ( InlineUnrenderableError ) : <EOL> pass <EOL> class InlineUnparsableError ( InlineUnrenderableError ) : <EOL> pass <EOL> def parse_inline ( text ) : <EOL> """<STR_LIT>""" <EOL> m = INLINE_SPLITTER . match ( text ) <EOL> if not m : <EOL> raise InlineUnparsableError <EOL> args = m . group ( '<STR_LIT:args>' ) <EOL> name = m . group ( '<STR_LIT:name>' ) <EOL> value = "<STR_LIT>" <EOL> kwtxt = "<STR_LIT>" <EOL> kwargs = { } <EOL> if args : <EOL> kwtxt = INLINE_KWARG_PARSER . search ( args ) . group ( '<STR_LIT>' ) <EOL> value = re . sub ( "<STR_LIT>" % kwtxt , "<STR_LIT>" , args ) <EOL> value = value . strip ( ) <EOL> if m . group ( '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = m . group ( '<STR_LIT>' ) <EOL> if kwtxt : <EOL> for kws in kwtxt . split ( ) : <EOL> k , v = kws . split ( '<STR_LIT:=>' ) <EOL> kwargs [ str ( k ) ] = v <EOL> return ( name , value , kwargs ) <EOL> def inline_for_model ( model , variants = [ ] , inline_args = { } ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( model , ModelBase ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> d = dict ( model = model ) <EOL> if variants : <EOL> d [ '<STR_LIT>' ] = variants <EOL> if inline_args : <EOL> d [ '<STR_LIT:args>' ] = inline_args <EOL> class_name = "<STR_LIT>" % model . _meta . module_name . capitalize ( ) <EOL> return type ( class_name , ( ModelInline , ) , d ) <EOL> class InlineBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , variant = None , context = None , template_dir = "<STR_LIT>" , ** kwargs ) : <EOL> self . value = value <EOL> self . variant = variant <EOL> self . kwargs = kwargs <EOL> def render ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class TemplateInline ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , variant = None , context = None , template_dir = None , ** kwargs ) : <EOL> self . value = value <EOL> self . variant = variant <EOL> self . context = context <EOL> self . kwargs = kwargs <EOL> self . template_dirs = [ ] <EOL> if template_dir : <EOL> self . template_dirs . append ( template_dir . strip ( '<STR_LIT:/>' ) . replace ( "<STR_LIT:'>" , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) ) <EOL> self . template_dirs . append ( '<STR_LIT>' ) <EOL> def get_context ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_template_name ( self ) : <EOL> templates = [ ] <EOL> name = self . __class__ . name <EOL> for dir in self . template_dirs : <EOL> if self . variant : <EOL> templates . append ( '<STR_LIT>' % ( dir , name , self . variant ) ) <EOL> templates . append ( '<STR_LIT>' % ( dir , name ) ) <EOL> return templates <EOL> def render ( self ) : <EOL> if self . context : <EOL> context = self . context <EOL> else : <EOL> context = Context ( ) <EOL> context . update ( self . kwargs ) <EOL> context [ '<STR_LIT>' ] = self . variant <EOL> output = render_to_string ( self . get_template_name ( ) , self . get_context ( ) , context ) <EOL> context . pop ( ) <EOL> return output <EOL> class ModelInline ( TemplateInline ) : <EOL> """<STR_LIT>""" <EOL> model = None <EOL> help_text = "<STR_LIT>" <EOL> @ classmethod <EOL> def get_app_label ( self ) : <EOL> return "<STR_LIT>" % ( self . model . _meta . app_label , self . model . _meta . module_name ) <EOL> def get_context ( self ) : <EOL> model = self . __class__ . model <EOL> if not isinstance ( model , ModelBase ) : <EOL> raise InlineAttributeError ( '<STR_LIT>' ) <EOL> try : <EOL> value = int ( self . value ) <EOL> object = model . objects . get ( pk = value ) <EOL> except ValueError : <EOL> raise InlineInputError ( "<STR_LIT>" % self . value ) <EOL> except model . DoesNotExist : <EOL> raise InlineInputError ( "<STR_LIT>" % ( self . value , model . _meta . app_label , model . _meta . module_name ) ) <EOL> return { '<STR_LIT:object>' : object } <EOL> class Registry ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _registry = { } <EOL> self . START_TAG = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . END_TAG = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ property <EOL> def inline_finder ( self ) : <EOL> return re . compile ( r'<STR_LIT>' % { '<STR_LIT:start>' : self . START_TAG , '<STR_LIT:end>' : self . END_TAG } ) <EOL> def register ( self , name , cls ) : <EOL> if not hasattr ( cls , '<STR_LIT>' ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> cls . name = name <EOL> self . _registry [ name ] = cls <EOL> def unregister ( self , name ) : <EOL> if not name in self . _registry : <EOL> raise InlineNotRegisteredError ( "<STR_LIT>" % name ) <EOL> del ( self . _registry [ name ] ) <EOL> def process ( self , text , context = None , template_dir = None , ** kwargs ) : <EOL> def render ( matchobj ) : <EOL> try : <EOL> text = matchobj . group ( <NUM_LIT:1> ) <EOL> name , value , inline_kwargs = parse_inline ( text ) <EOL> try : <EOL> cls = self . _registry [ name ] <EOL> except KeyError : <EOL> raise InlineNotRegisteredError ( '<STR_LIT>' % name ) <EOL> inline = cls ( value , context = context , template_dir = template_dir , ** inline_kwargs ) <EOL> return str ( inline . render ( ) ) <EOL> except InlineUnrenderableError : <EOL> debug = getattr ( settings , "<STR_LIT>" , False ) <EOL> if debug : <EOL> raise <EOL> else : <EOL> return "<STR_LIT>" <EOL> text = self . inline_finder . sub ( render , text ) <EOL> return text <EOL> registry = Registry ( ) </s>
