<s> import collections <EOL> from flask import url_for , request , Markup <EOL> from . utils import freeze_dict , join_html_attrs <EOL> class Item ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , label , endpoint , args = None , url = None , html_attrs = None , <EOL> items = None ) : <EOL> self . label = label <EOL> self . endpoint = endpoint <EOL> self . _args = args <EOL> self . _url = url <EOL> self . html_attrs = { } if html_attrs is None else html_attrs <EOL> self . items = ItemCollection ( items or None ) <EOL> def __html__ ( self ) : <EOL> attrs = dict ( self . html_attrs ) <EOL> html_class = attrs . get ( '<STR_LIT:class>' , [ ] ) <EOL> if self . is_active : <EOL> html_class . append ( '<STR_LIT>' ) <EOL> attrs [ '<STR_LIT:class>' ] = '<STR_LIT:U+0020>' . join ( html_class ) <EOL> if not attrs [ '<STR_LIT:class>' ] : <EOL> del attrs [ '<STR_LIT:class>' ] <EOL> attrs [ '<STR_LIT>' ] = self . url <EOL> attrs_template , attrs_values = join_html_attrs ( attrs ) <EOL> return Markup ( '<STR_LIT>' % attrs_template ) . format ( <EOL> * attrs_values , label = self . label ) <EOL> def __html_format__ ( self , format_spec ) : <EOL> if format_spec == '<STR_LIT>' : <EOL> li_attrs = Markup ( '<STR_LIT>' ) if self . is_active else '<STR_LIT>' <EOL> return Markup ( '<STR_LIT>' ) . format ( li_attrs , self . __html__ ( ) ) <EOL> elif format_spec : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return self . __html__ ( ) <EOL> @ property <EOL> def args ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _args is None : <EOL> return { } <EOL> if callable ( self . _args ) : <EOL> return dict ( self . _args ( ) ) <EOL> return dict ( self . _args ) <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_internal : <EOL> return url_for ( self . endpoint , ** self . args ) <EOL> return self . _url <EOL> @ property <EOL> def is_active ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( request and self . is_current ) <EOL> @ property <EOL> def is_internal ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _url is None <EOL> @ property <EOL> def is_current ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_internal : <EOL> return False <EOL> has_same_endpoint = ( request . endpoint == self . endpoint ) <EOL> has_same_args = ( request . view_args == self . args ) <EOL> return has_same_endpoint and has_same_args <EOL> @ property <EOL> def ident ( self ) : <EOL> """<STR_LIT>""" <EOL> return ItemReference ( self . endpoint , self . args ) <EOL> class ItemCollection ( collections . MutableSequence , <EOL> collections . Iterable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterable = None ) : <EOL> self . _items = [ ] <EOL> self . _items_mapping = { } <EOL> self . extend ( iterable or [ ] ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . _items <EOL> def __getitem__ ( self , index ) : <EOL> if isinstance ( index , int ) : <EOL> return self . _items [ index ] <EOL> if isinstance ( index , tuple ) : <EOL> endpoint , args = index <EOL> else : <EOL> endpoint , args = index , { } <EOL> ident = ItemReference ( endpoint , args ) <EOL> return self . _items_mapping [ ident ] <EOL> def __setitem__ ( self , index , item ) : <EOL> old_item = self . _items [ index ] <EOL> del self . _items_mapping [ old_item . ident ] <EOL> self . _items [ index ] = item <EOL> self . _items_mapping [ item . ident ] = item <EOL> def __delitem__ ( self , index ) : <EOL> item = self [ index ] <EOL> del self . _items [ index ] <EOL> del self . _items_mapping [ item . ident ] <EOL> def __len__ ( self ) : <EOL> return len ( self . _items ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _items ) <EOL> def insert ( self , index , item ) : <EOL> self . _items . insert ( index , item ) <EOL> self . _items_mapping [ item . ident ] = item <EOL> class ItemReference ( collections . namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , endpoint , args = ( ) ) : <EOL> if isinstance ( args , dict ) : <EOL> args = freeze_dict ( args ) <EOL> return super ( cls , ItemReference ) . __new__ ( cls , endpoint , args ) </s>
