1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
7 # Created by David on 7/30/08.
8 # Copyright (c) 2009 David Fooshee. All rights reserved.
10 from Foundation
import *
16 class PyDict (NSObject
):
17 # Making class instance vars available across the PyObjC bridge:
18 textInput
= objc
.ivar(u
"textInput")
19 foundMatches
= objc
.ivar(u
"foundMatches")
20 simpDict
= objc
.ivar(u
"simpDict")
21 tradDict
= objc
.ivar(u
"tradDict")
22 boardChangeCount
= objc
.ivar(u
"boardChangeCount")
23 board
= objc
.ivar(u
"board")
24 userDefaultsDict
= objc
.ivar(u
"userDefaultsDict")
25 winCon
= objc
.ivar(u
"winCon")
29 self
= super(PyDict
, self
).init()
30 if self
is None: return None
31 NSTimer
.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1.0, self
, 'setTextOutput:', None, True)
32 self
.foundMatches
= ""
34 # Process the CEDICT file data to build our dictionaries
35 # We'll do this in a separate thread so as not to delay UI boot speed
36 t
= NSThread
.alloc().initWithTarget_selector_object_(self
, self
.buildDictionaries
, None)
39 NSLog("Initialization completed")
42 # Load and read the cedict file linewise, in a unicode format, into a list of definitions:
44 dictFile
= codecs
.open(os
.path
.join(NSBundle
.mainBundle().resourcePath(), "cedict.u8" ), encoding
='utf-8')
45 except (IOError, OSError):
46 NSLog("Could not open the CEDICT dictionary file.")
49 dict = dictFile
.readlines()
51 # Making some dictionaries
57 # Make a pasteboard object with which to monitor the system pasteboard for changes
58 board
= NSPasteboard
.generalPasteboard()
64 "rememberWindowSize" : False,
65 "rememberWindowLocation" : True }
67 userDefaults
= NSUserDefaults
.standardUserDefaults()
68 userDefaults
.registerDefaults_(userDefaultsDict
)
69 NSUserDefaultsController
.sharedUserDefaultsController().setInitialValues_(userDefaultsDict
)
72 if userDefaults
.objectForKey_("windowOnTop") is True:
73 NSLog("window on top is true!!")
75 NSLog(str(userDefaultsDict
))
79 # init the counter to None so we pick up whatever is already on the clipboard at app launch
80 boardChangeCount
= None
86 winCon
= NSWindowController
.alloc().initWithWindowNibName_("MainMenu")
91 def keepWindowOnTopToggle_(self
, sender
):
92 NSLog("keepWindowOnTop checkbox was toggled")
93 NSLog("value is now:")
94 NSLog(str(self
.userDefaults
.objectForKey_("windowOnTop")))
97 def buildDictionaries(self
):
99 # New thread, needs it's own autoreleasepool
100 pool
= NSAutoreleasePool
.alloc().init()
102 # Here we build up the Python dictionaries to be used for fast searching.
103 # Each line (definition) in the cedict has the following format:
104 # [traditional form] [simplified form] /definitions/
105 # ...hence the use of indices [0] and [1] after splitting lines by whitespace
106 for line
in self
.dict:
107 tokens
= line
.split()
108 self
.tradDict
[tokens
[0]] = line
109 self
.simpDict
[tokens
[1]] = line
110 NSLog("Finished building dictionaries")
116 def setTextOutput_(self
):
117 if self
.board
.changeCount() != self
.boardChangeCount
: # if new data is on the clipboard
118 self
.boardChangeCount
= self
.board
.changeCount() # update board changecount
120 textInBoard
= self
.board
.stringForType_(NSStringPboardType
)
122 if textInBoard
!= None:
123 charText
= textInBoard
.rstrip()
124 charText
= charText
.split()
126 self
.foundMatches
= ""
128 # for each whitespace-separated 'token' on the clipboard...
129 for token
in charText
:
130 if self
.foundMatches
!= "":
131 self
.foundMatches
+= '========' + '\n\n'
133 self
.foundMatches
= '\n'
135 tokenLength
= len(token
)
136 matchWasFound
= False
137 for c
in range(tokenLength
): # move starting iteration point through token
138 for i
in range(1, tokenLength
+1): # iterate through the length of the token from start pt.
139 if self
.tradDict
.has_key(token
[c
:i
]):
141 self
.foundMatches
+= self
.tradDict
[token
[c
:i
]] + '\n'
142 elif self
.simpDict
.has_key(token
[c
:i
]):
144 self
.foundMatches
+= self
.simpDict
[token
[c
:i
]] + '\n'
146 #if not matchWasFound: self.foundMatches += '"%s" not found in dictionary.\n\n' % token
147 if not matchWasFound
: self
.foundMatches
= "Clipboard does not seem to contain any Chinese characters"