File: utilities.py

package info (click to toggle)
python-bumps 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,688 kB
  • sloc: python: 24,446; ansic: 4,973; cpp: 4,849; javascript: 639; xml: 493; makefile: 147; perl: 108; sh: 94
file content (473 lines) | stat: -rw-r--r-- 16,655 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# Copyright (C) 2006-2011, University of Maryland
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/ or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Author: James Krycka

"""
This module contains utility functions and classes for the application.
"""

#==============================================================================
from __future__ import print_function

import os
import sys
import time
import glob

import wx
from wx.lib import delayedresult

# CRUFT: wx 3/4
phoenix = wx.version() >= '4.0'
BitmapFromImage = wx.Bitmap if phoenix else wx.BitmapFromImage


# Text string used to compare the string width in pixels for different fonts.
# This benchmark string has 273 characters, containing 92 distinct characters
# consisting of the lowercase alpha chars in the ratio used in an English
# Scrabble(TM) set, two sets of uppercase alpha chars, two sets of digits,
# special chars with multiples of commonly used ones, and many spaces to
# approximate spacing between words in sentences and labels.
BENCHMARK_TEXT =\
"aaaaaaaaa bb cc dddd eeeeeeeeeeee ff ggg hh iiiiiiiii j k llll mm "\
"nnnnnn oooooooo pp q rrrrrr ssss tttttt uuuu vv ww x yy z "\
"ABCD EFGH IJKL MNOP QRST UVW XYZ ABCD EFGH IJKL MNOP QRST UVW XYZ "\
"01234 56789 01234 56789 "\
"...... :::: ()()() \"\",,'' ++-- **//== {}[]<> ;|~\\_ ?!@#$%^&"

# The width and height in pixels of the test string using MS Windows default
# font "MS Shell Dlg 2" and a dpi of 96.
# Note: the MS Windows XP default font has the same width and height as Tahoma.
BENCHMARK_WIDTH = 1600
BENCHMARK_HEIGHT = 14

#==============================================================================

def choose_fontsize(fontname=None):
    """
    Determines the largest font size (in points) to use for a given font such
    that the rendered width of the benchmark string is less than or equal to
    101% of the rendered width of the string on a Windows XP computer using the
    Windows default font at 96 dpi.

    The width in pixels of a rendered string is affected by the choice of font,
    the point size of the font, and the resolution of the installed font as
    measured in dots-per-inch (aka points-per-inch).
    """

    frame = wx.Frame(parent=None, id=wx.ID_ANY, title="")
    if fontname is None:
        fontname = frame.GetFont().GetFaceName()
    max_width = BENCHMARK_WIDTH + BENCHMARK_WIDTH/100

    for fontsize in range(12, 5, -1):
        frame.SetFont(wx.Font(fontsize, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
                              fontname))
        benchmark = wx.StaticText(frame, wx.ID_ANY, label="")
        w, h = benchmark.GetTextExtent(BENCHMARK_TEXT)
        benchmark.Destroy()
        if w <= max_width: break

    frame.Destroy()
    return fontsize


def display_fontsize(fontname=None, benchmark_text=BENCHMARK_TEXT,
                                    benchmark_width=BENCHMARK_WIDTH,
                                    benchmark_height=BENCHMARK_HEIGHT):
    """
    Displays the width in pixels of a benchmark text string for a given font
    at various point sizes when rendered on the application's output device
    (which implicitly takes into account the resolution in dpi of the font
    faces at the various point sizes).
    """

    # Create a temporary frame that we will soon destroy.
    frame = wx.Frame(parent=None, id=wx.ID_ANY, title="")

    # Set the fontname if one is given, otherwise use the system default font.
    # Get the font name even if we just set it in case the specified font is
    # not installed and the system chooses another one.
    if fontname is not None:
        frame.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
                              fontname))
    fontname = frame.GetFont().GetFaceName()

    x, y = wx.ClientDC(frame).GetPPI()
    print("*** Benchmark text width and height in pixels = %4d %2d"\
          %(benchmark_width, benchmark_height))
    print("*** Compare against %s font with dpi resolution of %d:"\
          %(fontname, x))

    for fontsize in range(12, 5, -1):
        frame.SetFont(wx.Font(fontsize, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
                              fontname))
        benchmark = wx.StaticText(frame, wx.ID_ANY, label="")
        w, h = benchmark.GetTextExtent(benchmark_text)
        benchmark.Destroy()
        print("      For point size %2d, benchmark text w, h = %4d  %2d"\
              %(fontsize, w, h))

    frame.Destroy()

def _finddata():
    patterns = ['*.png','*.ico','*.jpg']
    path = resource_dir()
    files = []
    for p in patterns:
        files += glob.glob(os.path.join(path,p))
    return files

