Skip to content

Commit

Permalink
RF: import _translate in each file, no use of __builtins__
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremygray committed Feb 28, 2016
1 parent a233a81 commit e5ef08c
Show file tree
Hide file tree
Showing 44 changed files with 61 additions and 40 deletions.
1 change: 1 addition & 0 deletions psychopy/app/_psychopyApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from agw import advancedsplash as AS
except ImportError: # if it's not there locally, try the wxPython lib.
import wx.lib.agw.advancedsplash as AS
from psychopy.app.localization import _translate
# NB keep imports to a minimum here because splash screen has not yet shown
# e.g. coder and builder are imported during app.__init__ because they
# take a while
Expand Down
5 changes: 1 addition & 4 deletions psychopy/app/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import re
import numpy

try:
_translate # is the app-global text translation function defined?
except NameError:
from .. import localization
from ..localization import _translate

from . import experiment, components
from .. import stdOutRich, dialogs
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/builder/components/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..experiment import Param
from ..components import getInitVals
from psychopy.constants import FOREVER
from ...localization import _translate

# only use _localized values for label values, nothing functional
_localized = {
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/aperture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, getInitVals
from ._base import BaseVisualComponent, getInitVals, _translate

__author__ = 'Jeremy Gray, Jon Peirce'
# March 2011; builder-component for Yuri Spitsyn's visual.Aperture class
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/cedrusBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from .keyboard import KeyboardComponent, Param
from .keyboard import KeyboardComponent, Param, _translate
from ..experiment import CodeGenerationException, _valid_var_re

__author__ = 'Jon Peirce'
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/dots.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, Param, getInitVals
from ._base import BaseVisualComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/eyetracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/grating.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, Param, getInitVals
from ._base import BaseVisualComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, Param, getInitVals
from ._base import BaseVisualComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/ioLabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from os import path

from .keyboard import KeyboardComponent, Param
from .keyboard import KeyboardComponent, Param, _translate
from ..experiment import _valid_var_re

__author__ = 'Jeremy Gray'
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from os import path

from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate
from ..experiment import CodeGenerationException, _valid_var_re

# the absolute path to the folder containing this path
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Author: Jeremy R. Gray, 2012

from os import path
from _base import BaseComponent, Param, getInitVals
from _base import BaseComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, Param, getInitVals
from ._base import BaseVisualComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/parallelOut.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate
from psychopy import prefs

# the absolute path to the folder containing this path
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, Param, getInitVals
from ._base import BaseVisualComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, Param, getInitVals
from ._base import BaseVisualComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/ratingscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from os import path
import re
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate

__author__ = 'Jeremy Gray'

Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import wx
import copy
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate
from psychopy import logging

# this is not a standard component - it will appear on toolbar not in
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseComponent, Param, getInitVals
from ._base import BaseComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate

__author__ = 'Jon Peirce'

Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from os import path
from ._base import BaseVisualComponent, Param, getInitVals
from ._base import BaseVisualComponent, Param, getInitVals, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion psychopy/app/builder/components/unknown.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from os import path
from ._base import BaseComponent, Param
from ._base import BaseComponent, Param, _translate

# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/builder/dialogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .dlgsConditions import DlgConditions
from .dlgsCode import DlgCodeComponentProperties, CodeBox
from psychopy import data, logging
from ...localization import _translate


white = wx.Colour(255, 255, 255, 255)
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/builder/dialogs/dlgsCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from wx.lib import flatnotebook

from .. import validators
from ...localization import _translate

_unescapedDollarSign_re = re.compile(r"^\$|[^\\]\$")

Expand Down
1 change: 1 addition & 0 deletions psychopy/app/builder/dialogs/dlgsConditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from psychopy import gui
from .. experiment import _valid_var_re, _nonalphanumeric_re
from ...localization import _translate

darkblue = wx.Colour(30, 30, 150, 255)
darkgrey = wx.Colour(65, 65, 65, 255)
Expand Down
5 changes: 1 addition & 4 deletions psychopy/app/builder/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
from psychopy import data, __version__, logging, constants
from psychopy.constants import FOREVER

try:
_translate # is the app-global text translation function defined?
except NameError:
from .. import localization
from ..localization import _translate
import locale

# predefine some regex's; deepcopy complains if do in NameSpace.__init__()
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/builder/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from psychopy import logging, data
from .utils import FileDropTarget
from .dialogs import DlgLoopProperties
from ..localization import _translate


canvasColor = [200, 200, 200] # in prefs? ;-)
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/builder/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Module containing validators for various parameters.
'''
import wx
from ..localization import _translate


class NameValidator(wx.PyValidator):
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/coder/coder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from . import psychoParser, introspect
from .. import stdOutRich, dialogs
from psychopy import logging
from ..localization import _translate

# advanced prefs (not set in prefs files)
prefTestSubset = ""
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import psychopy
from . import dialogs
from .localization import _translate
from psychopy import logging
from psychopy import web
py3 = web.py3
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from wx.lib.newevent import NewEvent

from psychopy import logging
from .localization import _translate


class MessageDialog(wx.Dialog):
Expand Down
8 changes: 6 additions & 2 deletions psychopy/app/localization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ def getID(lang=None):

# PsychoPy app uses a nonstandard name _translate (instead of _)
# A dependency overwrites _ somewhere, clobbering use of _ as global:
__builtins__['_translate'] = _
del(__builtins__['_']) # idea: force psychopy code to use _translate
# __builtins__['_translate'] = _
# del(__builtins__['_']) # idea: force psychopy code to use _translate

# Feb 2016: require modules to explicitly import _translate from localization:
_translate = _ # _ is created by gettext, in builtins namespace
del(__builtins__['_'])


# __builtins__['_'] = wx.GetTranslation
Expand Down
5 changes: 3 additions & 2 deletions psychopy/app/localization/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Sources:

Intended usage:
- early in loading the app, e.g., in _psychopyApp.PsychoPyApp.OnInit(), do:
from psychopy.app import localization
from psychopy.app.localization import _translate
This should be done after importing wx
- this will: 1) detect the system default or preferred (pref) locale;
2) initialize the locale setting globally; and 3) install _() function globally
2) initialize the locale setting globally; and 3) make _translate available
in the module. (Feb 2016: move away from using __builtins__._translate)
- currently both the standard gettext and wxPython version are used

Known limitations (July 2014):
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/preferencesDlg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from . import localization, dialogs
from psychopy import logging
from .localization import _translate

# this will be overridden by the size of the scrolled panel making the prefs
dlgSize = (520, 600)
Expand Down
1 change: 1 addition & 0 deletions psychopy/app/stdOutRich.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import re
import wx.richtext
from .localization import _translate


class StdOutRich(wx.richtext.RichTextCtrl):
Expand Down
2 changes: 1 addition & 1 deletion psychopy/gui/qtgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import os
import sys
import json
from psychopy.app import localization
from psychopy.app.localization import _translate

OK = QtWidgets.QDialogButtonBox.Ok

Expand Down
2 changes: 1 addition & 1 deletion psychopy/gui/wxgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import wx
import numpy
import os
from psychopy.app import localization # pylint: disable=W0611
from psychopy.app.localization import _translate

OK = wx.ID_OK

Expand Down
2 changes: 1 addition & 1 deletion psychopy/monitors/MonitorCenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from wx import grid
from wx.lib import intctrl

from psychopy.app import localization # pylint: disable=W0611
from psychopy.app.localization import _translate
from psychopy import monitors, hardware, logging
from psychopy.app import dialogs

Expand Down
10 changes: 8 additions & 2 deletions psychopy/preferences/generateHints.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import re
import sys
from psychopy import core

hintsFile = 'hints.py'
comments_all = []
locations_all = []
# list of .spec files to parse.
Expand Down Expand Up @@ -62,12 +64,13 @@

# Output hint.py
try:
fp = open('hints.py', 'wb')
fp = open(hintsFile, 'wb')
fp.write('# This file was generated by generateHints.py.\n')
fp.write('# Following strings are used to localize hints in '
'Preference Dialog of PsychoPy application.\n')
'Preference Dialog of \n# the PsychoPy application.\n')
fp.write('# Rebuild this file if comments in *.spec files '
'are modified.\n\n')
fp.write('from psychopy.app.localization import _translate\n\n')
except Exception:
# If hints.py could not be opend as a writable file, output to STDOUT.
fp = sys.stdout
Expand All @@ -79,3 +82,6 @@
fp.write('_translate("%s")\n\n' % comments_all[i].replace(
'\\', '\\\\').replace('"', '\\"')) # escape double quotation marks
fp.close()

cmd = ['autopep8', hintsFile, '--in-place']
core.shellCall(cmd)
Loading

0 comments on commit e5ef08c

Please sign in to comment.