Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / objectcollectionfactory.py
1 import gtk
2 from kofoto.gkofoto.searchresult import SearchResult
3 from kofoto.gkofoto.albummembers import AlbumMembers
4 from kofoto.gkofoto.environment import env
5 from kofoto.search import BadTokenError, ParseError, UnterminatedStringError
6 from kofoto.shelf import \
7     AlbumDoesNotExistError, BadAlbumTagError, CategoryDoesNotExistError, \
8     verifyValidAlbumTag
9
10 class ObjectCollectionFactory:
11
12 ######################################################################
13 ### Public functions and constants
14
15     def __init__(self):
16         env.debug("Init ObjectCollectionFactory")
17         self.__searchResult = SearchResult()
18         self.__albumMembers = AlbumMembers()
19
20     def getObjectCollection(self, query, filterText=""):
21         env.debug("Object collection factory loading query: " + query)
22         self.__clear()
23         validAlbumTag = False
24         if query and query[0] == "/":
25             try:
26                 verifyValidAlbumTag(query[1:])
27                 validAlbumTag = True
28             except BadAlbumTagError:
29                 pass
30         try:
31             if validAlbumTag and not filterText:
32                 self.__albumMembers.loadAlbum(env.shelf.getAlbumByTag(query[1:]))
33                 return self.__albumMembers
34             else:
35                 if filterText:
36                     queryWithFilter = "(%s) and (%s)" % (query, filterText)
37                 else:
38                     queryWithFilter = query
39                 self.__searchResult.loadQuery(queryWithFilter)
40                 if not validAlbumTag:
41                     env.mainwindow.unselectAlbumTree()
42                 return self.__searchResult
43         except AlbumDoesNotExistError, tag:
44             errorText = "No such album tag: \"%s\"." % tag
45         except CategoryDoesNotExistError, tag:
46             errorText = "No such category tag: \"%s\"." % tag
47         except BadTokenError, pos:
48             errorText = "Error parsing query: bad token starting at position %s: \"%s\"." % (
49                 pos,
50                 query[pos[0]:])
51         except UnterminatedStringError, e:
52             errorText = "Error parsing query: unterminated string starting at position %s: \"%s\"." % (
53                 e.args[0],
54                 query[e.args[0]:])
55         except ParseError, text:
56             errorText = "Error parsing query: %s." % text
57         dialog = gtk.MessageDialog(
58             type=gtk.MESSAGE_ERROR,
59             buttons=gtk.BUTTONS_OK,
60             message_format=errorText)
61         dialog.run()
62         dialog.destroy()
63         self.__searchResult = SearchResult()
64         return self.__searchResult
65
66
67 ######################################################################
68 ### Private functions
69
70     def __clear(self):
71         self.__searchResult.clear()
72         self.__albumMembers.clear()