def data_files():
    """
    Return the data files associated with the package.

    The format is a list of (directory, [files...]) pairs which can be
    used directly in the py2exe setup script as::

        setup(...,
              data_files=data_files(),
              ...)
    """
    data_files = [('bumps-data', _finddata())]
    return data_files

def package_data():
    """
    Return the data files associated with the package.

    The format is a dictionary of {'fully.qualified.module', [files...]}
    used directly in the setup script as::

        setup(...,
              package_data=package_data(),
              ...)
    """
    return { 'bumps.gui': _finddata() }

self_cached_path = None
def resource_dir():
    """
    Return the path to the application data.

    This is either in the environment variable BUMPS_DATA, in the
    source tree in gui/resources, or beside the executable in
    bumps-data.
    """
    # If we already found it, then we are done
    global self_cached_path
    if self_cached_path is not None: return self_cached_path

    # Check for data path in the environment
    key = 'BUMPS_DATA'
    if key in os.environ:
        path = os.environ[key]
        if not os.path.isdir(path):
            raise RuntimeError('Path in environment %s not a directory'%key)
        self_cached_path = path
        return self_cached_path

    # Check for data path in the package
    path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'resources'))
    #print >>sys.stderr, "checking for resource in",path
    if os.path.isdir(path):
        self_cached_path = path
        return self_cached_path

    # Check in package root, which is where pyinstaller puts it
    root = os.path.dirname(os.path.dirname(os.path.dirname(path)))
    path = os.path.join(root, 'bumps-data')
    if os.path.isdir(path):
        self_cached_path = path
        return self_cached_path

    # Check for data path next to exe/zip file.
    exepath = os.path.dirname(sys.executable)
    path = os.path.join(exepath,'bumps-data')
    #print >>sys.stderr, "checking for resource in",path
    if os.path.isdir(path):
        self_cached_path = path
        return self_cached_path

    # py2app puts the data in Contents/Resources, but the executable
    # is in Contents/MacOS.
    path = os.path.join(exepath,'..','Resources','bumps-data')
    #print >>sys.stderr, "checking for resource in",path
    if os.path.isdir(path):
        self_cached_path = path
        return self_cached_path

    raise RuntimeError('Could not find the Bumps data files')

def resource(filename):
    return os.path.join(resource_dir(),filename)

def get_bitmap(filename, type=wx.BITMAP_TYPE_PNG, scale_factor=16):
    """
    Returns the scaled bitmap from an image file (bmp, jpg, png) stored in
    the data directory of the package.
    """

    path = resource(filename)

    return BitmapFromImage(wx.Image(name=path, type=type)
                           .Scale(scale_factor, scale_factor))


def popup_error_message(caption, message):
    """Displays an error message in a pop-up dialog box with an OK button."""

    msg = wx.MessageDialog(None, message, caption, style=wx.ICON_ERROR|wx.OK)
    msg.ShowModal()
    msg.Destroy()


def popup_information_message(caption, message):
    """Displays an informational message in a pop-up with an OK button."""

    msg = wx.MessageDialog(None, message, caption,
                           style=wx.ICON_INFORMATION|wx.OK)
    msg.ShowModal()
    msg.Destroy()


def popup_question(caption, message):
    """Displays a question in a pop-up dialog box with YES and NO buttons."""

    msg = wx.MessageDialog(None, message, caption,
                           style=wx.ICON_QUESTION|wx.YES_NO)
    msg.ShowModal()
    msg.Destroy()


def popup_warning_message(caption, message):
    """Displays a warning message in a pop-up dialog box with an OK button."""

    msg = wx.MessageDialog(None, message, caption, style=wx.ICON_WARNING|wx.OK)
    msg.ShowModal()
    msg.Destroy()

#==============================================================================

class StatusBarInfo():
    """This class writes, saves, and restores multi-field status bar text."""

    def __init__(self):
        frame = wx.FindWindowByName("AppFrame", parent=None)
        self.sb = frame.GetStatusBar()
        self.cnt = self.sb.GetFieldsCount()
        self.field = [""]*self.cnt


    def write(self, index=0, text=""):
        # Write text to the specified slot and save text locally.
        # Beware that if you use field 0, wxPython will likely overwite it.
        if index > self.cnt - 1:
            return
        self.sb.SetStatusText(text, index)
        self.field[index] = text


    def restore(self):
        # Restore saved text from fields 1 to n.
        # Note that wxPython updates field 0 with hints and other messages.
        for index in range(1, self.cnt):
            self.sb.SetStatusText(self.field[index], index)

#==============================================================================

