File: tk.py

package info (click to toggle)
nvpy 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,208 kB
  • sloc: python: 3,185; makefile: 26; sh: 11
file content (51 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download
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
# nvPY: cross-platform note-taking app with simplenote syncing
# copyright 2012 by Charl P. Botha <cpbotha@vxlabs.com>
# new BSD license

# Tkinter and ttk documentation recommend pulling all symbols into client
# module namespace. I don't like that, so first pulling into this module
# tk, then can use tk.whatever in main module.

from tkinter import *
from tkinter.ttk import *


class Ucs4NotSupportedError(BaseException):
    def __init__(self, char):
        self.char = char

    def __str__(self):
        return ('non-BMP character {} is not supported.  '
                'Please rebuild python interpreter and libraries with UCS-4 support.  '
                'See https://github.com/cpbotha/nvpy/blob/master/docs/ucs-4.rst').format(self.char)


def with_ucs4_error_handling(fn):
    """ Catch the non-BMP character error and reraise the Ucs4NotSupportedError. """
    import functools

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        try:
            return fn(*args, **kwargs)
        except TclError as e:
            import re
            result = re.match(r'character (U\+[0-9a-f]+) is above the range \(U\+0000-U\+FFFF\) allowed by Tcl', str(e))
            if result:
                char = result.group(1)
                raise Ucs4NotSupportedError(char)
            raise

    return wrapper


########################################################################
# Apply the monkey patches for convert TclError to Ucs4NotSupportedError

_Text = Text


class Text(_Text):
    @with_ucs4_error_handling
    def insert(self, *args, **kwargs):
        return _Text.insert(self, *args, **kwargs)