Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / environment.py
1 # pylint: disable-msg=W0222
2
3 import sys
4 import os
5 import re
6
7 import pygtk
8 try:
9     pygtk.require("2.0")
10 except AssertionError:
11     # pygtk.require("2.0") fails in the py2exe installer. Don't know why.
12     pass
13 import gtk
14 import gtk.gdk
15 import gtk.glade
16 from kofoto.gkofoto import crashdialog
17 sys.excepthook = crashdialog.show
18
19 from kofoto.clientenvironment import ClientEnvironment, ClientEnvironmentError
20 from kofoto.gkofoto.cachingpixbufloader import CachingPixbufLoader
21
22 class WidgetsWrapper:
23     def __init__(self):
24         self.widgets = gtk.glade.XML(env.gladeFile, "mainWindow")
25
26     def __getitem__(self, key):
27         return self.widgets.get_widget(key)
28
29 class Environment(ClientEnvironment):
30     def __init__(self):
31         ClientEnvironment.__init__(self)
32         self.startupNotices = []
33         self.openCommand = None
34         self.loadingPixbuf = None
35         self.gladeFile = None
36         self.unknownImageIconFileName = None
37         self.albumIconFileName = None
38         self.thumbnailSize = None
39         self.albumIconPixbuf = None
40         self.unknownImageIconPixbuf = None
41         self.defaultSortColumn = None
42         self.isDebug = None
43         self.clipboard = None
44         self.defaultTableViewColumns = None
45         self.iconDir = None
46         self.widgets = None
47         self.rotateRightCommand = None
48         self.rotateLeftCommand = None
49         self.pixbufLoader = CachingPixbufLoader()
50         self.pixbufLoader.set_pixel_limit(2 * 10**7) # TODO: Make configurable.
51
52     def setup(self, bindir, isDebug=False, configFileLocation=None,
53               shelfLocation=None):
54         try:
55             ClientEnvironment.setup(self, configFileLocation, shelfLocation)
56         except ClientEnvironmentError, e:
57             self.startupNotices += [e[0]]
58             return False
59
60         self.isDebug = isDebug
61         self.thumbnailSize = self.config.getcoordlist(
62             "gkofoto", "thumbnail_size_limit")[0]
63         self.defaultTableViewColumns = re.findall(
64             "\S+",
65             self.config.get("gkofoto", "default_table_columns"))
66         if not "versions" in self.defaultTableViewColumns:
67             # Ugly, temporary hack:
68             self.defaultTableViewColumns.insert(1, "versions")
69         self.defaultSortColumn = self.config.get(
70             "gkofoto", "default_sort_column")
71         self.openCommand = self.config.get(
72             "gkofoto", "open_command", True)
73         self.rotateRightCommand = self.config.get(
74             "gkofoto", "rotate_right_command", True)
75         self.rotateLeftCommand = self.config.get(
76             "gkofoto", "rotate_left_command", True)
77
78         # Case 1: Running from normal UNIX or Windows non-py2exe installation.
79         dataDir = os.path.join(bindir, "..", "share", "gkofoto")
80         if not os.path.isdir(dataDir):
81             # Case 2: Running from Windows py2exe installation.
82             dataDir = os.path.join(bindir, "share", "gkofoto")
83             if not os.path.isdir(dataDir):
84                 # Case 3: Running from source code.
85                 dataDir = bindir
86         self.iconDir = os.path.join(dataDir, "icons")
87         self.gladeFile = os.path.join(dataDir, "glade", "gkofoto.glade")
88         self.albumIconFileName = os.path.join(self.iconDir, "album.png")
89         self.albumIconPixbuf = gtk.gdk.pixbuf_new_from_file(self.albumIconFileName)
90         self.loadingPixbuf = self.albumIconPixbuf # TODO: create another icon with a hour-glass or something
91         self.unknownImageIconFileName = os.path.join(self.iconDir, "unknownimage.png")
92         self.unknownImageIconPixbuf = gtk.gdk.pixbuf_new_from_file(self.unknownImageIconFileName)
93         from kofoto.gkofoto.clipboard import Clipboard
94         self.clipboard = Clipboard()
95
96         self.widgets = WidgetsWrapper()
97
98         return True
99
100     def _writeInfo(self, infoString):
101         self.startupNotices += [infoString]
102
103     def debug(self, msg):
104         if self.isDebug:
105             print msg
106
107     def enter(self, method):
108         if self.isDebug:
109             print "-->", method
110
111     def exit(self, method):
112         if self.isDebug:
113             print "<--", method
114
115     def assertUnicode(self, obj):
116         assert isinstance(obj, unicode), \
117                "%s is not a unicode object: \"%s\"" % (type(obj), obj)
118
119 env = Environment()