Started working on some list classes to create nice categorised action lists.

This commit is contained in:
Raoul Snyman 2010-10-27 15:49:21 +02:00
parent 49faf7fb1f
commit 574d108a82

View File

@ -198,6 +198,127 @@ class AppLocation(object):
return path
class ActionCategory(object):
"""
The :class:`~openlp.core.utils.ActionCategory` class encapsulates a
category for the :class:`~openlp.core.utils.CategoryList` class.
"""
def __init__(self, name, weight=0):
self.name = name
self.weight = weight
self.actions = CategoryActionList()
class CategoryActionList(object):
"""
The :class:`~openlp.core.utils.CategoryActionList` class provides a sorted
list of actions within a category.
"""
def __init__(self):
self.index = 0
self.actions = []
def __iter__(self):
return self
def __next__(self):
"""
Python 3 "next" method.
"""
if self.index >= len(self.actions):
raise StopIteration
else:
self.index += 1
return self.actions[self.index - 1][0]
def next(self):
"""
Python 2 "next" method.
"""
return self.__next__()
def add(self, action, weight=0):
self.actions.append((weight, action))
self.actions.sort(key=lambda act: act[0])
class CategoryList(object):
"""
The :class:`~openlp.core.utils.ActionListCategory` class encapsulates a
category list for the :class:`~openlp.core.utils.ActionList` class and
provides an iterator interface for walking through the list of actions in
this category.
"""
def __init__(self):
self.index = 0
self.categories = []
def __iter__(self):
return self
def __next__(self):
"""
Python 3 "next" method for iterator.
"""
if self.index >= len(self.categories):
raise StopIteration
else:
self.index += 1
return self.categories[self.index - 1][0]
def next(self):
"""
Python 2 "next" method for iterator.
"""
return self.__next__()
def add(self, name, weight=0):
self.categories.append(ActionCategory(name, weight))
self.categories.sort(key=lambda cat: cat.weight)
class ActionList(object):
"""
The :class:`~openlp.core.utils.ActionList` class contains a list of menu
actions and categories associated with those actions. Each category also
has a weight by which it is sorted when iterating through the list of
actions or categories.
"""
def __init__(self):
self.categories = CategoryList()
def add_category(self, category, weight=0):
"""
Add a category to the action list, ordered by ``weight``.
``category``
The name of the category.
``weight``
**Defaults to 0.** The weight of the category. The weight
determines the sort order, with negative items appearing
higher than positive items.
"""
self.categories.add(category, weight)
def has_category(self, category):
for cat in self.categories:
if cat[u'name'] == category:
return True
return False
def add_action(self, action, category=u'Default', weight=0):
if not self.has_category:
self.add_category(category)
for index, cat in enumerate(self.categories):
if cat[u'name'] == category:
self.categories[index][u'actions'].append((weight, action))
return
def check_latest_version(current_version):
"""
Check the latest version of OpenLP against the version file on the OpenLP