Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / generatehtmldialog.py
1 import gtk
2 import os
3 import re
4 from kofoto.gkofoto.environment import env
5 import kofoto.generate
6 import kofoto.shelfexceptions
7
8 class GenerateHTMLDialog:
9     def __init__(self, album):
10         self.album = album
11         self.widgets = gtk.glade.XML(
12             env.gladeFile, "generateHtmlDialog")
13         self.dialog = self.widgets.get_widget("generateHtmlDialog")
14         self.browseButton = self.widgets.get_widget("browseButton")
15         self.cancelButton = self.widgets.get_widget("cancelButton")
16         self.directoryTextEntry = self.widgets.get_widget("directoryTextEntry")
17         self.generateButton = self.widgets.get_widget("generateButton")
18
19         self.browseButton.connect("clicked", self._onBrowse)
20         self.cancelButton.connect("clicked", self._onCancel)
21         self.generateButton.connect("clicked", self._onGenerate)
22
23         self.directoryTextEntry.connect(
24             "changed", self._onDirectoryTextEntryModified)
25
26         self.generateButton.set_sensitive(False)
27
28     def run(self):
29         self.dialog.show()
30
31     def _onDirectoryTextEntryModified(self, *unused):
32         self.generateButton.set_sensitive(
33             os.path.isdir(self.directoryTextEntry.get_text()))
34
35     def _onBrowse(self, *unused):
36         dirDialog = gtk.FileChooserDialog(
37             title="Choose directory",
38             action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
39             buttons=(
40                 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
41                 gtk.STOCK_OK, gtk.RESPONSE_OK))
42         if dirDialog.run() == gtk.RESPONSE_OK:
43             self.directoryTextEntry.set_text(dirDialog.get_filename())
44         dirDialog.destroy()
45
46     def _onCancel(self, *unused):
47         self.dialog.destroy()
48
49     def _onGenerate(self, *unused):
50         for widget in [self.directoryTextEntry, self.browseButton,
51                        self.cancelButton, self.generateButton]:
52             widget.set_sensitive(False)
53         self._generate(self.directoryTextEntry.get_text())
54         self.dialog.destroy()
55
56     def _generate(self, directoryName):
57         # TODO: Rewrite this gross hack.
58
59         def outputParser(string):
60             m = re.match(
61                 r"Creating album (\S+) \((\d+) of (\d+)\)",
62                 string)
63             if m:
64                 progressBar.set_text(m.group(1).decode(env.localeEncoding))
65                 progressBar.set_fraction(
66                     (int(m.group(2)) - 1) / float(m.group(3)))
67                 while gtk.events_pending():
68                     gtk.main_iteration()
69
70         progressBar = self.widgets.get_widget("progressBar")
71
72         env.out = outputParser
73         env.verbose = True
74         env.thumbnailsizelimit = env.config.getcoordlist(
75             "album generation", "thumbnail_size_limit")[0]
76         env.defaultsizelimit = env.config.getcoordlist(
77             "album generation", "default_image_size_limit")[0]
78
79         imgsizesval = env.config.getcoordlist(
80             "album generation", "other_image_size_limits")
81         imgsizesset = set(imgsizesval) # Get rid of duplicates.
82         defaultlimit = env.config.getcoordlist(
83             "album generation", "default_image_size_limit")[0]
84         imgsizesset.add(defaultlimit)
85         imgsizes = sorted(
86             imgsizesset, key=lambda x, y: cmp(x[0] * x[1], y[0] * y[1]))
87         env.imagesizelimits = imgsizes
88
89         try:
90             generator = kofoto.generate.Generator(u"woolly", env)
91             generator.generate(self.album, None, directoryName, "utf-8")
92             progressBar.set_fraction(1)
93             while gtk.events_pending():
94                 gtk.main_iteration()
95         except (IOError, kofoto.shelfexceptions.KofotoError), e:
96             dialog = gtk.MessageDialog(
97                 type=gtk.MESSAGE_ERROR,
98                 buttons=gtk.BUTTONS_OK,
99                 message_format="Error: \"%s\"" % e)
100             dialog.run()
101             dialog.destroy()