Remove unused imports, fix too long lines and indentions.
[Melange.git] / app / soc / logic / menu.py
blobd207496620fbb5acbc0cbe852fc46ffff4b82e4d
1 #!/usr/bin/python2.5
3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Representations and manipulation of arbitrarily nested menus.
18 """
20 __authors__ = [
21 '"Todd Larsen" <tlarsen@google.com>',
25 from django.utils import datastructures
28 class Menu:
29 """Ordered collection of MenuItem objects.
31 MenuItems are retrievable as an ordered list or individually by their
32 MenuItem.text key.
33 """
35 def __init__(self, items=None):
36 """Initializes ordered list of MenuItems.
38 Args:
39 items: list of MenuItem objects in display order
40 """
41 if not items:
42 items = []
44 items = [(i.name, i) for i in items]
45 self._items = datastructures.SortedDict(data=items)
47 def getItem(self, name):
48 """Returns a MenuItem retrieved by its MenuItem.text."""
49 return self._items.get(name)
51 def setItem(self, item):
52 """Overwrites an existing MenuItem, or appends a new one."""
53 self._items[item.name] = item
55 def delItem(self, name):
56 """Removes an existing MenuItem."""
57 del self._items[name]
59 def _getItems(self):
60 """Returns an ordered list of the MenuItems."""
61 return self._items.values()
63 items = property(_getItems, doc=
64 """Read-only list of MenuItems, for use in templates.""")
67 class MenuItem:
68 """Provides menu item properties as easily-accessible attributes.
69 """
71 def __init__(self, name, value=None, selected=False, annotation=None,
72 sub_menu=None):
73 """Initializes the menu item attributes from supplied arguments.
75 Args:
76 name: name of the menu item
77 value: optional value associated with the menu item;
78 default is None
79 selected: Boolean indicating if this menu item is selected;
80 default is False
81 annotation: optional annotation associated with the menu item;
82 default is None
83 sub_menu: a Menu of sub-items to display below this menu item;
84 default is None, indicating no sub-menu
85 """
86 self.name = name
87 self.value = value
88 self.selected = selected
89 self.annotation = annotation
90 self.sub_menu = sub_menu