@@ -159,26 +159,20 @@ def get_mofile(languages, localedir, domain=None):
159159 for locale in languages :
160160 if locale == "C" :
161161 break
162- if domain :
163- basename = os .path .join (localedir , locale , "LC_MESSAGES" , domain )
164- else :
165- basename = os .path .join (localedir , locale )
162+ basename = os .path .join (localedir , locale , "LC_MESSAGES" , domain ) if \
163+ domain else os .path .join (localedir , locale )
166164 # look for message catalog files, check timestamps
167165 mofile = basename + ".mo"
168- if os .path .isfile (mofile ):
169- motime = os .path .getmtime (mofile )
170- else :
171- motime = 0
166+ motime = os .path .getmtime (mofile ) if os .path .isfile (mofile ) else 0
172167 pofile = basename + ".po"
173- if os .path .isfile (pofile ):
174- potime = os .path .getmtime (pofile )
175- else :
176- potime = 0
168+ potime = os .path .getmtime (pofile ) if os .path .isfile (pofile ) else 0
169+
177170 # see what we've found
178171 if motime < potime :
179172 # compile
180173 mo = msgfmt .Msgfmt (pofile ).get ()
181- open (mofile , 'wb' ).write (mo )
174+ with open (mofile , 'wb' ) as m :
175+ m .write (mo )
182176 elif motime == 0 :
183177 # no files found - proceed to the next locale name
184178 continue
@@ -202,17 +196,17 @@ def get_translation(language=None, tracker_home=None,
202196 """
203197 mofiles = []
204198 # locale directory paths
205- if tracker_home is None :
206- tracker_locale = None
207- else :
208- tracker_locale = os .path .join (tracker_home , "locale" )
199+ tracker_locale = os .path .join (tracker_home , "locale" ) if \
200+ tracker_home is not None else None
201+
209202 # get the list of locales
210203 locales = find_locales (language )
211204 # add mofiles found in the tracker, then in the system locale directory
212205 if tracker_locale :
213206 mofiles .append (get_mofile (locales , tracker_locale ))
214- for system_locale in LOCALE_DIRS :
215- mofiles .append (get_mofile (locales , system_locale , DOMAIN ))
207+ mofiles += [get_mofile (locales , system_locale , DOMAIN )
208+ for system_locale in LOCALE_DIRS ]
209+
216210 # we want to fall back to english unless english is selected language
217211 if "en" not in locales :
218212 locales = find_locales ("en" )
@@ -226,19 +220,18 @@ def get_translation(language=None, tracker_home=None,
226220 translator = None
227221 for mofile in mofiles :
228222 try :
229- mo = open (mofile , "rb" )
230- if translator is None :
231- translator = translation_class (mo )
232- # the .mo file this translator loaded from
233- translator ._file = mofile
234- else :
235- # note: current implementation of gettext_module
236- # always adds fallback to the end of the fallback chain.
237- fallback = translation_class (mo )
238- fallback ._file = mofile
239- translator .add_fallback (fallback )
240- mo .close ()
241- except IOError :
223+ with open (mofile , "rb" ) as mo :
224+ if translator is None :
225+ translator = translation_class (mo )
226+ # the .mo file this translator loaded from
227+ translator ._file = mofile
228+ else :
229+ # note: current implementation of gettext_module
230+ # always adds fallback to the end of the fallback chain.
231+ fallback = translation_class (mo )
232+ fallback ._file = mofile
233+ translator .add_fallback (fallback )
234+ except IOError : # noqa: PERF203
242235 # ignore unreadable .mo files
243236 pass
244237 if translator is None :
0 commit comments