def get_advanced_query_param(self): return '-' + path_join(self.iommi_path, 'query')
def test_path_join(): assert path_join(None, 'foo') == 'foo' assert path_join('', 'foo') == 'foo' assert path_join('foo', 'bar') == 'foo/bar' assert path_join('foo', 'bar', separator='#') == 'foo#bar'
class MenuItem(MenuBase): """ Class that is used for the clickable menu items in a menu. See :doc:`Menu` for more complete examples. """ display_name: str = EvaluatedRefinable() url: str = Refinable( ) # attrs is evaluated, but in a special way so gets no EvaluatedRefinable type regex: str = EvaluatedRefinable() group: str = EvaluatedRefinable() a = Refinable() active_class = Refinable() @reinvokable @dispatch( display_name=lambda menu_item, **_: capitalize(menu_item.iommi_name()). replace('_', ' '), regex=lambda menu_item, **_: '^' + menu_item.url if menu_item.url else None, url=lambda menu_item, **_: '/' + path_join( getattr(menu_item.iommi_parent(), 'url', None), menu_item.iommi_name()) + '/', a=EMPTY, ) def __init__(self, **kwargs): super(MenuItem, self).__init__(**kwargs) def on_bind(self): super(MenuItem, self).on_bind() self.url = evaluate_strict(self.url, **self.iommi_evaluate_parameters()) # If this is a section header, and all sub-parts are hidden, hide myself if not self.url and self.sub_menu is not None and not self.sub_menu: self.include = False def own_evaluate_parameters(self): return dict(menu_item=self) def __repr__(self): r = f'{self._name} -> {self.url}' if self.sub_menu: for items in values(self.sub_menu): r += ''.join([f'\n {x}' for x in repr(items).split('\n')]) return r def __html__(self, *, render=None): a = setdefaults_path( Namespace(), self.a, children__text=self.display_name, attrs__href=self.url, _name='a', ) if self._active: setdefaults_path( a, attrs__class={self.active_class: True}, ) if self.url is None and a.tag == 'a': a.tag = None fragment = Fragment( children__a=a, tag=self.tag, template=self.template, attrs=self.attrs, _name='fragment', ) fragment = fragment.bind(parent=self) # need to do this here because otherwise the sub menu will get get double bind for name, item in items(self.sub_menu): assert name not in fragment.children fragment.children[name] = item return fragment.__html__()