3030from PyObjCTools import AppHelper
3131from plotdevice .gui import ScriptController
3232from plotdevice .util import rsrc_path
33+ from plotdevice .run import encoding
3334
3435STDOUT = sys .stdout
3536STDERR = sys .stderr
@@ -64,7 +65,6 @@ def initWithOpts_forMode_(self, opts, mode):
6465
6566 def applicationDidFinishLaunching_ (self , note ):
6667 pth = self .opts ['file' ]
67- src = file (pth ).read ()
6868
6969 if self .mode == 'windowed' :
7070 # load the viewer ui from the nib in plotdevice/rsrc
@@ -74,7 +74,7 @@ def applicationDidFinishLaunching_(self, note):
7474 NSApp ().setMainMenu_ (self .menu )
7575
7676 # configure the window script-controller, and update-watcher
77- self .script .setPath_source_options_ (pth , src , self .opts )
77+ self .script .setScript_options_ (pth , self .opts )
7878 self .window .setTitleWithRepresentedFilename_ (pth )
7979 # self.script.setWindowFrameAutosaveName_('plotdevice:%s'%self.opts['file'])
8080
@@ -86,7 +86,7 @@ def applicationDidFinishLaunching_(self, note):
8686 elif self .mode == 'headless' :
8787 # create a window-less WindowController
8888 self .script = ConsoleScript .alloc ().init ()
89- self .script .setPath_source_options_ (pth , src , self .opts )
89+ self .script .setScript_options_ (pth , self .opts )
9090
9191 # BUG? FEATURE? (it's a mystery!)
9292 # not sure why this is necessary. does this not get validated by the
@@ -152,12 +152,19 @@ def init(self):
152152 self ._init_state ()
153153 return super (ScriptController , self ).init ()
154154
155- def setPath_source_options_ (self , path , source , opts ):
155+ def setScript_options_ (self , path , opts ):
156156 self .vm .path = path
157- self .vm .source = source
157+ self .vm .source = self . unicode_src
158158 self .vm .metadata = self .opts = opts
159159 self .watcher = ScriptWatcher .alloc ().initWithScript_ (self )
160160
161+ @property
162+ def unicode_src (self ):
163+ """Read in our script file's contents (honoring its `# encoding: ...` if present)"""
164+ src = file (self .path ).read ()
165+ enc = encoding (src ) or 'utf-8'
166+ return src .decode (enc )
167+
161168 def scriptedRun (self ):
162169 # this is the first run that gets triggered at invocation
163170 # afterward any menu commands will go to runScript and runFullscreen
@@ -168,18 +175,18 @@ def scriptedRun(self):
168175
169176 def _refresh (self ):
170177 # file changed: reread the script (and potentially run it)
171- self .vm .source = file ( self .path ). read ()
178+ self .vm .source = self .unicode_src
172179 if self .opts ['live' ]:
173180 self .scriptedRun ()
174181
175182 def runScript (self ):
176183 if self .watcher .stale ():
177- self .vm .source = file ( self .path ). read ()
184+ self .vm .source = self .unicode_src
178185 super (ConsoleScript , self ).runScript ()
179186
180187 def runFullscreen_ (self , sender ):
181188 if self .watcher .stale ():
182- self .vm .source = file ( self .path ). read ()
189+ self .vm .source = self .unicode_src
183190 super (ConsoleScript , self ).runFullscreen_ (sender )
184191
185192 def windowWillClose_ (self , note ):
0 commit comments