2
# -*- mode: python; coding: utf-8; -*-
3
##---------------------------------------------------------------------------##
5
## Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
6
## Copyright (C) 2003 Mt. Hood Playing Card Co.
7
## Copyright (C) 2005-2009 Skomoroh
9
## This program is free software: you can redistribute it and/or modify
10
## it under the terms of the GNU General Public License as published by
11
## the Free Software Foundation, either version 3 of the License, or
12
## (at your option) any later version.
14
## This program is distributed in the hope that it will be useful,
15
## but WITHOUT ANY WARRANTY; without even the implied warranty of
16
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
## GNU General Public License for more details.
19
## You should have received a copy of the GNU General Public License
20
## along with this program. If not, see <http://www.gnu.org/licenses/>.
22
##---------------------------------------------------------------------------##
26
import sys, os, time, types, locale
30
from cPickle import Pickler, Unpickler, UnpicklingError
32
from pickle import Pickler, Unpickler, UnpicklingError
39
from settings import PACKAGE, TOOLKIT
41
Image = ImageTk = ImageOps = None
53
import JpegImagePlugin
56
Image._initialized = 2
59
# ************************************************************************
61
# ************************************************************************
63
class SubclassResponsibility(Exception):
67
# ************************************************************************
69
# ************************************************************************
72
def latin1_to_ascii(n):
74
n = n.encode('iso8859-1', 'replace')
75
## FIXME: rewrite this for better speed
76
n = (n.replace("\xc4", "Ae")
77
.replace("\xd6", "Oe")
78
.replace("\xdc", "Ue")
79
.replace("\xe4", "ae")
80
.replace("\xf6", "oe")
81
.replace("\xfc", "ue"))
86
##print 'format_time:', t
87
if t <= 0: return "0:00"
88
if t < 3600: return "%d:%02d" % (t / 60, t % 60)
89
return "%d:%02d:%02d" % (t / 3600, (t % 3600) / 60, t % 60)
92
def print_err(s, level=1):
94
ss = PACKAGE+': ERROR:'
96
ss = PACKAGE+': WARNING:'
98
ss = PACKAGE+': DEBUG WARNING:'
99
print >> sys.stderr, ss, s.encode(locale.getpreferredencoding())
103
# ************************************************************************
104
# * misc. portab stuff
105
# ************************************************************************
109
return win32_getusername()
110
user = os.environ.get("USER","").strip()
112
user = os.environ.get("LOGNAME","").strip()
116
def getprefdir(package):
118
return win32_getprefdir(package)
119
home = os.environ.get("HOME", "").strip()
120
if not home or not os.path.isdir(home):
122
return os.path.join(home, ".pysolfc")
125
# high resolution clock() and sleep()
128
if os.name == "posix":
131
# ************************************************************************
133
# ************************************************************************
135
def win32_getusername():
136
user = os.environ.get('USERNAME','').strip()
138
user = unicode(user, locale.getpreferredencoding())
143
def win32_getprefdir(package):
144
portprefdir = 'config' # portable varsion
145
if os.path.isdir(portprefdir):
147
# %USERPROFILE%, %APPDATA%
148
hd = os.environ.get('APPDATA')
150
hd = os.path.expanduser('~')
151
if hd == '~': # win9x
152
hd = os.path.abspath('/windows/Application Data')
153
if not os.path.exists(hd):
154
hd = os.path.abspath('/')
155
return os.path.join(hd, 'PySolFC')
158
# ************************************************************************
160
# ************************************************************************
163
# assist in breaking circular references
165
assert isinstance(obj, types.InstanceType)
166
for k in obj.__dict__.keys():
167
obj.__dict__[k] = None
168
##del obj.__dict__[k]
171
# ************************************************************************
173
# ************************************************************************
176
def __init__(self, **kw):
177
self.__dict__.update(kw)
180
return str(self.__dict__)
182
def __setattr__(self, key, value):
183
if key not in self.__dict__:
184
raise AttributeError(key)
185
self.__dict__[key] = value
187
def addattr(self, **kw):
188
for key in kw.keys():
189
if hasattr(self, key):
190
raise AttributeError(key)
191
self.__dict__.update(kw)
193
def update(self, dict):
194
for key in dict.keys():
195
if key not in self.__dict__:
196
raise AttributeError(key)
197
self.__dict__.update(dict)
200
for key in self.__dict__.keys():
201
if isinstance(key, list):
202
self.__dict__[key] = []
203
elif isinstance(key, tuple):
204
self.__dict__[key] = ()
205
elif isinstance(key, dict):
206
self.__dict__[key] = {}
208
self.__dict__[key] = None
212
c.__dict__.update(self.__dict__)
216
# ************************************************************************
217
# * keyword argument util
218
# ************************************************************************
220
# update keyword arguments with default arguments
221
def kwdefault(kw, **defaults):
222
for k, v in defaults.items():
228
def __init__(self, kw={}, **defaults):
229
if isinstance(kw, KwStruct):
231
if isinstance(defaults, KwStruct):
232
defaults = defaults.__dict__
235
for k, v in defaults.items():
238
self.__dict__.update(kw)
240
def __setattr__(self, key, value):
241
if key not in self.__dict__:
242
raise AttributeError(key)
243
self.__dict__[key] = value
245
def __getitem__(self, key):
246
return getattr(self, key)
248
def get(self, key, default=None):
249
return self.__dict__.get(key, default)
255
# ************************************************************************
257
# ************************************************************************
259
def pickle(obj, filename, protocol=0):
262
f = open(filename, "wb")
263
p = Pickler(f, protocol)
266
##print "Pickled", filename
271
def unpickle(filename):
274
f = open(filename, "rb")
279
##print "Unpickled", filename
285
# ************************************************************************
287
# ************************************************************************
292
except OSError: # raised on windows if link is unreadable