class ExecuteInThread():
    """
    This class executes the specified function in a separate thread and calls a
    designated callback function when the execution completes.  Control is
    immediately given back to the caller of ExecuteInThread which can execute
    in parallel in the main thread.

    Note that wx.lib.delayedresult provides a simple interface to threading
    that does not include mechanism to stop the thread.
    """

    def __init__(self, callback, function, *args, **kwargs):
        if callback is None: callback = self._callback
        #print "*** ExecuteInThread init:", callback, function, args, kwargs
        delayedresult.startWorker(consumer=callback, workerFn=function,
                                  wargs=args, wkwargs=kwargs)

    def _callback(self, delayedResult):
        '''
        jobID = delayedResult.getJobID()
        assert jobID == self.jobID
        try:
            result = delayedResult.get()
        except Exception, e:
            popup_error_message(self, "job %s raised exception: %s"%(jobID, e)
            return
        '''
        return

#==============================================================================

class WorkInProgress(wx.Panel):
    """
    This class implements a rotating 'work in progress' gauge.
    """

    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        self.gauge = wx.Gauge(self, wx.ID_ANY, range=50, size=(250, 25))

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.TimerHandler)
        #self.count = 0

    def Start(self):
        self.timer.Start(100)

    def Stop(self):
        self.timer.Stop()

    def TimerHandler(self, event):
        #self.count += 1
        #print "*** count = ", self.count
        self.gauge.Pulse()

#==============================================================================

log_time_handle = None  # global variable for holding TimeStamp instance handle

def log_time(text=None, reset=False):
    """
    This is a convenience function for using the TimeStamp class from any
    module in the application for logging elapsed and delta time information.
    This data is prefixed by a timestamp and optionally suffixed by a comment.
    log_time maintains a single instance of TimeStamp during program execution.
    Example output from calls to log_time('...'):

    ==>    0.000s    0.000s  Starting <application name>
    ==>    0.016s    0.016s  Starting to display the splash screen
    ==>    0.015s    0.031s  Starting to build the GUI application
    ==>    0.094s    0.125s  Entering the event loop
    ==>    2.906s    3.031s  Terminating the splash screen and showing the GUI
    """

    global log_time_handle
    if log_time_handle is None:
        log_time_handle = TimeStamp()
    if reset:
        log_time_handle.reset()
    log_time_handle.log_interval(text=text)


class TimeStamp():
    """
    This class provides timestamp, delta time, and elapsed time services for
    displaying wall clock time usage by the application.
    """

    def __init__(self):
        self.reset()


    def reset(self):
        # Starts new timing interval.
        self.t0 = self.t1 = time.time()


    def gettime3(self):
        # Gets current time in timestamp, delta time, and elapsed time format.
        now = time.time()
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now))
        elapsed = now - self.t0
        delta = now - self.t1
        self.t1 = now
        return timestamp, delta, elapsed


    def gettime2(self):
        # Gets current time in delta time and elapsed time format.
        now = time.time()
        elapsed = now - self.t0
        delta = now - self.t1
        self.t1 = now
        return delta, elapsed


    def log_time_info(self, text=""):
        # Prints timestamp, delta time, elapsed time, and optional comment.
        t, d, e = self.gettime3()
        print("==> %s%9.3fs%9.3fs  %s" %(t, d, e, text))


    def log_timestamp(self, text=""):
        # Prints timestamp and optional comment.
        t, d, e = self.gettime3()
        print("==> %s  %s" %(t, text))


    def log_interval(self, text=""):
        # Prints elapsed time, delta time, and optional comment.
        d, e = self.gettime2()
        print("==>%9.3fs%9.3fs  %s" %(d, e, text))

#==============================================================================

if __name__ == '__main__':
    # Test the display_fontsize and choose_fontsize functions.
    app = wx.PySimpleApp()
    print("For Arial font:")
    display_fontsize(fontname="Arial")
    print("    Calculated font size =", choose_fontsize(fontname="Arial"))
    app.Destroy()

    print("")
    print("*** Data directory is:          ", resource_dir())

    # Test the TimeStamp class and the convenience function.
    print("")
    log_time("Using log_time() function")
    print("Sleeping for 0.54 seconds ...")
    time.sleep(0.54)
    log_time("Using log_time() function")
    print("Sleeping for 0.83 seconds ...")
    time.sleep(0.83)
    log_time("Using log_time() function")
    print("Creating an instance of TimeStamp (as the second timing class)")
    ts = TimeStamp()
    print("Sleeping for 0.66 seconds ...")
    time.sleep(0.66)
    ts.log_time_info(text="Using log_time_info() method")
    ts.log_timestamp(text="Using log_timestamp() method")
    ts.log_interval(text="Using log_interval() method")
    print("Sleeping for 0.35 seconds ...")
    time.sleep(0.35)
    ts.log_interval(text="Using log_interval() method")
    print("Sleeping for 0.42 seconds ...")
    time.sleep(0.42)
    ts.log_interval(text="Using log_interval() method")
    print("Resetting the clock ...")
    ts.reset()
    ts.log_interval(text="Using log_interval() method")
    print("Sleeping for 0.33 seconds ...")
    time.sleep(0.33)
    ts.log_interval(text="Using log_interval() method")
    print("Switch back to the first timing class")
    log_time("Using log_time() function")