root/tags/release-0.6.2/lib/misc.py

Revision 1, 3.7 kB (checked in by mjoc, 2 years ago)

Initial import.

Line 
1 # OpenDict
2 # Copyright (c) 2003 Martynas Jocius <mjoc@delfi.lt>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your opinion) any later version.
8 #
9 # This program is distributed in the hope that will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more detals.
13 #
14 # You shoud have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 # 02111-1307 USA
18 #
19 # Module: misc
20
21 from wxPython.wx import wxGetTranslation, wxGetApp
22 from os.path import *
23 import string
24 import traceback
25 import sys
26 import os
27
28 _ = wxGetTranslation
29
30
31 #
32 # FIXME: Remove
33 #
34 errors = {1: _("Not found"),
35           2: _("Dictionary error, please report to its author"),
36           3: _("Syntax error"),
37           4: _("You must be connected to the internet to use this dictionary"),
38           5: _("Time out"),
39           6: _("Bad encoding is set for this dictionary, try another")}
40
41
42 #
43 # Character Encodings
44 # FIXME: translations does not work, why?
45 #
46 encodings = {_("Unicode (UTF-8)"): "UTF-8",
47              _("Western (ISO-8859-1)"): "ISO-8859-1",
48              _("Central European (ISO-8859-2)"): "ISO-8859-2",
49              _("Nordic (ISO-8859-10)"): "ISO-8859-10",
50              _("South European (ISO-8859-3)"): "ISO-8859-3",
51              _("Greek (ISO-8859-7)"): "ISO-8859-7",
52              _("Baltic (ISO-8859-13)"): "ISO-8859-13",
53              _("Cyrillic (KOI8-R)"): "KOI8-R",
54              _("Arabic (ISO-8859-6)"): "ISO-8859-6"}
55
56 #
57 # Font faces
58 #
59 fontFaces = {"Fixed": "fixed",
60              "Helvetica": "helvetica",
61              "Courier": "courier",
62              "Times": "Times",
63              "Verdana": "Verdana",
64              "Lucida": "Lucida"}
65
66
67
68 def numVersion(str):
69     """Return a float number made from x.y.z[-preV] version number"""
70
71     nver = str.split('-')[0]
72     numbers = nver.split('.')
73     try:
74         return (float(numbers[0]) + float(numbers[1]) * 0.1 + float(number[2]) * 0.01)
75     except:
76         return 0.0
77
78 def printError():
79     print string.join(traceback.format_exception(sys.exc_info()[0],
80                                                  sys.exc_info()[1],
81                                                  sys.exc_info()[2]), "")
82
83
84 def getTraceback():
85     return string.join(traceback.format_exception(sys.exc_info()[0],
86                                                  sys.exc_info()[1],
87                                                  sys.exc_info()[2]), "")
88
89
90
91 def getFileSize(path):
92     """Returns the size of file in bytes"""
93    
94     size = -1
95    
96     try:
97         size = os.stat(path)[6]
98     except:
99         print "ERROR (misc.getFileSize): path '%s' does not exist" % path
100    
101     return size
102
103
104 def getDirSize(start, followLinks, myDepth, maxDepth):
105     """Return total directory size"""
106    
107     total = 0L
108     try:
109         dirList = os.listdir(start)
110     except:
111         if isdir(start):
112             print 'ERROR: Cannot list directory %s' % start
113         return 0
114    
115     for item in dirList:
116         path = '%s/%s' % (start, item)
117         try:
118             stats = os.stat(path)
119         except:
120             print 'ERROR: Cannot stat %s' % path
121             continue
122        
123         total += stats[6]
124         if isdir(path) and (followLinks or \
125                              (not followLinks and not islink(path))):
126             bytes = getDirSize(path, followLinks,
127                                myDepth + 1,
128                                maxDepth)
129             total += bytes
130
131     return total
132    
Note: See TracBrowser for help on using the browser.